Skip to content

Commit d3b9ece

Browse files
add tests related to tuple scalar layout with ZST in last field
1 parent 85fbf49 commit d3b9ece

File tree

3 files changed

+89
-0
lines changed

3 files changed

+89
-0
lines changed

src/test/codegen/tuple-layout-opt.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// ignore-emscripten
2+
// compile-flags: -C no-prepopulate-passes
3+
4+
// Test that tuples get optimized layout, in particular with a ZST in the last field (#63244)
5+
6+
#![crate_type="lib"]
7+
8+
type ScalarZstLast = (u128, ());
9+
// CHECK: define i128 @test_ScalarZstLast(i128 %_1)
10+
#[no_mangle]
11+
pub fn test_ScalarZstLast(_: ScalarZstLast) -> ScalarZstLast { loop {} }
12+
13+
type ScalarZstFirst = ((), u128);
14+
// CHECK: define i128 @test_ScalarZstFirst(i128 %_1)
15+
#[no_mangle]
16+
pub fn test_ScalarZstFirst(_: ScalarZstFirst) -> ScalarZstFirst { loop {} }
17+
18+
type ScalarPairZstLast = (u8, u128, ());
19+
// CHECK: define { i128, i8 } @test_ScalarPairZstLast(i128 %_1.0, i8 %_1.1)
20+
#[no_mangle]
21+
pub fn test_ScalarPairZstLast(_: ScalarPairZstLast) -> ScalarPairZstLast { loop {} }
22+
23+
type ScalarPairZstFirst = ((), u8, u128);
24+
// CHECK: define { i8, i128 } @test_ScalarPairZstFirst(i8 %_1.0, i128 %_1.1)
25+
#[no_mangle]
26+
pub fn test_ScalarPairZstFirst(_: ScalarPairZstFirst) -> ScalarPairZstFirst { loop {} }
27+
28+
type ScalarPairLotsOfZsts = ((), u8, (), u128, ());
29+
// CHECK: define { i128, i8 } @test_ScalarPairLotsOfZsts(i128 %_1.0, i8 %_1.1)
30+
#[no_mangle]
31+
pub fn test_ScalarPairLotsOfZsts(_: ScalarPairLotsOfZsts) -> ScalarPairLotsOfZsts { loop {} }
32+
33+
type ScalarPairLottaNesting = (((), ((), u8, (), u128, ())), ());
34+
// CHECK: define { i128, i8 } @test_ScalarPairLottaNesting(i128 %_1.0, i8 %_1.1)
35+
#[no_mangle]
36+
pub fn test_ScalarPairLottaNesting(_: ScalarPairLottaNesting) -> ScalarPairLottaNesting { loop {} }
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// run-pass
2+
3+
#![feature(unsized_tuple_coercion)]
4+
5+
// Ensure that unsizable fields that might be accessed don't get reordered
6+
7+
fn nonzero_size() {
8+
let sized: (u8, [u32; 2]) = (123, [456, 789]);
9+
let unsize: &(u8, [u32]) = &sized;
10+
assert_eq!(unsize.0, 123);
11+
assert_eq!(unsize.1.len(), 2);
12+
assert_eq!(unsize.1[0], 456);
13+
assert_eq!(unsize.1[1], 789);
14+
}
15+
16+
fn zst() {
17+
let sized: (u8, [u32; 0]) = (123, []);
18+
let unsize: &(u8, [u32]) = &sized;
19+
assert_eq!(unsize.0, 123);
20+
assert_eq!(unsize.1.len(), 0);
21+
}
22+
23+
pub fn main() {
24+
nonzero_size();
25+
zst();
26+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// compile-flags: -Z mir-opt-level=2
2+
// build-pass
3+
#![crate_type="lib"]
4+
5+
// This used to ICE: const-prop did not account for field reordering of scalar pairs,
6+
// and would generate a tuple like `(0x1337, VariantBar): (FooEnum, isize)`,
7+
// causing assertion failures in codegen when trying to read 0x1337 at the wrong type.
8+
9+
pub enum FooEnum {
10+
VariantBar,
11+
VariantBaz,
12+
VariantBuz,
13+
}
14+
15+
pub fn wrong_index() -> isize {
16+
let (_, b) = id((FooEnum::VariantBar, 0x1337));
17+
b
18+
}
19+
20+
pub fn wrong_index_two() -> isize {
21+
let (_, (_, b)) = id(((), (FooEnum::VariantBar, 0x1338)));
22+
b
23+
}
24+
25+
fn id<T>(x: T) -> T {
26+
x
27+
}

0 commit comments

Comments
 (0)