File tree 3 files changed +17
-17
lines changed
compiler/rustc_parse/src/parser
3 files changed +17
-17
lines changed Original file line number Diff line number Diff line change @@ -92,6 +92,21 @@ impl<'a> Parser<'a> {
92
92
self . parse_expr_res ( Restrictions :: empty ( ) , None )
93
93
}
94
94
95
+ /// Parses an expression, forcing tokens to be collected
96
+ pub fn parse_expr_force_collect ( & mut self ) -> PResult < ' a , P < Expr > > {
97
+ // If we have outer attributes, then the call to `collect_tokens_trailing_token`
98
+ // will be made for us.
99
+ if matches ! ( self . token. kind, TokenKind :: Pound | TokenKind :: DocComment ( ..) ) {
100
+ self . parse_expr ( )
101
+ } else {
102
+ // If we don't have outer attributes, then we need to ensure
103
+ // that collection happens by using `collect_tokens_no_attrs`.
104
+ // Expression don't support custom inner attributes, so `parse_expr`
105
+ // will never try to collect tokens if we don't have outer attributes.
106
+ self . collect_tokens_no_attrs ( |this| this. parse_expr ( ) )
107
+ }
108
+ }
109
+
95
110
pub ( super ) fn parse_anon_const_expr ( & mut self ) -> PResult < ' a , AnonConst > {
96
111
self . parse_expr ( ) . map ( |value| AnonConst { id : DUMMY_NODE_ID , value } )
97
112
}
Original file line number Diff line number Diff line change @@ -987,7 +987,7 @@ impl<'a> Parser<'a> {
987
987
}
988
988
989
989
// Collect tokens because they are used during lowering to HIR.
990
- let expr = self . collect_tokens_no_attrs ( |this| this . parse_expr ( ) ) ?;
990
+ let expr = self . parse_expr_force_collect ( ) ?;
991
991
let span = expr. span ;
992
992
993
993
match & expr. kind {
Original file line number Diff line number Diff line change @@ -128,22 +128,7 @@ impl<'a> Parser<'a> {
128
128
} ) ?)
129
129
}
130
130
131
- // If there are attributes present, then `parse_expr` will end up collecting tokens,
132
- // turning the outer `collect_tokens_no_attrs` into a no-op due to the already present
133
- // tokens. If there are *not* attributes present, then the outer
134
- // `collect_tokens_no_attrs` will ensure that we will end up collecting tokens for the
135
- // expressions.
136
- //
137
- // This is less efficient than it could be, since the outer `collect_tokens_no_attrs`
138
- // still needs to snapshot the `TokenCursor` before calling `parse_expr`, even when
139
- // `parse_expr` will end up collecting tokens. Ideally, this would work more like
140
- // `parse_item`, and take in a `ForceCollect` parameter. However, this would require
141
- // adding a `ForceCollect` parameter in a bunch of places in expression parsing
142
- // for little gain. If the perf impact from this turns out to be noticeable, we should
143
- // revisit this apporach.
144
- NonterminalKind :: Expr => {
145
- token:: NtExpr ( self . collect_tokens_no_attrs ( |this| this. parse_expr ( ) ) ?)
146
- }
131
+ NonterminalKind :: Expr => token:: NtExpr ( self . parse_expr_force_collect ( ) ?) ,
147
132
NonterminalKind :: Literal => {
148
133
// The `:literal` matcher does not support attributes
149
134
token:: NtLiteral (
You can’t perform that action at this time.
0 commit comments