Skip to content

Commit e138e87

Browse files
Never patterns constitute a read for unsafety
1 parent f79a912 commit e138e87

File tree

3 files changed

+41
-4
lines changed

3 files changed

+41
-4
lines changed

compiler/rustc_mir_build/src/check_unsafety.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -315,14 +315,15 @@ impl<'a, 'tcx> Visitor<'a, 'tcx> for UnsafetyVisitor<'a, 'tcx> {
315315
| PatKind::DerefPattern { .. }
316316
| PatKind::Range { .. }
317317
| PatKind::Slice { .. }
318-
| PatKind::Array { .. } => {
318+
| PatKind::Array { .. }
319+
// Never constitutes a witness of uninhabitedness.
320+
| PatKind::Never => {
319321
self.requires_unsafe(pat.span, AccessToUnionField);
320322
return; // we can return here since this already requires unsafe
321323
}
322-
// wildcard/never don't take anything
324+
// wildcard doesn't read anything.
323325
PatKind::Wild |
324-
PatKind::Never |
325-
// these just wrap other patterns
326+
// these just wrap other patterns, which we recurse on below.
326327
PatKind::Or { .. } |
327328
PatKind::InlineConstant { .. } |
328329
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)