Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit f16068e

Browse files
committedSep 24, 2016
Completely kill represent_type and the adt::Repr type that goes with it.
1 parent bdad702 commit f16068e

20 files changed

+520
-915
lines changed
 

‎src/librustc/ty/context.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,9 @@ pub struct GlobalCtxt<'tcx> {
489489
/// Cache for layouts computed from types.
490490
pub layout_cache: RefCell<FnvHashMap<Ty<'tcx>, &'tcx Layout>>,
491491

492+
//Used to prevent layout from recursing too deeply.
493+
pub layout_depth: Cell<usize>,
494+
492495
/// Map from function to the `#[derive]` mode that it's defining. Only used
493496
/// by `rustc-macro` crates.
494497
pub derive_macros: RefCell<NodeMap<token::InternedString>>,
@@ -760,6 +763,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
760763
crate_name: token::intern_and_get_ident(crate_name),
761764
data_layout: data_layout,
762765
layout_cache: RefCell::new(FnvHashMap()),
766+
layout_depth: Cell::new(0),
763767
derive_macros: RefCell::new(NodeMap()),
764768
}, f)
765769
}

‎src/librustc/ty/layout.rs

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,33 @@ pub enum Integer {
328328
}
329329

330330
impl Integer {
331+
332+
pub fn size(&self) -> Size {
333+
match *self {
334+
I1 => Size::from_bits(1),
335+
I8 => Size::from_bytes(1),
336+
I16 => Size::from_bytes(2),
337+
I32 => Size::from_bytes(4),
338+
I64 => Size::from_bytes(8),
339+
}
340+
}
341+
342+
pub fn to_ty<'a, 'tcx>(&self, tcx: &ty::TyCtxt<'a, 'tcx, 'tcx>,
343+
signed: bool) -> Ty<'tcx> {
344+
match (*self, signed) {
345+
(I1, false) => tcx.types.u8,
346+
(I8, false) => tcx.types.u8,
347+
(I16, false) => tcx.types.u16,
348+
(I32, false) => tcx.types.u32,
349+
(I64, false) => tcx.types.u64,
350+
(I1, true) => tcx.types.i8,
351+
(I8, true) => tcx.types.i8,
352+
(I16, true) => tcx.types.i16,
353+
(I32, true) => tcx.types.i32,
354+
(I64, true) => tcx.types.i64,
355+
}
356+
}
357+
331358
/// Find the smallest Integer type which can represent the signed value.
332359
pub fn fit_signed(x: i64) -> Integer {
333360
match x {
@@ -912,7 +939,7 @@ impl<'a, 'gcx, 'tcx> Layout {
912939
Univariant { variant: unit, non_zero: false }
913940
}
914941

915-
// Tuples.
942+
// Tuples and closures.
916943
ty::TyClosure(_, ty::ClosureSubsts { upvar_tys: tys, .. }) |
917944
ty::TyTuple(tys) => {
918945
let mut st = Struct::new(dl, false);
@@ -975,7 +1002,7 @@ impl<'a, 'gcx, 'tcx> Layout {
9751002
if def.variants.len() == 1 {
9761003
// Struct, or union, or univariant enum equivalent to a struct.
9771004
// (Typechecking will reject discriminant-sizing attrs.)
978-
assert!(!def.is_enum() || hint == attr::ReprAny);
1005+
9791006
let fields = def.variants[0].fields.iter().map(|field| {
9801007
field.ty(tcx, substs).layout(infcx)
9811008
});
@@ -1003,6 +1030,16 @@ impl<'a, 'gcx, 'tcx> Layout {
10031030
}
10041031
}
10051032

1033+
if def.variants.len() == 1 && hint == attr::ReprAny{
1034+
// Equivalent to a struct/tuple/newtype.
1035+
let fields = def.variants[0].fields.iter().map(|field| {
1036+
field.ty(tcx, substs).layout(infcx)
1037+
});
1038+
let mut st = Struct::new(dl, false);
1039+
st.extend(dl, fields, ty)?;
1040+
return success(Univariant { variant: st, non_zero: false });
1041+
}
1042+
10061043
// Cache the substituted and normalized variant field types.
10071044
let variants = def.variants.iter().map(|v| {
10081045
v.fields.iter().map(|field| field.ty(tcx, substs)).collect::<Vec<_>>()

‎src/librustc/ty/util.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -608,10 +608,19 @@ impl<'a, 'tcx> ty::TyS<'tcx> {
608608
}
609609
}
610610

611+
let rec_limit = tcx.sess.recursion_limit.get();
612+
let depth = tcx.layout_depth.get();
613+
if depth > rec_limit {
614+
tcx.sess.fatal(
615+
&format!("overflow representing the type `{}`", self));
616+
}
617+
618+
tcx.layout_depth.set(depth+1);
611619
let layout = Layout::compute_uncached(self, infcx)?;
612620
if can_cache {
613621
tcx.layout_cache.borrow_mut().insert(self, layout);
614622
}
623+
tcx.layout_depth.set(depth);
615624
Ok(layout)
616625
}
617626

‎src/librustc_trans/abi.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use cabi_s390x;
2424
use cabi_mips;
2525
use cabi_mips64;
2626
use cabi_asmjs;
27-
use machine::{llalign_of_min, llsize_of, llsize_of_real, llsize_of_store};
27+
use machine::{llalign_of_min, llsize_of, llsize_of_alloc};
2828
use type_::Type;
2929
use type_of;
3030

@@ -102,7 +102,7 @@ impl ArgType {
102102
// Wipe old attributes, likely not valid through indirection.
103103
self.attrs = llvm::Attributes::default();
104104

105-
let llarg_sz = llsize_of_real(ccx, self.ty);
105+
let llarg_sz = llsize_of_alloc(ccx, self.ty);
106106

107107
// For non-immediate arguments the callee gets its own copy of
108108
// the value on the stack, so there are no aliases. It's also
@@ -200,7 +200,7 @@ impl ArgType {
200200
base::call_memcpy(bcx,
201201
bcx.pointercast(dst, Type::i8p(ccx)),
202202
bcx.pointercast(llscratch, Type::i8p(ccx)),
203-
C_uint(ccx, llsize_of_store(ccx, self.ty)),
203+
C_uint(ccx, llsize_of_alloc(ccx, self.ty)),
204204
cmp::min(llalign_of_min(ccx, self.ty),
205205
llalign_of_min(ccx, ty)) as u32);
206206

@@ -327,7 +327,7 @@ impl FnType {
327327
if let Layout::CEnum { signed, .. } = *ccx.layout_of(ty) {
328328
arg.signedness = Some(signed);
329329
}
330-
if llsize_of_real(ccx, arg.ty) == 0 {
330+
if llsize_of_alloc(ccx, arg.ty) == 0 {
331331
// For some forsaken reason, x86_64-pc-windows-gnu
332332
// doesn't ignore zero-sized struct arguments.
333333
// The same is true for s390x-unknown-linux-gnu.
@@ -358,7 +358,7 @@ impl FnType {
358358
ty::TyRef(_, ty::TypeAndMut { ty, .. }) |
359359
ty::TyBox(ty) => {
360360
let llty = type_of::sizing_type_of(ccx, ty);
361-
let llsz = llsize_of_real(ccx, llty);
361+
let llsz = llsize_of_alloc(ccx, llty);
362362
ret.attrs.set_dereferenceable(llsz);
363363
}
364364
_ => {}
@@ -427,7 +427,7 @@ impl FnType {
427427
} else {
428428
if let Some(inner) = rust_ptr_attrs(ty, &mut arg) {
429429
let llty = type_of::sizing_type_of(ccx, inner);
430-
let llsz = llsize_of_real(ccx, llty);
430+
let llsz = llsize_of_alloc(ccx, llty);
431431
arg.attrs.set_dereferenceable(llsz);
432432
}
433433
args.push(arg);
@@ -469,8 +469,8 @@ impl FnType {
469469
return;
470470
}
471471

472-
let size = llsize_of_real(ccx, llty);
473-
if size > llsize_of_real(ccx, ccx.int_type()) {
472+
let size = llsize_of_alloc(ccx, llty);
473+
if size > llsize_of_alloc(ccx, ccx.int_type()) {
474474
arg.make_indirect(ccx);
475475
} else if size > 0 {
476476
// We want to pass small aggregates as immediates, but using

0 commit comments

Comments
 (0)
Please sign in to comment.