Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Place binder correctly for arbitrary trait bound suggestion #108294

Merged
merged 1 commit into from
Mar 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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`.