|
| 1 | +/* |
| 2 | + * The region checking pass. Ensures that region-annotated pointers never |
| 3 | + * outlive their referents. |
| 4 | + */ |
| 5 | + |
| 6 | +import driver::session::session; |
| 7 | +import middle::ty; |
| 8 | +import std::map::hashmap; |
| 9 | +import syntax::{ast, visit}; |
| 10 | + |
| 11 | +type ctxt = { |
| 12 | + tcx: ty::ctxt, |
| 13 | + enclosing_block: option<ast::node_id> |
| 14 | +}; |
| 15 | + |
| 16 | +fn check_expr(expr: @ast::expr, cx: ctxt, visitor: visit::vt<ctxt>) { |
| 17 | + ty::walk_ty(cx.tcx, ty::expr_ty(cx.tcx, expr)) { |t| |
| 18 | + alt ty::get(t).struct { |
| 19 | + ty::ty_rptr(region, _) { |
| 20 | + alt region { |
| 21 | + ty::re_named(_) | ty::re_caller(_) { /* ok */ } |
| 22 | + ty::re_block(rbi) { |
| 23 | + let referent_block_id = rbi; |
| 24 | + let enclosing_block_id = alt cx.enclosing_block { |
| 25 | + none { |
| 26 | + cx.tcx.sess.span_bug(expr.span, "block " + |
| 27 | + "region type outside " + |
| 28 | + "a block?!"); |
| 29 | + } |
| 30 | + some(eb) { eb } |
| 31 | + }; |
| 32 | + |
| 33 | + let parent_blocks = cx.tcx.region_map.parent_blocks; |
| 34 | + while enclosing_block_id != referent_block_id { |
| 35 | + if parent_blocks.contains_key(referent_block_id) { |
| 36 | + referent_block_id = |
| 37 | + parent_blocks.get(referent_block_id); |
| 38 | + } else { |
| 39 | + // TODO: Enable this. |
| 40 | + |
| 41 | + //cx.tcx.sess.span_err(expr.span, |
| 42 | + // "reference escapes " + |
| 43 | + // "its block"); |
| 44 | + break; |
| 45 | + } |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | + _ { /* no-op */ } |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + visit::visit_expr(expr, cx, visitor); |
| 55 | +} |
| 56 | + |
| 57 | +fn check_block(blk: ast::blk, cx: ctxt, visitor: visit::vt<ctxt>) { |
| 58 | + let new_cx: ctxt = { enclosing_block: some(blk.node.id) with cx }; |
| 59 | + visit::visit_block(blk, new_cx, visitor); |
| 60 | +} |
| 61 | + |
| 62 | +fn check_crate(ty_cx: ty::ctxt, crate: @ast::crate) { |
| 63 | + let cx: ctxt = {tcx: ty_cx, enclosing_block: none}; |
| 64 | + let visitor = visit::mk_vt(@{ |
| 65 | + visit_expr: check_expr, |
| 66 | + visit_block: check_block |
| 67 | + with *visit::default_visitor() |
| 68 | + }); |
| 69 | + visit::visit_crate(*crate, cx, visitor); |
| 70 | +} |
| 71 | + |
0 commit comments