-
Notifications
You must be signed in to change notification settings - Fork 14.1k
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
6
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.
+635
−21
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
0b2db02
Forbid object lifetime changing pointer casts
BoxyUwU 2eace27
Add test for principal-less cast
Darksonn 8a06cde
Add check for non-principal trait casts
Darksonn 152a6f1
Test for casts removing principal trait
Darksonn 5a4d526
tidy
BoxyUwU 06e4c91
reviews
BoxyUwU 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
Some comments aren't visible on the classic Files Changed page.
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
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
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,11 @@ | ||
| //@ check-pass | ||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. comment pls |
||
| trait Trait { | ||
| fn foo(&self) {} | ||
| } | ||
|
|
||
| fn bar<'a>(a: *mut *mut (dyn Trait + 'a)) -> *mut *mut (dyn Trait + 'static) { | ||
| a as _ | ||
| } | ||
|
|
||
| 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
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 @@ | ||
| error: lifetime may not live long enough | ||
| --> $DIR/ptr-to-ptr-different-regions.rs:17:5 | ||
| | | ||
| LL | fn assert_static<'a>(ptr: *mut (dyn Trait + 'a)) -> *mut (dyn Trait + 'static) { | ||
| | -- lifetime `'a` defined here | ||
| LL | ptr as _ | ||
| | ^^^^^^^^ returning this value requires that `'a` must outlive `'static` | ||
| | | ||
| = note: requirement occurs because of a mutable pointer to `dyn Trait` | ||
| = 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 cannot extend lifetimes | ||
| --> $DIR/ptr-to-ptr-different-regions.rs:17:5 | ||
| | | ||
| LL | ptr 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 `ptr` | ||
| | | ||
| LL - fn assert_static<'a>(ptr: *mut (dyn Trait + 'a)) -> *mut (dyn Trait + 'static) { | ||
| LL + fn assert_static<'a>(ptr: *mut (dyn Trait + 'a)) -> *mut (dyn Trait + 'a) { | ||
| | | ||
| help: alternatively, add an explicit `'static` bound to this reference | ||
| | | ||
| LL - fn assert_static<'a>(ptr: *mut (dyn Trait + 'a)) -> *mut (dyn Trait + 'static) { | ||
| LL + fn assert_static<'a>(ptr: *mut (dyn Trait + 'static)) -> *mut (dyn Trait + 'static) { | ||
| | | ||
|
|
||
| error: aborting due to 1 previous error | ||
|
|
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 @@ | ||
| trait Trait { | ||
| fn foo(&self) {} | ||
| } | ||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same, maybe also merge with the above test? |
||
| struct MyWrap<T: ?Sized>(T); | ||
|
|
||
| fn bar<'a>(a: *mut MyWrap<(dyn Trait + 'a)>) -> *mut MyWrap<(dyn Trait + 'static)> { | ||
| a as _ | ||
| //~^ ERROR: lifetime may not live long enough | ||
| } | ||
|
|
||
| fn main() {} | ||
31 changes: 31 additions & 0 deletions
31
tests/ui/cast/ptr-to-ptr-indirect-different-regions.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,31 @@ | ||
| error: lifetime may not live long enough | ||
| --> $DIR/ptr-to-ptr-indirect-different-regions.rs:8:5 | ||
| | | ||
| LL | fn bar<'a>(a: *mut MyWrap<(dyn Trait + 'a)>) -> *mut MyWrap<(dyn Trait + 'static)> { | ||
| | -- lifetime `'a` defined here | ||
| LL | a as _ | ||
| | ^^^^^^ returning this value requires that `'a` must outlive `'static` | ||
| | | ||
| = note: requirement occurs because of a mutable pointer to `MyWrap<dyn Trait>` | ||
| = 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 cannot extend lifetimes | ||
| --> $DIR/ptr-to-ptr-indirect-different-regions.rs:8:5 | ||
| | | ||
| LL | a 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 `a` | ||
| | | ||
| LL - fn bar<'a>(a: *mut MyWrap<(dyn Trait + 'a)>) -> *mut MyWrap<(dyn Trait + 'static)> { | ||
| LL + fn bar<'a>(a: *mut MyWrap<(dyn Trait + 'a)>) -> *mut MyWrap<(dyn Trait + 'a)> { | ||
| | | ||
| help: alternatively, add an explicit `'static` bound to this reference | ||
| | | ||
| LL - fn bar<'a>(a: *mut MyWrap<(dyn Trait + 'a)>) -> *mut MyWrap<(dyn Trait + 'static)> { | ||
| LL + fn bar<'a>(a: *mut MyWrap<(dyn Trait + 'static)>) -> *mut MyWrap<(dyn Trait + 'static)> { | ||
| | | ||
|
|
||
| error: aborting due to 1 previous error | ||
|
|
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.
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.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 think this logic makes sense to me 👍 Thank you
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 believe that's true.
dyn Trait + Sendtodyn Sendiirc doesn't change the vtable. The vtable of objects without principals is just size+align+drop_fn or sth which is the same for all traits so iirc @compiler-errors implemented trait upcasting to principal-less traits to just keep the old vtable