Skip to content

Commit 20b1c2a

Browse files
committed
Account for unbounded type param receiver in suggestions
When encountering ```rust fn f<T>(a: T, b: T) -> std::cmp::Ordering { a.cmp(&b) //~ ERROR E0599 } ``` output ``` error[E0599]: no method named `cmp` found for type parameter `T` in the current scope --> $DIR/method-on-unbounded-type-param.rs:2:7 | LL | fn f<T>(a: T, b: T) -> std::cmp::Ordering { | - method `cmp` not found for this type parameter LL | a.cmp(&b) | ^^^ method cannot be called on `T` due to unsatisfied trait bounds | = help: items from traits can only be used if the type parameter is bounded by the trait help: the following traits define an item `cmp`, perhaps you need to restrict type parameter `T` with one of them: | LL | fn f<T: Ord>(a: T, b: T) -> std::cmp::Ordering { | +++++ LL | fn f<T: Iterator>(a: T, b: T) -> std::cmp::Ordering { | ++++++++++ ``` Fix #120186.
1 parent d34b0fa commit 20b1c2a

File tree

2 files changed

+35
-22
lines changed

2 files changed

+35
-22
lines changed

compiler/rustc_hir_typeck/src/method/suggest.rs

+27-14
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,17 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
554554
"`count` is defined on `{iterator_trait}`, which `{rcvr_ty}` does not implement"
555555
));
556556
}
557+
} else if !unsatisfied_predicates.is_empty() && matches!(rcvr_ty.kind(), ty::Param(_)) {
558+
// We special case the situation where we are looking for `_` in
559+
// `<TypeParam as _>::method` because otherwise the machinery will look for blanket
560+
// implementations that have unsatisfied trait bounds to suggest, leading us to claim
561+
// things like "we're looking for a trait with method `cmp`, both `Iterator` and `Ord`
562+
// have one, in order to implement `Ord` you need to restrict `TypeParam: FnPtr` so
563+
// that `impl<T: FnPtr> Ord for T` can apply", which is not what we want. We have a type
564+
// parameter, we want to directly say "`Ord::cmp` and `Iterator::cmp` exist, restrict
565+
// `TypeParam: Ord` or `TypeParam: Iterator`"". That is done further down when calling
566+
// `self.suggest_traits_to_import`, so we ignore the `unsatisfied_predicates`
567+
// suggestions.
557568
} else if !unsatisfied_predicates.is_empty() {
558569
let mut type_params = FxIndexMap::default();
559570

@@ -1325,7 +1336,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
13251336
}
13261337
}
13271338
self.note_derefed_ty_has_method(&mut err, source, rcvr_ty, item_name, expected);
1328-
return Some(err);
1339+
Some(err)
13291340
}
13301341

13311342
fn note_candidates_on_method_error(
@@ -2918,19 +2929,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
29182929
// this isn't perfect (that is, there are cases when
29192930
// implementing a trait would be legal but is rejected
29202931
// here).
2921-
unsatisfied_predicates.iter().all(|(p, _, _)| {
2922-
match p.kind().skip_binder() {
2923-
// Hide traits if they are present in predicates as they can be fixed without
2924-
// having to implement them.
2925-
ty::PredicateKind::Clause(ty::ClauseKind::Trait(t)) => {
2926-
t.def_id() == info.def_id
2927-
}
2928-
ty::PredicateKind::Clause(ty::ClauseKind::Projection(p)) => {
2929-
p.projection_ty.def_id == info.def_id
2930-
}
2931-
_ => false,
2932-
}
2933-
}) && (type_is_local || info.def_id.is_local())
2932+
(type_is_local || info.def_id.is_local())
29342933
&& !self.tcx.trait_is_auto(info.def_id)
29352934
&& self
29362935
.associated_value(info.def_id, item_name)
@@ -2978,6 +2977,20 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
29782977
item.visibility(self.tcx).is_public() || info.def_id.is_local()
29792978
})
29802979
.is_some()
2980+
&& (matches!(rcvr_ty.kind(), ty::Param(_))
2981+
|| unsatisfied_predicates.iter().all(|(p, _, _)| {
2982+
match p.kind().skip_binder() {
2983+
// Hide traits if they are present in predicates as they can be fixed without
2984+
// having to implement them.
2985+
ty::PredicateKind::Clause(ty::ClauseKind::Trait(t)) => {
2986+
t.def_id() == info.def_id
2987+
}
2988+
ty::PredicateKind::Clause(ty::ClauseKind::Projection(p)) => {
2989+
p.projection_ty.def_id == info.def_id
2990+
}
2991+
_ => false,
2992+
}
2993+
}))
29812994
})
29822995
.collect::<Vec<_>>();
29832996
for span in &arbitrary_rcvr {

tests/ui/traits/method-on-unbounded-type-param.stderr

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
error[E0599]: `T` is not an iterator
1+
error[E0599]: no method named `cmp` found for type parameter `T` in the current scope
22
--> $DIR/method-on-unbounded-type-param.rs:2:7
33
|
44
LL | fn f<T>(a: T, b: T) -> std::cmp::Ordering {
55
| - method `cmp` not found for this type parameter
66
LL | a.cmp(&b)
7-
| ^^^ `T` is not an iterator
7+
| ^^^ method cannot be called on `T` due to unsatisfied trait bounds
88
|
9-
= note: the following trait bounds were not satisfied:
10-
`T: Iterator`
11-
which is required by `&mut T: Iterator`
12-
help: consider restricting the type parameter to satisfy the trait bound
9+
= help: items from traits can only be used if the type parameter is bounded by the trait
10+
help: the following traits define an item `cmp`, perhaps you need to restrict type parameter `T` with one of them:
1311
|
14-
LL | fn f<T>(a: T, b: T) -> std::cmp::Ordering where T: Iterator {
15-
| +++++++++++++++++
12+
LL | fn f<T: Ord>(a: T, b: T) -> std::cmp::Ordering {
13+
| +++++
14+
LL | fn f<T: Iterator>(a: T, b: T) -> std::cmp::Ordering {
15+
| ++++++++++
1616

1717
error[E0599]: the method `cmp` exists for reference `&T`, but its trait bounds were not satisfied
1818
--> $DIR/method-on-unbounded-type-param.rs:5:10

0 commit comments

Comments
 (0)