Skip to content

Commit cbe8da0

Browse files
nikomatsakisbrson
authored andcommitted
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.
1 parent e956937 commit cbe8da0

File tree

7 files changed

+28
-20
lines changed

7 files changed

+28
-20
lines changed

src/comp/front/test.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ fn mk_tests(cx: test_ctxt) -> @ast::item {
184184
let test_descs = mk_test_desc_vec(cx);
185185

186186
let body_: ast::blk_ =
187-
checked_block([], option::some(test_descs), cx.next_node_id());
187+
default_block([], option::some(test_descs), cx.next_node_id());
188188
let body = nospan(body_);
189189

190190
let fn_ = {decl: decl, proto: proto, body: body};
@@ -303,7 +303,7 @@ fn mk_main(cx: test_ctxt) -> @ast::item {
303303
let test_main_call_expr = mk_test_main_call(cx);
304304

305305
let body_: ast::blk_ =
306-
checked_block([], option::some(test_main_call_expr),
306+
default_block([], option::some(test_main_call_expr),
307307
cx.next_node_id());
308308
let body = {node: body_, span: dummy_sp()};
309309

src/comp/middle/typeck.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2090,13 +2090,7 @@ fn check_expr_with_unifier(fcx: @fn_ctxt, expr: @ast::expr, unify: unifier,
20902090
}
20912091
ast::expr_block(b) {
20922092
// If this is an unchecked block, turn off purity-checking
2093-
let fcx_for_block =
2094-
alt b.node.rules {
2095-
ast::unchecked_blk. { @{purity: ast::impure_fn with *fcx} }
2096-
ast::unsafe_blk. { @{purity: ast::unsafe_fn with *fcx} }
2097-
ast::checked_blk. { fcx }
2098-
};
2099-
bot = check_block(fcx_for_block, b);
2093+
bot = check_block(fcx, b);
21002094
let typ =
21012095
alt b.node.expr {
21022096
some(expr) { expr_ty(tcx, expr) }
@@ -2553,7 +2547,12 @@ fn check_stmt(fcx: @fn_ctxt, stmt: @ast::stmt) -> bool {
25532547
ret bot;
25542548
}
25552549

2556-
fn check_block(fcx: @fn_ctxt, blk: ast::blk) -> bool {
2550+
fn check_block(fcx0: @fn_ctxt, blk: ast::blk) -> bool {
2551+
let fcx = alt blk.node.rules {
2552+
ast::unchecked_blk. { @{purity: ast::impure_fn with *fcx0} }
2553+
ast::unsafe_blk. { @{purity: ast::unsafe_fn with *fcx0} }
2554+
ast::default_blk. { fcx0 }
2555+
};
25572556
let bot = false;
25582557
let warned = false;
25592558
for s: @ast::stmt in blk.node.stmts {

src/comp/syntax/ast.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ type field_ = {mut: mutability, ident: ident, expr: @expr};
174174

175175
type field = spanned<field_>;
176176

177-
tag blk_check_mode { checked_blk; unchecked_blk; unsafe_blk; }
177+
tag blk_check_mode { default_blk; unchecked_blk; unsafe_blk; }
178178

179179
tag expr_check_mode { claimed_expr; checked_expr; }
180180

src/comp/syntax/ast_util.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -185,13 +185,13 @@ fn eq_ty(&&a: @ty, &&b: @ty) -> bool { ret std::box::ptr_eq(a, b); }
185185
fn hash_ty(&&t: @ty) -> uint { ret t.span.lo << 16u + t.span.hi; }
186186

187187
fn block_from_expr(e: @expr) -> blk {
188-
let blk_ = checked_block([], option::some::<@expr>(e), e.id);
188+
let blk_ = default_block([], option::some::<@expr>(e), e.id);
189189
ret {node: blk_, span: e.span};
190190
}
191191

192-
fn checked_block(stmts1: [@stmt], expr1: option::t<@expr>, id1: node_id) ->
192+
fn default_block(stmts1: [@stmt], expr1: option::t<@expr>, id1: node_id) ->
193193
blk_ {
194-
ret {stmts: stmts1, expr: expr1, id: id1, rules: checked_blk};
194+
ret {stmts: stmts1, expr: expr1, id: id1, rules: default_blk};
195195
}
196196

197197
fn obj_field_from_anon_obj_field(f: anon_obj_field) -> obj_field {

src/comp/syntax/parse/parser.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -828,7 +828,7 @@ fn parse_bottom_expr(p: parser) -> @ast::expr {
828828
p.peek() == token::OROR {
829829
ret parse_fn_block_expr(p);
830830
} else {
831-
let blk = parse_block_tail(p, lo, ast::checked_blk);
831+
let blk = parse_block_tail(p, lo, ast::default_blk);
832832
ret mk_expr(p, blk.span.lo, blk.span.hi, ast::expr_block(blk));
833833
}
834834
} else if eat_word(p, "if") {
@@ -873,7 +873,7 @@ fn parse_bottom_expr(p: parser) -> @ast::expr {
873873
} else if p.peek() == token::POUND_LBRACE {
874874
p.bump();
875875
let blk = ast::mac_embed_block(
876-
parse_block_tail(p, lo, ast::checked_blk));
876+
parse_block_tail(p, lo, ast::default_blk));
877877
ret mk_mac_expr(p, lo, p.get_hi_pos(), blk);
878878
} else if p.peek() == token::ELLIPSIS {
879879
p.bump();
@@ -1320,7 +1320,7 @@ fn parse_fn_expr(p: parser, proto: ast::proto) -> @ast::expr {
13201320
fn parse_fn_block_expr(p: parser) -> @ast::expr {
13211321
let lo = p.get_last_lo_pos();
13221322
let decl = parse_fn_block_decl(p);
1323-
let body = parse_block_tail(p, lo, ast::checked_blk);
1323+
let body = parse_block_tail(p, lo, ast::default_blk);
13241324
let _fn = {decl: decl, proto: ast::proto_block, body: body};
13251325
ret mk_expr(p, lo, body.span.hi, ast::expr_fn(_fn));
13261326
}
@@ -1684,12 +1684,14 @@ fn stmt_ends_with_semi(stmt: ast::stmt) -> bool {
16841684
fn parse_block(p: parser) -> ast::blk {
16851685
let lo = p.get_lo_pos();
16861686
if eat_word(p, "unchecked") {
1687+
expect(p, token::LBRACE);
16871688
be parse_block_tail(p, lo, ast::unchecked_blk);
16881689
} else if eat_word(p, "unsafe") {
1690+
expect(p, token::LBRACE);
16891691
be parse_block_tail(p, lo, ast::unsafe_blk);
16901692
} else {
16911693
expect(p, token::LBRACE);
1692-
be parse_block_tail(p, lo, ast::checked_blk);
1694+
be parse_block_tail(p, lo, ast::default_blk);
16931695
}
16941696
}
16951697

src/comp/syntax/print/pprust.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -575,7 +575,7 @@ fn print_possibly_embedded_block(s: ps, blk: ast::blk, embedded: embed_type,
575575
alt blk.node.rules {
576576
ast::unchecked_blk. { word(s.s, "unchecked"); }
577577
ast::unsafe_blk. { word(s.s, "unsafe"); }
578-
ast::checked_blk. { }
578+
ast::default_blk. { }
579579
}
580580

581581
maybe_print_comment(s, blk.span.lo);

src/test/run-pass/unsafe-fn-called-from-unsafe-blk.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,15 @@
44

55
unsafe fn f() { ret; }
66

7-
fn main() {
7+
fn g() {
88
unsafe {
99
f();
1010
}
1111
}
12+
13+
fn h() unsafe {
14+
f();
15+
}
16+
17+
fn main() {
18+
}

0 commit comments

Comments
 (0)