Skip to content

Commit

Permalink
Rollup merge of #108294 - compiler-errors:arbitary-sugg-binder, r=TaK…
Browse files Browse the repository at this point in the history
…O8Ki

Place binder correctly for arbitrary trait bound suggestion

suggest `for<'a> &'a T: Trait` instead of `&'a T: for<'a> T`.
  • Loading branch information
matthiaskrgr authored Mar 9, 2023
2 parents 83791f9 + 16e2b9f commit ac258e9
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
4 changes: 2 additions & 2 deletions compiler/rustc_middle/src/ty/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ pub fn suggest_arbitrary_trait_bound<'tcx>(
}

let param_name = trait_pred.skip_binder().self_ty().to_string();
let mut constraint = trait_pred.print_modifiers_and_trait_path().to_string();
let mut constraint = trait_pred.to_string();

if let Some((name, term)) = associated_ty {
// FIXME: this case overlaps with code in TyCtxt::note_and_explain_type_err.
Expand All @@ -144,7 +144,7 @@ pub fn suggest_arbitrary_trait_bound<'tcx>(
this requirement",
if generics.where_clause_span.is_empty() { "introducing a" } else { "extending the" },
),
format!("{} {}: {}", generics.add_where_or_trailing_comma(), param_name, constraint),
format!("{} {constraint}", generics.add_where_or_trailing_comma()),
Applicability::MaybeIncorrect,
);
true
Expand Down
16 changes: 16 additions & 0 deletions tests/ui/suggestions/correct-binder-for-arbitrary-bound-sugg.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
trait Foo
where
for<'a> &'a Self: Bar,
{
}

impl Foo for () {}

trait Bar {}

impl Bar for &() {}

fn foo<T: Foo>() {}
//~^ ERROR the trait bound `for<'a> &'a T: Bar` is not satisfied

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
error[E0277]: the trait bound `for<'a> &'a T: Bar` is not satisfied
--> $DIR/correct-binder-for-arbitrary-bound-sugg.rs:13:11
|
LL | fn foo<T: Foo>() {}
| ^^^ the trait `for<'a> Bar` is not implemented for `&'a T`
|
note: required by a bound in `Foo`
--> $DIR/correct-binder-for-arbitrary-bound-sugg.rs:3:23
|
LL | trait Foo
| --- required by a bound in this trait
LL | where
LL | for<'a> &'a Self: Bar,
| ^^^ required by this bound in `Foo`
help: consider introducing a `where` clause, but there might be an alternative better way to express this requirement
|
LL | fn foo<T: Foo>() where for<'a> &'a T: Bar {}
| ++++++++++++++++++++++++

error: aborting due to previous error

For more information about this error, try `rustc --explain E0277`.

0 comments on commit ac258e9

Please sign in to comment.