Skip to content

Commit

Permalink
Place binder correctly for arbitrary trait bound suggestion
Browse files Browse the repository at this point in the history
  • Loading branch information
compiler-errors committed Feb 21, 2023
1 parent 8f55d60 commit b2f8f60
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 @@ -118,7 +118,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 @@ -145,7 +145,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
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 b2f8f60

Please sign in to comment.