Skip to content

Commit 888e5da

Browse files
authored
Rollup merge of #147362 - chenyukang:yukang-fix-bound-147356, r=fee1-dead
Avoid suggesting constrain the associated type with unknown type Fixes #147356
2 parents 1eb0657 + 76bd555 commit 888e5da

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
@@ -1572,26 +1572,31 @@ impl<'ast, 'ra, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
15721572
[ast::PathSegment { args: None, .. }],
15731573
[ast::GenericBound::Trait(poly_trait_ref)],
15741574
) = (&type_param_path.segments[..], &bounds[..])
1575+
&& let [ast::PathSegment { ident, args: None, id }] =
1576+
&poly_trait_ref.trait_ref.path.segments[..]
15751577
&& poly_trait_ref.modifiers == ast::TraitBoundModifiers::NONE
15761578
{
1577-
if let [ast::PathSegment { ident, args: None, .. }] =
1578-
&poly_trait_ref.trait_ref.path.segments[..]
1579-
{
1580-
if ident.span == span {
1581-
let Some(new_where_bound_predicate) =
1582-
mk_where_bound_predicate(path, poly_trait_ref, ty)
1583-
else {
1584-
return false;
1585-
};
1586-
err.span_suggestion_verbose(
1587-
*where_span,
1588-
format!("constrain the associated type to `{ident}`"),
1589-
where_bound_predicate_to_string(&new_where_bound_predicate),
1590-
Applicability::MaybeIncorrect,
1591-
);
1579+
if ident.span == span {
1580+
let Some(partial_res) = self.r.partial_res_map.get(&id) else {
1581+
return false;
1582+
};
1583+
if !matches!(partial_res.full_res(), Some(hir::def::Res::Def(..))) {
1584+
return false;
15921585
}
1593-
return true;
1586+
1587+
let Some(new_where_bound_predicate) =
1588+
mk_where_bound_predicate(path, poly_trait_ref, ty)
1589+
else {
1590+
return false;
1591+
};
1592+
err.span_suggestion_verbose(
1593+
*where_span,
1594+
format!("constrain the associated type to `{ident}`"),
1595+
where_bound_predicate_to_string(&new_where_bound_predicate),
1596+
Applicability::MaybeIncorrect,
1597+
);
15941598
}
1599+
return true;
15951600
}
15961601
}
15971602
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)