@@ -44,33 +44,27 @@ impl<'a> Parser<'a> {
44
44
let lo = self . token . span ;
45
45
46
46
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) )
52
49
} else if let Some ( macro_def) = self . eat_macro_def (
53
50
& attrs,
54
51
& respan ( lo, VisibilityKind :: Inherited ) ,
55
52
lo,
56
53
) ? {
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) )
62
55
// Starts like a simple path, being careful to avoid contextual keywords
63
56
// such as a union items, item with `crate` visibility or auto trait items.
64
57
// Our goal here is to parse an arbitrary path `a::b::c` but not something that starts
65
58
// like a path (1 token), but it fact not a path.
66
59
// `union::b::c` - path, `union U { ... }` - not a path.
67
60
// `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
+ {
74
68
let path = self . parse_path ( PathStyle :: Expr ) ?;
75
69
76
70
if !self . eat ( & token:: Not ) {
@@ -85,12 +79,7 @@ impl<'a> Parser<'a> {
85
79
let expr = this. parse_dot_or_call_expr_with ( expr, lo, attrs. into ( ) ) ?;
86
80
this. parse_assoc_expr_with ( 0 , LhsExpr :: AlreadyParsed ( expr) )
87
81
} ) ?;
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) ) ) ) ;
94
83
}
95
84
96
85
let args = self . parse_mac_args ( ) ?;
@@ -108,15 +97,19 @@ impl<'a> Parser<'a> {
108
97
args,
109
98
prior_type_ascription : self . last_type_ascription ,
110
99
} ;
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
+ {
113
105
StmtKind :: Mac ( P ( ( mac, style, attrs. into ( ) ) ) )
114
106
}
115
107
// We used to incorrectly stop parsing macro-expanded statements here.
116
108
// If the next token will be an error anyway but could have parsed with the
117
109
// 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 {
120
113
// These can continue an expression, so we can't stop parsing and warn.
121
114
token:: OpenDelim ( token:: Paren ) | token:: OpenDelim ( token:: Bracket ) |
122
115
token:: BinOp ( token:: Minus ) | token:: BinOp ( token:: Star ) |
@@ -135,11 +128,7 @@ impl<'a> Parser<'a> {
135
128
let e = self . parse_assoc_expr_with ( 0 , LhsExpr :: AlreadyParsed ( e) ) ?;
136
129
StmtKind :: Expr ( e)
137
130
} ;
138
- Stmt {
139
- id : DUMMY_NODE_ID ,
140
- span : lo. to ( hi) ,
141
- kind,
142
- }
131
+ self . mk_stmt ( lo. to ( hi) , kind)
143
132
} else {
144
133
// FIXME: Bad copy of attrs
145
134
let old_directory_ownership =
@@ -148,11 +137,7 @@ impl<'a> Parser<'a> {
148
137
self . directory . ownership = old_directory_ownership;
149
138
150
139
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) ) ,
156
141
None => {
157
142
let unused_attrs = |attrs : & [ Attribute ] , s : & mut Self | {
158
143
if !attrs. is_empty ( ) {
@@ -178,14 +163,12 @@ impl<'a> Parser<'a> {
178
163
// We are encoding a string of semicolons as an
179
164
// an empty tuple that spans the excess semicolons
180
165
// 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) ) ) ;
189
172
}
190
173
191
174
if self . token == token:: CloseDelim ( token:: Brace ) {
@@ -194,13 +177,8 @@ impl<'a> Parser<'a> {
194
177
}
195
178
196
179
// 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) )
204
182
}
205
183
}
206
184
} ) )
@@ -402,11 +380,10 @@ impl<'a> Parser<'a> {
402
380
self . maybe_annotate_with_ascription ( & mut err, false ) ;
403
381
err. emit ( ) ;
404
382
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
+ ) )
410
387
}
411
388
Ok ( stmt) => stmt,
412
389
} ;
@@ -478,4 +455,8 @@ impl<'a> Parser<'a> {
478
455
"this was erroneously allowed and will become a hard error in a future release"
479
456
} ) . emit ( ) ;
480
457
}
458
+
459
+ fn mk_stmt ( & self , span : Span , kind : StmtKind ) -> Stmt {
460
+ Stmt { id : DUMMY_NODE_ID , kind, span }
461
+ }
481
462
}
0 commit comments