Skip to content

Commit 467c86f

Browse files
committed
parse_stmt_without_recovery: readability!
1 parent 74d9c4b commit 467c86f

File tree

1 file changed

+54
-54
lines changed

1 file changed

+54
-54
lines changed

src/librustc_parse/parser/stmt.rs

Lines changed: 54 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -39,20 +39,21 @@ impl<'a> Parser<'a> {
3939
let attrs = self.parse_outer_attributes()?;
4040
let lo = self.token.span;
4141

42-
Ok(Some(if self.eat_keyword(kw::Let) {
42+
if self.eat_keyword(kw::Let) {
4343
let local = self.parse_local(attrs.into())?;
44-
self.mk_stmt(lo.to(self.prev_span), StmtKind::Local(local))
45-
} else if let Some(macro_def) = self.eat_macro_def(
46-
&attrs,
47-
&respan(lo, VisibilityKind::Inherited),
48-
lo,
49-
)? {
50-
self.mk_stmt(lo.to(self.prev_span), StmtKind::Item(macro_def))
44+
return Ok(Some(self.mk_stmt(lo.to(self.prev_span), StmtKind::Local(local))));
45+
}
46+
47+
let mac_vis = respan(lo, VisibilityKind::Inherited);
48+
if let Some(macro_def) = self.eat_macro_def(&attrs, &mac_vis, lo)? {
49+
return Ok(Some(self.mk_stmt(lo.to(self.prev_span), StmtKind::Item(macro_def))));
50+
}
51+
5152
// Starts like a simple path, being careful to avoid contextual keywords
5253
// such as a union items, item with `crate` visibility or auto trait items.
5354
// Our goal here is to parse an arbitrary path `a::b::c` but not something that starts
5455
// like a path (1 token), but it fact not a path.
55-
} else if self.token.is_path_start()
56+
if self.token.is_path_start()
5657
&& !self.token.is_qpath_start()
5758
&& !self.is_union_item() // `union::b::c` - path, `union U { ... }` - not a path.
5859
&& !self.is_crate_vis() // `crate::b::c` - path, `crate struct S;` - not a path.
@@ -77,58 +78,57 @@ impl<'a> Parser<'a> {
7778
this.parse_assoc_expr_with(0, LhsExpr::AlreadyParsed(expr))
7879
})?;
7980
return Ok(Some(self.mk_stmt(lo.to(self.prev_span), StmtKind::Expr(expr))));
80-
} else {
81-
// FIXME: Bad copy of attrs
82-
let old_directory_ownership =
83-
mem::replace(&mut self.directory.ownership, DirectoryOwnership::UnownedViaBlock);
84-
let item = self.parse_item_(attrs.clone(), false, true)?;
85-
self.directory.ownership = old_directory_ownership;
86-
87-
if let Some(item) = item {
88-
return Ok(Some(self.mk_stmt(lo.to(item.span), StmtKind::Item(item))));
89-
}
81+
}
9082

91-
let unused_attrs = |attrs: &[Attribute], s: &mut Self| {
92-
if !attrs.is_empty() {
93-
if s.prev_token_kind == PrevTokenKind::DocComment {
94-
s.span_fatal_err(s.prev_span, Error::UselessDocComment).emit();
95-
} else if attrs.iter().any(|a| a.style == AttrStyle::Outer) {
96-
s.span_err(
97-
s.token.span, "expected statement after outer attribute"
98-
);
99-
}
100-
}
101-
};
83+
// FIXME: Bad copy of attrs
84+
let old_directory_ownership =
85+
mem::replace(&mut self.directory.ownership, DirectoryOwnership::UnownedViaBlock);
86+
let item = self.parse_item_(attrs.clone(), false, true)?;
87+
self.directory.ownership = old_directory_ownership;
10288

103-
// Do not attempt to parse an expression if we're done here.
104-
if self.token == token::Semi {
105-
unused_attrs(&attrs, self);
106-
self.bump();
107-
let mut last_semi = lo;
108-
while self.token == token::Semi {
109-
last_semi = self.token.span;
110-
self.bump();
89+
if let Some(item) = item {
90+
return Ok(Some(self.mk_stmt(lo.to(item.span), StmtKind::Item(item))));
91+
}
92+
93+
let unused_attrs = |attrs: &[Attribute], s: &mut Self| {
94+
if !attrs.is_empty() {
95+
if s.prev_token_kind == PrevTokenKind::DocComment {
96+
s.span_fatal_err(s.prev_span, Error::UselessDocComment).emit();
97+
} else if attrs.iter().any(|a| a.style == AttrStyle::Outer) {
98+
s.span_err(
99+
s.token.span, "expected statement after outer attribute"
100+
);
111101
}
112-
// We are encoding a string of semicolons as an
113-
// an empty tuple that spans the excess semicolons
114-
// to preserve this info until the lint stage
115-
let kind = StmtKind::Semi(self.mk_expr(
116-
lo.to(last_semi),
117-
ExprKind::Tup(Vec::new()),
118-
ThinVec::new()
119-
));
120-
return Ok(Some(self.mk_stmt(lo.to(last_semi), kind)));
121102
}
103+
};
122104

123-
if self.token == token::CloseDelim(token::Brace) {
124-
unused_attrs(&attrs, self);
125-
return Ok(None);
105+
// Do not attempt to parse an expression if we're done here.
106+
if self.token == token::Semi {
107+
unused_attrs(&attrs, self);
108+
self.bump();
109+
let mut last_semi = lo;
110+
while self.token == token::Semi {
111+
last_semi = self.token.span;
112+
self.bump();
126113
}
114+
// We are encoding a string of semicolons as an an empty tuple that spans
115+
// the excess semicolons to preserve this info until the lint stage.
116+
let kind = StmtKind::Semi(self.mk_expr(
117+
lo.to(last_semi),
118+
ExprKind::Tup(Vec::new()),
119+
ThinVec::new()
120+
));
121+
return Ok(Some(self.mk_stmt(lo.to(last_semi), kind)));
122+
}
127123

128-
// Remainder are line-expr stmts.
129-
let e = self.parse_expr_res( Restrictions::STMT_EXPR, Some(attrs.into()))?;
130-
self.mk_stmt(lo.to(e.span), StmtKind::Expr(e))
131-
}))
124+
if self.token == token::CloseDelim(token::Brace) {
125+
unused_attrs(&attrs, self);
126+
return Ok(None);
127+
}
128+
129+
// Remainder are line-expr stmts.
130+
let e = self.parse_expr_res(Restrictions::STMT_EXPR, Some(attrs.into()))?;
131+
Ok(Some(self.mk_stmt(lo.to(e.span), StmtKind::Expr(e))))
132132
}
133133

134134
/// Parses a statement macro `mac!(args)` provided a `path` representing `mac`.

0 commit comments

Comments
 (0)