make treatment of unchecked/unsafe blocks more uniform

also repair various errors in the parser related to such blocks.
rename checked_blk to default_blk to reflect the fact that it
inherits its purity from the surrounding context.
This commit is contained in:
Niko Matsakis 2011-10-07 15:51:55 -07:00 committed by Brian Anderson
parent e9569371f7
commit cbe8da0655
7 changed files with 28 additions and 20 deletions

View File

@ -184,7 +184,7 @@ fn mk_tests(cx: test_ctxt) -> @ast::item {
let test_descs = mk_test_desc_vec(cx);
let body_: ast::blk_ =
checked_block([], option::some(test_descs), cx.next_node_id());
default_block([], option::some(test_descs), cx.next_node_id());
let body = nospan(body_);
let fn_ = {decl: decl, proto: proto, body: body};
@ -303,7 +303,7 @@ fn mk_main(cx: test_ctxt) -> @ast::item {
let test_main_call_expr = mk_test_main_call(cx);
let body_: ast::blk_ =
checked_block([], option::some(test_main_call_expr),
default_block([], option::some(test_main_call_expr),
cx.next_node_id());
let body = {node: body_, span: dummy_sp()};

View File

@ -2090,13 +2090,7 @@ fn check_expr_with_unifier(fcx: @fn_ctxt, expr: @ast::expr, unify: unifier,
}
ast::expr_block(b) {
// If this is an unchecked block, turn off purity-checking
let fcx_for_block =
alt b.node.rules {
ast::unchecked_blk. { @{purity: ast::impure_fn with *fcx} }
ast::unsafe_blk. { @{purity: ast::unsafe_fn with *fcx} }
ast::checked_blk. { fcx }
};
bot = check_block(fcx_for_block, b);
bot = check_block(fcx, b);
let typ =
alt b.node.expr {
some(expr) { expr_ty(tcx, expr) }
@ -2553,7 +2547,12 @@ fn check_stmt(fcx: @fn_ctxt, stmt: @ast::stmt) -> bool {
ret bot;
}
fn check_block(fcx: @fn_ctxt, blk: ast::blk) -> bool {
fn check_block(fcx0: @fn_ctxt, blk: ast::blk) -> bool {
let fcx = alt blk.node.rules {
ast::unchecked_blk. { @{purity: ast::impure_fn with *fcx0} }
ast::unsafe_blk. { @{purity: ast::unsafe_fn with *fcx0} }
ast::default_blk. { fcx0 }
};
let bot = false;
let warned = false;
for s: @ast::stmt in blk.node.stmts {

View File

@ -174,7 +174,7 @@ type field_ = {mut: mutability, ident: ident, expr: @expr};
type field = spanned<field_>;
tag blk_check_mode { checked_blk; unchecked_blk; unsafe_blk; }
tag blk_check_mode { default_blk; unchecked_blk; unsafe_blk; }
tag expr_check_mode { claimed_expr; checked_expr; }

View File

@ -185,13 +185,13 @@ fn eq_ty(&&a: @ty, &&b: @ty) -> bool { ret std::box::ptr_eq(a, b); }
fn hash_ty(&&t: @ty) -> uint { ret t.span.lo << 16u + t.span.hi; }
fn block_from_expr(e: @expr) -> blk {
let blk_ = checked_block([], option::some::<@expr>(e), e.id);
let blk_ = default_block([], option::some::<@expr>(e), e.id);
ret {node: blk_, span: e.span};
}
fn checked_block(stmts1: [@stmt], expr1: option::t<@expr>, id1: node_id) ->
fn default_block(stmts1: [@stmt], expr1: option::t<@expr>, id1: node_id) ->
blk_ {
ret {stmts: stmts1, expr: expr1, id: id1, rules: checked_blk};
ret {stmts: stmts1, expr: expr1, id: id1, rules: default_blk};
}
fn obj_field_from_anon_obj_field(f: anon_obj_field) -> obj_field {

View File

@ -828,7 +828,7 @@ fn parse_bottom_expr(p: parser) -> @ast::expr {
p.peek() == token::OROR {
ret parse_fn_block_expr(p);
} else {
let blk = parse_block_tail(p, lo, ast::checked_blk);
let blk = parse_block_tail(p, lo, ast::default_blk);
ret mk_expr(p, blk.span.lo, blk.span.hi, ast::expr_block(blk));
}
} else if eat_word(p, "if") {
@ -873,7 +873,7 @@ fn parse_bottom_expr(p: parser) -> @ast::expr {
} else if p.peek() == token::POUND_LBRACE {
p.bump();
let blk = ast::mac_embed_block(
parse_block_tail(p, lo, ast::checked_blk));
parse_block_tail(p, lo, ast::default_blk));
ret mk_mac_expr(p, lo, p.get_hi_pos(), blk);
} else if p.peek() == token::ELLIPSIS {
p.bump();
@ -1320,7 +1320,7 @@ fn parse_fn_expr(p: parser, proto: ast::proto) -> @ast::expr {
fn parse_fn_block_expr(p: parser) -> @ast::expr {
let lo = p.get_last_lo_pos();
let decl = parse_fn_block_decl(p);
let body = parse_block_tail(p, lo, ast::checked_blk);
let body = parse_block_tail(p, lo, ast::default_blk);
let _fn = {decl: decl, proto: ast::proto_block, body: body};
ret mk_expr(p, lo, body.span.hi, ast::expr_fn(_fn));
}
@ -1684,12 +1684,14 @@ fn stmt_ends_with_semi(stmt: ast::stmt) -> bool {
fn parse_block(p: parser) -> ast::blk {
let lo = p.get_lo_pos();
if eat_word(p, "unchecked") {
expect(p, token::LBRACE);
be parse_block_tail(p, lo, ast::unchecked_blk);
} else if eat_word(p, "unsafe") {
expect(p, token::LBRACE);
be parse_block_tail(p, lo, ast::unsafe_blk);
} else {
expect(p, token::LBRACE);
be parse_block_tail(p, lo, ast::checked_blk);
be parse_block_tail(p, lo, ast::default_blk);
}
}

View File

@ -575,7 +575,7 @@ fn print_possibly_embedded_block(s: ps, blk: ast::blk, embedded: embed_type,
alt blk.node.rules {
ast::unchecked_blk. { word(s.s, "unchecked"); }
ast::unsafe_blk. { word(s.s, "unsafe"); }
ast::checked_blk. { }
ast::default_blk. { }
}
maybe_print_comment(s, blk.span.lo);

View File

@ -4,8 +4,15 @@
unsafe fn f() { ret; }
fn main() {
fn g() {
unsafe {
f();
}
}
fn h() unsafe {
f();
}
fn main() {
}