Skip to content

Commit 2733a1f

Browse files
committed
undo abstraction over whether to parse attrs in a block
In principle, it seems like a nice idea to abstract over the two functions that parse blocks (one with inner attrs allowed, one not). However, the existing one wound up making things more complex than just having two separate functions, especially after the obsolete syntax is (will be) removed.
1 parent ab03c1e commit 2733a1f

File tree

1 file changed

+19
-32
lines changed

1 file changed

+19
-32
lines changed

src/libsyntax/parse/parser.rs

Lines changed: 19 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -520,7 +520,7 @@ pub impl Parser {
520520
token::LBRACE => {
521521
debug!("parse_trait_methods(): parsing provided method");
522522
let (inner_attrs, body) =
523-
p.parse_inner_attrs_and_block(true);
523+
p.parse_inner_attrs_and_block();
524524
let attrs = vec::append(attrs, inner_attrs);
525525
provided(@ast::method {
526526
ident: ident,
@@ -1975,7 +1975,7 @@ pub impl Parser {
19751975
fn parse_while_expr(&self) -> @expr {
19761976
let lo = self.last_span.lo;
19771977
let cond = self.parse_expr();
1978-
let body = self.parse_block_no_value();
1978+
let body = self.parse_block();
19791979
let hi = body.span.hi;
19801980
return self.mk_expr(lo, hi, expr_while(cond, body));
19811981
}
@@ -2003,7 +2003,7 @@ pub impl Parser {
20032003
}
20042004

20052005
let lo = self.last_span.lo;
2006-
let body = self.parse_block_no_value();
2006+
let body = self.parse_block();
20072007
let hi = body.span.hi;
20082008
return self.mk_expr(lo, hi, expr_loop(body, opt_ident));
20092009
} else {
@@ -2581,47 +2581,35 @@ pub impl Parser {
25812581
!classify::expr_requires_semi_to_be_stmt(e);
25822582
}
25832583

2584+
// parse a block. No inner attrs are allowed.
25842585
fn parse_block(&self) -> blk {
2585-
// disallow inner attrs:
2586-
let (attrs, blk) = self.parse_inner_attrs_and_block(false);
2587-
assert!(vec::is_empty(attrs));
2588-
return blk;
2586+
maybe_whole!(self, nt_block);
2587+
2588+
let lo = self.span.lo;
2589+
if self.eat_keyword(&~"unsafe") {
2590+
self.obsolete(copy *self.span, ObsoleteUnsafeBlock);
2591+
}
2592+
self.expect(&token::LBRACE);
2593+
2594+
return self.parse_block_tail_(lo, default_blk, ~[]);
25892595
}
25902596

2591-
// I claim the existence of the 'parse_attrs' flag strongly
2592-
// suggests a name-change or refactoring for this function.
2593-
fn parse_inner_attrs_and_block(&self, parse_attrs: bool)
2597+
// parse a block. Inner attrs are allowed.
2598+
fn parse_inner_attrs_and_block(&self)
25942599
-> (~[attribute], blk) {
25952600

25962601
maybe_whole!(pair_empty self, nt_block);
25972602

2598-
fn maybe_parse_inner_attrs_and_next(p: &Parser, parse_attrs: bool) ->
2599-
(~[attribute], ~[attribute]) {
2600-
if parse_attrs {
2601-
p.parse_inner_attrs_and_next()
2602-
} else {
2603-
(~[], ~[])
2604-
}
2605-
}
2606-
26072603
let lo = self.span.lo;
26082604
if self.eat_keyword(&~"unsafe") {
26092605
self.obsolete(copy *self.span, ObsoleteUnsafeBlock);
26102606
}
26112607
self.expect(&token::LBRACE);
2612-
let (inner, next) =
2613-
maybe_parse_inner_attrs_and_next(self, parse_attrs);
2608+
let (inner, next) = self.parse_inner_attrs_and_next();
26142609

26152610
(inner, self.parse_block_tail_(lo, default_blk, next))
26162611
}
2617-
2618-
fn parse_block_no_value(&self) -> blk {
2619-
// We parse blocks that cannot have a value the same as any other
2620-
// block; the type checker will make sure that the tail expression (if
2621-
// any) has unit type.
2622-
return self.parse_block();
2623-
}
2624-
2612+
26252613
// Precondition: already parsed the '{' or '#{'
26262614
// I guess that also means "already parsed the 'impure'" if
26272615
// necessary, and this should take a qualifier.
@@ -3108,7 +3096,7 @@ pub impl Parser {
31083096
fn parse_item_fn(&self, purity: purity, abis: AbiSet) -> item_info {
31093097
let (ident, generics) = self.parse_fn_header();
31103098
let decl = self.parse_fn_decl();
3111-
let (inner_attrs, body) = self.parse_inner_attrs_and_block(true);
3099+
let (inner_attrs, body) = self.parse_inner_attrs_and_block();
31123100
(ident,
31133101
item_fn(decl, purity, abis, generics, body),
31143102
Some(inner_attrs))
@@ -3126,7 +3114,7 @@ pub impl Parser {
31263114
p.parse_arg()
31273115
};
31283116

3129-
let (inner_attrs, body) = self.parse_inner_attrs_and_block(true);
3117+
let (inner_attrs, body) = self.parse_inner_attrs_and_block();
31303118
let hi = body.span.hi;
31313119
let attrs = vec::append(attrs, inner_attrs);
31323120
@ast::method {
@@ -4126,7 +4114,6 @@ pub impl Parser {
41264114
}
41274115

41284116
// parse a foreign item; on failure, return iovi_none.
4129-
// trying to differentiate this from the other parse_foreign_item....
41304117
fn parse_foreign_item(
41314118
&self,
41324119
attrs: ~[attribute],

0 commit comments

Comments
 (0)