Skip to content

Commit 9b2beca

Browse files
committed
Auto merge of #34128 - eddyb:mir-trans-fixes, r=luqmana
[MIR] Fix MIR trans edge cases that showed up on crater. These fixes cover all of the [regressions found by crater](https://gist.github.com/nikomatsakis/88ce89ed06ef7f7f19bfd1e221d7f7ec) (for #34096). Two of them were `Pair` edge cases (ZSTs and constants) causing LLVM assertions, the other one was causing stack overflows in debug scripts compiled in debug mode, due to the `fn_ret_cast` `alloca` ending up in a loop.
2 parents f352550 + 02cbc0e commit 9b2beca

File tree

5 files changed

+33
-11
lines changed

5 files changed

+33
-11
lines changed

src/librustc_trans/mir/block.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -894,8 +894,11 @@ impl<'bcx, 'tcx> MirContext<'bcx, 'tcx> {
894894
// bitcasting to the struct type yields invalid cast errors.
895895

896896
// We instead thus allocate some scratch space...
897-
let llscratch = bcx.alloca(llcast_ty, "fn_ret_cast");
898-
bcx.with_block(|bcx| base::call_lifetime_start(bcx, llscratch));
897+
let llscratch = bcx.with_block(|bcx| {
898+
let alloca = base::alloca(bcx, llcast_ty, "fn_ret_cast");
899+
base::call_lifetime_start(bcx, alloca);
900+
alloca
901+
});
899902

900903
// ...where we first store the value...
901904
bcx.store(op.immediate(), llscratch);

src/librustc_trans/mir/constant.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -125,10 +125,10 @@ impl<'tcx> Const<'tcx> {
125125
let llty = type_of::immediate_type_of(ccx, self.ty);
126126
let llvalty = val_ty(self.llval);
127127

128-
let val = if common::type_is_imm_pair(ccx, self.ty) {
128+
let val = if llty == llvalty && common::type_is_imm_pair(ccx, self.ty) {
129129
let (a, b) = self.get_pair();
130130
OperandValue::Pair(a, b)
131-
} else if common::type_is_immediate(ccx, self.ty) && llty == llvalty {
131+
} else if llty == llvalty && common::type_is_immediate(ccx, self.ty) {
132132
// If the types match, we can use the value directly.
133133
OperandValue::Immediate(self.llval)
134134
} else {

src/librustc_trans/mir/mod.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,12 @@ impl<'tcx> TempRef<'tcx> {
124124
// Zero-size temporaries aren't always initialized, which
125125
// doesn't matter because they don't contain data, but
126126
// we need something in the operand.
127-
let val = OperandValue::Immediate(common::C_nil(ccx));
127+
let nil = common::C_nil(ccx);
128+
let val = if common::type_is_imm_pair(ccx, ty) {
129+
OperandValue::Pair(nil, nil)
130+
} else {
131+
OperandValue::Immediate(nil)
132+
};
128133
let op = OperandRef {
129134
val: val,
130135
ty: ty

src/test/run-pass/mir_constval_adts.rs

+8-6
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,23 @@ struct Point {
1515
_y: i32,
1616
}
1717

18+
#[derive(PartialEq, Eq, Debug)]
19+
struct Newtype<T>(T);
20+
1821
const STRUCT: Point = Point { _x: 42, _y: 42 };
1922
const TUPLE1: (i32, i32) = (42, 42);
2023
const TUPLE2: (&'static str, &'static str) = ("hello","world");
24+
const PAIR_NEWTYPE: (Newtype<i32>, Newtype<i32>) = (Newtype(42), Newtype(42));
2125

2226
#[rustc_mir]
23-
fn mir() -> (Point, (i32, i32), (&'static str, &'static str)){
27+
fn mir() -> (Point, (i32, i32), (&'static str, &'static str), (Newtype<i32>, Newtype<i32>)) {
2428
let struct1 = STRUCT;
2529
let tuple1 = TUPLE1;
2630
let tuple2 = TUPLE2;
27-
(struct1, tuple1, tuple2)
31+
let pair_newtype = PAIR_NEWTYPE;
32+
(struct1, tuple1, tuple2, pair_newtype)
2833
}
2934

30-
#[derive(PartialEq, Eq, Debug)]
31-
struct Newtype<T>(T);
32-
3335
const NEWTYPE: Newtype<&'static str> = Newtype("foobar");
3436

3537
#[rustc_mir]
@@ -39,7 +41,7 @@ fn test_promoted_newtype_str_ref() {
3941
}
4042

4143
fn main(){
42-
assert_eq!(mir(), (STRUCT, TUPLE1, TUPLE2));
44+
assert_eq!(mir(), (STRUCT, TUPLE1, TUPLE2, PAIR_NEWTYPE));
4345
test_promoted_newtype_str_ref();
4446
}
4547

src/test/run-pass/mir_trans_calls.rs

+12
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,16 @@ fn test_fn_transmute_zst(x: ()) -> [(); 1] {
147147
})
148148
}
149149

150+
#[rustc_mir]
151+
fn test_fn_ignored_pair() -> ((), ()) {
152+
((), ())
153+
}
154+
155+
#[rustc_mir]
156+
fn test_fn_ignored_pair_0() {
157+
test_fn_ignored_pair().0
158+
}
159+
150160
fn main() {
151161
assert_eq!(test1(1, (2, 3), &[4, 5, 6]), (1, (2, 3), &[4, 5, 6][..]));
152162
assert_eq!(test2(98), 98);
@@ -169,4 +179,6 @@ fn main() {
169179

170180
assert_eq!(test_fn_nil_call(&(|| 42)), 42);
171181
assert_eq!(test_fn_transmute_zst(()), [()]);
182+
183+
assert_eq!(test_fn_ignored_pair_0(), ());
172184
}

0 commit comments

Comments
 (0)