Skip to content

Commit ac258e9

Browse files
authored
Rollup merge of #108294 - compiler-errors:arbitary-sugg-binder, r=TaKO8Ki
Place binder correctly for arbitrary trait bound suggestion suggest `for<'a> &'a T: Trait` instead of `&'a T: for<'a> T`.
2 parents 83791f9 + 16e2b9f commit ac258e9

File tree

3 files changed

+40
-2
lines changed

3 files changed

+40
-2
lines changed

compiler/rustc_middle/src/ty/diagnostics.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ pub fn suggest_arbitrary_trait_bound<'tcx>(
117117
}
118118

119119
let param_name = trait_pred.skip_binder().self_ty().to_string();
120-
let mut constraint = trait_pred.print_modifiers_and_trait_path().to_string();
120+
let mut constraint = trait_pred.to_string();
121121

122122
if let Some((name, term)) = associated_ty {
123123
// FIXME: this case overlaps with code in TyCtxt::note_and_explain_type_err.
@@ -144,7 +144,7 @@ pub fn suggest_arbitrary_trait_bound<'tcx>(
144144
this requirement",
145145
if generics.where_clause_span.is_empty() { "introducing a" } else { "extending the" },
146146
),
147-
format!("{} {}: {}", generics.add_where_or_trailing_comma(), param_name, constraint),
147+
format!("{} {constraint}", generics.add_where_or_trailing_comma()),
148148
Applicability::MaybeIncorrect,
149149
);
150150
true
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
trait Foo
2+
where
3+
for<'a> &'a Self: Bar,
4+
{
5+
}
6+
7+
impl Foo for () {}
8+
9+
trait Bar {}
10+
11+
impl Bar for &() {}
12+
13+
fn foo<T: Foo>() {}
14+
//~^ ERROR the trait bound `for<'a> &'a T: Bar` is not satisfied
15+
16+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
error[E0277]: the trait bound `for<'a> &'a T: Bar` is not satisfied
2+
--> $DIR/correct-binder-for-arbitrary-bound-sugg.rs:13:11
3+
|
4+
LL | fn foo<T: Foo>() {}
5+
| ^^^ the trait `for<'a> Bar` is not implemented for `&'a T`
6+
|
7+
note: required by a bound in `Foo`
8+
--> $DIR/correct-binder-for-arbitrary-bound-sugg.rs:3:23
9+
|
10+
LL | trait Foo
11+
| --- required by a bound in this trait
12+
LL | where
13+
LL | for<'a> &'a Self: Bar,
14+
| ^^^ required by this bound in `Foo`
15+
help: consider introducing a `where` clause, but there might be an alternative better way to express this requirement
16+
|
17+
LL | fn foo<T: Foo>() where for<'a> &'a T: Bar {}
18+
| ++++++++++++++++++++++++
19+
20+
error: aborting due to previous error
21+
22+
For more information about this error, try `rustc --explain E0277`.

0 commit comments

Comments
 (0)