-
Notifications
You must be signed in to change notification settings - Fork 14k
Forbid freely casting lifetime bounds of dyn-types #136776
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
BoxyUwU
wants to merge
4
commits into
rust-lang:main
Choose a base branch
from
BoxyUwU:forbid_object_lifetime_casts
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 1 commit
Commits
Show all changes
4 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,49 @@ | ||
| //@ check-pass | ||
| // Test cases involving principal-less traits (dyn Send without a primary trait). | ||
|
|
||
| fn lifetime_cast_send<'a, 'b>(a: *mut (dyn Send + 'a)) -> *mut (dyn Send + 'b) { | ||
| a as _ | ||
| struct Wrapper<T: ?Sized>(T); | ||
|
|
||
| // Cast to same auto trait | ||
|
|
||
| fn unprincipled<'a, 'b>(x: *mut (dyn Send + 'a)) -> *mut (dyn Send + 'b) { | ||
| x as _ | ||
| //~^ ERROR: lifetime may not live long enough | ||
| } | ||
|
|
||
| fn unprincipled_static<'a>(x: *mut (dyn Send + 'a)) -> *mut (dyn Send + 'static) { | ||
| x as _ | ||
| //~^ ERROR: lifetime may not live long enough | ||
| } | ||
|
|
||
| fn unprincipled_wrap<'a, 'b>(x: *mut (dyn Send + 'a)) -> *mut Wrapper<dyn Send + 'b> { | ||
| x as _ | ||
| //~^ ERROR: lifetime may not live long enough | ||
| } | ||
|
|
||
| fn unprincipled_wrap_static<'a>(x: *mut (dyn Send + 'a)) -> *mut Wrapper<dyn Send + 'static> { | ||
| x as _ | ||
| //~^ ERROR: lifetime may not live long enough | ||
| } | ||
|
|
||
| // Cast to different auto trait | ||
|
|
||
| fn unprincipled2<'a, 'b>(x: *mut (dyn Send + 'a)) -> *mut (dyn Sync + 'b) { | ||
| x as _ | ||
| //~^ ERROR: lifetime may not live long enough | ||
| } | ||
|
|
||
| fn unprincipled2_static<'a>(x: *mut (dyn Send + 'a)) -> *mut (dyn Sync + 'static) { | ||
| x as _ | ||
| //~^ ERROR: lifetime may not live long enough | ||
| } | ||
|
|
||
| fn unprincipled_wrap2<'a, 'b>(x: *mut (dyn Send + 'a)) -> *mut Wrapper<dyn Sync + 'b> { | ||
| x as _ | ||
| //~^ ERROR: lifetime may not live long enough | ||
| } | ||
|
|
||
| fn unprincipled_wrap2_static<'a>(x: *mut (dyn Send + 'a)) -> *mut Wrapper<dyn Sync + 'static> { | ||
| x as _ | ||
| //~^ ERROR: lifetime may not live long enough | ||
| } | ||
|
|
||
| fn main() {} |
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,206 @@ | ||
| error: lifetime may not live long enough | ||
| --> $DIR/ptr-to-ptr-principalless.rs:8:5 | ||
| | | ||
| LL | fn unprincipled<'a, 'b>(x: *mut (dyn Send + 'a)) -> *mut (dyn Send + 'b) { | ||
| | -- -- lifetime `'b` defined here | ||
| | | | ||
| | lifetime `'a` defined here | ||
| LL | x as _ | ||
| | ^^^^^^ function was supposed to return data with lifetime `'b` but it is returning data with lifetime `'a` | ||
| | | ||
| = help: consider adding the following bound: `'a: 'b` | ||
| = note: requirement occurs because of a mutable pointer to `dyn Send` | ||
| = note: mutable pointers are invariant over their type parameter | ||
| = help: see <https://doc.rust-lang.org/nomicon/subtyping.html> for more information about variance | ||
| note: raw pointer casts of trait objects do not cast away lifetimes | ||
| --> $DIR/ptr-to-ptr-principalless.rs:8:5 | ||
| | | ||
| LL | x as _ | ||
| | ^^^^^^ | ||
| = note: this was previously accepted by the compiler but was changed recently | ||
| = help: see <https://github.com/rust-lang/rust/issues/141402> for more information | ||
|
|
||
| error: lifetime may not live long enough | ||
| --> $DIR/ptr-to-ptr-principalless.rs:13:5 | ||
| | | ||
| LL | fn unprincipled_static<'a>(x: *mut (dyn Send + 'a)) -> *mut (dyn Send + 'static) { | ||
| | -- lifetime `'a` defined here | ||
| LL | x as _ | ||
| | ^^^^^^ returning this value requires that `'a` must outlive `'static` | ||
| | | ||
| = note: requirement occurs because of a mutable pointer to `dyn Send` | ||
| = note: mutable pointers are invariant over their type parameter | ||
| = help: see <https://doc.rust-lang.org/nomicon/subtyping.html> for more information about variance | ||
| note: raw pointer casts of trait objects do not cast away lifetimes | ||
| --> $DIR/ptr-to-ptr-principalless.rs:13:5 | ||
| | | ||
| LL | x as _ | ||
| | ^^^^^^ | ||
| = note: this was previously accepted by the compiler but was changed recently | ||
| = help: see <https://github.com/rust-lang/rust/issues/141402> for more information | ||
| help: consider changing the trait object's explicit `'static` bound to the lifetime of argument `x` | ||
| | | ||
| LL - fn unprincipled_static<'a>(x: *mut (dyn Send + 'a)) -> *mut (dyn Send + 'static) { | ||
| LL + fn unprincipled_static<'a>(x: *mut (dyn Send + 'a)) -> *mut (dyn Send + 'a) { | ||
| | | ||
| help: alternatively, add an explicit `'static` bound to this reference | ||
| | | ||
| LL - fn unprincipled_static<'a>(x: *mut (dyn Send + 'a)) -> *mut (dyn Send + 'static) { | ||
| LL + fn unprincipled_static<'a>(x: *mut (dyn Send + 'static)) -> *mut (dyn Send + 'static) { | ||
| | | ||
|
|
||
| error: lifetime may not live long enough | ||
| --> $DIR/ptr-to-ptr-principalless.rs:18:5 | ||
| | | ||
| LL | fn unprincipled_wrap<'a, 'b>(x: *mut (dyn Send + 'a)) -> *mut Wrapper<dyn Send + 'b> { | ||
| | -- -- lifetime `'b` defined here | ||
| | | | ||
| | lifetime `'a` defined here | ||
| LL | x as _ | ||
| | ^^^^^^ function was supposed to return data with lifetime `'b` but it is returning data with lifetime `'a` | ||
| | | ||
| = help: consider adding the following bound: `'a: 'b` | ||
| = note: requirement occurs because of a mutable pointer to `Wrapper<dyn Send>` | ||
| = note: mutable pointers are invariant over their type parameter | ||
| = help: see <https://doc.rust-lang.org/nomicon/subtyping.html> for more information about variance | ||
| note: raw pointer casts of trait objects do not cast away lifetimes | ||
| --> $DIR/ptr-to-ptr-principalless.rs:18:5 | ||
| | | ||
| LL | x as _ | ||
| | ^^^^^^ | ||
| = note: this was previously accepted by the compiler but was changed recently | ||
| = help: see <https://github.com/rust-lang/rust/issues/141402> for more information | ||
|
|
||
| error: lifetime may not live long enough | ||
| --> $DIR/ptr-to-ptr-principalless.rs:23:5 | ||
| | | ||
| LL | fn unprincipled_wrap_static<'a>(x: *mut (dyn Send + 'a)) -> *mut Wrapper<dyn Send + 'static> { | ||
| | -- lifetime `'a` defined here | ||
| LL | x as _ | ||
| | ^^^^^^ returning this value requires that `'a` must outlive `'static` | ||
| | | ||
| = note: requirement occurs because of a mutable pointer to `Wrapper<dyn Send>` | ||
| = note: mutable pointers are invariant over their type parameter | ||
| = help: see <https://doc.rust-lang.org/nomicon/subtyping.html> for more information about variance | ||
| note: raw pointer casts of trait objects do not cast away lifetimes | ||
| --> $DIR/ptr-to-ptr-principalless.rs:23:5 | ||
| | | ||
| LL | x as _ | ||
| | ^^^^^^ | ||
| = note: this was previously accepted by the compiler but was changed recently | ||
| = help: see <https://github.com/rust-lang/rust/issues/141402> for more information | ||
| help: consider changing the trait object's explicit `'static` bound to the lifetime of argument `x` | ||
| | | ||
| LL - fn unprincipled_wrap_static<'a>(x: *mut (dyn Send + 'a)) -> *mut Wrapper<dyn Send + 'static> { | ||
| LL + fn unprincipled_wrap_static<'a>(x: *mut (dyn Send + 'a)) -> *mut Wrapper<dyn Send + 'a> { | ||
| | | ||
| help: alternatively, add an explicit `'static` bound to this reference | ||
| | | ||
| LL - fn unprincipled_wrap_static<'a>(x: *mut (dyn Send + 'a)) -> *mut Wrapper<dyn Send + 'static> { | ||
| LL + fn unprincipled_wrap_static<'a>(x: *mut (dyn Send + 'static)) -> *mut Wrapper<dyn Send + 'static> { | ||
| | | ||
|
|
||
| error: lifetime may not live long enough | ||
| --> $DIR/ptr-to-ptr-principalless.rs:30:5 | ||
| | | ||
| LL | fn unprincipled2<'a, 'b>(x: *mut (dyn Send + 'a)) -> *mut (dyn Sync + 'b) { | ||
| | -- -- lifetime `'b` defined here | ||
| | | | ||
| | lifetime `'a` defined here | ||
| LL | x as _ | ||
| | ^^^^^^ function was supposed to return data with lifetime `'b` but it is returning data with lifetime `'a` | ||
| | | ||
| = help: consider adding the following bound: `'a: 'b` | ||
| = note: requirement occurs because of a mutable pointer to `dyn Sync` | ||
| = note: mutable pointers are invariant over their type parameter | ||
| = help: see <https://doc.rust-lang.org/nomicon/subtyping.html> for more information about variance | ||
| note: raw pointer casts of trait objects do not cast away lifetimes | ||
| --> $DIR/ptr-to-ptr-principalless.rs:30:5 | ||
| | | ||
| LL | x as _ | ||
| | ^^^^^^ | ||
| = note: this was previously accepted by the compiler but was changed recently | ||
| = help: see <https://github.com/rust-lang/rust/issues/141402> for more information | ||
|
|
||
| error: lifetime may not live long enough | ||
| --> $DIR/ptr-to-ptr-principalless.rs:35:5 | ||
| | | ||
| LL | fn unprincipled2_static<'a>(x: *mut (dyn Send + 'a)) -> *mut (dyn Sync + 'static) { | ||
| | -- lifetime `'a` defined here | ||
| LL | x as _ | ||
| | ^^^^^^ returning this value requires that `'a` must outlive `'static` | ||
| | | ||
| = note: requirement occurs because of a mutable pointer to `dyn Sync` | ||
| = note: mutable pointers are invariant over their type parameter | ||
| = help: see <https://doc.rust-lang.org/nomicon/subtyping.html> for more information about variance | ||
| note: raw pointer casts of trait objects do not cast away lifetimes | ||
| --> $DIR/ptr-to-ptr-principalless.rs:35:5 | ||
| | | ||
| LL | x as _ | ||
| | ^^^^^^ | ||
| = note: this was previously accepted by the compiler but was changed recently | ||
| = help: see <https://github.com/rust-lang/rust/issues/141402> for more information | ||
| help: consider changing the trait object's explicit `'static` bound to the lifetime of argument `x` | ||
| | | ||
| LL - fn unprincipled2_static<'a>(x: *mut (dyn Send + 'a)) -> *mut (dyn Sync + 'static) { | ||
| LL + fn unprincipled2_static<'a>(x: *mut (dyn Send + 'a)) -> *mut (dyn Sync + 'a) { | ||
| | | ||
| help: alternatively, add an explicit `'static` bound to this reference | ||
| | | ||
| LL - fn unprincipled2_static<'a>(x: *mut (dyn Send + 'a)) -> *mut (dyn Sync + 'static) { | ||
| LL + fn unprincipled2_static<'a>(x: *mut (dyn Send + 'static)) -> *mut (dyn Sync + 'static) { | ||
| | | ||
|
|
||
| error: lifetime may not live long enough | ||
| --> $DIR/ptr-to-ptr-principalless.rs:40:5 | ||
| | | ||
| LL | fn unprincipled_wrap2<'a, 'b>(x: *mut (dyn Send + 'a)) -> *mut Wrapper<dyn Sync + 'b> { | ||
| | -- -- lifetime `'b` defined here | ||
| | | | ||
| | lifetime `'a` defined here | ||
| LL | x as _ | ||
| | ^^^^^^ function was supposed to return data with lifetime `'b` but it is returning data with lifetime `'a` | ||
| | | ||
| = help: consider adding the following bound: `'a: 'b` | ||
| = note: requirement occurs because of a mutable pointer to `Wrapper<dyn Sync>` | ||
| = note: mutable pointers are invariant over their type parameter | ||
| = help: see <https://doc.rust-lang.org/nomicon/subtyping.html> for more information about variance | ||
| note: raw pointer casts of trait objects do not cast away lifetimes | ||
| --> $DIR/ptr-to-ptr-principalless.rs:40:5 | ||
| | | ||
| LL | x as _ | ||
| | ^^^^^^ | ||
| = note: this was previously accepted by the compiler but was changed recently | ||
| = help: see <https://github.com/rust-lang/rust/issues/141402> for more information | ||
|
|
||
| error: lifetime may not live long enough | ||
| --> $DIR/ptr-to-ptr-principalless.rs:45:5 | ||
| | | ||
| LL | fn unprincipled_wrap2_static<'a>(x: *mut (dyn Send + 'a)) -> *mut Wrapper<dyn Sync + 'static> { | ||
| | -- lifetime `'a` defined here | ||
| LL | x as _ | ||
| | ^^^^^^ returning this value requires that `'a` must outlive `'static` | ||
| | | ||
| = note: requirement occurs because of a mutable pointer to `Wrapper<dyn Sync>` | ||
| = note: mutable pointers are invariant over their type parameter | ||
| = help: see <https://doc.rust-lang.org/nomicon/subtyping.html> for more information about variance | ||
| note: raw pointer casts of trait objects do not cast away lifetimes | ||
| --> $DIR/ptr-to-ptr-principalless.rs:45:5 | ||
| | | ||
| LL | x as _ | ||
| | ^^^^^^ | ||
| = note: this was previously accepted by the compiler but was changed recently | ||
| = help: see <https://github.com/rust-lang/rust/issues/141402> for more information | ||
| help: consider changing the trait object's explicit `'static` bound to the lifetime of argument `x` | ||
| | | ||
| LL - fn unprincipled_wrap2_static<'a>(x: *mut (dyn Send + 'a)) -> *mut Wrapper<dyn Sync + 'static> { | ||
| LL + fn unprincipled_wrap2_static<'a>(x: *mut (dyn Send + 'a)) -> *mut Wrapper<dyn Sync + 'a> { | ||
| | | ||
| help: alternatively, add an explicit `'static` bound to this reference | ||
| | | ||
| LL - fn unprincipled_wrap2_static<'a>(x: *mut (dyn Send + 'a)) -> *mut Wrapper<dyn Sync + 'static> { | ||
| LL + fn unprincipled_wrap2_static<'a>(x: *mut (dyn Send + 'static)) -> *mut Wrapper<dyn Sync + 'static> { | ||
| | | ||
|
|
||
| error: aborting due to 8 previous errors | ||
|
|
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
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.
@Darksonn I think this currently means that casting something like:
*mut dyn Trait + Send + 'ato*mut Send + 'bwon't check'a: 'b. We ought to drop thesrc_tty.principal().is_none()check here, unless I'm misreading this code :)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.
Could you help come up with a test that catches this? I tried adding a few, but I got the results I expected.
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.
gotta say I would have expected those tests to not be emitting errors right now and can't figure out just from skimming the code why they wouldn't be 🤔
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.
ah, it just happens to work due to MIR lowering jank. doing this kind of cast seems to result in both an
Unsizecast to drop the principal, and then also aPtrToPtrcast where neither side has a principal (which then gets lifetime checked from ur changes)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 is the mir from that test case. note the 3 separate pointer casts, one of which is a PtrToPtr between two pointers to
dyn Sendtrait objects xdUh 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.
Could ICE on encountering
(Some, None), run crater and if anything blows up then you've got a test case, otherwise just leave it like that and then in the future if someone files an ICE report we have a test case :>Alternatively we just write the code to handle (Some, None) properly and then hope it's correct without a test case
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.
Do you expect uncovered
(Some, None)cases to ICE rather than be accepted without error?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.
It makes sense that it happens in the coercion. After all, ptr-to-ptr casts are only possible in cases where the vtable is the same, but I would not expect
dyn T1 + Sendto have the same vtable asdyn Send. Especially considering that impliesdyn T1 + Sendhas same vtable asdyn Sendhas same vtable asdyn T2 + Send. So such ptr-to-ptr casts probably simply do not exist.