Skip to content

Commit 888990b

Browse files
Rollup merge of #153967 - estebank:infer_must_implement, r=petrochenkov
Tweak wording of failed predicate in inference error Special case message talking about `Predicate` that couldn't be satisfied when in inference errors so that we don't say "cannot satisfy `_: Trait`" and instead say "type must implement `Trait`".
2 parents b2a26be + 9b1eb93 commit 888990b

24 files changed

Lines changed: 39 additions & 28 deletions

compiler/rustc_trait_selection/src/error_reporting/traits/ambiguity.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use rustc_infer::traits::util::elaborate;
1111
use rustc_infer::traits::{
1212
Obligation, ObligationCause, ObligationCauseCode, PolyTraitObligation, PredicateObligation,
1313
};
14+
use rustc_middle::ty::print::PrintPolyTraitPredicateExt;
1415
use rustc_middle::ty::{self, Ty, TyCtxt, TypeVisitable as _, TypeVisitableExt as _};
1516
use rustc_session::parse::feature_err_unstable_feature_bound;
1617
use rustc_span::{DUMMY_SP, ErrorGuaranteed, Span};
@@ -306,8 +307,18 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
306307
err.cancel();
307308
return e;
308309
}
309-
let pred = self.tcx.short_string(predicate, &mut err.long_ty_path());
310-
err.note(format!("cannot satisfy `{pred}`"));
310+
if let Some(clause) = predicate.as_trait_clause()
311+
&& let ty::Infer(_) = clause.self_ty().skip_binder().kind()
312+
{
313+
let tr = self.tcx.short_string(
314+
clause.print_modifiers_and_trait_path(),
315+
&mut err.long_ty_path(),
316+
);
317+
err.note(format!("the type must implement `{tr}`"));
318+
} else {
319+
let pred = self.tcx.short_string(predicate, &mut err.long_ty_path());
320+
err.note(format!("cannot satisfy `{pred}`"));
321+
}
311322
let impl_candidates =
312323
self.find_similar_impl_candidates(predicate.as_trait_clause().unwrap());
313324
if impl_candidates.len() < 40 {

tests/ui/const-generics/issues/issue-83249.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ LL | let _ = foo([0; 1]);
66
| |
77
| required by a bound introduced by this call
88
|
9-
= note: cannot satisfy `_: Foo`
9+
= note: the type must implement `Foo`
1010
help: the trait `Foo` is implemented for `u8`
1111
--> $DIR/issue-83249.rs:8:1
1212
|

tests/ui/generic-associated-types/ambig-hr-projection-issue-93340.old.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0283]: type annotations needed
44
LL | cmp_eq
55
| ^^^^^^ cannot infer type of the type parameter `A` declared on the function `cmp_eq`
66
|
7-
= note: cannot satisfy `_: Scalar`
7+
= note: the type must implement `Scalar`
88
note: required by a bound in `cmp_eq`
99
--> $DIR/ambig-hr-projection-issue-93340.rs:10:22
1010
|

tests/ui/generic-associated-types/bugs/issue-88382.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0283]: type annotations needed
44
LL | do_something(SomeImplementation(), test);
55
| ^^^^ cannot infer type of the type parameter `I` declared on the function `test`
66
|
7-
= note: cannot satisfy `_: Iterable`
7+
= note: the type must implement `Iterable`
88
help: the trait `Iterable` is implemented for `SomeImplementation`
99
--> $DIR/issue-88382.rs:13:1
1010
|

tests/ui/impl-trait/in-trait/not-inferred-generic.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0283]: type annotations needed
44
LL | ().publish_typed();
55
| ^^^^^^^^^^^^^ cannot infer type of the type parameter `F` declared on the method `publish_typed`
66
|
7-
= note: cannot satisfy `_: Clone`
7+
= note: the type must implement `Clone`
88
= note: opaque types cannot be accessed directly on a `trait`, they can only be accessed through a specific `impl`
99
note: required by a bound in `TypedClient::publish_typed::{anon_assoc#0}`
1010
--> $DIR/not-inferred-generic.rs:4:12

tests/ui/impl-trait/opaque-cast-field-access-in-future.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ LL |
77
LL | loop {}
88
| ------- return type was inferred to be `!` here
99
|
10-
= note: cannot satisfy `_: Future`
10+
= note: the type must implement `Future`
1111

1212
error: aborting due to 1 previous error
1313

tests/ui/impl-trait/where-allowed-2.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0283]: type annotations needed
44
LL | fn in_adt_in_return() -> Vec<impl Debug> { panic!() }
55
| ^^^^^^^^^^ cannot infer type
66
|
7-
= note: cannot satisfy `_: Debug`
7+
= note: the type must implement `Debug`
88

99
error: aborting due to 1 previous error
1010

tests/ui/impl-trait/where-allowed.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ error[E0283]: type annotations needed
374374
LL | fn in_dyn_Fn_return_in_return() -> &'static dyn Fn() -> impl Debug { panic!() }
375375
| ^^^^^^^^^^ cannot infer type
376376
|
377-
= note: cannot satisfy `_: Debug`
377+
= note: the type must implement `Debug`
378378

379379
error[E0283]: type annotations needed
380380
--> $DIR/where-allowed.rs:65:46

tests/ui/imports/import-trait-method.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ error[E0283]: type annotations needed
1414
LL | fn main() { foo(); }
1515
| ^^^^^ cannot infer type
1616
|
17-
= note: cannot satisfy `_: Foo`
17+
= note: the type must implement `Foo`
1818

1919
error: aborting due to 2 previous errors
2020

tests/ui/inference/erase-type-params-in-label.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0283]: type annotations needed for `Foo<i32, &str, _, _>`
44
LL | let foo = foo(1, "");
55
| ^^^ ---------- type must be known at this point
66
|
7-
= note: cannot satisfy `_: Default`
7+
= note: the type must implement `Default`
88
note: required by a bound in `foo`
99
--> $DIR/erase-type-params-in-label.rs:25:17
1010
|
@@ -21,7 +21,7 @@ error[E0283]: type annotations needed for `Bar<i32, &str, _>`
2121
LL | let bar = bar(1, "");
2222
| ^^^ ---------- type must be known at this point
2323
|
24-
= note: cannot satisfy `_: Default`
24+
= note: the type must implement `Default`
2525
note: required by a bound in `bar`
2626
--> $DIR/erase-type-params-in-label.rs:14:17
2727
|

0 commit comments

Comments
 (0)