Skip to content

Commit 8170acb

Browse files
committed
Refactor parse_expr_res.
This removes the final `Option<AttrWrapper>` argument.
1 parent 43eae4c commit 8170acb

File tree

5 files changed

+33
-28
lines changed

5 files changed

+33
-28
lines changed

compiler/rustc_parse/src/parser/diagnostics.rs

+10-3
Original file line numberDiff line numberDiff line change
@@ -2502,7 +2502,8 @@ impl<'a> Parser<'a> {
25022502
/// wrapped in braces.
25032503
pub(super) fn handle_unambiguous_unbraced_const_arg(&mut self) -> PResult<'a, P<Expr>> {
25042504
let start = self.token.span;
2505-
let expr = self.parse_expr_res(Restrictions::CONST_EXPR, None).map_err(|mut err| {
2505+
let attrs = self.parse_outer_attributes()?;
2506+
let expr = self.parse_expr_res(Restrictions::CONST_EXPR, attrs).map_err(|mut err| {
25062507
err.span_label(
25072508
start.shrink_to_lo(),
25082509
"while parsing a const generic argument starting here",
@@ -2624,7 +2625,10 @@ impl<'a> Parser<'a> {
26242625
if is_op_or_dot {
26252626
self.bump();
26262627
}
2627-
match self.parse_expr_res(Restrictions::CONST_EXPR, None) {
2628+
match (|| {
2629+
let attrs = self.parse_outer_attributes()?;
2630+
self.parse_expr_res(Restrictions::CONST_EXPR, attrs)
2631+
})() {
26282632
Ok(expr) => {
26292633
// Find a mistake like `MyTrait<Assoc == S::Assoc>`.
26302634
if token::EqEq == snapshot.token.kind {
@@ -2678,7 +2682,10 @@ impl<'a> Parser<'a> {
26782682
&mut self,
26792683
mut snapshot: SnapshotParser<'a>,
26802684
) -> Option<P<ast::Expr>> {
2681-
match snapshot.parse_expr_res(Restrictions::CONST_EXPR, None) {
2685+
match (|| {
2686+
let attrs = self.parse_outer_attributes()?;
2687+
snapshot.parse_expr_res(Restrictions::CONST_EXPR, attrs)
2688+
})() {
26822689
// Since we don't know the exact reason why we failed to parse the type or the
26832690
// expression, employ a simple heuristic to weed out some pathological cases.
26842691
Ok(expr) if let token::Comma | token::Gt = snapshot.token.kind => {

compiler/rustc_parse/src/parser/expr.rs

+19-11
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,8 @@ impl<'a> Parser<'a> {
9494
pub fn parse_expr(&mut self) -> PResult<'a, P<Expr>> {
9595
self.current_closure.take();
9696

97-
self.parse_expr_res(Restrictions::empty(), None)
97+
let attrs = self.parse_outer_attributes()?;
98+
self.parse_expr_res(Restrictions::empty(), attrs)
9899
}
99100

100101
/// Parses an expression, forcing tokens to be collected
@@ -107,7 +108,8 @@ impl<'a> Parser<'a> {
107108
}
108109

109110
fn parse_expr_catch_underscore(&mut self, restrictions: Restrictions) -> PResult<'a, P<Expr>> {
110-
match self.parse_expr_res(restrictions, None) {
111+
let attrs = self.parse_outer_attributes()?;
112+
match self.parse_expr_res(restrictions, attrs) {
111113
Ok(expr) => Ok(expr),
112114
Err(err) => match self.token.ident() {
113115
Some((Ident { name: kw::Underscore, .. }, IdentIsRaw::No))
@@ -134,9 +136,8 @@ impl<'a> Parser<'a> {
134136
pub(super) fn parse_expr_res(
135137
&mut self,
136138
r: Restrictions,
137-
attrs: Option<AttrWrapper>,
139+
attrs: AttrWrapper,
138140
) -> PResult<'a, P<Expr>> {
139-
let attrs = self.parse_or_use_outer_attributes(attrs)?;
140141
self.with_res(r, |this| this.parse_expr_assoc_with(0, LhsExpr::Unparsed { attrs }))
141142
}
142143

@@ -2343,7 +2344,8 @@ impl<'a> Parser<'a> {
23432344
self.restrictions - Restrictions::STMT_EXPR - Restrictions::ALLOW_LET;
23442345
let prev = self.prev_token.clone();
23452346
let token = self.token.clone();
2346-
match self.parse_expr_res(restrictions, None) {
2347+
let attrs = self.parse_outer_attributes()?;
2348+
match self.parse_expr_res(restrictions, attrs) {
23472349
Ok(expr) => expr,
23482350
Err(err) => self.recover_closure_body(err, before, prev, token, lo, decl_hi)?,
23492351
}
@@ -2591,8 +2593,9 @@ impl<'a> Parser<'a> {
25912593

25922594
/// Parses the condition of a `if` or `while` expression.
25932595
fn parse_expr_cond(&mut self) -> PResult<'a, P<Expr>> {
2596+
let attrs = self.parse_outer_attributes()?;
25942597
let mut cond =
2595-
self.parse_expr_res(Restrictions::NO_STRUCT_LITERAL | Restrictions::ALLOW_LET, None)?;
2598+
self.parse_expr_res(Restrictions::NO_STRUCT_LITERAL | Restrictions::ALLOW_LET, attrs)?;
25962599

25972600
CondChecker::new(self).visit_expr(&mut cond);
25982601

@@ -2776,7 +2779,8 @@ impl<'a> Parser<'a> {
27762779
(Err(err), Some((start_span, left))) if self.eat_keyword(kw::In) => {
27772780
// We know for sure we have seen `for ($SOMETHING in`. In the happy path this would
27782781
// happen right before the return of this method.
2779-
let expr = match self.parse_expr_res(Restrictions::NO_STRUCT_LITERAL, None) {
2782+
let attrs = self.parse_outer_attributes()?;
2783+
let expr = match self.parse_expr_res(Restrictions::NO_STRUCT_LITERAL, attrs) {
27802784
Ok(expr) => expr,
27812785
Err(expr_err) => {
27822786
// We don't know what followed the `in`, so cancel and bubble up the
@@ -2810,7 +2814,8 @@ impl<'a> Parser<'a> {
28102814
self.error_missing_in_for_loop();
28112815
}
28122816
self.check_for_for_in_in_typo(self.prev_token.span);
2813-
let expr = self.parse_expr_res(Restrictions::NO_STRUCT_LITERAL, None)?;
2817+
let attrs = self.parse_outer_attributes()?;
2818+
let expr = self.parse_expr_res(Restrictions::NO_STRUCT_LITERAL, attrs)?;
28142819
Ok((pat, expr))
28152820
}
28162821

@@ -2922,7 +2927,8 @@ impl<'a> Parser<'a> {
29222927
/// Parses a `match ... { ... }` expression (`match` token already eaten).
29232928
fn parse_expr_match(&mut self) -> PResult<'a, P<Expr>> {
29242929
let match_span = self.prev_token.span;
2925-
let scrutinee = self.parse_expr_res(Restrictions::NO_STRUCT_LITERAL, None)?;
2930+
let attrs = self.parse_outer_attributes()?;
2931+
let scrutinee = self.parse_expr_res(Restrictions::NO_STRUCT_LITERAL, attrs)?;
29262932

29272933
self.parse_match_block(match_span, match_span, scrutinee, MatchKind::Prefix)
29282934
}
@@ -3126,8 +3132,9 @@ impl<'a> Parser<'a> {
31263132
let arrow_span = this.prev_token.span;
31273133
let arm_start_span = this.token.span;
31283134

3135+
let attrs = this.parse_outer_attributes()?;
31293136
let expr =
3130-
this.parse_expr_res(Restrictions::STMT_EXPR, None).map_err(|mut err| {
3137+
this.parse_expr_res(Restrictions::STMT_EXPR, attrs).map_err(|mut err| {
31313138
err.span_label(arrow_span, "while parsing the `match` arm starting here");
31323139
err
31333140
})?;
@@ -3332,7 +3339,8 @@ impl<'a> Parser<'a> {
33323339
}
33333340

33343341
fn parse_match_guard_condition(&mut self) -> PResult<'a, P<Expr>> {
3335-
self.parse_expr_res(Restrictions::ALLOW_LET | Restrictions::IN_IF_GUARD, None).map_err(
3342+
let attrs = self.parse_outer_attributes()?;
3343+
self.parse_expr_res(Restrictions::ALLOW_LET | Restrictions::IN_IF_GUARD, attrs).map_err(
33363344
|mut err| {
33373345
if self.prev_token == token::OpenDelim(Delimiter::Brace) {
33383346
let sugg_sp = self.prev_token.span.shrink_to_lo();

compiler/rustc_parse/src/parser/mod.rs

-11
Original file line numberDiff line numberDiff line change
@@ -1337,17 +1337,6 @@ impl<'a> Parser<'a> {
13371337
})
13381338
}
13391339

1340-
fn parse_or_use_outer_attributes(
1341-
&mut self,
1342-
already_parsed_attrs: Option<AttrWrapper>,
1343-
) -> PResult<'a, AttrWrapper> {
1344-
if let Some(attrs) = already_parsed_attrs {
1345-
Ok(attrs)
1346-
} else {
1347-
self.parse_outer_attributes()
1348-
}
1349-
}
1350-
13511340
/// Parses a single token tree from the input.
13521341
pub fn parse_token_tree(&mut self) -> TokenTree {
13531342
match self.token.kind {

compiler/rustc_parse/src/parser/path.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -920,7 +920,8 @@ impl<'a> Parser<'a> {
920920
// Fall back by trying to parse a const-expr expression. If we successfully do so,
921921
// then we should report an error that it needs to be wrapped in braces.
922922
let snapshot = self.create_snapshot_for_diagnostic();
923-
match self.parse_expr_res(Restrictions::CONST_EXPR, None) {
923+
let attrs = self.parse_outer_attributes()?;
924+
match self.parse_expr_res(Restrictions::CONST_EXPR, attrs) {
924925
Ok(expr) => {
925926
return Ok(Some(self.dummy_const_arg_needs_braces(
926927
self.dcx().struct_span_err(expr.span, "invalid const generic expression"),

compiler/rustc_parse/src/parser/stmt.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,9 @@ impl<'a> Parser<'a> {
126126
// Remainder are line-expr stmts.
127127
let e = match force_collect {
128128
ForceCollect::Yes => self.collect_tokens_no_attrs(|this| {
129-
this.parse_expr_res(Restrictions::STMT_EXPR, Some(attrs))
129+
this.parse_expr_res(Restrictions::STMT_EXPR, attrs)
130130
})?,
131-
ForceCollect::No => self.parse_expr_res(Restrictions::STMT_EXPR, Some(attrs))?,
131+
ForceCollect::No => self.parse_expr_res(Restrictions::STMT_EXPR, attrs)?,
132132
};
133133
if matches!(e.kind, ExprKind::Assign(..)) && self.eat_keyword(kw::Else) {
134134
let bl = self.parse_block()?;

0 commit comments

Comments
 (0)