Skip to content

Commit 666ff8f

Browse files
committed
reduce repetition in stmt parsing
1 parent 01a4650 commit 666ff8f

File tree

1 file changed

+37
-56
lines changed

1 file changed

+37
-56
lines changed

src/librustc_parse/parser/stmt.rs

Lines changed: 37 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -44,33 +44,27 @@ impl<'a> Parser<'a> {
4444
let lo = self.token.span;
4545

4646
Ok(Some(if self.eat_keyword(kw::Let) {
47-
Stmt {
48-
id: DUMMY_NODE_ID,
49-
kind: StmtKind::Local(self.parse_local(attrs.into())?),
50-
span: lo.to(self.prev_span),
51-
}
47+
let stmt = self.parse_local(attrs.into())?;
48+
self.mk_stmt(lo.to(self.prev_span), StmtKind::Local(stmt))
5249
} else if let Some(macro_def) = self.eat_macro_def(
5350
&attrs,
5451
&respan(lo, VisibilityKind::Inherited),
5552
lo,
5653
)? {
57-
Stmt {
58-
id: DUMMY_NODE_ID,
59-
kind: StmtKind::Item(macro_def),
60-
span: lo.to(self.prev_span),
61-
}
54+
self.mk_stmt(lo.to(self.prev_span), StmtKind::Item(macro_def))
6255
// Starts like a simple path, being careful to avoid contextual keywords
6356
// such as a union items, item with `crate` visibility or auto trait items.
6457
// Our goal here is to parse an arbitrary path `a::b::c` but not something that starts
6558
// like a path (1 token), but it fact not a path.
6659
// `union::b::c` - path, `union U { ... }` - not a path.
6760
// `crate::b::c` - path, `crate struct S;` - not a path.
68-
} else if self.token.is_path_start() &&
69-
!self.token.is_qpath_start() &&
70-
!self.is_union_item() &&
71-
!self.is_crate_vis() &&
72-
!self.is_auto_trait_item() &&
73-
!self.is_async_fn() {
61+
} else if self.token.is_path_start()
62+
&& !self.token.is_qpath_start()
63+
&& !self.is_union_item()
64+
&& !self.is_crate_vis()
65+
&& !self.is_auto_trait_item()
66+
&& !self.is_async_fn()
67+
{
7468
let path = self.parse_path(PathStyle::Expr)?;
7569

7670
if !self.eat(&token::Not) {
@@ -85,12 +79,7 @@ impl<'a> Parser<'a> {
8579
let expr = this.parse_dot_or_call_expr_with(expr, lo, attrs.into())?;
8680
this.parse_assoc_expr_with(0, LhsExpr::AlreadyParsed(expr))
8781
})?;
88-
89-
return Ok(Some(Stmt {
90-
id: DUMMY_NODE_ID,
91-
kind: StmtKind::Expr(expr),
92-
span: lo.to(self.prev_span),
93-
}));
82+
return Ok(Some(self.mk_stmt(lo.to(self.prev_span), StmtKind::Expr(expr))));
9483
}
9584

9685
let args = self.parse_mac_args()?;
@@ -108,15 +97,19 @@ impl<'a> Parser<'a> {
10897
args,
10998
prior_type_ascription: self.last_type_ascription,
11099
};
111-
let kind = if delim == token::Brace ||
112-
self.token == token::Semi || self.token == token::Eof {
100+
101+
let kind = if delim == token::Brace
102+
|| self.token == token::Semi
103+
|| self.token == token::Eof
104+
{
113105
StmtKind::Mac(P((mac, style, attrs.into())))
114106
}
115107
// We used to incorrectly stop parsing macro-expanded statements here.
116108
// If the next token will be an error anyway but could have parsed with the
117109
// earlier behavior, stop parsing here and emit a warning to avoid breakage.
118-
else if macro_legacy_warnings && self.token.can_begin_expr() &&
119-
match self.token.kind {
110+
else if macro_legacy_warnings
111+
&& self.token.can_begin_expr()
112+
&& match self.token.kind {
120113
// These can continue an expression, so we can't stop parsing and warn.
121114
token::OpenDelim(token::Paren) | token::OpenDelim(token::Bracket) |
122115
token::BinOp(token::Minus) | token::BinOp(token::Star) |
@@ -135,11 +128,7 @@ impl<'a> Parser<'a> {
135128
let e = self.parse_assoc_expr_with(0, LhsExpr::AlreadyParsed(e))?;
136129
StmtKind::Expr(e)
137130
};
138-
Stmt {
139-
id: DUMMY_NODE_ID,
140-
span: lo.to(hi),
141-
kind,
142-
}
131+
self.mk_stmt(lo.to(hi), kind)
143132
} else {
144133
// FIXME: Bad copy of attrs
145134
let old_directory_ownership =
@@ -148,11 +137,7 @@ impl<'a> Parser<'a> {
148137
self.directory.ownership = old_directory_ownership;
149138

150139
match item {
151-
Some(i) => Stmt {
152-
id: DUMMY_NODE_ID,
153-
span: lo.to(i.span),
154-
kind: StmtKind::Item(i),
155-
},
140+
Some(i) => self.mk_stmt(lo.to(i.span), StmtKind::Item(i)),
156141
None => {
157142
let unused_attrs = |attrs: &[Attribute], s: &mut Self| {
158143
if !attrs.is_empty() {
@@ -178,14 +163,12 @@ impl<'a> Parser<'a> {
178163
// We are encoding a string of semicolons as an
179164
// an empty tuple that spans the excess semicolons
180165
// to preserve this info until the lint stage
181-
return Ok(Some(Stmt {
182-
id: DUMMY_NODE_ID,
183-
span: lo.to(last_semi),
184-
kind: StmtKind::Semi(self.mk_expr(lo.to(last_semi),
185-
ExprKind::Tup(Vec::new()),
186-
ThinVec::new()
187-
)),
188-
}));
166+
let kind = StmtKind::Semi(self.mk_expr(
167+
lo.to(last_semi),
168+
ExprKind::Tup(Vec::new()),
169+
ThinVec::new()
170+
));
171+
return Ok(Some(self.mk_stmt(lo.to(last_semi), kind)));
189172
}
190173

191174
if self.token == token::CloseDelim(token::Brace) {
@@ -194,13 +177,8 @@ impl<'a> Parser<'a> {
194177
}
195178

196179
// Remainder are line-expr stmts.
197-
let e = self.parse_expr_res(
198-
Restrictions::STMT_EXPR, Some(attrs.into()))?;
199-
Stmt {
200-
id: DUMMY_NODE_ID,
201-
span: lo.to(e.span),
202-
kind: StmtKind::Expr(e),
203-
}
180+
let e = self.parse_expr_res( Restrictions::STMT_EXPR, Some(attrs.into()))?;
181+
self.mk_stmt(lo.to(e.span), StmtKind::Expr(e))
204182
}
205183
}
206184
}))
@@ -402,11 +380,10 @@ impl<'a> Parser<'a> {
402380
self.maybe_annotate_with_ascription(&mut err, false);
403381
err.emit();
404382
self.recover_stmt_(SemiColonMode::Ignore, BlockMode::Ignore);
405-
Some(Stmt {
406-
id: DUMMY_NODE_ID,
407-
kind: StmtKind::Expr(self.mk_expr_err(self.token.span)),
408-
span: self.token.span,
409-
})
383+
Some(self.mk_stmt(
384+
self.token.span,
385+
StmtKind::Expr(self.mk_expr_err(self.token.span)),
386+
))
410387
}
411388
Ok(stmt) => stmt,
412389
};
@@ -478,4 +455,8 @@ impl<'a> Parser<'a> {
478455
"this was erroneously allowed and will become a hard error in a future release"
479456
}).emit();
480457
}
458+
459+
fn mk_stmt(&self, span: Span, kind: StmtKind) -> Stmt {
460+
Stmt { id: DUMMY_NODE_ID, kind, span }
461+
}
481462
}

0 commit comments

Comments
 (0)