-
-
Notifications
You must be signed in to change notification settings - Fork 15.5k
Require coherence of supertrait associated item bounds for dyn-compability #158915
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
theemathas
wants to merge
3
commits into
rust-lang:main
Choose a base branch
from
theemathas:coherent-dyn-compat
base: main
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.
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
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
48 changes: 48 additions & 0 deletions
48
tests/ui/associated-type-bounds/conflicting-bounds-different-generics-legitimate.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,48 @@ | ||
| //@ run-pass | ||
|
|
||
| // Legitimate use case #1: | ||
| // The `dyn Sub1` trait object ends up having two different "values" for `Assoc`. | ||
| // This is allowed because we know that the two values are for `Super<i32>` and | ||
| // `Super<i64>`, which can't possibly be the same trait. | ||
|
|
||
| trait Super<T> { | ||
| type Assoc; | ||
| } | ||
|
|
||
| trait Sub1: Super<i32, Assoc = u32> + Super<i64, Assoc = u64> { | ||
| fn method1(&self) {} | ||
| } | ||
|
|
||
| fn foo(x: &dyn Sub1) { | ||
| x.method1(); | ||
| } | ||
|
|
||
| // Legitimate use case #2: | ||
| // The `dyn Sub2<T, U>` trait object has "values" for `Assoc` specified twice, | ||
| // on different generics that might be actually equal types. | ||
| // This is allowed because the two values are the same, so there's no conflict. | ||
|
|
||
| trait Sub2<T, U>: Super<T, Assoc = u32> + Super<U, Assoc = u32> { | ||
| fn method2(&self) {} | ||
| } | ||
|
|
||
| fn bar<T, U>(x: &dyn Sub2<T, U>) { | ||
| x.method2(); | ||
| } | ||
|
|
||
| // Actually call the two functions. | ||
|
|
||
| struct Thing; | ||
| impl Super<i32> for Thing { | ||
| type Assoc = u32; | ||
| } | ||
| impl Super<i64> for Thing { | ||
| type Assoc = u64; | ||
| } | ||
| impl Sub1 for Thing {} | ||
| impl Sub2<i32, i32> for Thing {} | ||
|
|
||
| fn main() { | ||
| foo(&Thing); | ||
| bar(&Thing); | ||
| } |
28 changes: 28 additions & 0 deletions
28
tests/ui/associated-type-bounds/conflicting-bounds-different-generics-simple.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,28 @@ | ||
| // We previously accepted conflicting associated type bounds with different generics, | ||
| // which resulted in an ICE, since those generics can be instantiated with the | ||
| // same concrete type. | ||
| // See https://github.com/rust-lang/rust/issues/154662 | ||
|
|
||
| trait Super<T> { | ||
| type Assoc; | ||
| } | ||
|
|
||
| trait Sub<T, U>: Super<T, Assoc = u32> + Super<U, Assoc = u64> { | ||
| fn method(&self) {} | ||
| } | ||
|
|
||
| fn foo<T, U>(x: Option<&dyn Sub<T, U>>) { | ||
| //~^ ERROR the trait `Sub` is not dyn compatible | ||
| if false { | ||
| x.unwrap().method(); | ||
| //~^ ERROR the trait `Sub` is not dyn compatible | ||
| } | ||
| } | ||
|
|
||
| fn main() { | ||
| // This would end up proving that `dyn Sub<i16, i16>` implements `Super<i16>`. | ||
| // However, `dyn Sub<i16, i16>` has bounds for both `Assoc = u32` and `Assoc = u64`, | ||
| // which is nonsense. | ||
| foo::<i16, i16>(None); | ||
| //~^ ERROR the trait `Sub` is not dyn compatible | ||
| } |
51 changes: 51 additions & 0 deletions
51
tests/ui/associated-type-bounds/conflicting-bounds-different-generics-simple.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,51 @@ | ||
| error[E0038]: the trait `Sub` is not dyn compatible | ||
| --> $DIR/conflicting-bounds-different-generics-simple.rs:14:25 | ||
| | | ||
| LL | fn foo<T, U>(x: Option<&dyn Sub<T, U>>) { | ||
| | ^^^^^^^^^^^^^ `Sub` is not dyn compatible | ||
| | | ||
| note: for a trait to be dyn compatible it needs to allow building a vtable | ||
| for more information, visit <https://doc.rust-lang.org/reference/items/traits.html#dyn-compatibility> | ||
| --> $DIR/conflicting-bounds-different-generics-simple.rs:10:27 | ||
| | | ||
| LL | trait Sub<T, U>: Super<T, Assoc = u32> + Super<U, Assoc = u64> { | ||
| | --- ^^^^^^^^^^^ ^^^^^^^^^^^ ...because it has conflicting associated item bounds for `Assoc` in supertraits: `<Self as Super<T>>::Assoc == u32` and `<Self as Super<U>>::Assoc == u64` | ||
| | | | | ||
| | | ...because it has conflicting associated item bounds for `Assoc` in supertraits: `<Self as Super<T>>::Assoc == u32` and `<Self as Super<U>>::Assoc == u64` | ||
| | this trait is not dyn compatible... | ||
|
|
||
| error[E0038]: the trait `Sub` is not dyn compatible | ||
| --> $DIR/conflicting-bounds-different-generics-simple.rs:17:9 | ||
| | | ||
| LL | x.unwrap().method(); | ||
| | ^^^^^^^^^^ `Sub` is not dyn compatible | ||
| | | ||
| note: for a trait to be dyn compatible it needs to allow building a vtable | ||
| for more information, visit <https://doc.rust-lang.org/reference/items/traits.html#dyn-compatibility> | ||
| --> $DIR/conflicting-bounds-different-generics-simple.rs:10:27 | ||
| | | ||
| LL | trait Sub<T, U>: Super<T, Assoc = u32> + Super<U, Assoc = u64> { | ||
| | --- ^^^^^^^^^^^ ^^^^^^^^^^^ ...because it has conflicting associated item bounds for `Assoc` in supertraits: `<Self as Super<T>>::Assoc == u32` and `<Self as Super<U>>::Assoc == u64` | ||
| | | | | ||
| | | ...because it has conflicting associated item bounds for `Assoc` in supertraits: `<Self as Super<T>>::Assoc == u32` and `<Self as Super<U>>::Assoc == u64` | ||
| | this trait is not dyn compatible... | ||
|
|
||
| error[E0038]: the trait `Sub` is not dyn compatible | ||
| --> $DIR/conflicting-bounds-different-generics-simple.rs:26:21 | ||
| | | ||
| LL | foo::<i16, i16>(None); | ||
| | ^^^^ `Sub` is not dyn compatible | ||
| | | ||
| note: for a trait to be dyn compatible it needs to allow building a vtable | ||
| for more information, visit <https://doc.rust-lang.org/reference/items/traits.html#dyn-compatibility> | ||
| --> $DIR/conflicting-bounds-different-generics-simple.rs:10:27 | ||
| | | ||
| LL | trait Sub<T, U>: Super<T, Assoc = u32> + Super<U, Assoc = u64> { | ||
| | --- ^^^^^^^^^^^ ^^^^^^^^^^^ ...because it has conflicting associated item bounds for `Assoc` in supertraits: `<Self as Super<T>>::Assoc == u32` and `<Self as Super<U>>::Assoc == u64` | ||
| | | | | ||
| | | ...because it has conflicting associated item bounds for `Assoc` in supertraits: `<Self as Super<T>>::Assoc == u32` and `<Self as Super<U>>::Assoc == u64` | ||
| | this trait is not dyn compatible... | ||
|
|
||
| error: aborting due to 3 previous errors | ||
|
|
||
| For more information about this error, try `rustc --explain E0038`. |
28 changes: 28 additions & 0 deletions
28
tests/ui/associated-type-bounds/conflicting-bounds-different-generics-transitive.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,28 @@ | ||
| // Make sure that the dyn-compatibility check for coherent associated types | ||
| // work correctly when the associated type bounds come from the supertrait. | ||
|
|
||
| // Case #1: | ||
| // Both the conflicting bounds come from a supertrait. | ||
|
|
||
| trait OneSuper<T> { | ||
| type Assoc; | ||
| } | ||
| trait OneSub<T, U>: OneSuper<T, Assoc = u32> + OneSuper<U, Assoc = u64> {} | ||
| trait OneSubSub<T, U>: OneSub<T, U> {} | ||
|
|
||
| fn one_check<T, U>(_: &dyn OneSubSub<T, U>) {} | ||
| //~^ ERROR the trait `OneSubSub` is not dyn compatible | ||
|
|
||
| // Case #2: | ||
| // One of the conflicting bounds come from a supertrait, and one is direct. | ||
|
|
||
| trait TwoSuper<T> { | ||
| type Assoc; | ||
| } | ||
| trait TwoSub<T>: TwoSuper<T, Assoc = u32> {} | ||
| trait TwoSubSub<T, U>: TwoSub<T> + TwoSuper<U, Assoc = u64> {} | ||
|
|
||
| fn two_check<T, U>(_: &dyn TwoSubSub<T, U>) {} | ||
| //~^ ERROR the trait `TwoSubSub` is not dyn compatible | ||
|
|
||
| fn main() {} |
Oops, something went wrong.
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.
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.
The LLM, in reviewing my code, told me that I have to instantiate the
param_envwithtrait_argshere. However, I should not do this instantiation in thecan_equate_genericscomputation. Is the LLM right here? I don't understand at all what this does.View changes since the review