Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 4 additions & 8 deletions compiler/rustc_hir_typeck/src/expr_use_visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -912,7 +912,7 @@ impl<'tcx, Cx: TypeInformationCtxt<'tcx>, D: Delegate<'tcx>> ExprUseVisitor<'tcx
_ => {
// Otherwise, this is a struct/enum variant, and so it's
// only a read if we need to read the discriminant.
if self.is_multivariant_adt(place.place.ty(), *span) {
if self.is_enum(place.place.ty(), *span) {
read_discriminant();
}
}
Expand All @@ -931,7 +931,7 @@ impl<'tcx, Cx: TypeInformationCtxt<'tcx>, D: Delegate<'tcx>> ExprUseVisitor<'tcx
read_discriminant();
}
PatKind::Struct(..) | PatKind::TupleStruct(..) => {
if self.is_multivariant_adt(place.place.ty(), pat.span) {
if self.is_enum(place.place.ty(), pat.span) {
read_discriminant();
}
}
Expand Down Expand Up @@ -1842,14 +1842,10 @@ impl<'tcx, Cx: TypeInformationCtxt<'tcx>, D: Delegate<'tcx>> ExprUseVisitor<'tcx
}
}

/// Checks whether a type has multiple variants, and therefore, whether a
/// read of the discriminant might be necessary.
#[instrument(skip(self, span), level = "debug")]
fn is_multivariant_adt(&self, ty: Ty<'tcx>, span: Span) -> bool {
fn is_enum(&self, ty: Ty<'tcx>, span: Span) -> bool {
if let ty::Adt(def, _) = self.cx.structurally_resolve_type(span, ty).kind() {
// We treat non-exhaustive enums the same independent of the crate they are
// defined in, to avoid differences in the operational semantics between crates.
def.variants().len() > 1 || def.is_variant_list_non_exhaustive()
def.is_enum()
} else {
false
}
Expand Down
6 changes: 1 addition & 5 deletions compiler/rustc_mir_build/src/builder/matches/match_pair.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,11 +295,7 @@ impl<'tcx> MatchPairTree<'tcx> {
let downcast_place = place_builder.downcast(adt_def, variant_index); // `(x as Variant)`
cx.field_match_pairs(&mut subpairs, extra_data, downcast_place, subpatterns);

// We treat non-exhaustive enums the same independent of the crate they are
// defined in, to avoid differences in the operational semantics between crates.
let refutable =
adt_def.variants().len() > 1 || adt_def.is_variant_list_non_exhaustive();
if refutable {
if adt_def.is_enum() {
Some(TestableCase::Variant { adt_def, variant_index })
} else {
None
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ pub union U {

fn main() {
let E::A(ref _a) = unsafe { &(&U { u: () }).e };
//~^ ERROR: read discriminant of an uninhabited enum variant
}
13 changes: 13 additions & 0 deletions src/tools/miri/tests/fail/issue-120337-irrefutable-let-ice.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
error: Undefined Behavior: read discriminant of an uninhabited enum variant
--> tests/fail/issue-120337-irrefutable-let-ice.rs:LL:CC
|
LL | let E::A(ref _a) = unsafe { &(&U { u: () }).e };
| ^^^^^^^^^^^^ Undefined Behavior occurred here
|
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information

note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace

error: aborting due to 1 previous error

2 changes: 2 additions & 0 deletions src/tools/miri/tests/fail/match/all_variants_uninhabited.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// This UB should be detected even with validation disabled.
//@compile-flags: -Zmiri-disable-validation
#![allow(deref_nullptr)]

enum Never {}
Expand Down
3 changes: 3 additions & 0 deletions src/tools/miri/tests/fail/match/only_inhabited_variant.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
// rust-lang/miri#4778
//
// This UB should be detected even with validation disabled.
//@compile-flags: -Zmiri-disable-validation
#![feature(never_type)]

#[repr(C)]
Expand Down
19 changes: 4 additions & 15 deletions src/tools/miri/tests/fail/match/single_variant.rs
Original file line number Diff line number Diff line change
@@ -1,32 +1,21 @@
// Ideally, this would be UB regardless of #[non_exhaustive]. For now,
// at least the semantics don't depend on the crate you're in.
//
// See: rust-lang/rust#147722
//
// This UB should be detected even with validation disabled.
//@compile-flags: -Zmiri-disable-validation
#![allow(dead_code)]

#[repr(u8)]
enum Exhaustive {
A(u8) = 42,
}

#[repr(u8)]
#[non_exhaustive]
enum NonExhaustive {
A(u8) = 42,
}

fn main() {
unsafe {
let x: &[u8; 2] = &[21, 37];
let y: &Exhaustive = std::mem::transmute(x);
match y {
Exhaustive::A(_) => {}
}

let y: &NonExhaustive = std::mem::transmute(x);
match y {
//~^ ERROR: enum value has invalid tag
NonExhaustive::A(_) => {}
Exhaustive::A(_) => {}
}
}
}
25 changes: 25 additions & 0 deletions src/tools/miri/tests/fail/match/single_variant_non_exhaustive.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Like single_variant.rs, but with a non_exhaustive enum, as the generated MIR used to differ
// between these cases.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like it could use revisions to avoid duplicating the code? Same for "single_variant_uninit".

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I wasn't aware that revisions are a thing! I would've done that in the first place had I known about it.

//
// See: rust-lang/rust#147722
//
// This UB should be detected even with validation disabled.
//@compile-flags: -Zmiri-disable-validation
#![allow(dead_code)]

#[repr(u8)]
#[non_exhaustive]
enum NonExhaustive {
A(u8) = 42,
}

fn main() {
unsafe {
let x: &[u8; 2] = &[21, 37];
let y: &NonExhaustive = std::mem::transmute(x);
match y {
//~^ ERROR: enum value has invalid tag
NonExhaustive::A(_) => {}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
error: Undefined Behavior: enum value has invalid tag: 0x15
--> tests/fail/match/single_variant_non_exhaustive.rs:LL:CC
|
LL | match y {
| ^ Undefined Behavior occurred here
|
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information

note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace

error: aborting due to 1 previous error

20 changes: 4 additions & 16 deletions src/tools/miri/tests/fail/match/single_variant_uninit.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Ideally, this would be UB regardless of #[non_exhaustive]. For now,
// at least the semantics don't depend on the crate you're in.
//
// See: rust-lang/rust#147722
//
// This UB should be detected even with validation disabled.
//@compile-flags: -Zmiri-disable-validation
#![allow(dead_code)]
#![allow(unreachable_patterns)]

Expand All @@ -10,27 +10,15 @@ enum Exhaustive {
A(u8) = 0,
}

#[repr(u8)]
#[non_exhaustive]
enum NonExhaustive {
A(u8) = 0,
}

use std::mem::MaybeUninit;

fn main() {
let buffer: [MaybeUninit<u8>; 2] = [MaybeUninit::uninit(), MaybeUninit::new(0u8)];
let exh: *const Exhaustive = (&raw const buffer).cast();
let nexh: *const NonExhaustive = (&raw const buffer).cast();
unsafe {
match *exh {
Exhaustive::A(ref _val) => {}
_ => {}
}

match *nexh {
//~^ ERROR: memory is uninitialized
NonExhaustive::A(ref _val) => {}
Exhaustive::A(ref _val) => {}
_ => {}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/tools/miri/tests/fail/match/single_variant_uninit.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
error: Undefined Behavior: reading memory at ALLOC[0x0..0x1], but memory is uninitialized at [0x0..0x1], and this operation requires initialized memory
--> tests/fail/match/single_variant_uninit.rs:LL:CC
|
LL | match *nexh {
| ^^^^^ Undefined Behavior occurred here
LL | match *exh {
| ^^^^ Undefined Behavior occurred here
|
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Like single_variant_uninit.rs, but with a non_exhaustive enum, as the generated MIR used to
// differ between these cases.
//
// See: rust-lang/rust#147722
//
// This UB should be detected even with validation disabled.
//@compile-flags: -Zmiri-disable-validation
#![allow(dead_code)]
#![allow(unreachable_patterns)]

#[repr(u8)]
#[non_exhaustive]
enum NonExhaustive {
A(u8) = 0,
}

use std::mem::MaybeUninit;

fn main() {
let buffer: [MaybeUninit<u8>; 2] = [MaybeUninit::uninit(), MaybeUninit::new(0u8)];
let nexh: *const NonExhaustive = (&raw const buffer).cast();
unsafe {
match *nexh {
//~^ ERROR: memory is uninitialized
NonExhaustive::A(ref _val) => {}
_ => {}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
error: Undefined Behavior: reading memory at ALLOC[0x0..0x1], but memory is uninitialized at [0x0..0x1], and this operation requires initialized memory
--> tests/fail/match/single_variant_uninit_non_exhaustive.rs:LL:CC
|
LL | match *nexh {
| ^^^^^ Undefined Behavior occurred here
|
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information

Uninitialized memory occurred at ALLOC[0x0..0x1], in this allocation:
ALLOC (stack variable, size: 2, align: 1) {
__ 00 │ ░.
}

note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace

error: aborting due to 1 previous error

46 changes: 29 additions & 17 deletions tests/mir-opt/gvn_copy_aggregate.remove_storage_dead.GVN.diff
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@
let _2: T;
let mut _3: AlwaysSome<T>;
let mut _4: fn() -> AlwaysSome<T>;
let _5: T;
let mut _6: T;
let mut _7: isize;
let mut _5: isize;
let _6: T;
let mut _7: T;
let mut _8: isize;
let mut _9: isize;
scope 1 {
debug v => _2;
}
scope 2 {
debug v => _5;
debug v => _6;
}

bb0: {
Expand All @@ -30,23 +31,34 @@

bb1: {
StorageDead(_4);
- StorageLive(_5);
- _5 = move ((_3 as Some).0: T);
- _2 = move _5;
- StorageDead(_5);
_5 = discriminant(_3);
- switchInt(move _5) -> [0: bb3, otherwise: bb2];
+ switchInt(copy _5) -> [0: bb3, otherwise: bb2];
}

bb2: {
unreachable;
}

bb3: {
- StorageLive(_6);
- _6 = move ((_3 as Some).0: T);
- _2 = move _6;
- StorageDead(_6);
- _8 = discriminant(_3);
- StorageDead(_3);
+ nop;
+ _5 = copy ((_3 as Some).0: T);
+ _2 = copy _5;
+ _6 = copy ((_3 as Some).0: T);
+ _2 = copy _6;
+ nop;
_7 = discriminant(_3);
- StorageDead(_3);
+ _8 = copy _5;
+ nop;
StorageLive(_6);
- _6 = move _2;
- _0 = AlwaysSome::<T>::Some(move _6);
+ _6 = copy _5;
StorageLive(_7);
- _7 = move _2;
- _0 = AlwaysSome::<T>::Some(move _7);
+ _7 = copy _6;
+ _0 = copy _3;
StorageDead(_6);
StorageDead(_7);
StorageDead(_2);
return;
}
Expand Down
16 changes: 13 additions & 3 deletions tests/mir-opt/gvn_uninhabited.f.GVN.panic-unwind.diff
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,28 @@
let mut _2: E;
let mut _3: &U;
let _4: U;
let mut _5: &U;
let mut _5: isize;
let mut _6: &U;
scope 1 {
debug i => _1;
}

bb0: {
StorageLive(_2);
StorageLive(_3);
_5 = const f::promoted[0];
_3 = &(*_5);
_6 = const f::promoted[0];
_3 = &(*_6);
- _2 = copy ((*_3).1: E);
+ _2 = const Scalar(0x00000000): E;
_5 = discriminant(_2);
switchInt(move _5) -> [0: bb2, otherwise: bb1];
}

bb1: {
unreachable;
}

bb2: {
StorageLive(_1);
- _1 = copy ((_2 as A).1: u32);
+ _1 = const 0_u32;
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/borrowck/borrowck-describe-lvalue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ fn main() {
{
let mut e = Baz::X(2);
let x = e.x();
match e {
match e { //~ ERROR cannot use `e` because it was mutably borrowed
Baz::X(value) => value //~ ERROR cannot use `e.0` because it was mutably borrowed
};
drop(x);
Expand Down Expand Up @@ -92,7 +92,7 @@ fn main() {
{
let mut e = Box::new(Baz::X(3));
let x = e.x();
match *e {
match *e { //~ ERROR cannot use `*e` because it was mutably borrowed
Baz::X(value) => value
//~^ ERROR cannot use `e.0` because it was mutably borrowed
};
Expand Down
Loading
Loading