-
Notifications
You must be signed in to change notification settings - Fork 13.7k
Make sure to treat only param where clauses as inherent #145262
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
Merged
Merged
Changes from all commits
Commits
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
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,17 @@ | ||
// Regression test for <github.com/rust-lang/rust/issues/145185>. | ||
|
||
mod module { | ||
pub trait Trait { | ||
fn method(&self); | ||
} | ||
} | ||
|
||
// Note that we do not import Trait | ||
use std::ops::Deref; | ||
|
||
fn foo(x: impl Deref<Target: module::Trait>) { | ||
x.method(); | ||
//~^ ERROR no method named `method` found for type parameter | ||
} | ||
|
||
fn main() {} |
17 changes: 17 additions & 0 deletions
17
tests/ui/methods/rigid-alias-bound-is-not-inherent-2.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,17 @@ | ||
error[E0599]: no method named `method` found for type parameter `impl Deref<Target : module::Trait>` in the current scope | ||
--> $DIR/rigid-alias-bound-is-not-inherent-2.rs:13:7 | ||
| | ||
LL | fn foo(x: impl Deref<Target: module::Trait>) { | ||
| --------------------------------- method `method` not found for this type parameter | ||
LL | x.method(); | ||
| ^^^^^^ method not found in `impl Deref<Target : module::Trait>` | ||
| | ||
= help: items from traits can only be used if the trait is in scope | ||
help: trait `Trait` which provides `method` is implemented but not in scope; perhaps you want to import it | ||
| | ||
LL + use module::Trait; | ||
| | ||
|
||
error: aborting due to 1 previous error | ||
|
||
For more information about this error, try `rustc --explain E0599`. |
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,26 @@ | ||
use std::ops::Deref; | ||
|
||
trait Trait1 { | ||
fn call_me(&self) {} | ||
} | ||
|
||
impl<T> Trait1 for T {} | ||
|
||
trait Trait2 { | ||
fn call_me(&self) {} | ||
} | ||
|
||
impl<T> Trait2 for T {} | ||
|
||
pub fn foo<T, U>(x: T) | ||
where | ||
T: Deref<Target = U>, | ||
U: Trait1, | ||
{ | ||
// This should be ambiguous. The fact that there's an inherent where-bound | ||
// candidate for `U` should not impact the candidates for `T` | ||
x.call_me(); | ||
//~^ ERROR multiple applicable items in scope | ||
} | ||
|
||
fn main() {} |
30 changes: 30 additions & 0 deletions
30
tests/ui/methods/rigid-alias-bound-is-not-inherent-3.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,30 @@ | ||
error[E0034]: multiple applicable items in scope | ||
--> $DIR/rigid-alias-bound-is-not-inherent-3.rs:22:7 | ||
| | ||
LL | x.call_me(); | ||
| ^^^^^^^ multiple `call_me` found | ||
| | ||
note: candidate #1 is defined in an impl of the trait `Trait1` for the type `T` | ||
--> $DIR/rigid-alias-bound-is-not-inherent-3.rs:4:5 | ||
| | ||
LL | fn call_me(&self) {} | ||
| ^^^^^^^^^^^^^^^^^ | ||
note: candidate #2 is defined in an impl of the trait `Trait2` for the type `T` | ||
--> $DIR/rigid-alias-bound-is-not-inherent-3.rs:10:5 | ||
| | ||
LL | fn call_me(&self) {} | ||
| ^^^^^^^^^^^^^^^^^ | ||
help: disambiguate the method for candidate #1 | ||
| | ||
LL - x.call_me(); | ||
LL + Trait1::call_me(&x); | ||
| | ||
help: disambiguate the method for candidate #2 | ||
| | ||
LL - x.call_me(); | ||
LL + Trait2::call_me(&x); | ||
| | ||
|
||
error: aborting due to 1 previous error | ||
|
||
For more information about this error, try `rustc --explain E0034`. |
30 changes: 30 additions & 0 deletions
30
tests/ui/methods/rigid-alias-bound-is-not-inherent.current.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,30 @@ | ||
error[E0034]: multiple applicable items in scope | ||
--> $DIR/rigid-alias-bound-is-not-inherent.rs:42:7 | ||
| | ||
LL | x.method(); | ||
| ^^^^^^ multiple `method` found | ||
| | ||
note: candidate #1 is defined in the trait `Trait1` | ||
--> $DIR/rigid-alias-bound-is-not-inherent.rs:21:5 | ||
| | ||
LL | fn method(&self) { | ||
| ^^^^^^^^^^^^^^^^ | ||
note: candidate #2 is defined in an impl of the trait `Trait2` for the type `T` | ||
--> $DIR/rigid-alias-bound-is-not-inherent.rs:27:5 | ||
| | ||
LL | fn method(&self) { | ||
| ^^^^^^^^^^^^^^^^ | ||
help: disambiguate the method for candidate #1 | ||
| | ||
LL - x.method(); | ||
LL + Trait1::method(&x); | ||
| | ||
help: disambiguate the method for candidate #2 | ||
| | ||
LL - x.method(); | ||
LL + Trait2::method(&x); | ||
| | ||
|
||
error: aborting due to 1 previous error | ||
|
||
For more information about this error, try `rustc --explain E0034`. |
30 changes: 30 additions & 0 deletions
30
tests/ui/methods/rigid-alias-bound-is-not-inherent.next.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,30 @@ | ||
error[E0034]: multiple applicable items in scope | ||
--> $DIR/rigid-alias-bound-is-not-inherent.rs:42:7 | ||
| | ||
LL | x.method(); | ||
| ^^^^^^ multiple `method` found | ||
| | ||
note: candidate #1 is defined in the trait `Trait1` | ||
--> $DIR/rigid-alias-bound-is-not-inherent.rs:21:5 | ||
| | ||
LL | fn method(&self) { | ||
| ^^^^^^^^^^^^^^^^ | ||
note: candidate #2 is defined in the trait `Trait2` | ||
--> $DIR/rigid-alias-bound-is-not-inherent.rs:27:5 | ||
| | ||
LL | fn method(&self) { | ||
| ^^^^^^^^^^^^^^^^ | ||
help: disambiguate the method for candidate #1 | ||
| | ||
LL - x.method(); | ||
LL + Trait1::method(&x); | ||
| | ||
help: disambiguate the method for candidate #2 | ||
| | ||
LL - x.method(); | ||
LL + Trait2::method(&x); | ||
| | ||
|
||
error: aborting due to 1 previous error | ||
|
||
For more information about this error, try `rustc --explain E0034`. |
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,46 @@ | ||
//@ revisions: current next | ||
//@ ignore-compare-mode-next-solver (explicit revisions) | ||
//@[next] compile-flags: -Znext-solver | ||
|
||
// See the code below. | ||
// | ||
// We were using `DeepRejectCtxt` to ensure that `assemble_inherent_candidates_from_param` | ||
// did not rely on the param-env being eagerly normalized. Since aliases unify with all | ||
// types, this meant that a rigid param-env candidate like `<T as Deref>::Target: Trait1` | ||
// would be registered as a "WhereClauseCandidate", which is treated as inherent. Since | ||
// we evaluate these candidates for all self types in the deref chain, this candidate | ||
// would be satisfied for `<T as Deref>::Target`, meaning that it would be preferred over | ||
// an "extension" candidate like `<T as Deref>::Target: Trait2` even though it holds. | ||
// This is problematic, since it causes ambiguities to be broken somewhat arbitrarily. | ||
// And as a side-effect, it also caused our computation of "used" traits to be miscalculated | ||
// since inherent candidates don't count as an import usage. | ||
compiler-errors marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
use std::ops::Deref; | ||
|
||
trait Trait1 { | ||
fn method(&self) { | ||
println!("1"); | ||
} | ||
} | ||
|
||
trait Trait2 { | ||
fn method(&self) { | ||
println!("2"); | ||
} | ||
} | ||
impl<T: Other + ?Sized> Trait2 for T {} | ||
|
||
trait Other {} | ||
|
||
fn foo<T>(x: T) | ||
where | ||
T: Deref, | ||
<T as Deref>::Target: Trait1 + Other, | ||
{ | ||
// Make sure that we don't prefer methods from where clauses for rigid aliases, | ||
// just for params. We could revisit this behavior, but it would be a lang change. | ||
x.method(); | ||
//~^ ERROR multiple applicable items in scope | ||
} | ||
|
||
fn main() {} |
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.
We could fast path this for the old trait solver, since we expect things to be normalized always.
Uh oh!
There was an error while loading. Please reload this page.
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.
hm 🤔
so what's going on is:
assemble_inherent_candidates_from_param
We can't normalize in assembly because it doesn't use a probe, so we instead do it here. This totally makes sense to me, r=me on this impl
Can you add a comment to
assemble_inherent_candidates_from_param
that we only filter thebounds
as an fast-path optimization and we properly reject non-param self type where-clauses inconsider_probe
?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.
actually, move this
structurally_normalize_ty
above thexform
and then use the normalizedty
inxform_self_ty
? This should avoid some work given that this change seems to actually impact perf in the new solverUh oh!
There was an error while loading. Please reload this page.
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.
I don't see how moving it would change things for the better here, and it makes the structure kinda worse, so I'm not going to do this. I'm happy to review the change if you'd like to make a follow-up.
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.
My theory here is that the perf improvement derives from us pruning candidates with this check that would otherwise need to do unnecessary work below.
Maybe structurally normalizing eagerly here would mean we wouldn't normalize when proving things in the un-pruned (i.e. actually param) candidates, but I didn't want to tangle those changes into this PR.