Skip to content

Commit 7396ec3

Browse files
committed
Address PR feedback
1 parent 6e34369 commit 7396ec3

5 files changed

+54
-13
lines changed

compiler/rustc_mir_transform/src/remove_zsts.rs

+11-9
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,17 @@ fn trivially_zst<'tcx>(ty: Ty<'tcx>, tcx: TyCtxt<'tcx>) -> Option<bool> {
4646
ty::FnDef(..) | ty::Never => Some(true),
4747
ty::Tuple(fields) if fields.is_empty() => Some(true),
4848
ty::Array(_ty, len) if let Some(0) = len.try_to_target_usize(tcx) => Some(true),
49-
// maybe ZST (could be more precise)
50-
ty::Adt(..)
51-
| ty::Array(..)
52-
| ty::Closure(..)
53-
| ty::CoroutineClosure(..)
54-
| ty::Tuple(..)
55-
| ty::Alias(ty::Opaque, ..) => None,
56-
// unreachable or can't be ZST
57-
_ => Some(false),
49+
// clearly not ZST
50+
ty::Bool
51+
| ty::Char
52+
| ty::Int(..)
53+
| ty::Uint(..)
54+
| ty::Float(..)
55+
| ty::RawPtr(..)
56+
| ty::Ref(..)
57+
| ty::FnPtr(..) => Some(false),
58+
// check `layout_of` to see (including unreachable things we won't actually see)
59+
_ => None,
5860
}
5961
}
6062

tests/mir-opt/instsimplify/simplify_repeat.repeat_once_to_aggregate.InstSimplify-after-simplifycfg.diff

+16-4
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,25 @@
44
fn repeat_once_to_aggregate(_1: T) -> [T; 1] {
55
debug x => _1;
66
let mut _0: [T; 1];
7-
let mut _2: T;
7+
let _2: [T; 1];
8+
let mut _3: T;
9+
let mut _4: T;
10+
scope 1 {
11+
debug other => _2;
12+
}
813

914
bb0: {
1015
StorageLive(_2);
11-
_2 = copy _1;
12-
- _0 = [move _2; 1];
13-
+ _0 = [move _2];
16+
StorageLive(_3);
17+
_3 = copy _1;
18+
- _2 = [move _3; 1];
19+
+ _2 = [move _3];
20+
StorageDead(_3);
21+
StorageLive(_4);
22+
_4 = copy _1;
23+
- _0 = [move _4; 1];
24+
+ _0 = [move _4];
25+
StorageDead(_4);
1426
StorageDead(_2);
1527
return;
1628
}

tests/mir-opt/instsimplify/simplify_repeat.rs

+7
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,19 @@
22
//@ compile-flags: -C panic=abort
33
#![crate_type = "lib"]
44

5+
const MYSTERY: usize = 3_usize.pow(2) - 2_usize.pow(3);
6+
57
// EMIT_MIR simplify_repeat.repeat_once_to_aggregate.InstSimplify-after-simplifycfg.diff
68
pub fn repeat_once_to_aggregate<T: Copy>(x: T) -> [T; 1] {
79
// CHECK-LABEL: fn repeat_once_to_aggregate(
10+
// CHECK: debug other => [[OTHER:_[0-9]+]]
11+
// CHECK-NOT: [move {{_[0-9]+}}; 1]
12+
// CHECK: [[OTHER]] = [move {{_[0-9]+}}];
813
// CHECK-NOT: [move {{_[0-9]+}}; 1]
914
// CHECK: _0 = [move {{_[0-9]+}}];
1015
// CHECK-NOT: [move {{_[0-9]+}}; 1]
1116

17+
let other = [x; MYSTERY];
18+
1219
[x; 1]
1320
}

tests/mir-opt/remove_zsts.remove_generic_array.RemoveZsts.diff

+15
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,15 @@
66
let mut _0: ();
77
let _2: [T; 0];
88
let mut _3: T;
9+
let mut _5: T;
910
scope 1 {
1011
- debug a => _2;
1112
+ debug a => const ZeroSized: [T; 0];
13+
let _4: [T; 0];
14+
scope 2 {
15+
- debug b => _4;
16+
+ debug b => const ZeroSized: [T; 0];
17+
}
1218
}
1319

1420
bb0: {
@@ -19,9 +25,18 @@
1925
- _2 = [];
2026
+ nop;
2127
StorageDead(_3);
28+
- StorageLive(_4);
29+
+ nop;
30+
StorageLive(_5);
31+
_5 = copy _1;
32+
- _4 = [];
33+
+ nop;
34+
StorageDead(_5);
2235
- _0 = const ();
36+
- StorageDead(_4);
2337
- StorageDead(_2);
2438
+ nop;
39+
+ nop;
2540
+ nop;
2641
return;
2742
}

tests/mir-opt/remove_zsts.rs

+5
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,17 @@ fn get_union() -> Foo {
1212
Foo { x: () }
1313
}
1414

15+
const MYSTERY: usize = 280_usize.isqrt() - 260_usize.isqrt();
16+
1517
// EMIT_MIR remove_zsts.remove_generic_array.RemoveZsts.diff
1618
fn remove_generic_array<T: Copy>(x: T) {
1719
// CHECK-LABEL: fn remove_generic_array
1820
// CHECK: debug a => const ZeroSized: [T; 0];
21+
// CHECK: debug b => const ZeroSized: [T; 0];
1922
// CHECK-NOT: = [];
23+
// CHECK-NOT: ; 1]
2024
let a = [x; 0];
25+
let b = [x; MYSTERY];
2126
}
2227

2328
fn main() {

0 commit comments

Comments
 (0)