Skip to content

Commit 09a7b28

Browse files
authored
Unrolled build for rust-lang#125282
Rollup merge of rust-lang#125282 - WaffleLapkin:never-type-unsafe-improvements, r=compiler-errors Never type unsafe lint improvements - Move linting code to a separate method - Remove mentions of `core::convert::absurd` (rust-lang#124311 was rejected) - Make the lint into FCW The last thing is a bit weird though. On one hand it should be `EditionSemanticsChange(2024)`, but on the other hand it shouldn't, because we also plan to break it on all editions some time later. _Also_, it's weird that we don't have `FutureReleaseSemanticsChangeReportInDeps`, IMO "this might cause UB in a future release" is important enough to be reported in deps... IMO we ought to have three enums instead of [`FutureIncompatibilityReason`](https://doc.rust-lang.org/nightly/nightly-rustc/rustc_lint_defs/enum.FutureIncompatibilityReason.html#): ```rust enum IncompatibilityWhen { FutureRelease, Edition(Edition), } enum IncompatibilyWhat { Error, SemanticChange, } enum IncompatibilityReportInDeps { No, Yes, } ``` Tracking: - rust-lang#123748
2 parents f092f73 + 434221b commit 09a7b28

File tree

4 files changed

+86
-47
lines changed

4 files changed

+86
-47
lines changed

compiler/rustc_hir_typeck/src/fallback.rs

+40-35
Original file line numberDiff line numberDiff line change
@@ -364,41 +364,11 @@ impl<'tcx> FnCtxt<'_, 'tcx> {
364364
};
365365

366366
let mut fallback_to = |ty| {
367-
let unsafe_infer_vars = unsafe_infer_vars.get_or_init(|| {
368-
let unsafe_infer_vars = compute_unsafe_infer_vars(self.root_ctxt, self.body_id);
369-
debug!(?unsafe_infer_vars);
370-
unsafe_infer_vars
371-
});
372-
373-
let affected_unsafe_infer_vars =
374-
graph::depth_first_search_as_undirected(&coercion_graph, root_vid)
375-
.filter_map(|x| unsafe_infer_vars.get(&x).copied())
376-
.collect::<Vec<_>>();
377-
378-
for (hir_id, span, reason) in affected_unsafe_infer_vars {
379-
self.tcx.emit_node_span_lint(
380-
lint::builtin::NEVER_TYPE_FALLBACK_FLOWING_INTO_UNSAFE,
381-
hir_id,
382-
span,
383-
match reason {
384-
UnsafeUseReason::Call => {
385-
errors::NeverTypeFallbackFlowingIntoUnsafe::Call
386-
}
387-
UnsafeUseReason::Method => {
388-
errors::NeverTypeFallbackFlowingIntoUnsafe::Method
389-
}
390-
UnsafeUseReason::Path => {
391-
errors::NeverTypeFallbackFlowingIntoUnsafe::Path
392-
}
393-
UnsafeUseReason::UnionField => {
394-
errors::NeverTypeFallbackFlowingIntoUnsafe::UnionField
395-
}
396-
UnsafeUseReason::Deref => {
397-
errors::NeverTypeFallbackFlowingIntoUnsafe::Deref
398-
}
399-
},
400-
);
401-
}
367+
self.lint_never_type_fallback_flowing_into_unsafe_code(
368+
&unsafe_infer_vars,
369+
&coercion_graph,
370+
root_vid,
371+
);
402372

403373
diverging_fallback.insert(diverging_ty, ty);
404374
};
@@ -464,6 +434,41 @@ impl<'tcx> FnCtxt<'_, 'tcx> {
464434
diverging_fallback
465435
}
466436

437+
fn lint_never_type_fallback_flowing_into_unsafe_code(
438+
&self,
439+
unsafe_infer_vars: &OnceCell<UnordMap<ty::TyVid, (HirId, Span, UnsafeUseReason)>>,
440+
coercion_graph: &VecGraph<ty::TyVid, true>,
441+
root_vid: ty::TyVid,
442+
) {
443+
let unsafe_infer_vars = unsafe_infer_vars.get_or_init(|| {
444+
let unsafe_infer_vars = compute_unsafe_infer_vars(self.root_ctxt, self.body_id);
445+
debug!(?unsafe_infer_vars);
446+
unsafe_infer_vars
447+
});
448+
449+
let affected_unsafe_infer_vars =
450+
graph::depth_first_search_as_undirected(&coercion_graph, root_vid)
451+
.filter_map(|x| unsafe_infer_vars.get(&x).copied())
452+
.collect::<Vec<_>>();
453+
454+
for (hir_id, span, reason) in affected_unsafe_infer_vars {
455+
self.tcx.emit_node_span_lint(
456+
lint::builtin::NEVER_TYPE_FALLBACK_FLOWING_INTO_UNSAFE,
457+
hir_id,
458+
span,
459+
match reason {
460+
UnsafeUseReason::Call => errors::NeverTypeFallbackFlowingIntoUnsafe::Call,
461+
UnsafeUseReason::Method => errors::NeverTypeFallbackFlowingIntoUnsafe::Method,
462+
UnsafeUseReason::Path => errors::NeverTypeFallbackFlowingIntoUnsafe::Path,
463+
UnsafeUseReason::UnionField => {
464+
errors::NeverTypeFallbackFlowingIntoUnsafe::UnionField
465+
}
466+
UnsafeUseReason::Deref => errors::NeverTypeFallbackFlowingIntoUnsafe::Deref,
467+
},
468+
);
469+
}
470+
}
471+
467472
/// Returns a graph whose nodes are (unresolved) inference variables and where
468473
/// an edge `?A -> ?B` indicates that the variable `?A` is coerced to `?B`.
469474
fn create_coercion_graph(&self) -> VecGraph<ty::TyVid, true> {

compiler/rustc_lint_defs/src/builtin.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -4263,8 +4263,7 @@ declare_lint! {
42634263
///
42644264
/// // where absurd is a function with the following signature
42654265
/// // (it's sound, because `!` always marks unreachable code):
4266-
/// fn absurd<T>(_: !) -> T { ... }
4267-
// FIXME: use `core::convert::absurd` here instead, once it's merged
4266+
/// fn absurd<T>(never: !) -> T { ... }
42684267
/// ```
42694268
///
42704269
/// While it's convenient to be able to use non-diverging code in one of the branches (like
@@ -4321,7 +4320,12 @@ declare_lint! {
43214320
/// [`()`]: https://doc.rust-lang.org/core/primitive.unit.html
43224321
pub NEVER_TYPE_FALLBACK_FLOWING_INTO_UNSAFE,
43234322
Warn,
4324-
"never type fallback affecting unsafe function calls"
4323+
"never type fallback affecting unsafe function calls",
4324+
@future_incompatible = FutureIncompatibleInfo {
4325+
reason: FutureIncompatibilityReason::FutureReleaseSemanticsChange,
4326+
reference: "issue #123748 <https://github.com/rust-lang/rust/issues/123748>",
4327+
};
4328+
report_in_external_macro
43254329
}
43264330

43274331
declare_lint! {

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

+10
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ fn _zero() {
77
if false {
88
unsafe { mem::zeroed() }
99
//~^ warn: never type fallback affects this call to an `unsafe` function
10+
//~| warn: this will change its meaning in a future release!
1011
} else {
1112
return;
1213
};
@@ -21,6 +22,7 @@ fn _trans() {
2122
struct Zst;
2223
core::mem::transmute(Zst)
2324
//~^ warn: never type fallback affects this call to an `unsafe` function
25+
//~| warn: this will change its meaning in a future release!
2426
}
2527
} else {
2628
return;
@@ -36,6 +38,7 @@ fn _union() {
3638

3739
unsafe { Union { a: () }.b }
3840
//~^ warn: never type fallback affects this union access
41+
//~| warn: this will change its meaning in a future release!
3942
} else {
4043
return;
4144
};
@@ -45,6 +48,7 @@ fn _deref() {
4548
if false {
4649
unsafe { *ptr::from_ref(&()).cast() }
4750
//~^ warn: never type fallback affects this raw pointer dereference
51+
//~| warn: this will change its meaning in a future release!
4852
} else {
4953
return;
5054
};
@@ -62,6 +66,7 @@ fn _only_generics() {
6266

6367
unsafe { internally_create(x) }
6468
//~^ warn: never type fallback affects this call to an `unsafe` function
69+
//~| warn: this will change its meaning in a future release!
6570

6671
x.unwrap()
6772
} else {
@@ -73,9 +78,11 @@ fn _stored_function() {
7378
if false {
7479
let zeroed = mem::zeroed;
7580
//~^ warn: never type fallback affects this `unsafe` function
81+
//~| warn: this will change its meaning in a future release!
7682

7783
unsafe { zeroed() }
7884
//~^ warn: never type fallback affects this call to an `unsafe` function
85+
//~| warn: this will change its meaning in a future release!
7986
} else {
8087
return;
8188
};
@@ -90,6 +97,7 @@ fn _only_generics_stored_function() {
9097
let x = None;
9198
let f = internally_create;
9299
//~^ warn: never type fallback affects this `unsafe` function
100+
//~| warn: this will change its meaning in a future release!
93101

94102
unsafe { f(x) }
95103

@@ -113,6 +121,7 @@ fn _method() {
113121
unsafe {
114122
S(marker::PhantomData).create_out_of_thin_air()
115123
//~^ warn: never type fallback affects this call to an `unsafe` method
124+
//~| warn: this will change its meaning in a future release!
116125
}
117126
} else {
118127
return;
@@ -129,6 +138,7 @@ fn _objc() {
129138
() => {
130139
match send_message::<_ /* ?0 */>() {
131140
//~^ warn: never type fallback affects this call to an `unsafe` function
141+
//~| warn: this will change its meaning in a future release!
132142
Ok(x) => x,
133143
Err(_) => loop {},
134144
}

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

+29-9
Original file line numberDiff line numberDiff line change
@@ -4,82 +4,102 @@ warning: never type fallback affects this call to an `unsafe` function
44
LL | unsafe { mem::zeroed() }
55
| ^^^^^^^^^^^^^
66
|
7+
= warning: this will change its meaning in a future release!
8+
= note: for more information, see issue #123748 <https://github.com/rust-lang/rust/issues/123748>
79
= help: specify the type explicitly
810
= note: `#[warn(never_type_fallback_flowing_into_unsafe)]` on by default
911

1012
warning: never type fallback affects this call to an `unsafe` function
11-
--> $DIR/lint-never-type-fallback-flowing-into-unsafe.rs:22:13
13+
--> $DIR/lint-never-type-fallback-flowing-into-unsafe.rs:23:13
1214
|
1315
LL | core::mem::transmute(Zst)
1416
| ^^^^^^^^^^^^^^^^^^^^^^^^^
1517
|
18+
= warning: this will change its meaning in a future release!
19+
= note: for more information, see issue #123748 <https://github.com/rust-lang/rust/issues/123748>
1620
= help: specify the type explicitly
1721

1822
warning: never type fallback affects this union access
19-
--> $DIR/lint-never-type-fallback-flowing-into-unsafe.rs:37:18
23+
--> $DIR/lint-never-type-fallback-flowing-into-unsafe.rs:39:18
2024
|
2125
LL | unsafe { Union { a: () }.b }
2226
| ^^^^^^^^^^^^^^^^^
2327
|
28+
= warning: this will change its meaning in a future release!
29+
= note: for more information, see issue #123748 <https://github.com/rust-lang/rust/issues/123748>
2430
= help: specify the type explicitly
2531

2632
warning: never type fallback affects this raw pointer dereference
27-
--> $DIR/lint-never-type-fallback-flowing-into-unsafe.rs:46:18
33+
--> $DIR/lint-never-type-fallback-flowing-into-unsafe.rs:49:18
2834
|
2935
LL | unsafe { *ptr::from_ref(&()).cast() }
3036
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
3137
|
38+
= warning: this will change its meaning in a future release!
39+
= note: for more information, see issue #123748 <https://github.com/rust-lang/rust/issues/123748>
3240
= help: specify the type explicitly
3341

3442
warning: never type fallback affects this call to an `unsafe` function
35-
--> $DIR/lint-never-type-fallback-flowing-into-unsafe.rs:63:18
43+
--> $DIR/lint-never-type-fallback-flowing-into-unsafe.rs:67:18
3644
|
3745
LL | unsafe { internally_create(x) }
3846
| ^^^^^^^^^^^^^^^^^^^^
3947
|
48+
= warning: this will change its meaning in a future release!
49+
= note: for more information, see issue #123748 <https://github.com/rust-lang/rust/issues/123748>
4050
= help: specify the type explicitly
4151

4252
warning: never type fallback affects this call to an `unsafe` function
43-
--> $DIR/lint-never-type-fallback-flowing-into-unsafe.rs:77:18
53+
--> $DIR/lint-never-type-fallback-flowing-into-unsafe.rs:83:18
4454
|
4555
LL | unsafe { zeroed() }
4656
| ^^^^^^^^
4757
|
58+
= warning: this will change its meaning in a future release!
59+
= note: for more information, see issue #123748 <https://github.com/rust-lang/rust/issues/123748>
4860
= help: specify the type explicitly
4961

5062
warning: never type fallback affects this `unsafe` function
51-
--> $DIR/lint-never-type-fallback-flowing-into-unsafe.rs:74:22
63+
--> $DIR/lint-never-type-fallback-flowing-into-unsafe.rs:79:22
5264
|
5365
LL | let zeroed = mem::zeroed;
5466
| ^^^^^^^^^^^
5567
|
68+
= warning: this will change its meaning in a future release!
69+
= note: for more information, see issue #123748 <https://github.com/rust-lang/rust/issues/123748>
5670
= help: specify the type explicitly
5771

5872
warning: never type fallback affects this `unsafe` function
59-
--> $DIR/lint-never-type-fallback-flowing-into-unsafe.rs:91:17
73+
--> $DIR/lint-never-type-fallback-flowing-into-unsafe.rs:98:17
6074
|
6175
LL | let f = internally_create;
6276
| ^^^^^^^^^^^^^^^^^
6377
|
78+
= warning: this will change its meaning in a future release!
79+
= note: for more information, see issue #123748 <https://github.com/rust-lang/rust/issues/123748>
6480
= help: specify the type explicitly
6581

6682
warning: never type fallback affects this call to an `unsafe` method
67-
--> $DIR/lint-never-type-fallback-flowing-into-unsafe.rs:114:13
83+
--> $DIR/lint-never-type-fallback-flowing-into-unsafe.rs:122:13
6884
|
6985
LL | S(marker::PhantomData).create_out_of_thin_air()
7086
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
7187
|
88+
= warning: this will change its meaning in a future release!
89+
= note: for more information, see issue #123748 <https://github.com/rust-lang/rust/issues/123748>
7290
= help: specify the type explicitly
7391

7492
warning: never type fallback affects this call to an `unsafe` function
75-
--> $DIR/lint-never-type-fallback-flowing-into-unsafe.rs:130:19
93+
--> $DIR/lint-never-type-fallback-flowing-into-unsafe.rs:139:19
7694
|
7795
LL | match send_message::<_ /* ?0 */>() {
7896
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
7997
...
8098
LL | msg_send!();
8199
| ----------- in this macro invocation
82100
|
101+
= warning: this will change its meaning in a future release!
102+
= note: for more information, see issue #123748 <https://github.com/rust-lang/rust/issues/123748>
83103
= help: specify the type explicitly
84104
= note: this warning originates in the macro `msg_send` (in Nightly builds, run with -Z macro-backtrace for more info)
85105

0 commit comments

Comments
 (0)