rustc: Introduce re_params into the typechecker

This is the first step of the region refactoring I need to do in order to handle named regions properly.
This commit is contained in:
Patrick Walton
2012-03-21 12:21:06 -07:00
parent 071dedde79
commit 68e364b54d
5 changed files with 36 additions and 17 deletions

View File

@@ -106,7 +106,9 @@ fn enc_region(w: io::writer, cx: @ctxt, r: ty::region) {
ty::re_self(did) { ty::re_self(did) {
w.write_char('s'); w.write_str(cx.ds(did)); w.write_char('|'); w.write_char('s'); w.write_str(cx.ds(did)); w.write_char('|');
} }
ty::re_inferred { w.write_char('?'); } ty::re_param(id) {
w.write_char('p'); w.write_uint(id); w.write_char('|');
}
} }
} }
fn enc_sty(w: io::writer, cx: @ctxt, st: ty::sty) { fn enc_sty(w: io::writer, cx: @ctxt, st: ty::sty) {

View File

@@ -80,7 +80,7 @@ fn region_to_scope(region_map: @region_map, region: ty::region)
ty::re_caller(def_id) | ty::re_self(def_id) { def_id.node } ty::re_caller(def_id) | ty::re_self(def_id) { def_id.node }
ty::re_named(def_id) { region_map.region_name_to_fn.get(def_id) } ty::re_named(def_id) { region_map.region_name_to_fn.get(def_id) }
ty::re_block(node_id) { node_id } ty::re_block(node_id) { node_id }
ty::re_inferred { fail "unresolved region in region_to_scope" } ty::re_param(_) { fail "unresolved region in region_to_scope" }
}; };
} }
@@ -109,7 +109,7 @@ fn get_inferred_region(cx: ctxt, sp: syntax::codemap::span) -> ty::region {
ty::re_caller({crate: ast::local_crate, node: item_id}) ty::re_caller({crate: ast::local_crate, node: item_id})
} }
pa_block(block_id) { ty::re_block(block_id) } pa_block(block_id) { ty::re_block(block_id) }
pa_item(_) { ty::re_inferred } pa_item(_) { ty::re_param(0u) }
pa_crate { cx.sess.span_bug(sp, "inferred region at crate level?!"); } pa_crate { cx.sess.span_bug(sp, "inferred region at crate level?!"); }
} }
} }

View File

@@ -51,7 +51,7 @@ fn check_expr(expr: @ast::expr, cx: ctxt, visitor: visit::vt<ctxt>) {
"escapes its block"); "escapes its block");
} }
} }
ty::re_inferred { ty::re_param(_) {
cx.tcx.sess.span_bug(expr.span, cx.tcx.sess.span_bug(expr.span,
"unresolved region"); "unresolved region");
} }

View File

@@ -89,7 +89,7 @@ export ty_uint, mk_uint, mk_mach_uint;
export ty_uniq, mk_uniq, mk_imm_uniq, type_is_unique_box; export ty_uniq, mk_uniq, mk_imm_uniq, type_is_unique_box;
export ty_var, mk_var; export ty_var, mk_var;
export ty_self, mk_self; export ty_self, mk_self;
export region, re_named, re_caller, re_block, re_inferred; export region, re_named, re_caller, re_block, re_param;
export get, type_has_params, type_has_vars, type_has_rptrs, type_id; export get, type_has_params, type_has_vars, type_has_rptrs, type_id;
export same_type; export same_type;
export ty_var_id; export ty_var_id;
@@ -242,7 +242,10 @@ enum region {
re_caller(def_id), re_caller(def_id),
re_self(def_id), re_self(def_id),
re_block(node_id), re_block(node_id),
re_inferred /* currently unresolved (for typedefs) */
// A region parameter. Currently used only for typedefs.
// TODO: Use this for caller and named regions as well.
re_param(uint)
} }
// NB: If you change this, you'll probably want to change the corresponding // NB: If you change this, you'll probably want to change the corresponding
@@ -1163,7 +1166,7 @@ fn hash_type_structure(st: sty) -> uint {
re_caller(_) { 2u } re_caller(_) { 2u }
re_self(_) { 3u } re_self(_) { 3u }
re_block(_) { 4u } re_block(_) { 4u }
re_inferred { 5u } re_param(_) { 5u }
} }
} }
alt st { alt st {
@@ -1928,12 +1931,17 @@ mod unify {
} }
} }
if sub == ty::re_inferred || super == ty::re_inferred { // FIXME: This is wrong. We should be keeping a set of region bindings
ret if sub == super { // around.
nxt(super) alt (sub, super) {
} else { (ty::re_param(_), _) | (_, ty::re_param(_)) {
err(terr_regions_differ(true, super, sub)) ret if sub == super {
}; nxt(super)
} else {
err(terr_regions_differ(true, super, sub))
}
}
_ { /* fall through */ }
} }
// Outer regions are subtypes of inner regions. (This is somewhat // Outer regions are subtypes of inner regions. (This is somewhat

View File

@@ -256,7 +256,8 @@ fn ast_ty_to_ty(tcx: ty::ctxt, mode: mode, &&ast_ty: @ast::ty) -> ty::t {
ty: ty::t) -> ty::t { ty: ty::t) -> ty::t {
ret ty::fold_ty(tcx, ty::fm_rptr({|r| ret ty::fold_ty(tcx, ty::fm_rptr({|r|
alt r { alt r {
ty::re_inferred | ty::re_self(_) { // FIXME: This is probably wrong for params.
ty::re_param(_) | ty::re_self(_) {
tcx.region_map.ast_type_to_inferred_region.get(use_site) tcx.region_map.ast_type_to_inferred_region.get(use_site)
} }
_ { r } _ { r }
@@ -356,7 +357,11 @@ fn ast_ty_to_ty(tcx: ty::ctxt, mode: mode, &&ast_ty: @ast::ty) -> ty::t {
let attir = tcx.region_map.ast_type_to_inferred_region; let attir = tcx.region_map.ast_type_to_inferred_region;
alt attir.find(ast_ty.id) { alt attir.find(ast_ty.id) {
some(resolved_region) { resolved_region } some(resolved_region) { resolved_region }
none { ty::re_inferred } none {
// FIXME: Shouldn't be 0u and should instead be
// a fresh variable.
ty::re_param(0u)
}
} }
} }
ast::re_named(_) | ast::re_self { ast::re_named(_) | ast::re_self {
@@ -1488,7 +1493,8 @@ fn instantiate_self_regions(tcx: ty::ctxt, region: ty::region, &&ty: ty::t)
if ty::type_has_rptrs(ty) { if ty::type_has_rptrs(ty) {
ty::fold_ty(tcx, ty::fm_rptr({|r| ty::fold_ty(tcx, ty::fm_rptr({|r|
alt r { alt r {
ty::re_inferred | ty::re_caller(_) | ty::re_self(_) { region } // FIXME: Should not happen for re_param.
ty::re_param(_) | ty::re_caller(_) | ty::re_self(_) { region }
_ { r } _ { r }
} }
}), ty) }), ty)
@@ -1502,7 +1508,10 @@ fn instantiate_self_regions(tcx: ty::ctxt, region: ty::region, &&ty: ty::t)
// refer to inferred regions. // refer to inferred regions.
fn universally_quantify_regions(tcx: ty::ctxt, ty: ty::t) -> ty::t { fn universally_quantify_regions(tcx: ty::ctxt, ty: ty::t) -> ty::t {
if ty::type_has_rptrs(ty) { if ty::type_has_rptrs(ty) {
ty::fold_ty(tcx, ty::fm_rptr({|_r| ty::re_inferred}), ty) ty::fold_ty(tcx, ty::fm_rptr({|_r|
// FIXME: Very wrong. Shouldn't be 0u.
ty::re_param(0u)
}), ty)
} else { } else {
ty ty
} }