Skip to content

Commit 639d406

Browse files
committed
Make NEVER_TYPE_FALLBACK_FLOWING_INTO_UNSAFE a hard error in e2024
1 parent 33422e7 commit 639d406

5 files changed

+159
-48
lines changed

compiler/rustc_hir_typeck/src/errors.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ pub struct MissingParenthesesInRange {
165165
pub add_missing_parentheses: Option<AddMissingParenthesesInRange>,
166166
}
167167

168-
#[derive(LintDiagnostic)]
168+
#[derive(LintDiagnostic, Diagnostic)]
169169
pub enum NeverTypeFallbackFlowingIntoUnsafe {
170170
#[help]
171171
#[diag(hir_typeck_never_type_fallback_flowing_into_unsafe_call)]

compiler/rustc_hir_typeck/src/fallback.rs

+20-14
Original file line numberDiff line numberDiff line change
@@ -455,20 +455,26 @@ impl<'tcx> FnCtxt<'_, 'tcx> {
455455
.collect::<Vec<_>>();
456456

457457
for (hir_id, span, reason) in affected_unsafe_infer_vars {
458-
self.tcx.emit_node_span_lint(
459-
lint::builtin::NEVER_TYPE_FALLBACK_FLOWING_INTO_UNSAFE,
460-
hir_id,
461-
span,
462-
match reason {
463-
UnsafeUseReason::Call => errors::NeverTypeFallbackFlowingIntoUnsafe::Call,
464-
UnsafeUseReason::Method => errors::NeverTypeFallbackFlowingIntoUnsafe::Method,
465-
UnsafeUseReason::Path => errors::NeverTypeFallbackFlowingIntoUnsafe::Path,
466-
UnsafeUseReason::UnionField => {
467-
errors::NeverTypeFallbackFlowingIntoUnsafe::UnionField
468-
}
469-
UnsafeUseReason::Deref => errors::NeverTypeFallbackFlowingIntoUnsafe::Deref,
470-
},
471-
);
458+
let error = match reason {
459+
UnsafeUseReason::Call => errors::NeverTypeFallbackFlowingIntoUnsafe::Call,
460+
UnsafeUseReason::Method => errors::NeverTypeFallbackFlowingIntoUnsafe::Method,
461+
UnsafeUseReason::Path => errors::NeverTypeFallbackFlowingIntoUnsafe::Path,
462+
UnsafeUseReason::UnionField => {
463+
errors::NeverTypeFallbackFlowingIntoUnsafe::UnionField
464+
}
465+
UnsafeUseReason::Deref => errors::NeverTypeFallbackFlowingIntoUnsafe::Deref,
466+
};
467+
468+
if span.at_least_rust_2024() {
469+
self.tcx.dcx().create_err(error).with_span(span).emit();
470+
} else {
471+
self.tcx.emit_node_span_lint(
472+
lint::builtin::NEVER_TYPE_FALLBACK_FLOWING_INTO_UNSAFE,
473+
hir_id,
474+
span,
475+
error,
476+
);
477+
}
472478
}
473479
}
474480

tests/ui/never_type/lint-never-type-fallback-flowing-into-unsafe.stderr renamed to tests/ui/never_type/lint-never-type-fallback-flowing-into-unsafe.e2015.stderr

+10-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
warning: never type fallback affects this call to an `unsafe` function
2-
--> $DIR/lint-never-type-fallback-flowing-into-unsafe.rs:8:18
2+
--> $DIR/lint-never-type-fallback-flowing-into-unsafe.rs:13:18
33
|
44
LL | unsafe { mem::zeroed() }
55
| ^^^^^^^^^^^^^
@@ -10,7 +10,7 @@ LL | unsafe { mem::zeroed() }
1010
= note: `#[warn(never_type_fallback_flowing_into_unsafe)]` on by default
1111

1212
warning: never type fallback affects this call to an `unsafe` function
13-
--> $DIR/lint-never-type-fallback-flowing-into-unsafe.rs:23:13
13+
--> $DIR/lint-never-type-fallback-flowing-into-unsafe.rs:29:13
1414
|
1515
LL | core::mem::transmute(Zst)
1616
| ^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -20,7 +20,7 @@ LL | core::mem::transmute(Zst)
2020
= help: specify the type explicitly
2121

2222
warning: never type fallback affects this union access
23-
--> $DIR/lint-never-type-fallback-flowing-into-unsafe.rs:39:18
23+
--> $DIR/lint-never-type-fallback-flowing-into-unsafe.rs:46:18
2424
|
2525
LL | unsafe { Union { a: () }.b }
2626
| ^^^^^^^^^^^^^^^^^
@@ -30,7 +30,7 @@ LL | unsafe { Union { a: () }.b }
3030
= help: specify the type explicitly
3131

3232
warning: never type fallback affects this raw pointer dereference
33-
--> $DIR/lint-never-type-fallback-flowing-into-unsafe.rs:49:18
33+
--> $DIR/lint-never-type-fallback-flowing-into-unsafe.rs:57:18
3434
|
3535
LL | unsafe { *ptr::from_ref(&()).cast() }
3636
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -40,7 +40,7 @@ LL | unsafe { *ptr::from_ref(&()).cast() }
4040
= help: specify the type explicitly
4141

4242
warning: never type fallback affects this call to an `unsafe` function
43-
--> $DIR/lint-never-type-fallback-flowing-into-unsafe.rs:67:18
43+
--> $DIR/lint-never-type-fallback-flowing-into-unsafe.rs:78:18
4444
|
4545
LL | unsafe { internally_create(x) }
4646
| ^^^^^^^^^^^^^^^^^^^^
@@ -50,7 +50,7 @@ LL | unsafe { internally_create(x) }
5050
= help: specify the type explicitly
5151

5252
warning: never type fallback affects this call to an `unsafe` function
53-
--> $DIR/lint-never-type-fallback-flowing-into-unsafe.rs:83:18
53+
--> $DIR/lint-never-type-fallback-flowing-into-unsafe.rs:96:18
5454
|
5555
LL | unsafe { zeroed() }
5656
| ^^^^^^^^
@@ -60,7 +60,7 @@ LL | unsafe { zeroed() }
6060
= help: specify the type explicitly
6161

6262
warning: never type fallback affects this `unsafe` function
63-
--> $DIR/lint-never-type-fallback-flowing-into-unsafe.rs:79:22
63+
--> $DIR/lint-never-type-fallback-flowing-into-unsafe.rs:91:22
6464
|
6565
LL | let zeroed = mem::zeroed;
6666
| ^^^^^^^^^^^
@@ -70,7 +70,7 @@ LL | let zeroed = mem::zeroed;
7070
= help: specify the type explicitly
7171

7272
warning: never type fallback affects this `unsafe` function
73-
--> $DIR/lint-never-type-fallback-flowing-into-unsafe.rs:98:17
73+
--> $DIR/lint-never-type-fallback-flowing-into-unsafe.rs:114:17
7474
|
7575
LL | let f = internally_create;
7676
| ^^^^^^^^^^^^^^^^^
@@ -80,7 +80,7 @@ LL | let f = internally_create;
8080
= help: specify the type explicitly
8181

8282
warning: never type fallback affects this call to an `unsafe` method
83-
--> $DIR/lint-never-type-fallback-flowing-into-unsafe.rs:122:13
83+
--> $DIR/lint-never-type-fallback-flowing-into-unsafe.rs:139:13
8484
|
8585
LL | S(marker::PhantomData).create_out_of_thin_air()
8686
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -90,7 +90,7 @@ LL | S(marker::PhantomData).create_out_of_thin_air()
9090
= help: specify the type explicitly
9191

9292
warning: never type fallback affects this call to an `unsafe` function
93-
--> $DIR/lint-never-type-fallback-flowing-into-unsafe.rs:139:19
93+
--> $DIR/lint-never-type-fallback-flowing-into-unsafe.rs:157:19
9494
|
9595
LL | match send_message::<_ /* ?0 */>() {
9696
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
error: never type fallback affects this call to an `unsafe` function
2+
--> $DIR/lint-never-type-fallback-flowing-into-unsafe.rs:13:18
3+
|
4+
LL | unsafe { mem::zeroed() }
5+
| ^^^^^^^^^^^^^
6+
|
7+
= help: specify the type explicitly
8+
9+
error: never type fallback affects this call to an `unsafe` function
10+
--> $DIR/lint-never-type-fallback-flowing-into-unsafe.rs:29:13
11+
|
12+
LL | core::mem::transmute(Zst)
13+
| ^^^^^^^^^^^^^^^^^^^^^^^^^
14+
|
15+
= help: specify the type explicitly
16+
17+
error: never type fallback affects this union access
18+
--> $DIR/lint-never-type-fallback-flowing-into-unsafe.rs:46:18
19+
|
20+
LL | unsafe { Union { a: () }.b }
21+
| ^^^^^^^^^^^^^^^^^
22+
|
23+
= help: specify the type explicitly
24+
25+
error: never type fallback affects this raw pointer dereference
26+
--> $DIR/lint-never-type-fallback-flowing-into-unsafe.rs:57:18
27+
|
28+
LL | unsafe { *ptr::from_ref(&()).cast() }
29+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
30+
|
31+
= help: specify the type explicitly
32+
33+
error: never type fallback affects this call to an `unsafe` function
34+
--> $DIR/lint-never-type-fallback-flowing-into-unsafe.rs:78:18
35+
|
36+
LL | unsafe { internally_create(x) }
37+
| ^^^^^^^^^^^^^^^^^^^^
38+
|
39+
= help: specify the type explicitly
40+
41+
error: never type fallback affects this call to an `unsafe` function
42+
--> $DIR/lint-never-type-fallback-flowing-into-unsafe.rs:96:18
43+
|
44+
LL | unsafe { zeroed() }
45+
| ^^^^^^^^
46+
|
47+
= help: specify the type explicitly
48+
49+
error: never type fallback affects this `unsafe` function
50+
--> $DIR/lint-never-type-fallback-flowing-into-unsafe.rs:91:22
51+
|
52+
LL | let zeroed = mem::zeroed;
53+
| ^^^^^^^^^^^
54+
|
55+
= help: specify the type explicitly
56+
57+
error: never type fallback affects this `unsafe` function
58+
--> $DIR/lint-never-type-fallback-flowing-into-unsafe.rs:114:17
59+
|
60+
LL | let f = internally_create;
61+
| ^^^^^^^^^^^^^^^^^
62+
|
63+
= help: specify the type explicitly
64+
65+
error: never type fallback affects this call to an `unsafe` method
66+
--> $DIR/lint-never-type-fallback-flowing-into-unsafe.rs:139:13
67+
|
68+
LL | S(marker::PhantomData).create_out_of_thin_air()
69+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
70+
|
71+
= help: specify the type explicitly
72+
73+
error: never type fallback affects this call to an `unsafe` function
74+
--> $DIR/lint-never-type-fallback-flowing-into-unsafe.rs:157:19
75+
|
76+
LL | match send_message::<_ /* ?0 */>() {
77+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
78+
...
79+
LL | msg_send!();
80+
| ----------- in this macro invocation
81+
|
82+
= help: specify the type explicitly
83+
= note: this error originates in the macro `msg_send` (in Nightly builds, run with -Z macro-backtrace for more info)
84+
85+
error: aborting due to 10 previous errors
86+

tests/ui/never_type/lint-never-type-fallback-flowing-into-unsafe.rs

+42-23
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
1-
//@ check-pass
1+
//@ revisions: e2015 e2024
2+
//@[e2015] check-pass
3+
//@[e2024] check-fail
4+
//@[e2024] edition:2024
5+
//@[e2024] compile-flags: -Zunstable-options
6+
27
use std::{marker, mem, ptr};
38

49
fn main() {}
510

611
fn _zero() {
712
if false {
813
unsafe { mem::zeroed() }
9-
//~^ warn: never type fallback affects this call to an `unsafe` function
10-
//~| warn: this will change its meaning in a future release!
14+
//[e2015]~^ warn: never type fallback affects this call to an `unsafe` function
15+
//[e2015]~| warn: this will change its meaning in a future release!
16+
//[e2024]~^^^ error: never type fallback affects this call to an `unsafe` function
1117
} else {
1218
return;
1319
};
@@ -21,8 +27,9 @@ fn _trans() {
2127
unsafe {
2228
struct Zst;
2329
core::mem::transmute(Zst)
24-
//~^ warn: never type fallback affects this call to an `unsafe` function
25-
//~| warn: this will change its meaning in a future release!
30+
//[e2015]~^ warn: never type fallback affects this call to an `unsafe` function
31+
//[e2015]~| warn: this will change its meaning in a future release!
32+
//[e2024]~^^^ error: never type fallback affects this call to an `unsafe` function
2633
}
2734
} else {
2835
return;
@@ -37,8 +44,9 @@ fn _union() {
3744
}
3845

3946
unsafe { Union { a: () }.b }
40-
//~^ warn: never type fallback affects this union access
41-
//~| warn: this will change its meaning in a future release!
47+
//[e2015]~^ warn: never type fallback affects this union access
48+
//[e2015]~| warn: this will change its meaning in a future release!
49+
//[e2024]~^^^ error: never type fallback affects this union access
4250
} else {
4351
return;
4452
};
@@ -47,8 +55,9 @@ fn _union() {
4755
fn _deref() {
4856
if false {
4957
unsafe { *ptr::from_ref(&()).cast() }
50-
//~^ warn: never type fallback affects this raw pointer dereference
51-
//~| warn: this will change its meaning in a future release!
58+
//[e2015]~^ warn: never type fallback affects this raw pointer dereference
59+
//[e2015]~| warn: this will change its meaning in a future release!
60+
//[e2024]~^^^ error: never type fallback affects this raw pointer dereference
5261
} else {
5362
return;
5463
};
@@ -57,16 +66,19 @@ fn _deref() {
5766
fn _only_generics() {
5867
if false {
5968
unsafe fn internally_create<T>(_: Option<T>) {
60-
let _ = mem::zeroed::<T>();
69+
unsafe {
70+
let _ = mem::zeroed::<T>();
71+
}
6172
}
6273

6374
// We need the option (and unwrap later) to call a function in a way,
6475
// which makes it affected by the fallback, but without having it return anything
6576
let x = None;
6677

6778
unsafe { internally_create(x) }
68-
//~^ warn: never type fallback affects this call to an `unsafe` function
69-
//~| warn: this will change its meaning in a future release!
79+
//[e2015]~^ warn: never type fallback affects this call to an `unsafe` function
80+
//[e2015]~| warn: this will change its meaning in a future release!
81+
//[e2024]~^^^ error: never type fallback affects this call to an `unsafe` function
7082

7183
x.unwrap()
7284
} else {
@@ -77,12 +89,14 @@ fn _only_generics() {
7789
fn _stored_function() {
7890
if false {
7991
let zeroed = mem::zeroed;
80-
//~^ warn: never type fallback affects this `unsafe` function
81-
//~| warn: this will change its meaning in a future release!
92+
//[e2015]~^ warn: never type fallback affects this `unsafe` function
93+
//[e2015]~| warn: this will change its meaning in a future release!
94+
//[e2024]~^^^ error: never type fallback affects this `unsafe` function
8295

8396
unsafe { zeroed() }
84-
//~^ warn: never type fallback affects this call to an `unsafe` function
85-
//~| warn: this will change its meaning in a future release!
97+
//[e2015]~^ warn: never type fallback affects this call to an `unsafe` function
98+
//[e2015]~| warn: this will change its meaning in a future release!
99+
//[e2024]~^^^ error: never type fallback affects this call to an `unsafe` function
86100
} else {
87101
return;
88102
};
@@ -91,13 +105,16 @@ fn _stored_function() {
91105
fn _only_generics_stored_function() {
92106
if false {
93107
unsafe fn internally_create<T>(_: Option<T>) {
94-
let _ = mem::zeroed::<T>();
108+
unsafe {
109+
let _ = mem::zeroed::<T>();
110+
}
95111
}
96112

97113
let x = None;
98114
let f = internally_create;
99-
//~^ warn: never type fallback affects this `unsafe` function
100-
//~| warn: this will change its meaning in a future release!
115+
//[e2015]~^ warn: never type fallback affects this `unsafe` function
116+
//[e2015]~| warn: this will change its meaning in a future release!
117+
//[e2024]~^^^ error: never type fallback affects this `unsafe` function
101118

102119
unsafe { f(x) }
103120

@@ -120,8 +137,9 @@ fn _method() {
120137
if false {
121138
unsafe {
122139
S(marker::PhantomData).create_out_of_thin_air()
123-
//~^ warn: never type fallback affects this call to an `unsafe` method
124-
//~| warn: this will change its meaning in a future release!
140+
//[e2015]~^ warn: never type fallback affects this call to an `unsafe` method
141+
//[e2015]~| warn: this will change its meaning in a future release!
142+
//[e2024]~^^^ error: never type fallback affects this call to an `unsafe` method
125143
}
126144
} else {
127145
return;
@@ -137,8 +155,9 @@ fn _objc() {
137155
macro_rules! msg_send {
138156
() => {
139157
match send_message::<_ /* ?0 */>() {
140-
//~^ warn: never type fallback affects this call to an `unsafe` function
141-
//~| warn: this will change its meaning in a future release!
158+
//[e2015]~^ warn: never type fallback affects this call to an `unsafe` function
159+
//[e2015]~| warn: this will change its meaning in a future release!
160+
//[e2024]~^^^ error: never type fallback affects this call to an `unsafe` function
142161
Ok(x) => x,
143162
Err(_) => loop {},
144163
}

0 commit comments

Comments
 (0)