-
-
Notifications
You must be signed in to change notification settings - Fork 15.4k
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
2
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
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
31 changes: 31 additions & 0 deletions
31
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,31 @@ | ||
| //@ run-pass | ||
|
|
||
| // This is a legitimate use case, where the `dyn Sub` 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 Sub: Super<i32, Assoc = u32> + Super<i64, Assoc = u64> { | ||
| fn method(&self) {} | ||
| } | ||
|
|
||
| fn foo(x: &dyn Sub) { | ||
| x.method(); | ||
| } | ||
|
|
||
| struct Thing; | ||
| impl Super<i32> for Thing { | ||
| type Assoc = u32; | ||
| } | ||
| impl Super<i64> for Thing { | ||
| type Assoc = u64; | ||
| } | ||
| impl Sub for Thing {} | ||
|
|
||
| fn main() { | ||
| foo(&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 currently accept conflicting associated type bounds with different generics, | ||
| // which results 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 ends 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:7:10 | ||
| | | ||
| LL | type Assoc; | ||
| | ^^^^^ ...because it has conflicting associated item bounds for Assoc in supertraits | ||
| ... | ||
| LL | trait Sub<T, U>: Super<T, Assoc = u32> + 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:7:10 | ||
| | | ||
| LL | type Assoc; | ||
| | ^^^^^ ...because it has conflicting associated item bounds for Assoc in supertraits | ||
| ... | ||
| LL | trait Sub<T, U>: Super<T, Assoc = u32> + 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:7:10 | ||
| | | ||
| LL | type Assoc; | ||
| | ^^^^^ ...because it has conflicting associated item bounds for Assoc in supertraits | ||
| ... | ||
| LL | trait Sub<T, U>: Super<T, Assoc = u32> + 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`. |
55 changes: 55 additions & 0 deletions
55
tests/ui/associated-type-bounds/conflicting-bounds-different-generics-unsound.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,55 @@ | ||
| // We currently accept conflicting associated type bounds with different generics, | ||
| // which results in unsoundness, since those generics can be instantiated with the | ||
| // same concrete type. | ||
| // See https://github.com/rust-lang/rust/issues/154662 | ||
|
|
||
| type Payload = Box<i32>; | ||
| type Src<'a> = &'a Payload; | ||
| type Dst = &'static Payload; | ||
|
|
||
| trait Super<T> { | ||
| type Assoc; | ||
| } | ||
|
|
||
| trait Sub<'a, A1, A2>: Super<A1, Assoc = Src<'a>> + Super<A2, Assoc = Dst> {} | ||
|
|
||
| trait Callback<A1, A2> { | ||
| fn callback<U: Super<A1> + Super<A2> + ?Sized>( | ||
| payload: <U as Super<A1>>::Assoc, | ||
| ) -> <U as Super<A2>>::Assoc; | ||
| } | ||
| struct CallbackStruct; | ||
| impl Callback<i16, i16> for CallbackStruct { | ||
| fn callback<U: Super<i16> + ?Sized>(payload: U::Assoc) -> U::Assoc { | ||
| payload | ||
| } | ||
| } | ||
|
|
||
| fn require_trait< | ||
| 'a, | ||
| A1, | ||
| A2, | ||
| U: Super<A1, Assoc = Src<'a>> + Super<A2, Assoc = Dst> + ?Sized, | ||
| C: Callback<A1, A2>, | ||
| >( | ||
| payload: Src<'a>, | ||
| ) -> Dst { | ||
| C::callback::<U>(payload) | ||
| } | ||
|
|
||
| fn use_dyn<'a, A1, A2, C: Callback<A1, A2>>(payload: Src<'a>) -> Dst { | ||
| require_trait::<'a, A1, A2, dyn Sub<'a, A1, A2>, C>(payload) | ||
| //~^ ERROR the trait `Sub` is not dyn compatible | ||
| } | ||
|
|
||
| fn extend<'a>(payload: Src<'a>) -> Dst { | ||
| // `dyn Sub<'a, i16, i16>` has both an `Assoc = Src<'a>` bound and an `Assoc = Dst` bound. | ||
| use_dyn::<i16, i16, CallbackStruct>(payload) | ||
| } | ||
|
|
||
| fn main() { | ||
| let payload: Box<Payload> = Box::new(Box::new(1)); | ||
| let wrong: &'static Payload = extend(&*payload); | ||
| drop(payload); | ||
| println!("{wrong}"); | ||
| } |
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