Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error message, instead of segfault, when recursive types are used. #447

Merged
merged 2 commits into from
Jun 10, 2011
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/comp/front/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,14 @@ fn is_constraint_arg(@expr e) -> bool {
}
}

fn eq_ty(&@ty a, &@ty b) -> bool {
ret std::box::ptr_eq(a,b);
}

fn hash_ty(&@ty t) -> uint {
ret t.span.lo << 16u + t.span.hi;
}

//
// Local Variables:
// mode: rust
Expand Down
7 changes: 5 additions & 2 deletions src/comp/middle/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ type ctxt = rec(@type_store ts,
item_table items,
type_cache tcache,
creader_cache rcache,
hashmap[t,str] short_names_cache);
hashmap[t,str] short_names_cache,
hashmap[@ast::ty,option::t[t]] ast_ty_to_ty_cache);
type ty_ctxt = ctxt; // Needed for disambiguation from unify::ctxt.

// Convert from method type to function type. Pretty easy; we just drop
Expand Down Expand Up @@ -245,7 +246,9 @@ fn mk_ctxt(session::session s, resolve::def_map dm) -> ctxt {
tcache = tcache,
rcache = mk_rcache(),
short_names_cache =
map::mk_hashmap[ty::t,str](ty::hash_ty, ty::eq_ty));
map::mk_hashmap[ty::t,str](ty::hash_ty, ty::eq_ty),
ast_ty_to_ty_cache =
map::mk_hashmap[@ast::ty,option::t[t]](ast::hash_ty, ast::eq_ty));

populate_type_store(cx);
ret cx;
Expand Down
14 changes: 13 additions & 1 deletion src/comp/middle/typeck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,16 @@ fn ast_mode_to_mode(ast::mode mode) -> ty::mode {
// notion of a type. `getter` is a function that returns the type
// corresponding to a definition ID:
fn ast_ty_to_ty(&ty::ctxt tcx, &ty_getter getter, &@ast::ty ast_ty) -> ty::t {
alt (tcx.ast_ty_to_ty_cache.find(ast_ty)) {
case (some[option::t[ty::t]](some[ty::t](?ty))) { ret ty; }
case (some[option::t[ty::t]](none)) {
tcx.sess.span_err(ast_ty.span, "illegal recursive type "
+ "(insert a tag in the cycle, if this is desired)");
}
case (none[option::t[ty::t]]) { } /* go on */
}
tcx.ast_ty_to_ty_cache.insert(ast_ty, none[ty::t]);

fn ast_arg_to_arg(&ty::ctxt tcx,
&ty_getter getter,
&rec(ast::mode mode, @ast::ty ty) arg)
Expand Down Expand Up @@ -329,7 +339,7 @@ fn ast_ty_to_ty(&ty::ctxt tcx, &ty_getter getter, &@ast::ty ast_ty) -> ty::t {
case (_) {
tcx.sess.span_err(ast_ty.span,
"found type name used as a variable");
fail; }
}
}

cname = some(path_to_str(path));
Expand Down Expand Up @@ -360,6 +370,8 @@ fn ast_ty_to_ty(&ty::ctxt tcx, &ty_getter getter, &@ast::ty ast_ty) -> ty::t {
typ = ty::rename(tcx, typ, cname_str);
}
}

tcx.ast_ty_to_ty_cache.insert(ast_ty, some(typ));
ret typ;
}

Expand Down
4 changes: 4 additions & 0 deletions src/test/compile-fail/type-recursive.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// error-pattern:illegal recursive type
type t1 = rec(int foo, t1 foolish);

fn main() {}