Skip to content

Commit cb3a557

Browse files
committed
trans: deal with the fact that ZSTs are always "initialized".
1 parent d460597 commit cb3a557

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

Diff for: src/librustc_trans/mir/lvalue.rs

+16-7
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ use abi;
1616
use adt;
1717
use base;
1818
use builder::Builder;
19-
use common::{self, BlockAndBuilder, CrateContext, C_uint};
19+
use common::{self, BlockAndBuilder, CrateContext, C_uint, C_undef};
2020
use consts;
2121
use machine;
22+
use type_of::type_of;
2223
use mir::drop;
23-
use llvm;
2424
use Disr;
2525

2626
use std::ptr;
@@ -116,10 +116,7 @@ impl<'bcx, 'tcx> MirContext<'bcx, 'tcx> {
116116
// Ergo, we return an undef ValueRef, so we do not have to special-case every
117117
// place using lvalues, and could use it the same way you use a regular
118118
// ReturnPointer LValue (i.e. store into it, load from it etc).
119-
let llty = fcx.fn_ty.ret.original_ty.ptr_to();
120-
unsafe {
121-
llvm::LLVMGetUndef(llty.to_ref())
122-
}
119+
C_undef(fcx.fn_ty.ret.original_ty.ptr_to())
123120
};
124121
let fn_return_ty = bcx.monomorphize(&self.mir.return_ty);
125122
let return_ty = fn_return_ty.unwrap();
@@ -228,7 +225,19 @@ impl<'bcx, 'tcx> MirContext<'bcx, 'tcx> {
228225
ret
229226
}
230227
TempRef::Operand(Some(_)) => {
231-
bug!("Lvalue temp already set");
228+
let lvalue_ty = self.mir.lvalue_ty(bcx.tcx(), lvalue);
229+
let lvalue_ty = bcx.monomorphize(&lvalue_ty);
230+
231+
// See comments in TempRef::new_operand as to why
232+
// we always have Some in a ZST TempRef::Operand.
233+
let ty = lvalue_ty.to_ty(bcx.tcx());
234+
if common::type_is_zero_size(bcx.ccx(), ty) {
235+
// Pass an undef pointer as no stores can actually occur.
236+
let llptr = C_undef(type_of(bcx.ccx(), ty).ptr_to());
237+
f(self, LvalueRef::new_sized(llptr, lvalue_ty))
238+
} else {
239+
bug!("Lvalue temp already set");
240+
}
232241
}
233242
}
234243
}

Diff for: src/test/run-pass/mir_trans_calls.rs

+10
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,15 @@ fn test_fn_nil_call<F>(f: &F) -> i32
138138
f()
139139
}
140140

141+
#[rustc_mir]
142+
fn test_fn_transmute_zst(x: ()) -> [(); 1] {
143+
fn id<T>(x: T) -> T {x}
144+
145+
id(unsafe {
146+
std::mem::transmute(x)
147+
})
148+
}
149+
141150
fn main() {
142151
assert_eq!(test1(1, (2, 3), &[4, 5, 6]), (1, (2, 3), &[4, 5, 6][..]));
143152
assert_eq!(test2(98), 98);
@@ -159,4 +168,5 @@ fn main() {
159168
assert_eq!(test_fn_direct_call(&closure, 100, 4), 324);
160169

161170
assert_eq!(test_fn_nil_call(&(|| 42)), 42);
171+
assert_eq!(test_fn_transmute_zst(()), [()]);
162172
}

0 commit comments

Comments
 (0)