-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Make ptr_cast_add_auto_to_object
lint into hard error
#136764
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
bors
merged 1 commit into
rust-lang:master
from
traviscross:TC/make-ptr_cast_add_auto_to_object-hard-error
Mar 5, 2025
Merged
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
An auto trait cannot be added to the bounds of a `dyn Trait` type via | ||
a pointer cast. | ||
|
||
Erroneous code example: | ||
|
||
```rust,edition2021,compile_fail,E0804 | ||
let ptr: *const dyn core::any::Any = &(); | ||
_ = ptr as *const (dyn core::any::Any + Send); | ||
``` | ||
|
||
Adding an auto trait can make the vtable invalid, potentially causing | ||
UB in safe code afterwards. For example: | ||
|
||
```rust,edition2021,no_run | ||
use core::{mem::transmute, ptr::NonNull}; | ||
|
||
trait Trait { | ||
fn f(&self) | ||
where | ||
Self: Send; | ||
} | ||
|
||
impl Trait for NonNull<()> { | ||
fn f(&self) { | ||
unreachable!() | ||
} | ||
} | ||
|
||
fn main() { | ||
let unsend: &dyn Trait = &NonNull::dangling(); | ||
let bad: &(dyn Trait + Send) = unsafe { transmute(unsend) }; | ||
// This crashes, since the vtable for `NonNull as dyn Trait` does | ||
// not have an entry for `Trait::f`. | ||
bad.f(); | ||
} | ||
``` | ||
|
||
To fix this error, you can use `transmute` rather than pointer casts, | ||
but you must ensure that the vtable is valid for the pointer's type | ||
before calling a method on the trait object or allowing other code to | ||
do so. |
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 |
---|---|---|
|
@@ -547,6 +547,7 @@ E0800: 0800, | |
E0801: 0801, | ||
E0802: 0802, | ||
E0803: 0803, | ||
E0804: 0804, | ||
); | ||
) | ||
} | ||
|
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 |
---|---|---|
@@ -1,18 +1,20 @@ | ||
//@ check-pass | ||
|
||
trait Trait<'a> {} | ||
|
||
fn add_auto<'a>(x: *mut dyn Trait<'a>) -> *mut (dyn Trait<'a> + Send) { | ||
x as _ | ||
//~^ warning: adding an auto trait `Send` to a trait object in a pointer cast may cause UB later on | ||
//~| warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! | ||
//~^ ERROR cannot add auto trait `Send` to dyn bound via pointer cast | ||
//~| NOTE unsupported cast | ||
//~| NOTE this could allow UB elsewhere | ||
//~| HELP use `transmute` if you're sure this is sound | ||
} | ||
|
||
// (to test diagnostic list formatting) | ||
fn add_multiple_auto<'a>(x: *mut dyn Trait<'a>) -> *mut (dyn Trait<'a> + Send + Sync + Unpin) { | ||
x as _ | ||
//~^ warning: adding auto traits `Send`, `Sync`, and `Unpin` to a trait object in a pointer cast may cause UB later on | ||
//~| warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! | ||
//~^ ERROR cannot add auto traits `Send`, `Sync`, and `Unpin` to dyn bound via pointer cast | ||
//~| NOTE unsupported cast | ||
//~| NOTE this could allow UB elsewhere | ||
//~| HELP use `transmute` if you're sure this is sound | ||
} | ||
|
||
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 |
---|---|---|
@@ -1,43 +1,21 @@ | ||
warning: adding an auto trait `Send` to a trait object in a pointer cast may cause UB later on | ||
--> $DIR/ptr-to-trait-obj-add-auto.rs:6:5 | ||
error[E0804]: cannot add auto trait `Send` to dyn bound via pointer cast | ||
--> $DIR/ptr-to-trait-obj-add-auto.rs:4:5 | ||
| | ||
LL | x as _ | ||
| ^^^^^^ | ||
| ^^^^^^ unsupported cast | ||
| | ||
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! | ||
= note: for more information, see issue #127323 <https://github.com/rust-lang/rust/issues/127323> | ||
= note: `#[warn(ptr_cast_add_auto_to_object)]` on by default | ||
= note: this could allow UB elsewhere | ||
= help: use `transmute` if you're sure this is sound | ||
|
||
warning: adding auto traits `Send`, `Sync`, and `Unpin` to a trait object in a pointer cast may cause UB later on | ||
error[E0804]: cannot add auto traits `Send`, `Sync`, and `Unpin` to dyn bound via pointer cast | ||
--> $DIR/ptr-to-trait-obj-add-auto.rs:13:5 | ||
| | ||
LL | x as _ | ||
| ^^^^^^ | ||
| ^^^^^^ unsupported cast | ||
| | ||
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! | ||
= note: for more information, see issue #127323 <https://github.com/rust-lang/rust/issues/127323> | ||
= note: this could allow UB elsewhere | ||
= help: use `transmute` if you're sure this is sound | ||
|
||
warning: 2 warnings emitted | ||
|
||
Future incompatibility report: Future breakage diagnostic: | ||
warning: adding an auto trait `Send` to a trait object in a pointer cast may cause UB later on | ||
--> $DIR/ptr-to-trait-obj-add-auto.rs:6:5 | ||
| | ||
LL | x as _ | ||
| ^^^^^^ | ||
| | ||
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! | ||
= note: for more information, see issue #127323 <https://github.com/rust-lang/rust/issues/127323> | ||
= note: `#[warn(ptr_cast_add_auto_to_object)]` on by default | ||
|
||
Future breakage diagnostic: | ||
warning: adding auto traits `Send`, `Sync`, and `Unpin` to a trait object in a pointer cast may cause UB later on | ||
--> $DIR/ptr-to-trait-obj-add-auto.rs:13:5 | ||
| | ||
LL | x as _ | ||
| ^^^^^^ | ||
| | ||
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! | ||
= note: for more information, see issue #127323 <https://github.com/rust-lang/rust/issues/127323> | ||
= note: `#[warn(ptr_cast_add_auto_to_object)]` on by default | ||
error: aborting due to 2 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0804`. |
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,4 @@ | ||
# Removed lints | ||
|
||
This directory contains tests to confirm that lints that have been | ||
removed do not cause errors and produce the appropriate warnings. | ||
traviscross marked this conversation as resolved.
Show resolved
Hide resolved
|
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,5 @@ | ||
//@ check-pass | ||
|
||
#![deny(ptr_cast_add_auto_to_object)] | ||
//~^ WARN lint `ptr_cast_add_auto_to_object` has been removed | ||
fn main() {} |
10 changes: 10 additions & 0 deletions
10
tests/ui/lint/removed-lints/ptr_cast_add_auto_to_object.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,10 @@ | ||
warning: lint `ptr_cast_add_auto_to_object` has been removed: converted into hard error, see issue #127323 <https://github.com/rust-lang/rust/issues/127323> for more information | ||
--> $DIR/ptr_cast_add_auto_to_object.rs:3:9 | ||
| | ||
LL | #![deny(ptr_cast_add_auto_to_object)] | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= note: `#[warn(renamed_and_removed_lints)]` on by default | ||
|
||
warning: 1 warning emitted | ||
|
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.