Skip to content

Commit c9876c6

Browse files
committed
Make type_is_size recurse over container types
1 parent 8201f15 commit c9876c6

File tree

1 file changed

+29
-6
lines changed

1 file changed

+29
-6
lines changed

src/librustc/middle/ty.rs

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2548,15 +2548,38 @@ pub fn type_is_enum(ty: t) -> bool {
25482548
// Is the type's representation size known at compile time?
25492549
pub fn type_is_sized(cx: ctxt, ty: ty::t) -> bool {
25502550
match get(ty).sty {
2551-
// FIXME(#6308) add trait, vec, str, etc here.
2551+
/* Container types depend on their elements. */
2552+
ty_enum(did, ref substs) => {
2553+
let variants = enum_variants(cx, did);
2554+
do variants.all |variant| {
2555+
let tup_ty = mk_tup(cx, /*bad*/copy variant.args);
2556+
let tup_ty = subst(cx, substs, tup_ty);
2557+
type_is_sized(cx, tup_ty)
2558+
}
2559+
},
2560+
ty_tup(ref elts) => {
2561+
do elts.all |elt| { type_is_sized(cx, *elt) }
2562+
},
2563+
ty_struct(did, ref substs) => {
2564+
do lookup_struct_fields(cx, did).all |f| {
2565+
let fty = ty::lookup_item_type(cx, f.id);
2566+
let sty = subst(cx, substs, fty.ty);
2567+
type_is_sized(cx, sty)
2568+
}
2569+
},
2570+
ty_evec(ref mt, vstore_fixed(_)) => {
2571+
type_is_sized(cx, mt.ty)
2572+
},
2573+
/* Type parameters must have "Sized" bound. */
25522574
ty_param(p) => {
25532575
let param_def = cx.ty_param_defs.get(&p.def_id.node);
2554-
if param_def.bounds.builtin_bounds.contains_elem(BoundSized) {
2555-
return true;
2556-
}
2557-
return false;
2576+
param_def.bounds.builtin_bounds.contains_elem(BoundSized)
25582577
},
2559-
_ => return true,
2578+
/* Dynamically-sized types. */
2579+
// FIXME(#6308) add bare trait, vec, str, etc => false
2580+
ty_unboxed_vec(*) => false,
2581+
/* Primitives and pointers are all sized. */
2582+
_ => true,
25602583
}
25612584
}
25622585

0 commit comments

Comments
 (0)