Skip to content

Commit 3948f13

Browse files
committed
Write a type_structurally_contains, use it to rewrite has_dynamic_size
(I'll be using this for type-needs-copy-glue in the near future.)
1 parent 4286437 commit 3948f13

File tree

1 file changed

+29
-35
lines changed

1 file changed

+29
-35
lines changed

src/comp/middle/ty.rs

Lines changed: 29 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1131,56 +1131,50 @@ fn type_is_native(cx: &ctxt, ty: t) -> bool {
11311131
alt struct(cx, ty) { ty_native(_) { ret true; } _ { ret false; } }
11321132
}
11331133

1134-
fn type_has_dynamic_size(cx: &ctxt, ty: t) -> bool {
1135-
alt struct(cx, ty) {
1136-
ty_nil. { ret false; }
1137-
ty_bot. { ret false; }
1138-
ty_bool. { ret false; }
1139-
ty_int. { ret false; }
1140-
ty_float. { ret false; }
1141-
ty_uint. { ret false; }
1142-
ty_machine(_) { ret false; }
1143-
ty_char. { ret false; }
1144-
ty_str. { ret false; }
1145-
ty_istr. { ret false; }
1146-
ty_tag(_, subtys) {
1147-
let i = 0u;
1148-
while i < vec::len::<t>(subtys) {
1149-
if type_has_dynamic_size(cx, subtys[i]) { ret true; }
1150-
i += 1u;
1134+
fn type_structurally_contains(cx: &ctxt, ty: t,
1135+
test: fn(&sty) -> bool) -> bool {
1136+
let sty = struct(cx, ty);
1137+
if test(sty) { ret true; }
1138+
alt sty {
1139+
ty_tag(did, tps) {
1140+
for variant in tag_variants(cx, did) {
1141+
for aty in variant.args {
1142+
let sty = substitute_type_params(cx, tps, aty);
1143+
if type_structurally_contains(cx, sty, test) { ret true; }
1144+
}
11511145
}
11521146
ret false;
11531147
}
1154-
ty_box(_) { ret false; }
1155-
ty_vec(mt) { ret type_has_dynamic_size(cx, mt.ty); }
1156-
ty_ptr(_) { ret false; }
1148+
ty_vec(mt) { ret type_structurally_contains(cx, mt.ty, test); }
11571149
ty_rec(fields) {
1158-
let i = 0u;
1159-
while i < vec::len::<field>(fields) {
1160-
if type_has_dynamic_size(cx, fields[i].mt.ty) { ret true; }
1161-
i += 1u;
1150+
for field in fields {
1151+
if type_structurally_contains(cx, field.mt.ty, test) { ret true; }
11621152
}
11631153
ret false;
11641154
}
11651155
ty_tup(ts) {
1166-
for tt in ts { if type_has_dynamic_size(cx, tt) { ret true; } }
1156+
for tt in ts {
1157+
if type_structurally_contains(cx, tt, test) { ret true; }
1158+
}
11671159
ret false;
11681160
}
1169-
ty_fn(_, _, _, _, _) { ret false; }
1170-
ty_native_fn(_, _, _) { ret false; }
1171-
ty_obj(_) { ret false; }
11721161
ty_res(_, sub, tps) {
1173-
for tp: t in tps { if type_has_dynamic_size(cx, tp) { ret true; } }
1174-
ret type_has_dynamic_size(cx, sub);
1162+
let sty = substitute_type_params(cx, tps, sub);
1163+
ret type_structurally_contains(cx, sty, test);
11751164
}
1176-
ty_var(_) { fail "ty_var in type_has_dynamic_size()"; }
1177-
ty_param(_, _) { ret true; }
1178-
ty_type. { ret false; }
1179-
ty_native(_) { ret false; }
1180-
ty_uniq(_) { ret false; }
1165+
_ { ret false; }
11811166
}
11821167
}
11831168

1169+
fn type_has_dynamic_size(cx: &ctxt, ty: t) -> bool {
1170+
ret type_structurally_contains(cx, ty, fn(sty: &sty) -> bool {
1171+
ret alt sty {
1172+
ty_param(_, _) { true }
1173+
_ { false }
1174+
};
1175+
});
1176+
}
1177+
11841178
fn type_is_integral(cx: &ctxt, ty: t) -> bool {
11851179
alt struct(cx, ty) {
11861180
ty_int. { ret true; }

0 commit comments

Comments
 (0)