-
-
Notifications
You must be signed in to change notification settings - Fork 14.3k
add fut/back compat tests for implied trait bounds #93732
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 1 commit
Commits
Show all changes
2 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
51 changes: 51 additions & 0 deletions
51
src/test/ui/implied-bounds/hrlt-implied-trait-bounds-guard.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,51 @@ | ||
| // A test exploiting the bug behind #25860 except with | ||
| // implied trait bounds which currently don't exist, | ||
| // | ||
| // please ping @lcnr if your changes end up causing `badboi` to compile. | ||
| use std::marker::PhantomData; | ||
| struct Foo<'a, 'b, T>(PhantomData<(&'a (), &'b (), T)>) | ||
| where | ||
| T: Convert<'a, 'b>; | ||
|
|
||
| trait Convert<'a, 'b>: Sized { | ||
| fn cast(&'a self) -> &'b Self; | ||
| } | ||
| impl<'long: 'short, 'short, T> Convert<'long, 'short> for T { | ||
| fn cast(&'long self) -> &'short T { | ||
| self | ||
| } | ||
| } | ||
|
|
||
| // This function will compile once we add implied trait bounds. | ||
| // | ||
| // If we're not careful with our impl, the transformations | ||
| // in `bad` would succeed, which is unsound ✨ | ||
| // | ||
| // FIXME: the error is pretty bad, this should say | ||
| // | ||
| // `T: Convert<'in_, 'out>` is not implemented | ||
| // | ||
| // help: needed by `Foo<'in_, 'out, T>` | ||
| fn badboi<'in_, 'out, T>(x: Foo<'in_, 'out, T>, sadness: &'in_ T) -> &'out T { | ||
| //~^ ERROR lifetime mismatch | ||
| sadness.cast() | ||
| } | ||
|
|
||
| fn bad<'short, T>(value: &'short T) -> &'static T { | ||
| let x: for<'in_, 'out> fn(Foo<'in_, 'out, T>, &'in_ T) -> &'out T = badboi; | ||
| let x: for<'out> fn(Foo<'short, 'out, T>, &'short T) -> &'out T = x; | ||
| let x: for<'out> fn(Foo<'static, 'out, T>, &'short T) -> &'out T = x; | ||
| let x: fn(Foo<'static, 'static, T>, &'short T) -> &'static T = x; | ||
| x(Foo(PhantomData), value) | ||
| } | ||
|
|
||
| // Use `bad` to cause a segfault. | ||
| fn main() { | ||
| let mut outer: Option<&'static u32> = Some(&3); | ||
| let static_ref: &'static &'static u32 = match outer { | ||
| Some(ref reference) => bad(reference), | ||
| None => unreachable!(), | ||
| }; | ||
| outer = None; | ||
| println!("{}", static_ref); | ||
| } | ||
12 changes: 12 additions & 0 deletions
12
src/test/ui/implied-bounds/hrlt-implied-trait-bounds-guard.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,12 @@ | ||
| error[E0623]: lifetime mismatch | ||
| --> $DIR/hrlt-implied-trait-bounds-guard.rs:29:29 | ||
| | | ||
| LL | fn badboi<'in_, 'out, T>(x: Foo<'in_, 'out, T>, sadness: &'in_ T) -> &'out T { | ||
| | ^^^^^^^^^^^^^^^^^^ ------- | ||
| | | | ||
| | this parameter and the return type are declared with different lifetimes... | ||
| | ...but data from `x` is returned here | ||
|
|
||
| error: aborting due to previous error | ||
|
|
||
| For more information about this error, try `rustc --explain E0623`. |
35 changes: 35 additions & 0 deletions
35
src/test/ui/implied-bounds/hrlt-implied-trait-bounds-roundtrip.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,35 @@ | ||
| // check-pass | ||
| struct Foo<'a>(&'a ()) | ||
| where | ||
| (): Trait<'a>; | ||
|
|
||
| trait Trait<'a> { | ||
| fn id<T>(value: &'a T) -> &'static T; | ||
| } | ||
|
|
||
| impl Trait<'static> for () { | ||
| fn id<T>(value: &'static T) -> &'static T { | ||
| value | ||
| } | ||
| } | ||
|
|
||
| fn could_use_implied_bounds<'a, T>(_: Foo<'a>, x: &'a T) -> &'static T | ||
| where | ||
| (): Trait<'a>, // This could be an implied bound | ||
| { | ||
| <()>::id(x) | ||
| } | ||
|
|
||
| fn main() { | ||
| let bar: for<'a, 'b> fn(Foo<'a>, &'b ()) = |_, _| {}; | ||
|
|
||
| // If `could_use_implied_bounds` were to use implied bounds, | ||
| // keeping 'a late-bound, then we could assign that function | ||
| // to this variable. | ||
| let bar: for<'a> fn(Foo<'a>, &'a ()) = bar; | ||
|
|
||
| // In this case, the subtyping relation here would be unsound, | ||
| // allowing us to transmute lifetimes. This currently compiles | ||
| // because we incorrectly deal with implied bounds inside of binders. | ||
| let _bar: for<'a, 'b> fn(Foo<'a>, &'b ()) = bar; | ||
| } |
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.