Skip to content

Commit baeee34

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

File tree

3 files changed

+58
-16
lines changed

3 files changed

+58
-16
lines changed

compiler/rustc_resolve/src/late/diagnostics.rs

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1533,26 +1533,31 @@ impl<'ast, 'ra, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
15331533
[ast::PathSegment { args: None, .. }],
15341534
[ast::GenericBound::Trait(poly_trait_ref)],
15351535
) = (&type_param_path.segments[..], &bounds[..])
1536+
&& let [ast::PathSegment { ident, args: None, id }] =
1537+
&poly_trait_ref.trait_ref.path.segments[..]
15361538
&& poly_trait_ref.modifiers == ast::TraitBoundModifiers::NONE
15371539
{
1538-
if let [ast::PathSegment { ident, args: None, .. }] =
1539-
&poly_trait_ref.trait_ref.path.segments[..]
1540-
{
1541-
if ident.span == span {
1542-
let Some(new_where_bound_predicate) =
1543-
mk_where_bound_predicate(path, poly_trait_ref, ty)
1544-
else {
1545-
return false;
1546-
};
1547-
err.span_suggestion_verbose(
1548-
*where_span,
1549-
format!("constrain the associated type to `{ident}`"),
1550-
where_bound_predicate_to_string(&new_where_bound_predicate),
1551-
Applicability::MaybeIncorrect,
1552-
);
1540+
if ident.span == span {
1541+
let Some(partial_res) = self.r.partial_res_map.get(&id) else {
1542+
return false;
1543+
};
1544+
if !matches!(partial_res.full_res(), Some(hir::def::Res::Def(..))) {
1545+
return false;
15531546
}
1554-
return true;
1547+
1548+
let Some(new_where_bound_predicate) =
1549+
mk_where_bound_predicate(path, poly_trait_ref, ty)
1550+
else {
1551+
return false;
1552+
};
1553+
err.span_suggestion_verbose(
1554+
*where_span,
1555+
format!("constrain the associated type to `{ident}`"),
1556+
where_bound_predicate_to_string(&new_where_bound_predicate),
1557+
Applicability::MaybeIncorrect,
1558+
);
15551559
}
1560+
return true;
15561561
}
15571562
}
15581563
false
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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 bar<T: FromStr>() -> T
10+
where
11+
<T as FromStr>::Err: some_unknown_name, //~ ERROR cannot find trait `some_unknown_name` in this scope
12+
{
13+
"".parse().unwrap()
14+
}
15+
16+
fn main() {}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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[E0405]: cannot find trait `some_unknown_name` in this scope
13+
--> $DIR/assoc-type-maybe-trait-147356.rs:11:26
14+
|
15+
LL | <T as FromStr>::Err: some_unknown_name,
16+
| ^^^^^^^^^^^^^^^^^ not found in this scope
17+
18+
error: aborting due to 2 previous errors
19+
20+
Some errors have detailed explanations: E0404, E0405.
21+
For more information about an error, try `rustc --explain E0404`.

0 commit comments

Comments
 (0)