Skip to content

Commit cf69604

Browse files
committed
Incorporate class fields into recursive-type check
Noticed while investigating issue 2718 that the typechecker allowed some non-instantiable types involving classes. This wasn't the root of 2718, but fixed it anyway.
1 parent d513d98 commit cf69604

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

src/rustc/middle/ty.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1218,7 +1218,6 @@ fn type_needs_drop(cx: ctxt, ty: t) -> bool {
12181218
accum
12191219
}
12201220
}
1221-
12221221
ty_tup(elts) {
12231222
for elts.each {|m| if type_needs_drop(cx, m) { accum = true; } }
12241223
accum
@@ -1720,6 +1719,7 @@ fn is_instantiable(cx: ctxt, r_ty: t) -> bool {
17201719
fn type_structurally_contains(cx: ctxt, ty: t, test: fn(sty) -> bool) ->
17211720
bool {
17221721
let sty = get(ty).struct;
1722+
#debug("type_structurally_contains: %s", ty_to_str(cx, ty));
17231723
if test(sty) { ret true; }
17241724
alt sty {
17251725
ty_enum(did, substs) {
@@ -1737,6 +1737,14 @@ fn type_structurally_contains(cx: ctxt, ty: t, test: fn(sty) -> bool) ->
17371737
}
17381738
ret false;
17391739
}
1740+
ty_class(did, substs) {
1741+
for lookup_class_fields(cx, did).each {|field|
1742+
let ft = lookup_field_type(cx, did, field.id, substs);
1743+
if type_structurally_contains(cx, ft, test) { ret true; }
1744+
}
1745+
ret false;
1746+
}
1747+
17401748
ty_tup(ts) {
17411749
for ts.each {|tt|
17421750
if type_structurally_contains(cx, tt, test) { ret true; }

src/test/compile-fail/issue-2718-a.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class send_packet<T: copy> {
2+
let p: T;
3+
new(p: T) { self.p = p; }
4+
}
5+
6+
7+
mod pingpong {
8+
type ping = send_packet<pong>;
9+
enum pong = send_packet<ping>; //! ERROR illegal recursive enum type; wrap the inner value in a box to make it representable
10+
}
11+
12+
fn main() {}

0 commit comments

Comments
 (0)