Skip to content

Commit f9b8ef0

Browse files
Rollup merge of #130533 - compiler-errors:never-pat-unsafeck, r=Nadrieril
Never patterns constitute a read for unsafety This code is otherwise unsound if we don't emit an unsafety error here. Noticed when fixing #130528, but it's totally unrelated. r? `@Nadrieril`
2 parents 944df8e + e138e87 commit f9b8ef0

File tree

3 files changed

+41
-4
lines changed

3 files changed

+41
-4
lines changed

Diff for: compiler/rustc_mir_build/src/check_unsafety.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -322,14 +322,15 @@ impl<'a, 'tcx> Visitor<'a, 'tcx> for UnsafetyVisitor<'a, 'tcx> {
322322
| PatKind::DerefPattern { .. }
323323
| PatKind::Range { .. }
324324
| PatKind::Slice { .. }
325-
| PatKind::Array { .. } => {
325+
| PatKind::Array { .. }
326+
// Never constitutes a witness of uninhabitedness.
327+
| PatKind::Never => {
326328
self.requires_unsafe(pat.span, AccessToUnionField);
327329
return; // we can return here since this already requires unsafe
328330
}
329-
// wildcard/never don't take anything
331+
// wildcard doesn't read anything.
330332
PatKind::Wild |
331-
PatKind::Never |
332-
// these just wrap other patterns
333+
// these just wrap other patterns, which we recurse on below.
333334
PatKind::Or { .. } |
334335
PatKind::InlineConstant { .. } |
335336
PatKind::AscribeUserType { .. } |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Make sure we consider `!` to be a union read.
2+
3+
#![feature(never_type, never_patterns)]
4+
//~^ WARN the feature `never_patterns` is incomplete
5+
6+
union U {
7+
a: !,
8+
b: usize,
9+
}
10+
11+
fn foo<T>(u: U) -> ! {
12+
let U { a: ! } = u;
13+
//~^ ERROR access to union field is unsafe
14+
}
15+
16+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
warning: the feature `never_patterns` is incomplete and may not be safe to use and/or cause compiler crashes
2+
--> $DIR/never-pattern-is-a-read.rs:3:24
3+
|
4+
LL | #![feature(never_type, never_patterns)]
5+
| ^^^^^^^^^^^^^^
6+
|
7+
= note: see issue #118155 <https://github.com/rust-lang/rust/issues/118155> for more information
8+
= note: `#[warn(incomplete_features)]` on by default
9+
10+
error[E0133]: access to union field is unsafe and requires unsafe function or block
11+
--> $DIR/never-pattern-is-a-read.rs:12:16
12+
|
13+
LL | let U { a: ! } = u;
14+
| ^ access to union field
15+
|
16+
= note: the field may not be properly initialized: using uninitialized data will cause undefined behavior
17+
18+
error: aborting due to 1 previous error; 1 warning emitted
19+
20+
For more information about this error, try `rustc --explain E0133`.

0 commit comments

Comments
 (0)