Skip to content

Commit

Permalink
Unrolled build for rust-lang#126881
Browse files Browse the repository at this point in the history
Rollup merge of rust-lang#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:

- rust-lang#123748
  • Loading branch information
rust-timer committed Jul 8, 2024
2 parents 9af6fee + 18c248b commit 05aa77a
Show file tree
Hide file tree
Showing 4 changed files with 160 additions and 23 deletions.
1 change: 1 addition & 0 deletions compiler/rustc_lint_defs/src/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4189,6 +4189,7 @@ declare_lint! {
reason: FutureIncompatibilityReason::FutureReleaseSemanticsChange,
reference: "issue #123748 <https://github.com/rust-lang/rust/issues/123748>",
};
@edition Edition2024 => Deny;
report_in_external_macro
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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() }
| ^^^^^^^^^^^^^
Expand All @@ -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)
| ^^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -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 }
| ^^^^^^^^^^^^^^^^^
Expand All @@ -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() }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -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) }
| ^^^^^^^^^^^^^^^^^^^^
Expand All @@ -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() }
| ^^^^^^^^
Expand All @@ -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;
| ^^^^^^^^^^^
Expand All @@ -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;
| ^^^^^^^^^^^^^^^^^
Expand All @@ -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()
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -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 */>() {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down
Original file line number Diff line number Diff line change
@@ -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

46 changes: 33 additions & 13 deletions tests/ui/never_type/lint-never-type-fallback-flowing-into-unsafe.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
//@ 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() {}

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;
};
Expand All @@ -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 {
Expand All @@ -37,7 +45,8 @@ fn _union() {
}

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;
Expand All @@ -47,7 +56,8 @@ fn _union() {
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;
Expand All @@ -57,15 +67,18 @@ 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,
// which makes it affected by the fallback, but without having it return anything
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()
Expand All @@ -77,11 +90,13 @@ fn _only_generics() {
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;
Expand All @@ -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) }
Expand All @@ -120,7 +138,8 @@ fn _method() {
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 {
Expand All @@ -137,7 +156,8 @@ fn _objc() {
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 {},
Expand Down

0 comments on commit 05aa77a

Please sign in to comment.