Skip to content

Commit

Permalink
Make NEVER_TYPE_FALLBACK_FLOWING_INTO_UNSAFE a hard error in e2024
Browse files Browse the repository at this point in the history
  • Loading branch information
WaffleLapkin committed Jun 23, 2024
1 parent 33422e7 commit 639d406
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 48 deletions.
2 changes: 1 addition & 1 deletion compiler/rustc_hir_typeck/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ pub struct MissingParenthesesInRange {
pub add_missing_parentheses: Option<AddMissingParenthesesInRange>,
}

#[derive(LintDiagnostic)]
#[derive(LintDiagnostic, Diagnostic)]
pub enum NeverTypeFallbackFlowingIntoUnsafe {
#[help]
#[diag(hir_typeck_never_type_fallback_flowing_into_unsafe_call)]
Expand Down
34 changes: 20 additions & 14 deletions compiler/rustc_hir_typeck/src/fallback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -455,20 +455,26 @@ impl<'tcx> FnCtxt<'_, 'tcx> {
.collect::<Vec<_>>();

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,
);
}
}
}

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:29: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:46: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:57: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:78: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:96: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:91: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:114: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:139: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:157:19
|
LL | match send_message::<_ /* ?0 */>() {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down
Original file line number Diff line number Diff line change
@@ -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

65 changes: 42 additions & 23 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,19 @@
//@ 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
//~| 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;
};
Expand All @@ -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;
Expand All @@ -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;
};
Expand All @@ -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;
};
Expand All @@ -57,16 +66,19 @@ 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
//~| 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 {
Expand All @@ -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;
};
Expand All @@ -91,13 +105,16 @@ 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
//~| 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) }

Expand All @@ -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;
Expand All @@ -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 {},
}
Expand Down

0 comments on commit 639d406

Please sign in to comment.