From 639d4066d24de35e49da615585effa33968ab3a9 Mon Sep 17 00:00:00 2001 From: Maybe Lapkin Date: Sun, 23 Jun 2024 23:53:28 +0200 Subject: [PATCH] Make `NEVER_TYPE_FALLBACK_FLOWING_INTO_UNSAFE` a hard error in e2024 --- compiler/rustc_hir_typeck/src/errors.rs | 2 +- compiler/rustc_hir_typeck/src/fallback.rs | 34 +++++--- ...fallback-flowing-into-unsafe.e2015.stderr} | 20 ++--- ...-fallback-flowing-into-unsafe.e2024.stderr | 86 +++++++++++++++++++ ...never-type-fallback-flowing-into-unsafe.rs | 65 +++++++++----- 5 files changed, 159 insertions(+), 48 deletions(-) rename tests/ui/never_type/{lint-never-type-fallback-flowing-into-unsafe.stderr => lint-never-type-fallback-flowing-into-unsafe.e2015.stderr} (87%) create mode 100644 tests/ui/never_type/lint-never-type-fallback-flowing-into-unsafe.e2024.stderr diff --git a/compiler/rustc_hir_typeck/src/errors.rs b/compiler/rustc_hir_typeck/src/errors.rs index 98add86252c52..d74f0b4298946 100644 --- a/compiler/rustc_hir_typeck/src/errors.rs +++ b/compiler/rustc_hir_typeck/src/errors.rs @@ -165,7 +165,7 @@ pub struct MissingParenthesesInRange { pub add_missing_parentheses: Option, } -#[derive(LintDiagnostic)] +#[derive(LintDiagnostic, Diagnostic)] pub enum NeverTypeFallbackFlowingIntoUnsafe { #[help] #[diag(hir_typeck_never_type_fallback_flowing_into_unsafe_call)] diff --git a/compiler/rustc_hir_typeck/src/fallback.rs b/compiler/rustc_hir_typeck/src/fallback.rs index 3cecbfd42757e..33cd0dfabda7f 100644 --- a/compiler/rustc_hir_typeck/src/fallback.rs +++ b/compiler/rustc_hir_typeck/src/fallback.rs @@ -455,20 +455,26 @@ impl<'tcx> FnCtxt<'_, 'tcx> { .collect::>(); for (hir_id, span, reason) in affected_unsafe_infer_vars { - self.tcx.emit_node_span_lint( - lint::builtin::NEVER_TYPE_FALLBACK_FLOWING_INTO_UNSAFE, - hir_id, - span, - match reason { - UnsafeUseReason::Call => errors::NeverTypeFallbackFlowingIntoUnsafe::Call, - UnsafeUseReason::Method => errors::NeverTypeFallbackFlowingIntoUnsafe::Method, - UnsafeUseReason::Path => errors::NeverTypeFallbackFlowingIntoUnsafe::Path, - UnsafeUseReason::UnionField => { - errors::NeverTypeFallbackFlowingIntoUnsafe::UnionField - } - UnsafeUseReason::Deref => errors::NeverTypeFallbackFlowingIntoUnsafe::Deref, - }, - ); + let error = match reason { + UnsafeUseReason::Call => errors::NeverTypeFallbackFlowingIntoUnsafe::Call, + UnsafeUseReason::Method => errors::NeverTypeFallbackFlowingIntoUnsafe::Method, + UnsafeUseReason::Path => errors::NeverTypeFallbackFlowingIntoUnsafe::Path, + UnsafeUseReason::UnionField => { + errors::NeverTypeFallbackFlowingIntoUnsafe::UnionField + } + UnsafeUseReason::Deref => errors::NeverTypeFallbackFlowingIntoUnsafe::Deref, + }; + + if span.at_least_rust_2024() { + self.tcx.dcx().create_err(error).with_span(span).emit(); + } else { + self.tcx.emit_node_span_lint( + lint::builtin::NEVER_TYPE_FALLBACK_FLOWING_INTO_UNSAFE, + hir_id, + span, + error, + ); + } } } diff --git a/tests/ui/never_type/lint-never-type-fallback-flowing-into-unsafe.stderr b/tests/ui/never_type/lint-never-type-fallback-flowing-into-unsafe.e2015.stderr similarity index 87% rename from tests/ui/never_type/lint-never-type-fallback-flowing-into-unsafe.stderr rename to tests/ui/never_type/lint-never-type-fallback-flowing-into-unsafe.e2015.stderr index fbd92f8f662bb..8c11d8676160e 100644 --- a/tests/ui/never_type/lint-never-type-fallback-flowing-into-unsafe.stderr +++ b/tests/ui/never_type/lint-never-type-fallback-flowing-into-unsafe.e2015.stderr @@ -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:29: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:46: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:57: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:78: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:96: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:91: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:114: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:139: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:157:19 | LL | match send_message::<_ /* ?0 */>() { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/tests/ui/never_type/lint-never-type-fallback-flowing-into-unsafe.e2024.stderr b/tests/ui/never_type/lint-never-type-fallback-flowing-into-unsafe.e2024.stderr new file mode 100644 index 0000000000000..05b298cc3a7d9 --- /dev/null +++ b/tests/ui/never_type/lint-never-type-fallback-flowing-into-unsafe.e2024.stderr @@ -0,0 +1,86 @@ +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() } + | ^^^^^^^^^^^^^ + | + = 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:29:13 + | +LL | core::mem::transmute(Zst) + | ^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = help: specify the type explicitly + +error: never type fallback affects this union access + --> $DIR/lint-never-type-fallback-flowing-into-unsafe.rs:46:18 + | +LL | unsafe { Union { a: () }.b } + | ^^^^^^^^^^^^^^^^^ + | + = help: specify the type explicitly + +error: never type fallback affects this raw pointer dereference + --> $DIR/lint-never-type-fallback-flowing-into-unsafe.rs:57:18 + | +LL | unsafe { *ptr::from_ref(&()).cast() } + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = 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:78:18 + | +LL | unsafe { internally_create(x) } + | ^^^^^^^^^^^^^^^^^^^^ + | + = 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:96:18 + | +LL | unsafe { zeroed() } + | ^^^^^^^^ + | + = help: specify the type explicitly + +error: never type fallback affects this `unsafe` function + --> $DIR/lint-never-type-fallback-flowing-into-unsafe.rs:91:22 + | +LL | let zeroed = mem::zeroed; + | ^^^^^^^^^^^ + | + = help: specify the type explicitly + +error: never type fallback affects this `unsafe` function + --> $DIR/lint-never-type-fallback-flowing-into-unsafe.rs:114:17 + | +LL | let f = internally_create; + | ^^^^^^^^^^^^^^^^^ + | + = 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:139:13 + | +LL | S(marker::PhantomData).create_out_of_thin_air() + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = 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:157:19 + | +LL | match send_message::<_ /* ?0 */>() { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +... +LL | msg_send!(); + | ----------- in this macro invocation + | + = 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) + +error: aborting due to 10 previous errors + diff --git a/tests/ui/never_type/lint-never-type-fallback-flowing-into-unsafe.rs b/tests/ui/never_type/lint-never-type-fallback-flowing-into-unsafe.rs index d65bfee843e3e..9d0b80dbd3cd6 100644 --- a/tests/ui/never_type/lint-never-type-fallback-flowing-into-unsafe.rs +++ b/tests/ui/never_type/lint-never-type-fallback-flowing-into-unsafe.rs @@ -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,9 @@ fn main() {} fn _zero() { if false { unsafe { mem::zeroed() } - //~^ warn: never type fallback affects this call to an `unsafe` function - //~| warn: this will change its meaning in a future release! + //[e2015]~^ warn: never type fallback affects this call to an `unsafe` function + //[e2015]~| warn: this will change its meaning in a future release! + //[e2024]~^^^ error: never type fallback affects this call to an `unsafe` function } else { return; }; @@ -21,8 +27,9 @@ fn _trans() { unsafe { struct Zst; core::mem::transmute(Zst) - //~^ warn: never type fallback affects this call to an `unsafe` function - //~| warn: this will change its meaning in a future release! + //[e2015]~^ warn: never type fallback affects this call to an `unsafe` function + //[e2015]~| warn: this will change its meaning in a future release! + //[e2024]~^^^ error: never type fallback affects this call to an `unsafe` function } } else { return; @@ -37,8 +44,9 @@ fn _union() { } unsafe { Union { a: () }.b } - //~^ warn: never type fallback affects this union access - //~| warn: this will change its meaning in a future release! + //[e2015]~^ warn: never type fallback affects this union access + //[e2015]~| warn: this will change its meaning in a future release! + //[e2024]~^^^ error: never type fallback affects this union access } else { return; }; @@ -47,8 +55,9 @@ fn _union() { fn _deref() { if false { unsafe { *ptr::from_ref(&()).cast() } - //~^ warn: never type fallback affects this raw pointer dereference - //~| warn: this will change its meaning in a future release! + //[e2015]~^ warn: never type fallback affects this raw pointer dereference + //[e2015]~| warn: this will change its meaning in a future release! + //[e2024]~^^^ error: never type fallback affects this raw pointer dereference } else { return; }; @@ -57,7 +66,9 @@ fn _deref() { fn _only_generics() { if false { unsafe fn internally_create(_: Option) { - let _ = mem::zeroed::(); + unsafe { + let _ = mem::zeroed::(); + } } // We need the option (and unwrap later) to call a function in a way, @@ -65,8 +76,9 @@ fn _only_generics() { let x = None; unsafe { internally_create(x) } - //~^ warn: never type fallback affects this call to an `unsafe` function - //~| warn: this will change its meaning in a future release! + //[e2015]~^ warn: never type fallback affects this call to an `unsafe` function + //[e2015]~| warn: this will change its meaning in a future release! + //[e2024]~^^^ error: never type fallback affects this call to an `unsafe` function x.unwrap() } else { @@ -77,12 +89,14 @@ fn _only_generics() { fn _stored_function() { if false { let zeroed = mem::zeroed; - //~^ warn: never type fallback affects this `unsafe` function - //~| warn: this will change its meaning in a future release! + //[e2015]~^ warn: never type fallback affects this `unsafe` function + //[e2015]~| warn: this will change its meaning in a future release! + //[e2024]~^^^ error: never type fallback affects this `unsafe` function unsafe { zeroed() } - //~^ warn: never type fallback affects this call to an `unsafe` function - //~| warn: this will change its meaning in a future release! + //[e2015]~^ warn: never type fallback affects this call to an `unsafe` function + //[e2015]~| warn: this will change its meaning in a future release! + //[e2024]~^^^ error: never type fallback affects this call to an `unsafe` function } else { return; }; @@ -91,13 +105,16 @@ fn _stored_function() { fn _only_generics_stored_function() { if false { unsafe fn internally_create(_: Option) { - let _ = mem::zeroed::(); + unsafe { + let _ = mem::zeroed::(); + } } let x = None; let f = internally_create; - //~^ warn: never type fallback affects this `unsafe` function - //~| warn: this will change its meaning in a future release! + //[e2015]~^ warn: never type fallback affects this `unsafe` function + //[e2015]~| warn: this will change its meaning in a future release! + //[e2024]~^^^ error: never type fallback affects this `unsafe` function unsafe { f(x) } @@ -120,8 +137,9 @@ fn _method() { if false { unsafe { S(marker::PhantomData).create_out_of_thin_air() - //~^ warn: never type fallback affects this call to an `unsafe` method - //~| warn: this will change its meaning in a future release! + //[e2015]~^ warn: never type fallback affects this call to an `unsafe` method + //[e2015]~| warn: this will change its meaning in a future release! + //[e2024]~^^^ error: never type fallback affects this call to an `unsafe` method } } else { return; @@ -137,8 +155,9 @@ fn _objc() { macro_rules! msg_send { () => { match send_message::<_ /* ?0 */>() { - //~^ warn: never type fallback affects this call to an `unsafe` function - //~| warn: this will change its meaning in a future release! + //[e2015]~^ warn: never type fallback affects this call to an `unsafe` function + //[e2015]~| warn: this will change its meaning in a future release! + //[e2024]~^^^ error: never type fallback affects this call to an `unsafe` function Ok(x) => x, Err(_) => loop {}, }