Skip to content

Commit cf67565

Browse files
authored
Rollup merge of rust-lang#103016 - nbdd0121:enum, r=pnkfelix
Ensure enum cast moves Fix rust-lang#102389 r? ``@pnkfelix``
2 parents ea709c2 + 4a25a49 commit cf67565

File tree

8 files changed

+67
-26
lines changed

8 files changed

+67
-26
lines changed

Diff for: compiler/rustc_mir_build/src/build/expr/as_rvalue.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -197,13 +197,13 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
197197
// create all the steps directly in MIR with operations all backends need to support anyway.
198198
let (source, ty) = if let ty::Adt(adt_def, ..) = source.ty.kind() && adt_def.is_enum() {
199199
let discr_ty = adt_def.repr().discr_type().to_ty(this.tcx);
200-
let place = unpack!(block = this.as_place(block, source));
200+
let temp = unpack!(block = this.as_temp(block, scope, source, Mutability::Not));
201201
let discr = this.temp(discr_ty, source.span);
202202
this.cfg.push_assign(
203203
block,
204204
source_info,
205205
discr,
206-
Rvalue::Discriminant(place),
206+
Rvalue::Discriminant(temp.into()),
207207
);
208208

209209
(Operand::Move(discr), discr_ty)

Diff for: src/test/mir-opt/enum_cast.bar.mir_map.0.mir

+7-3
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,15 @@
33
fn bar(_1: Bar) -> usize {
44
debug bar => _1; // in scope 0 at $DIR/enum_cast.rs:+0:8: +0:11
55
let mut _0: usize; // return place in scope 0 at $DIR/enum_cast.rs:+0:21: +0:26
6-
let mut _2: isize; // in scope 0 at $DIR/enum_cast.rs:+1:5: +1:8
6+
let _2: Bar; // in scope 0 at $DIR/enum_cast.rs:+1:5: +1:8
7+
let mut _3: isize; // in scope 0 at $DIR/enum_cast.rs:+1:5: +1:8
78

89
bb0: {
9-
_2 = discriminant(_1); // scope 0 at $DIR/enum_cast.rs:+1:5: +1:17
10-
_0 = move _2 as usize (IntToInt); // scope 0 at $DIR/enum_cast.rs:+1:5: +1:17
10+
StorageLive(_2); // scope 0 at $DIR/enum_cast.rs:+1:5: +1:8
11+
_2 = move _1; // scope 0 at $DIR/enum_cast.rs:+1:5: +1:8
12+
_3 = discriminant(_2); // scope 0 at $DIR/enum_cast.rs:+1:5: +1:17
13+
_0 = move _3 as usize (IntToInt); // scope 0 at $DIR/enum_cast.rs:+1:5: +1:17
14+
StorageDead(_2); // scope 0 at $DIR/enum_cast.rs:+1:16: +1:17
1115
return; // scope 0 at $DIR/enum_cast.rs:+2:2: +2:2
1216
}
1317
}

Diff for: src/test/mir-opt/enum_cast.boo.mir_map.0.mir

+7-3
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,15 @@
33
fn boo(_1: Boo) -> usize {
44
debug boo => _1; // in scope 0 at $DIR/enum_cast.rs:+0:8: +0:11
55
let mut _0: usize; // return place in scope 0 at $DIR/enum_cast.rs:+0:21: +0:26
6-
let mut _2: u8; // in scope 0 at $DIR/enum_cast.rs:+1:5: +1:8
6+
let _2: Boo; // in scope 0 at $DIR/enum_cast.rs:+1:5: +1:8
7+
let mut _3: u8; // in scope 0 at $DIR/enum_cast.rs:+1:5: +1:8
78

89
bb0: {
9-
_2 = discriminant(_1); // scope 0 at $DIR/enum_cast.rs:+1:5: +1:17
10-
_0 = move _2 as usize (IntToInt); // scope 0 at $DIR/enum_cast.rs:+1:5: +1:17
10+
StorageLive(_2); // scope 0 at $DIR/enum_cast.rs:+1:5: +1:8
11+
_2 = move _1; // scope 0 at $DIR/enum_cast.rs:+1:5: +1:8
12+
_3 = discriminant(_2); // scope 0 at $DIR/enum_cast.rs:+1:5: +1:17
13+
_0 = move _3 as usize (IntToInt); // scope 0 at $DIR/enum_cast.rs:+1:5: +1:17
14+
StorageDead(_2); // scope 0 at $DIR/enum_cast.rs:+1:16: +1:17
1115
return; // scope 0 at $DIR/enum_cast.rs:+2:2: +2:2
1216
}
1317
}

Diff for: src/test/mir-opt/enum_cast.droppy.mir_map.0.mir

+26-14
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ fn droppy() -> () {
44
let mut _0: (); // return place in scope 0 at $DIR/enum_cast.rs:+0:13: +0:13
55
let _1: (); // in scope 0 at $DIR/enum_cast.rs:+1:5: +6:6
66
let _2: Droppy; // in scope 0 at $DIR/enum_cast.rs:+2:13: +2:14
7-
let mut _4: isize; // in scope 0 at $DIR/enum_cast.rs:+5:17: +5:18
8-
let _5: Droppy; // in scope 0 at $DIR/enum_cast.rs:+7:9: +7:10
7+
let _4: Droppy; // in scope 0 at $DIR/enum_cast.rs:+5:17: +5:18
8+
let mut _5: isize; // in scope 0 at $DIR/enum_cast.rs:+5:17: +5:18
9+
let _6: Droppy; // in scope 0 at $DIR/enum_cast.rs:+7:9: +7:10
910
scope 1 {
1011
debug x => _2; // in scope 1 at $DIR/enum_cast.rs:+2:13: +2:14
1112
scope 2 {
@@ -16,7 +17,7 @@ fn droppy() -> () {
1617
}
1718
}
1819
scope 4 {
19-
debug z => _5; // in scope 4 at $DIR/enum_cast.rs:+7:9: +7:10
20+
debug z => _6; // in scope 4 at $DIR/enum_cast.rs:+7:9: +7:10
2021
}
2122

2223
bb0: {
@@ -25,30 +26,41 @@ fn droppy() -> () {
2526
_2 = Droppy::C; // scope 0 at $DIR/enum_cast.rs:+2:17: +2:26
2627
FakeRead(ForLet(None), _2); // scope 0 at $DIR/enum_cast.rs:+2:13: +2:14
2728
StorageLive(_3); // scope 3 at $DIR/enum_cast.rs:+5:13: +5:14
28-
_4 = discriminant(_2); // scope 3 at $DIR/enum_cast.rs:+5:17: +5:27
29-
_3 = move _4 as usize (IntToInt); // scope 3 at $DIR/enum_cast.rs:+5:17: +5:27
29+
StorageLive(_4); // scope 3 at $DIR/enum_cast.rs:+5:17: +5:18
30+
_4 = move _2; // scope 3 at $DIR/enum_cast.rs:+5:17: +5:18
31+
_5 = discriminant(_4); // scope 3 at $DIR/enum_cast.rs:+5:17: +5:27
32+
_3 = move _5 as usize (IntToInt); // scope 3 at $DIR/enum_cast.rs:+5:17: +5:27
33+
drop(_4) -> [return: bb1, unwind: bb4]; // scope 3 at $DIR/enum_cast.rs:+5:26: +5:27
34+
}
35+
36+
bb1: {
37+
StorageDead(_4); // scope 3 at $DIR/enum_cast.rs:+5:26: +5:27
3038
FakeRead(ForLet(None), _3); // scope 3 at $DIR/enum_cast.rs:+5:13: +5:14
3139
_1 = const (); // scope 0 at $DIR/enum_cast.rs:+1:5: +6:6
3240
StorageDead(_3); // scope 1 at $DIR/enum_cast.rs:+6:5: +6:6
33-
drop(_2) -> [return: bb1, unwind: bb3]; // scope 0 at $DIR/enum_cast.rs:+6:5: +6:6
41+
drop(_2) -> [return: bb2, unwind: bb5]; // scope 0 at $DIR/enum_cast.rs:+6:5: +6:6
3442
}
3543

36-
bb1: {
44+
bb2: {
3745
StorageDead(_2); // scope 0 at $DIR/enum_cast.rs:+6:5: +6:6
3846
StorageDead(_1); // scope 0 at $DIR/enum_cast.rs:+6:5: +6:6
39-
StorageLive(_5); // scope 0 at $DIR/enum_cast.rs:+7:9: +7:10
40-
_5 = Droppy::B; // scope 0 at $DIR/enum_cast.rs:+7:13: +7:22
41-
FakeRead(ForLet(None), _5); // scope 0 at $DIR/enum_cast.rs:+7:9: +7:10
47+
StorageLive(_6); // scope 0 at $DIR/enum_cast.rs:+7:9: +7:10
48+
_6 = Droppy::B; // scope 0 at $DIR/enum_cast.rs:+7:13: +7:22
49+
FakeRead(ForLet(None), _6); // scope 0 at $DIR/enum_cast.rs:+7:9: +7:10
4250
_0 = const (); // scope 0 at $DIR/enum_cast.rs:+0:13: +8:2
43-
drop(_5) -> [return: bb2, unwind: bb3]; // scope 0 at $DIR/enum_cast.rs:+8:1: +8:2
51+
drop(_6) -> [return: bb3, unwind: bb5]; // scope 0 at $DIR/enum_cast.rs:+8:1: +8:2
4452
}
4553

46-
bb2: {
47-
StorageDead(_5); // scope 0 at $DIR/enum_cast.rs:+8:1: +8:2
54+
bb3: {
55+
StorageDead(_6); // scope 0 at $DIR/enum_cast.rs:+8:1: +8:2
4856
return; // scope 0 at $DIR/enum_cast.rs:+8:2: +8:2
4957
}
5058

51-
bb3 (cleanup): {
59+
bb4 (cleanup): {
60+
drop(_2) -> bb5; // scope 0 at $DIR/enum_cast.rs:+6:5: +6:6
61+
}
62+
63+
bb5 (cleanup): {
5264
resume; // scope 0 at $DIR/enum_cast.rs:+0:1: +8:2
5365
}
5466
}

Diff for: src/test/mir-opt/enum_cast.foo.mir_map.0.mir

+7-3
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,15 @@
33
fn foo(_1: Foo) -> usize {
44
debug foo => _1; // in scope 0 at $DIR/enum_cast.rs:+0:8: +0:11
55
let mut _0: usize; // return place in scope 0 at $DIR/enum_cast.rs:+0:21: +0:26
6-
let mut _2: isize; // in scope 0 at $DIR/enum_cast.rs:+1:5: +1:8
6+
let _2: Foo; // in scope 0 at $DIR/enum_cast.rs:+1:5: +1:8
7+
let mut _3: isize; // in scope 0 at $DIR/enum_cast.rs:+1:5: +1:8
78

89
bb0: {
9-
_2 = discriminant(_1); // scope 0 at $DIR/enum_cast.rs:+1:5: +1:17
10-
_0 = move _2 as usize (IntToInt); // scope 0 at $DIR/enum_cast.rs:+1:5: +1:17
10+
StorageLive(_2); // scope 0 at $DIR/enum_cast.rs:+1:5: +1:8
11+
_2 = move _1; // scope 0 at $DIR/enum_cast.rs:+1:5: +1:8
12+
_3 = discriminant(_2); // scope 0 at $DIR/enum_cast.rs:+1:5: +1:17
13+
_0 = move _3 as usize (IntToInt); // scope 0 at $DIR/enum_cast.rs:+1:5: +1:17
14+
StorageDead(_2); // scope 0 at $DIR/enum_cast.rs:+1:16: +1:17
1115
return; // scope 0 at $DIR/enum_cast.rs:+2:2: +2:2
1216
}
1317
}

Diff for: src/test/run-pass-valgrind/cast-enum-with-dtor.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ fn main() {
2828
{
2929
let e = E::C;
3030
assert_eq!(e as u32, 2);
31-
assert_eq!(FLAG.load(Ordering::SeqCst), 0);
31+
assert_eq!(FLAG.load(Ordering::SeqCst), 1);
3232
}
3333
assert_eq!(FLAG.load(Ordering::SeqCst), 1);
3434
}

Diff for: src/test/ui/mir/issue-102389.rs

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
enum Enum { A, B, C }
2+
3+
fn func(inbounds: &Enum, array: &[i16; 3]) -> i16 {
4+
array[*inbounds as usize]
5+
//~^ ERROR [E0507]
6+
}
7+
8+
fn main() {}

Diff for: src/test/ui/mir/issue-102389.stderr

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
error[E0507]: cannot move out of `*inbounds` which is behind a shared reference
2+
--> $DIR/issue-102389.rs:4:11
3+
|
4+
LL | array[*inbounds as usize]
5+
| ^^^^^^^^^ move occurs because `*inbounds` has type `Enum`, which does not implement the `Copy` trait
6+
7+
error: aborting due to previous error
8+
9+
For more information about this error, try `rustc --explain E0507`.

0 commit comments

Comments
 (0)