-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Suggest derive(Trait)
or T: Trait
from transitive obligation in some cases
#127997
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
Open
estebank
wants to merge
3
commits into
rust-lang:master
Choose a base branch
from
estebank:missing-trait-suggestion
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+313
−11
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1363,7 +1363,111 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { | |
"the following trait bounds were not satisfied:\n{bound_list}" | ||
)); | ||
} | ||
suggested_derive = self.suggest_derive(&mut err, unsatisfied_predicates); | ||
|
||
let mut suggest_derive = true; | ||
for (pred, _, cause) in unsatisfied_predicates { | ||
let Some(ty::PredicateKind::Clause(ty::ClauseKind::Trait(trait_pred))) = | ||
pred.kind().no_bound_vars() | ||
else { | ||
continue; | ||
}; | ||
let lang = tcx.lang_items(); | ||
if ![ | ||
lang.copy_trait(), | ||
lang.clone_trait(), | ||
tcx.get_diagnostic_item(sym::Debug), | ||
tcx.get_diagnostic_item(sym::PartialEq), | ||
tcx.get_diagnostic_item(sym::Default), | ||
] | ||
.contains(&Some(trait_pred.def_id())) | ||
{ | ||
// We restrict ourselves only to built-in `derive`s. | ||
continue; | ||
} | ||
let (adt, params) = match trait_pred.self_ty().kind() { | ||
ty::Adt(adt, params) if adt.did().is_local() => (*adt, params), | ||
_ => continue, | ||
}; | ||
if tcx | ||
.all_impls(trait_pred.def_id()) | ||
.filter_map(|imp_did| tcx.impl_trait_header(imp_did).map(|h| (imp_did, h))) | ||
.filter(|(did, header)| { | ||
let imp = header.trait_ref.instantiate_identity(); | ||
let impl_adt = match imp.self_ty().ty_adt_def() { | ||
Some(impl_adt) if adt.did().is_local() => impl_adt, | ||
_ => return false, | ||
}; | ||
header.polarity == ty::ImplPolarity::Positive | ||
&& impl_adt == adt | ||
&& tcx.is_automatically_derived(*did) | ||
}) | ||
.count() | ||
== 1 | ||
{ | ||
// We now know that for this predicate, there *was* a `derive(Trait)` for | ||
// the trait at hand, so we don't want to suggest writing that again. | ||
for param in ¶ms[..] { | ||
// Look at the type parameters for the currently obligated type to see | ||
// if a restriciton of `TypeParam: Trait` would help. If the | ||
// instantiated type param is not a type param but instead an actual | ||
// type, see if we can suggest `derive(Trait)` on *that* type. | ||
// See `tests/ui/suggestions/f1000.rs` | ||
let Some(ty) = param.as_type() else { | ||
continue; | ||
}; | ||
match ty.kind() { | ||
ty::Adt(adt, _) if adt.did().is_local() => { | ||
// The type param at hand is a local type, try to suggest | ||
// `derive(Trait)`. | ||
let trait_ref = | ||
ty::TraitRef::identity(tcx, trait_pred.trait_ref.def_id) | ||
.with_self_ty(tcx, ty); | ||
let trait_pred = ty::Binder::dummy(ty::TraitPredicate { | ||
trait_ref, | ||
polarity: ty::PredicatePolarity::Positive, | ||
}); | ||
suggested_derive = self.suggest_derive( | ||
&mut err, | ||
&[( | ||
<_ as ty::UpcastFrom<_, _>>::upcast_from( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't use |
||
trait_pred, self.tcx, | ||
), | ||
None, | ||
cause.clone(), | ||
)], | ||
); | ||
} | ||
ty::Param(_) => { | ||
// It was a type param. See if it corresponds to the current | ||
// `fn` and suggest `T: Trait`. | ||
if let Some(obligation) = cause { | ||
let trait_ref = ty::TraitRef::new( | ||
tcx, | ||
trait_pred.trait_ref.def_id, | ||
[ty], | ||
); | ||
let trait_pred = ty::Binder::dummy(ty::TraitPredicate { | ||
trait_ref, | ||
polarity: ty::PredicatePolarity::Positive, | ||
}); | ||
suggested_derive = | ||
self.err_ctxt().suggest_restricting_param_bound( | ||
&mut err, | ||
trait_pred, | ||
None, | ||
obligation.body_id, | ||
); | ||
} | ||
} | ||
_ => {} | ||
} | ||
} | ||
suggest_derive = false | ||
} | ||
} | ||
if suggest_derive { | ||
suggested_derive = self.suggest_derive(&mut err, unsatisfied_predicates); | ||
} | ||
|
||
unsatisfied_bounds = true; | ||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
tests/ui/suggestions/type-or-type-param-missing-transitive-trait-contraint.fixed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
//@ run-rustfix | ||
#![allow(warnings)] | ||
use std::collections::HashMap; | ||
|
||
#[derive(Clone)] | ||
struct Ctx<A> { | ||
a_map: HashMap<String, B<A>>, | ||
} | ||
|
||
#[derive(Clone, PartialEq, Eq)] | ||
struct B<A> { | ||
a: A, | ||
} | ||
|
||
fn foo<Z: std::clone::Clone>(ctx: &mut Ctx<Z>) { | ||
let a_map = ctx.a_map.clone(); //~ ERROR E0599 | ||
} | ||
|
||
#[derive(Clone)] | ||
#[derive(PartialEq)] | ||
#[derive(Eq)] | ||
struct S; | ||
fn bar(ctx: &mut Ctx<S>) { | ||
let a_map = ctx.a_map.clone(); //~ ERROR E0599 | ||
} | ||
|
||
fn qux<Z: std::cmp::Eq>(ctx: &mut Ctx<Z>) where Z: PartialEq { | ||
ctx.a_map["a"].eq(&ctx.a_map["a"]); //~ ERROR E0599 | ||
<_ as Eq>::assert_receiver_is_total_eq(&ctx.a_map["a"]); //~ ERROR E0277 | ||
} | ||
|
||
fn qut(ctx: &mut Ctx<S>) { | ||
ctx.a_map["a"].eq(&ctx.a_map["a"]); //~ ERROR E0599 | ||
<_ as Eq>::assert_receiver_is_total_eq(&ctx.a_map["a"]); //~ ERROR E0277 | ||
} | ||
|
||
fn main() {} |
34 changes: 34 additions & 0 deletions
34
tests/ui/suggestions/type-or-type-param-missing-transitive-trait-contraint.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
//@ run-rustfix | ||
#![allow(warnings)] | ||
use std::collections::HashMap; | ||
|
||
#[derive(Clone)] | ||
struct Ctx<A> { | ||
a_map: HashMap<String, B<A>>, | ||
} | ||
|
||
#[derive(Clone, PartialEq, Eq)] | ||
struct B<A> { | ||
a: A, | ||
} | ||
|
||
fn foo<Z>(ctx: &mut Ctx<Z>) { | ||
let a_map = ctx.a_map.clone(); //~ ERROR E0599 | ||
} | ||
|
||
struct S; | ||
fn bar(ctx: &mut Ctx<S>) { | ||
let a_map = ctx.a_map.clone(); //~ ERROR E0599 | ||
} | ||
|
||
fn qux<Z>(ctx: &mut Ctx<Z>) { | ||
ctx.a_map["a"].eq(&ctx.a_map["a"]); //~ ERROR E0599 | ||
<_ as Eq>::assert_receiver_is_total_eq(&ctx.a_map["a"]); //~ ERROR E0277 | ||
} | ||
|
||
fn qut(ctx: &mut Ctx<S>) { | ||
ctx.a_map["a"].eq(&ctx.a_map["a"]); //~ ERROR E0599 | ||
<_ as Eq>::assert_receiver_is_total_eq(&ctx.a_map["a"]); //~ ERROR E0277 | ||
} | ||
|
||
fn main() {} |
127 changes: 127 additions & 0 deletions
127
tests/ui/suggestions/type-or-type-param-missing-transitive-trait-contraint.stderr
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
error[E0599]: the method `clone` exists for struct `HashMap<String, B<Z>>`, but its trait bounds were not satisfied | ||
--> $DIR/type-or-type-param-missing-transitive-trait-contraint.rs:16:27 | ||
| | ||
LL | struct B<A> { | ||
| ----------- doesn't satisfy `B<Z>: Clone` | ||
... | ||
LL | let a_map = ctx.a_map.clone(); | ||
| ^^^^^ method cannot be called on `HashMap<String, B<Z>>` due to unsatisfied trait bounds | ||
| | ||
= note: the following trait bounds were not satisfied: | ||
`B<Z>: Clone` | ||
which is required by `HashMap<String, B<Z>>: Clone` | ||
help: consider restricting type parameter `Z` | ||
| | ||
LL | fn foo<Z: std::clone::Clone>(ctx: &mut Ctx<Z>) { | ||
| +++++++++++++++++++ | ||
|
||
error[E0599]: the method `clone` exists for struct `HashMap<String, B<S>>`, but its trait bounds were not satisfied | ||
--> $DIR/type-or-type-param-missing-transitive-trait-contraint.rs:21:27 | ||
| | ||
LL | struct B<A> { | ||
| ----------- doesn't satisfy `B<S>: Clone` | ||
... | ||
LL | let a_map = ctx.a_map.clone(); | ||
| ^^^^^ method cannot be called on `HashMap<String, B<S>>` due to unsatisfied trait bounds | ||
| | ||
= note: the following trait bounds were not satisfied: | ||
`B<S>: Clone` | ||
which is required by `HashMap<String, B<S>>: Clone` | ||
help: consider annotating `S` with `#[derive(Clone)]` | ||
| | ||
LL + #[derive(Clone)] | ||
LL | struct S; | ||
| | ||
|
||
error[E0599]: the method `eq` exists for struct `B<Z>`, but its trait bounds were not satisfied | ||
--> $DIR/type-or-type-param-missing-transitive-trait-contraint.rs:25:20 | ||
| | ||
LL | struct B<A> { | ||
| ----------- method `eq` not found for this struct because it doesn't satisfy `B<Z>: Iterator` or `B<Z>: PartialEq` | ||
... | ||
LL | ctx.a_map["a"].eq(&ctx.a_map["a"]); | ||
| ^^ method cannot be called on `B<Z>` due to unsatisfied trait bounds | ||
| | ||
note: trait bound `Z: PartialEq` was not satisfied | ||
--> $DIR/type-or-type-param-missing-transitive-trait-contraint.rs:10:17 | ||
| | ||
LL | #[derive(Clone, PartialEq, Eq)] | ||
| ^^^^^^^^^ unsatisfied trait bound introduced in this `derive` macro | ||
= note: the following trait bounds were not satisfied: | ||
`B<Z>: Iterator` | ||
which is required by `&mut B<Z>: Iterator` | ||
note: the trait `Iterator` must be implemented | ||
--> $SRC_DIR/core/src/iter/traits/iterator.rs:LL:COL | ||
help: consider restricting the type parameter to satisfy the trait bound | ||
| | ||
LL | fn qux<Z>(ctx: &mut Ctx<Z>) where Z: PartialEq { | ||
| ++++++++++++++++++ | ||
|
||
error[E0277]: the trait bound `Z: Eq` is not satisfied | ||
--> $DIR/type-or-type-param-missing-transitive-trait-contraint.rs:26:6 | ||
| | ||
LL | <_ as Eq>::assert_receiver_is_total_eq(&ctx.a_map["a"]); | ||
| ^ the trait `Eq` is not implemented for `Z`, which is required by `B<Z>: Eq` | ||
| | ||
note: required for `B<Z>` to implement `Eq` | ||
--> $DIR/type-or-type-param-missing-transitive-trait-contraint.rs:10:28 | ||
| | ||
LL | #[derive(Clone, PartialEq, Eq)] | ||
| ^^ unsatisfied trait bound introduced in this `derive` macro | ||
= note: this error originates in the derive macro `Eq` (in Nightly builds, run with -Z macro-backtrace for more info) | ||
help: consider restricting type parameter `Z` | ||
| | ||
LL | fn qux<Z: std::cmp::Eq>(ctx: &mut Ctx<Z>) { | ||
| ++++++++++++++ | ||
|
||
error[E0599]: the method `eq` exists for struct `B<S>`, but its trait bounds were not satisfied | ||
--> $DIR/type-or-type-param-missing-transitive-trait-contraint.rs:30:20 | ||
| | ||
LL | struct B<A> { | ||
| ----------- method `eq` not found for this struct because it doesn't satisfy `B<S>: Iterator` or `B<S>: PartialEq` | ||
... | ||
LL | struct S; | ||
| -------- doesn't satisfy `S: PartialEq` | ||
... | ||
LL | ctx.a_map["a"].eq(&ctx.a_map["a"]); | ||
| ^^ method cannot be called on `B<S>` due to unsatisfied trait bounds | ||
| | ||
note: trait bound `S: PartialEq` was not satisfied | ||
--> $DIR/type-or-type-param-missing-transitive-trait-contraint.rs:10:17 | ||
| | ||
LL | #[derive(Clone, PartialEq, Eq)] | ||
| ^^^^^^^^^ unsatisfied trait bound introduced in this `derive` macro | ||
= note: the following trait bounds were not satisfied: | ||
`B<S>: Iterator` | ||
which is required by `&mut B<S>: Iterator` | ||
note: the trait `Iterator` must be implemented | ||
--> $SRC_DIR/core/src/iter/traits/iterator.rs:LL:COL | ||
help: consider annotating `S` with `#[derive(PartialEq)]` | ||
| | ||
LL + #[derive(PartialEq)] | ||
LL | struct S; | ||
| | ||
|
||
error[E0277]: the trait bound `S: Eq` is not satisfied | ||
--> $DIR/type-or-type-param-missing-transitive-trait-contraint.rs:31:6 | ||
| | ||
LL | <_ as Eq>::assert_receiver_is_total_eq(&ctx.a_map["a"]); | ||
| ^ the trait `Eq` is not implemented for `S`, which is required by `B<S>: Eq` | ||
| | ||
= help: the trait `Eq` is implemented for `B<A>` | ||
note: required for `B<S>` to implement `Eq` | ||
--> $DIR/type-or-type-param-missing-transitive-trait-contraint.rs:10:28 | ||
| | ||
LL | #[derive(Clone, PartialEq, Eq)] | ||
| ^^ unsatisfied trait bound introduced in this `derive` macro | ||
= note: this error originates in the derive macro `Eq` (in Nightly builds, run with -Z macro-backtrace for more info) | ||
help: consider annotating `S` with `#[derive(Eq)]` | ||
| | ||
LL + #[derive(Eq)] | ||
LL | struct S; | ||
| | ||
|
||
error: aborting due to 6 previous errors | ||
|
||
Some errors have detailed explanations: E0277, E0599. | ||
For more information about an error, try `rustc --explain E0277`. |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This isn't right :(
PartialEq
has two type parameters --Self
andRhs
. This only substitutes the first one.