Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions compiler/rustc_resolve/src/late/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -934,7 +934,7 @@ impl<'ast, 'ra, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
}

fn suggest_trait_and_bounds(
&self,
&mut self,
err: &mut Diag<'_>,
source: PathSource<'_, '_, '_>,
res: Option<Res>,
Expand Down Expand Up @@ -1479,7 +1479,7 @@ impl<'ast, 'ra, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
}

/// Given `where <T as Bar>::Baz: String`, suggest `where T: Bar<Baz = String>`.
fn restrict_assoc_type_in_where_clause(&self, span: Span, err: &mut Diag<'_>) -> bool {
fn restrict_assoc_type_in_where_clause(&mut self, span: Span, err: &mut Diag<'_>) -> bool {
// Detect that we are actually in a `where` predicate.
let (bounded_ty, bounds, where_span) = if let Some(ast::WherePredicate {
kind:
Expand Down Expand Up @@ -1539,6 +1539,19 @@ impl<'ast, 'ra, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
&poly_trait_ref.trait_ref.path.segments[..]
{
if ident.span == span {
if matches!(
self.resolve_path(
&[Segment::from_ident(*ident)],
Some(TypeNS),
None,
PathSource::Type,
),
PathResult::Indeterminate | PathResult::Failed { .. }
) {
// if the ident doesn't resolve to a type, don't suggest constrain associated type.
return false;
}

let Some(new_where_bound_predicate) =
mk_where_bound_predicate(path, poly_trait_ref, ty)
else {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use std::str::FromStr;
fn foo<T: FromStr>() -> T
where
<T as FromStr>::Err: Debug, //~ ERROR expected trait
{
"".parse().unwrap()
}

fn bar<T: FromStr>() -> T
where
<T as FromStr>::Err: some_unknown_name, //~ ERROR cannot find trait `some_unknown_name` in this scope
{
"".parse().unwrap()
}

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
error[E0404]: expected trait, found derive macro `Debug`
--> $DIR/assoc-type-maybe-trait-147356.rs:4:26
|
LL | <T as FromStr>::Err: Debug,
| ^^^^^ not a trait
|
help: consider importing this trait instead
|
LL + use std::fmt::Debug;
|

error[E0405]: cannot find trait `some_unknown_name` in this scope
--> $DIR/assoc-type-maybe-trait-147356.rs:11:26
|
LL | <T as FromStr>::Err: some_unknown_name,
| ^^^^^^^^^^^^^^^^^ not found in this scope

error: aborting due to 2 previous errors

Some errors have detailed explanations: E0404, E0405.
For more information about an error, try `rustc --explain E0404`.
Loading