Skip to content

Commit d9c0b23

Browse files
committed
Avoid suggesting constrain the associated type to a trait
1 parent 2cb4e7d commit d9c0b23

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed

compiler/rustc_resolve/src/late/diagnostics.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -934,7 +934,7 @@ impl<'ast, 'ra, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
934934
}
935935

936936
fn suggest_trait_and_bounds(
937-
&self,
937+
&mut self,
938938
err: &mut Diag<'_>,
939939
source: PathSource<'_, '_, '_>,
940940
res: Option<Res>,
@@ -1479,7 +1479,7 @@ impl<'ast, 'ra, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
14791479
}
14801480

14811481
/// Given `where <T as Bar>::Baz: String`, suggest `where T: Bar<Baz = String>`.
1482-
fn restrict_assoc_type_in_where_clause(&self, span: Span, err: &mut Diag<'_>) -> bool {
1482+
fn restrict_assoc_type_in_where_clause(&mut self, span: Span, err: &mut Diag<'_>) -> bool {
14831483
// Detect that we are actually in a `where` predicate.
14841484
let (bounded_ty, bounds, where_span) = if let Some(ast::WherePredicate {
14851485
kind:
@@ -1539,6 +1539,18 @@ impl<'ast, 'ra, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
15391539
&poly_trait_ref.trait_ref.path.segments[..]
15401540
{
15411541
if ident.span == span {
1542+
let maybe_trait = !self
1543+
.r
1544+
.lookup_import_candidates(
1545+
*ident,
1546+
TypeNS,
1547+
&self.parent_scope,
1548+
&|res: Res| matches!(res, Res::Def(DefKind::Trait, _)),
1549+
)
1550+
.is_empty();
1551+
if maybe_trait {
1552+
return false;
1553+
}
15421554
let Some(new_where_bound_predicate) =
15431555
mk_where_bound_predicate(path, poly_trait_ref, ty)
15441556
else {
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
use std::str::FromStr;
2+
fn foo<T: FromStr>() -> T
3+
where
4+
<T as FromStr>::Err: Debug, //~ ERROR expected trait
5+
{
6+
"".parse().unwrap()
7+
}
8+
9+
fn main() {}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error[E0404]: expected trait, found derive macro `Debug`
2+
--> $DIR/assoc-type-maybe-trait-147356.rs:4:26
3+
|
4+
LL | <T as FromStr>::Err: Debug,
5+
| ^^^^^ not a trait
6+
|
7+
help: consider importing this trait instead
8+
|
9+
LL + use std::fmt::Debug;
10+
|
11+
12+
error: aborting due to 1 previous error
13+
14+
For more information about this error, try `rustc --explain E0404`.

0 commit comments

Comments
 (0)