Rollup merge of #126881 - WaffleLapkin:unsafe-code-affected-by-fallback-hard-in-2024, r=compiler-errors

Make `NEVER_TYPE_FALLBACK_FLOWING_INTO_UNSAFE` a deny-by-default lint in edition 2024

I don't actually really care about this, but ``@traviscross`` asked me to do this, because lang team briefly discussed this before.

(TC here:)

Specifically, our original FCPed plan included this step:

- Add a lint against fallback affecting a generic that is passed to an `unsafe` function.
   - Perhaps make this lint `deny-by-default` or a hard error in Rust 2024.

That is, we had left as an open question strengthening this in Rust 2024, and had marked it as an open question on the tracking issue.  We're nominating here to address the open question.  (Closing the remaining open question helps us to fully mark this off for Rust 2024.)

r? ``@compiler-errors``

Tracking:

- https://github.com/rust-lang/rust/issues/123748
This commit is contained in:
许杰友 Jieyou Xu (Joe) 2024-07-08 13:04:30 +08:00 committed by GitHub
commit 29c1a43403
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 160 additions and 23 deletions

View File

@ -4189,6 +4189,7 @@
reason: FutureIncompatibilityReason::FutureReleaseSemanticsChange,
reference: "issue #123748 <https://github.com/rust-lang/rust/issues/123748>",
};
@edition Edition2024 => Deny;
report_in_external_macro
}

View File

@ -1,5 +1,5 @@
warning: never type fallback affects this call to an `unsafe` function
--> $DIR/lint-never-type-fallback-flowing-into-unsafe.rs:8:18
--> $DIR/lint-never-type-fallback-flowing-into-unsafe.rs:13:18
|
LL | unsafe { mem::zeroed() }
| ^^^^^^^^^^^^^
@ -10,7 +10,7 @@ LL | unsafe { mem::zeroed() }
= note: `#[warn(never_type_fallback_flowing_into_unsafe)]` on by default
warning: never type fallback affects this call to an `unsafe` function
--> $DIR/lint-never-type-fallback-flowing-into-unsafe.rs:23:13
--> $DIR/lint-never-type-fallback-flowing-into-unsafe.rs:30:13
|
LL | core::mem::transmute(Zst)
| ^^^^^^^^^^^^^^^^^^^^^^^^^
@ -20,7 +20,7 @@ LL | core::mem::transmute(Zst)
= help: specify the type explicitly
warning: never type fallback affects this union access
--> $DIR/lint-never-type-fallback-flowing-into-unsafe.rs:39:18
--> $DIR/lint-never-type-fallback-flowing-into-unsafe.rs:47:18
|
LL | unsafe { Union { a: () }.b }
| ^^^^^^^^^^^^^^^^^
@ -30,7 +30,7 @@ LL | unsafe { Union { a: () }.b }
= help: specify the type explicitly
warning: never type fallback affects this raw pointer dereference
--> $DIR/lint-never-type-fallback-flowing-into-unsafe.rs:49:18
--> $DIR/lint-never-type-fallback-flowing-into-unsafe.rs:58:18
|
LL | unsafe { *ptr::from_ref(&()).cast() }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -40,7 +40,7 @@ LL | unsafe { *ptr::from_ref(&()).cast() }
= help: specify the type explicitly
warning: never type fallback affects this call to an `unsafe` function
--> $DIR/lint-never-type-fallback-flowing-into-unsafe.rs:67:18
--> $DIR/lint-never-type-fallback-flowing-into-unsafe.rs:79:18
|
LL | unsafe { internally_create(x) }
| ^^^^^^^^^^^^^^^^^^^^
@ -50,7 +50,7 @@ LL | unsafe { internally_create(x) }
= help: specify the type explicitly
warning: never type fallback affects this call to an `unsafe` function
--> $DIR/lint-never-type-fallback-flowing-into-unsafe.rs:83:18
--> $DIR/lint-never-type-fallback-flowing-into-unsafe.rs:97:18
|
LL | unsafe { zeroed() }
| ^^^^^^^^
@ -60,7 +60,7 @@ LL | unsafe { zeroed() }
= help: specify the type explicitly
warning: never type fallback affects this `unsafe` function
--> $DIR/lint-never-type-fallback-flowing-into-unsafe.rs:79:22
--> $DIR/lint-never-type-fallback-flowing-into-unsafe.rs:92:22
|
LL | let zeroed = mem::zeroed;
| ^^^^^^^^^^^
@ -70,7 +70,7 @@ LL | let zeroed = mem::zeroed;
= help: specify the type explicitly
warning: never type fallback affects this `unsafe` function
--> $DIR/lint-never-type-fallback-flowing-into-unsafe.rs:98:17
--> $DIR/lint-never-type-fallback-flowing-into-unsafe.rs:115:17
|
LL | let f = internally_create;
| ^^^^^^^^^^^^^^^^^
@ -80,7 +80,7 @@ LL | let f = internally_create;
= help: specify the type explicitly
warning: never type fallback affects this call to an `unsafe` method
--> $DIR/lint-never-type-fallback-flowing-into-unsafe.rs:122:13
--> $DIR/lint-never-type-fallback-flowing-into-unsafe.rs:140:13
|
LL | S(marker::PhantomData).create_out_of_thin_air()
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -90,7 +90,7 @@ LL | S(marker::PhantomData).create_out_of_thin_air()
= help: specify the type explicitly
warning: never type fallback affects this call to an `unsafe` function
--> $DIR/lint-never-type-fallback-flowing-into-unsafe.rs:139:19
--> $DIR/lint-never-type-fallback-flowing-into-unsafe.rs:158:19
|
LL | match send_message::<_ /* ?0 */>() {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^

View File

@ -0,0 +1,116 @@
error: never type fallback affects this call to an `unsafe` function
--> $DIR/lint-never-type-fallback-flowing-into-unsafe.rs:13:18
|
LL | unsafe { mem::zeroed() }
| ^^^^^^^^^^^^^
|
= warning: this will change its meaning in a future release!
= note: for more information, see issue #123748 <https://github.com/rust-lang/rust/issues/123748>
= help: specify the type explicitly
= note: `#[deny(never_type_fallback_flowing_into_unsafe)]` on by default
error: never type fallback affects this call to an `unsafe` function
--> $DIR/lint-never-type-fallback-flowing-into-unsafe.rs:30:13
|
LL | core::mem::transmute(Zst)
| ^^^^^^^^^^^^^^^^^^^^^^^^^
|
= warning: this will change its meaning in a future release!
= note: for more information, see issue #123748 <https://github.com/rust-lang/rust/issues/123748>
= help: specify the type explicitly
error: never type fallback affects this union access
--> $DIR/lint-never-type-fallback-flowing-into-unsafe.rs:47:18
|
LL | unsafe { Union { a: () }.b }
| ^^^^^^^^^^^^^^^^^
|
= warning: this will change its meaning in a future release!
= note: for more information, see issue #123748 <https://github.com/rust-lang/rust/issues/123748>
= help: specify the type explicitly
error: never type fallback affects this raw pointer dereference
--> $DIR/lint-never-type-fallback-flowing-into-unsafe.rs:58:18
|
LL | unsafe { *ptr::from_ref(&()).cast() }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= warning: this will change its meaning in a future release!
= note: for more information, see issue #123748 <https://github.com/rust-lang/rust/issues/123748>
= help: specify the type explicitly
error: never type fallback affects this call to an `unsafe` function
--> $DIR/lint-never-type-fallback-flowing-into-unsafe.rs:79:18
|
LL | unsafe { internally_create(x) }
| ^^^^^^^^^^^^^^^^^^^^
|
= warning: this will change its meaning in a future release!
= note: for more information, see issue #123748 <https://github.com/rust-lang/rust/issues/123748>
= help: specify the type explicitly
error: never type fallback affects this call to an `unsafe` function
--> $DIR/lint-never-type-fallback-flowing-into-unsafe.rs:97:18
|
LL | unsafe { zeroed() }
| ^^^^^^^^
|
= warning: this will change its meaning in a future release!
= note: for more information, see issue #123748 <https://github.com/rust-lang/rust/issues/123748>
= help: specify the type explicitly
error: never type fallback affects this `unsafe` function
--> $DIR/lint-never-type-fallback-flowing-into-unsafe.rs:92:22
|
LL | let zeroed = mem::zeroed;
| ^^^^^^^^^^^
|
= warning: this will change its meaning in a future release!
= note: for more information, see issue #123748 <https://github.com/rust-lang/rust/issues/123748>
= help: specify the type explicitly
error: never type fallback affects this `unsafe` function
--> $DIR/lint-never-type-fallback-flowing-into-unsafe.rs:115:17
|
LL | let f = internally_create;
| ^^^^^^^^^^^^^^^^^
|
= warning: this will change its meaning in a future release!
= note: for more information, see issue #123748 <https://github.com/rust-lang/rust/issues/123748>
= help: specify the type explicitly
error: never type fallback affects this call to an `unsafe` method
--> $DIR/lint-never-type-fallback-flowing-into-unsafe.rs:140:13
|
LL | S(marker::PhantomData).create_out_of_thin_air()
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= warning: this will change its meaning in a future release!
= note: for more information, see issue #123748 <https://github.com/rust-lang/rust/issues/123748>
= help: specify the type explicitly
error: never type fallback affects this call to an `unsafe` function
--> $DIR/lint-never-type-fallback-flowing-into-unsafe.rs:158:19
|
LL | match send_message::<_ /* ?0 */>() {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
...
LL | msg_send!();
| ----------- in this macro invocation
|
= warning: this will change its meaning in a future release!
= note: for more information, see issue #123748 <https://github.com/rust-lang/rust/issues/123748>
= help: specify the type explicitly
= note: this error originates in the macro `msg_send` (in Nightly builds, run with -Z macro-backtrace for more info)
warning: the type `!` does not permit zero-initialization
--> $DIR/lint-never-type-fallback-flowing-into-unsafe.rs:13:18
|
LL | unsafe { mem::zeroed() }
| ^^^^^^^^^^^^^ this code causes undefined behavior when executed
|
= note: the `!` type has no valid value
= note: `#[warn(invalid_value)]` on by default
error: aborting due to 10 previous errors; 1 warning emitted

View File

@ -1,4 +1,9 @@
//@ check-pass
//@ revisions: e2015 e2024
//@[e2015] check-pass
//@[e2024] check-fail
//@[e2024] edition:2024
//@[e2024] compile-flags: -Zunstable-options
use std::{marker, mem, ptr};
fn main() {}
@ -6,8 +11,10 @@ fn main() {}
fn _zero() {
if false {
unsafe { mem::zeroed() }
//~^ warn: never type fallback affects this call to an `unsafe` function
//[e2015]~^ warn: never type fallback affects this call to an `unsafe` function
//[e2024]~^^ error: never type fallback affects this call to an `unsafe` function
//~| warn: this will change its meaning in a future release!
//[e2024]~| warning: the type `!` does not permit zero-initialization
} else {
return;
};
@ -21,7 +28,8 @@ fn _trans() {
unsafe {
struct Zst;
core::mem::transmute(Zst)
//~^ warn: never type fallback affects this call to an `unsafe` function
//[e2015]~^ warn: never type fallback affects this call to an `unsafe` function
//[e2024]~^^ error: never type fallback affects this call to an `unsafe` function
//~| warn: this will change its meaning in a future release!
}
} else {
@ -37,7 +45,8 @@ union Union<T: Copy> {
}
unsafe { Union { a: () }.b }
//~^ warn: never type fallback affects this union access
//[e2015]~^ warn: never type fallback affects this union access
//[e2024]~^^ error: never type fallback affects this union access
//~| warn: this will change its meaning in a future release!
} else {
return;
@ -47,7 +56,8 @@ union Union<T: Copy> {
fn _deref() {
if false {
unsafe { *ptr::from_ref(&()).cast() }
//~^ warn: never type fallback affects this raw pointer dereference
//[e2015]~^ warn: never type fallback affects this raw pointer dereference
//[e2024]~^^ error: never type fallback affects this raw pointer dereference
//~| warn: this will change its meaning in a future release!
} else {
return;
@ -57,7 +67,9 @@ fn _deref() {
fn _only_generics() {
if false {
unsafe fn internally_create<T>(_: Option<T>) {
let _ = mem::zeroed::<T>();
unsafe {
let _ = mem::zeroed::<T>();
}
}
// We need the option (and unwrap later) to call a function in a way,
@ -65,7 +77,8 @@ unsafe fn internally_create<T>(_: Option<T>) {
let x = None;
unsafe { internally_create(x) }
//~^ warn: never type fallback affects this call to an `unsafe` function
//[e2015]~^ warn: never type fallback affects this call to an `unsafe` function
//[e2024]~^^ error: never type fallback affects this call to an `unsafe` function
//~| warn: this will change its meaning in a future release!
x.unwrap()
@ -77,11 +90,13 @@ unsafe fn internally_create<T>(_: Option<T>) {
fn _stored_function() {
if false {
let zeroed = mem::zeroed;
//~^ warn: never type fallback affects this `unsafe` function
//[e2015]~^ warn: never type fallback affects this `unsafe` function
//[e2024]~^^ error: never type fallback affects this `unsafe` function
//~| warn: this will change its meaning in a future release!
unsafe { zeroed() }
//~^ warn: never type fallback affects this call to an `unsafe` function
//[e2015]~^ warn: never type fallback affects this call to an `unsafe` function
//[e2024]~^^ error: never type fallback affects this call to an `unsafe` function
//~| warn: this will change its meaning in a future release!
} else {
return;
@ -91,12 +106,15 @@ fn _stored_function() {
fn _only_generics_stored_function() {
if false {
unsafe fn internally_create<T>(_: Option<T>) {
let _ = mem::zeroed::<T>();
unsafe {
let _ = mem::zeroed::<T>();
}
}
let x = None;
let f = internally_create;
//~^ warn: never type fallback affects this `unsafe` function
//[e2015]~^ warn: never type fallback affects this `unsafe` function
//[e2024]~^^ error: never type fallback affects this `unsafe` function
//~| warn: this will change its meaning in a future release!
unsafe { f(x) }
@ -120,7 +138,8 @@ unsafe fn create_out_of_thin_air(&self) -> T {
if false {
unsafe {
S(marker::PhantomData).create_out_of_thin_air()
//~^ warn: never type fallback affects this call to an `unsafe` method
//[e2015]~^ warn: never type fallback affects this call to an `unsafe` method
//[e2024]~^^ error: never type fallback affects this call to an `unsafe` method
//~| warn: this will change its meaning in a future release!
}
} else {
@ -137,7 +156,8 @@ pub unsafe fn send_message<R>() -> Result<R, ()> {
macro_rules! msg_send {
() => {
match send_message::<_ /* ?0 */>() {
//~^ warn: never type fallback affects this call to an `unsafe` function
//[e2015]~^ warn: never type fallback affects this call to an `unsafe` function
//[e2024]~^^ error: never type fallback affects this call to an `unsafe` function
//~| warn: this will change its meaning in a future release!
Ok(x) => x,
Err(_) => loop {},