Skip to content

Commit 24ca1ec

Browse files
committed
Guard against rustc::layout diverging from rustc_trans.
1 parent fe48a4a commit 24ca1ec

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

src/librustc_trans/type_of.rs

+32
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use abi::FnType;
1717
use adt;
1818
use common::*;
1919
use machine;
20+
use rustc::traits::ProjectionMode;
2021
use rustc::ty::{self, Ty, TypeFoldable};
2122

2223
use type_::Type;
@@ -121,6 +122,37 @@ pub fn sizing_type_of<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>, t: Ty<'tcx>) -> Typ
121122
debug!("--> mapped t={:?} to llsizingty={:?}", t, llsizingty);
122123

123124
cx.llsizingtypes().borrow_mut().insert(t, llsizingty);
125+
126+
// FIXME(eddyb) Temporary sanity check for ty::layout.
127+
let infcx = infer::normalizing_infer_ctxt(cx.tcx(), &cx.tcx().tables, ProjectionMode::Any);
128+
match t.layout(&infcx) {
129+
Ok(layout) => {
130+
if !type_is_sized(cx.tcx(), t) {
131+
if !layout.is_unsized() {
132+
bug!("layout should be unsized for type `{}` / {:#?}",
133+
t, layout);
134+
}
135+
136+
// Unsized types get turned into a fat pointer for LLVM.
137+
return llsizingty;
138+
}
139+
let r = layout.size(&cx.tcx().data_layout).bytes();
140+
let l = machine::llsize_of_alloc(cx, llsizingty);
141+
if r != l {
142+
bug!("size differs (rustc: {}, llvm: {}) for type `{}` / {:#?}",
143+
r, l, t, layout);
144+
}
145+
let r = layout.align(&cx.tcx().data_layout).abi();
146+
let l = machine::llalign_of_min(cx, llsizingty) as u64;
147+
if r != l {
148+
bug!("align differs (rustc: {}, llvm: {}) for type `{}` / {:#?}",
149+
r, l, t, layout);
150+
}
151+
}
152+
Err(e) => {
153+
bug!("failed to get layout for `{}`: {}", t, e);
154+
}
155+
}
124156
llsizingty
125157
}
126158

0 commit comments

Comments
 (0)