diff --git a/compiler/rustc_mir_build/src/build/expr/as_rvalue.rs b/compiler/rustc_mir_build/src/build/expr/as_rvalue.rs index 35a00da8d38b2..3dafdcb788710 100644 --- a/compiler/rustc_mir_build/src/build/expr/as_rvalue.rs +++ b/compiler/rustc_mir_build/src/build/expr/as_rvalue.rs @@ -197,13 +197,13 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { // create all the steps directly in MIR with operations all backends need to support anyway. let (source, ty) = if let ty::Adt(adt_def, ..) = source.ty.kind() && adt_def.is_enum() { let discr_ty = adt_def.repr().discr_type().to_ty(this.tcx); - let place = unpack!(block = this.as_place(block, source)); + let temp = unpack!(block = this.as_temp(block, scope, source, Mutability::Not)); let discr = this.temp(discr_ty, source.span); this.cfg.push_assign( block, source_info, discr, - Rvalue::Discriminant(place), + Rvalue::Discriminant(temp.into()), ); (Operand::Move(discr), discr_ty) diff --git a/src/test/mir-opt/enum_cast.bar.mir_map.0.mir b/src/test/mir-opt/enum_cast.bar.mir_map.0.mir index 8b12525b576ff..e58085f701ac2 100644 --- a/src/test/mir-opt/enum_cast.bar.mir_map.0.mir +++ b/src/test/mir-opt/enum_cast.bar.mir_map.0.mir @@ -3,11 +3,15 @@ fn bar(_1: Bar) -> usize { debug bar => _1; // in scope 0 at $DIR/enum_cast.rs:+0:8: +0:11 let mut _0: usize; // return place in scope 0 at $DIR/enum_cast.rs:+0:21: +0:26 - let mut _2: isize; // in scope 0 at $DIR/enum_cast.rs:+1:5: +1:8 + let _2: Bar; // in scope 0 at $DIR/enum_cast.rs:+1:5: +1:8 + let mut _3: isize; // in scope 0 at $DIR/enum_cast.rs:+1:5: +1:8 bb0: { - _2 = discriminant(_1); // scope 0 at $DIR/enum_cast.rs:+1:5: +1:17 - _0 = move _2 as usize (IntToInt); // scope 0 at $DIR/enum_cast.rs:+1:5: +1:17 + StorageLive(_2); // scope 0 at $DIR/enum_cast.rs:+1:5: +1:8 + _2 = move _1; // scope 0 at $DIR/enum_cast.rs:+1:5: +1:8 + _3 = discriminant(_2); // scope 0 at $DIR/enum_cast.rs:+1:5: +1:17 + _0 = move _3 as usize (IntToInt); // scope 0 at $DIR/enum_cast.rs:+1:5: +1:17 + StorageDead(_2); // scope 0 at $DIR/enum_cast.rs:+1:16: +1:17 return; // scope 0 at $DIR/enum_cast.rs:+2:2: +2:2 } } diff --git a/src/test/mir-opt/enum_cast.boo.mir_map.0.mir b/src/test/mir-opt/enum_cast.boo.mir_map.0.mir index a77f4d06c239b..525c6234ed31d 100644 --- a/src/test/mir-opt/enum_cast.boo.mir_map.0.mir +++ b/src/test/mir-opt/enum_cast.boo.mir_map.0.mir @@ -3,11 +3,15 @@ fn boo(_1: Boo) -> usize { debug boo => _1; // in scope 0 at $DIR/enum_cast.rs:+0:8: +0:11 let mut _0: usize; // return place in scope 0 at $DIR/enum_cast.rs:+0:21: +0:26 - let mut _2: u8; // in scope 0 at $DIR/enum_cast.rs:+1:5: +1:8 + let _2: Boo; // in scope 0 at $DIR/enum_cast.rs:+1:5: +1:8 + let mut _3: u8; // in scope 0 at $DIR/enum_cast.rs:+1:5: +1:8 bb0: { - _2 = discriminant(_1); // scope 0 at $DIR/enum_cast.rs:+1:5: +1:17 - _0 = move _2 as usize (IntToInt); // scope 0 at $DIR/enum_cast.rs:+1:5: +1:17 + StorageLive(_2); // scope 0 at $DIR/enum_cast.rs:+1:5: +1:8 + _2 = move _1; // scope 0 at $DIR/enum_cast.rs:+1:5: +1:8 + _3 = discriminant(_2); // scope 0 at $DIR/enum_cast.rs:+1:5: +1:17 + _0 = move _3 as usize (IntToInt); // scope 0 at $DIR/enum_cast.rs:+1:5: +1:17 + StorageDead(_2); // scope 0 at $DIR/enum_cast.rs:+1:16: +1:17 return; // scope 0 at $DIR/enum_cast.rs:+2:2: +2:2 } } diff --git a/src/test/mir-opt/enum_cast.droppy.mir_map.0.mir b/src/test/mir-opt/enum_cast.droppy.mir_map.0.mir index ae3e71df8c6df..bb5faa48047a9 100644 --- a/src/test/mir-opt/enum_cast.droppy.mir_map.0.mir +++ b/src/test/mir-opt/enum_cast.droppy.mir_map.0.mir @@ -4,8 +4,9 @@ fn droppy() -> () { let mut _0: (); // return place in scope 0 at $DIR/enum_cast.rs:+0:13: +0:13 let _1: (); // in scope 0 at $DIR/enum_cast.rs:+1:5: +6:6 let _2: Droppy; // in scope 0 at $DIR/enum_cast.rs:+2:13: +2:14 - let mut _4: isize; // in scope 0 at $DIR/enum_cast.rs:+5:17: +5:18 - let _5: Droppy; // in scope 0 at $DIR/enum_cast.rs:+7:9: +7:10 + let _4: Droppy; // in scope 0 at $DIR/enum_cast.rs:+5:17: +5:18 + let mut _5: isize; // in scope 0 at $DIR/enum_cast.rs:+5:17: +5:18 + let _6: Droppy; // in scope 0 at $DIR/enum_cast.rs:+7:9: +7:10 scope 1 { debug x => _2; // in scope 1 at $DIR/enum_cast.rs:+2:13: +2:14 scope 2 { @@ -16,7 +17,7 @@ fn droppy() -> () { } } scope 4 { - debug z => _5; // in scope 4 at $DIR/enum_cast.rs:+7:9: +7:10 + debug z => _6; // in scope 4 at $DIR/enum_cast.rs:+7:9: +7:10 } bb0: { @@ -25,30 +26,41 @@ fn droppy() -> () { _2 = Droppy::C; // scope 0 at $DIR/enum_cast.rs:+2:17: +2:26 FakeRead(ForLet(None), _2); // scope 0 at $DIR/enum_cast.rs:+2:13: +2:14 StorageLive(_3); // scope 3 at $DIR/enum_cast.rs:+5:13: +5:14 - _4 = discriminant(_2); // scope 3 at $DIR/enum_cast.rs:+5:17: +5:27 - _3 = move _4 as usize (IntToInt); // scope 3 at $DIR/enum_cast.rs:+5:17: +5:27 + StorageLive(_4); // scope 3 at $DIR/enum_cast.rs:+5:17: +5:18 + _4 = move _2; // scope 3 at $DIR/enum_cast.rs:+5:17: +5:18 + _5 = discriminant(_4); // scope 3 at $DIR/enum_cast.rs:+5:17: +5:27 + _3 = move _5 as usize (IntToInt); // scope 3 at $DIR/enum_cast.rs:+5:17: +5:27 + drop(_4) -> [return: bb1, unwind: bb4]; // scope 3 at $DIR/enum_cast.rs:+5:26: +5:27 + } + + bb1: { + StorageDead(_4); // scope 3 at $DIR/enum_cast.rs:+5:26: +5:27 FakeRead(ForLet(None), _3); // scope 3 at $DIR/enum_cast.rs:+5:13: +5:14 _1 = const (); // scope 0 at $DIR/enum_cast.rs:+1:5: +6:6 StorageDead(_3); // scope 1 at $DIR/enum_cast.rs:+6:5: +6:6 - drop(_2) -> [return: bb1, unwind: bb3]; // scope 0 at $DIR/enum_cast.rs:+6:5: +6:6 + drop(_2) -> [return: bb2, unwind: bb5]; // scope 0 at $DIR/enum_cast.rs:+6:5: +6:6 } - bb1: { + bb2: { StorageDead(_2); // scope 0 at $DIR/enum_cast.rs:+6:5: +6:6 StorageDead(_1); // scope 0 at $DIR/enum_cast.rs:+6:5: +6:6 - StorageLive(_5); // scope 0 at $DIR/enum_cast.rs:+7:9: +7:10 - _5 = Droppy::B; // scope 0 at $DIR/enum_cast.rs:+7:13: +7:22 - FakeRead(ForLet(None), _5); // scope 0 at $DIR/enum_cast.rs:+7:9: +7:10 + StorageLive(_6); // scope 0 at $DIR/enum_cast.rs:+7:9: +7:10 + _6 = Droppy::B; // scope 0 at $DIR/enum_cast.rs:+7:13: +7:22 + FakeRead(ForLet(None), _6); // scope 0 at $DIR/enum_cast.rs:+7:9: +7:10 _0 = const (); // scope 0 at $DIR/enum_cast.rs:+0:13: +8:2 - drop(_5) -> [return: bb2, unwind: bb3]; // scope 0 at $DIR/enum_cast.rs:+8:1: +8:2 + drop(_6) -> [return: bb3, unwind: bb5]; // scope 0 at $DIR/enum_cast.rs:+8:1: +8:2 } - bb2: { - StorageDead(_5); // scope 0 at $DIR/enum_cast.rs:+8:1: +8:2 + bb3: { + StorageDead(_6); // scope 0 at $DIR/enum_cast.rs:+8:1: +8:2 return; // scope 0 at $DIR/enum_cast.rs:+8:2: +8:2 } - bb3 (cleanup): { + bb4 (cleanup): { + drop(_2) -> bb5; // scope 0 at $DIR/enum_cast.rs:+6:5: +6:6 + } + + bb5 (cleanup): { resume; // scope 0 at $DIR/enum_cast.rs:+0:1: +8:2 } } diff --git a/src/test/mir-opt/enum_cast.foo.mir_map.0.mir b/src/test/mir-opt/enum_cast.foo.mir_map.0.mir index 9e44d9158e02f..a1d29a0b90320 100644 --- a/src/test/mir-opt/enum_cast.foo.mir_map.0.mir +++ b/src/test/mir-opt/enum_cast.foo.mir_map.0.mir @@ -3,11 +3,15 @@ fn foo(_1: Foo) -> usize { debug foo => _1; // in scope 0 at $DIR/enum_cast.rs:+0:8: +0:11 let mut _0: usize; // return place in scope 0 at $DIR/enum_cast.rs:+0:21: +0:26 - let mut _2: isize; // in scope 0 at $DIR/enum_cast.rs:+1:5: +1:8 + let _2: Foo; // in scope 0 at $DIR/enum_cast.rs:+1:5: +1:8 + let mut _3: isize; // in scope 0 at $DIR/enum_cast.rs:+1:5: +1:8 bb0: { - _2 = discriminant(_1); // scope 0 at $DIR/enum_cast.rs:+1:5: +1:17 - _0 = move _2 as usize (IntToInt); // scope 0 at $DIR/enum_cast.rs:+1:5: +1:17 + StorageLive(_2); // scope 0 at $DIR/enum_cast.rs:+1:5: +1:8 + _2 = move _1; // scope 0 at $DIR/enum_cast.rs:+1:5: +1:8 + _3 = discriminant(_2); // scope 0 at $DIR/enum_cast.rs:+1:5: +1:17 + _0 = move _3 as usize (IntToInt); // scope 0 at $DIR/enum_cast.rs:+1:5: +1:17 + StorageDead(_2); // scope 0 at $DIR/enum_cast.rs:+1:16: +1:17 return; // scope 0 at $DIR/enum_cast.rs:+2:2: +2:2 } } diff --git a/src/test/run-pass-valgrind/cast-enum-with-dtor.rs b/src/test/run-pass-valgrind/cast-enum-with-dtor.rs index f29bc50e84c4e..f7ef92df8fbec 100644 --- a/src/test/run-pass-valgrind/cast-enum-with-dtor.rs +++ b/src/test/run-pass-valgrind/cast-enum-with-dtor.rs @@ -28,7 +28,7 @@ fn main() { { let e = E::C; assert_eq!(e as u32, 2); - assert_eq!(FLAG.load(Ordering::SeqCst), 0); + assert_eq!(FLAG.load(Ordering::SeqCst), 1); } assert_eq!(FLAG.load(Ordering::SeqCst), 1); } diff --git a/src/test/ui/mir/issue-102389.rs b/src/test/ui/mir/issue-102389.rs new file mode 100644 index 0000000000000..8b27d5e55743d --- /dev/null +++ b/src/test/ui/mir/issue-102389.rs @@ -0,0 +1,8 @@ +enum Enum { A, B, C } + +fn func(inbounds: &Enum, array: &[i16; 3]) -> i16 { + array[*inbounds as usize] + //~^ ERROR [E0507] +} + +fn main() {} diff --git a/src/test/ui/mir/issue-102389.stderr b/src/test/ui/mir/issue-102389.stderr new file mode 100644 index 0000000000000..925dc258a4c3c --- /dev/null +++ b/src/test/ui/mir/issue-102389.stderr @@ -0,0 +1,9 @@ +error[E0507]: cannot move out of `*inbounds` which is behind a shared reference + --> $DIR/issue-102389.rs:4:11 + | +LL | array[*inbounds as usize] + | ^^^^^^^^^ move occurs because `*inbounds` has type `Enum`, which does not implement the `Copy` trait + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0507`.