@@ -152,11 +152,9 @@ impl<'a> Parser<'a> {
152
152
}
153
153
154
154
self . parse_item_const ( None ) ?
155
- } else if self . check_keyword ( kw:: Unsafe ) && self . is_keyword_ahead ( 1 , & [ kw:: Trait , kw:: Auto ] )
156
- {
157
- // UNSAFE TRAIT ITEM
158
- let unsafety = self . parse_unsafety ( ) ;
159
- self . parse_item_trait ( attrs, lo, unsafety) ?
155
+ } else if self . check_keyword ( kw:: Trait ) || self . check_auto_or_unsafe_trait_item ( ) {
156
+ // TRAIT ITEM
157
+ self . parse_item_trait ( attrs, lo) ?
160
158
} else if self . check_keyword ( kw:: Impl )
161
159
|| self . check_keyword ( kw:: Unsafe ) && self . is_keyword_ahead ( 1 , & [ kw:: Impl ] )
162
160
|| self . check_keyword ( kw:: Default ) && self . is_keyword_ahead ( 1 , & [ kw:: Impl , kw:: Unsafe ] )
@@ -176,11 +174,6 @@ impl<'a> Parser<'a> {
176
174
} else if self . eat_keyword ( kw:: Enum ) {
177
175
// ENUM ITEM
178
176
self . parse_item_enum ( ) ?
179
- } else if self . check_keyword ( kw:: Trait )
180
- || ( self . check_keyword ( kw:: Auto ) && self . is_keyword_ahead ( 1 , & [ kw:: Trait ] ) )
181
- {
182
- // TRAIT ITEM
183
- self . parse_item_trait ( attrs, lo, Unsafe :: No ) ?
184
177
} else if self . eat_keyword ( kw:: Struct ) {
185
178
// STRUCT ITEM
186
179
self . parse_item_struct ( ) ?
@@ -209,6 +202,15 @@ impl<'a> Parser<'a> {
209
202
Ok ( Some ( info) )
210
203
}
211
204
205
+ /// When parsing a statement, would the start of a path be an item?
206
+ pub ( super ) fn is_path_start_item ( & mut self ) -> bool {
207
+ self . is_crate_vis ( ) // no: `crate::b`, yes: `crate $item`
208
+ || self . is_union_item ( ) // no: `union::b`, yes: `union U { .. }`
209
+ || self . check_auto_or_unsafe_trait_item ( ) // no: `auto::b`, yes: `auto trait X { .. }`
210
+ || self . is_async_fn ( ) // no(2015): `async::b`, yes: `async fn`
211
+ || self . is_macro_rules_item ( ) // no: `macro_rules::b`, yes: `macro_rules! mac`
212
+ }
213
+
212
214
/// Recover on encountering a struct or method definition where the user
213
215
/// forgot to add the `struct` or `fn` keyword after writing `pub`: `pub S {}`.
214
216
fn recover_missing_kw_before_item ( & mut self ) -> PResult < ' a , ( ) > {
@@ -338,7 +340,7 @@ impl<'a> Parser<'a> {
338
340
Err ( err)
339
341
}
340
342
341
- pub ( super ) fn is_async_fn ( & self ) -> bool {
343
+ fn is_async_fn ( & self ) -> bool {
342
344
self . token . is_keyword ( kw:: Async ) && self . is_keyword_ahead ( 1 , & [ kw:: Fn ] )
343
345
}
344
346
@@ -609,13 +611,17 @@ impl<'a> Parser<'a> {
609
611
}
610
612
}
611
613
612
- /// Parses `auto? trait Foo { ... }` or `trait Foo = Bar;`.
613
- fn parse_item_trait (
614
- & mut self ,
615
- attrs : & mut Vec < Attribute > ,
616
- lo : Span ,
617
- unsafety : Unsafe ,
618
- ) -> PResult < ' a , ItemInfo > {
614
+ /// Is this an `(unsafe auto? | auto) trait` item?
615
+ fn check_auto_or_unsafe_trait_item ( & mut self ) -> bool {
616
+ // auto trait
617
+ self . check_keyword ( kw:: Auto ) && self . is_keyword_ahead ( 1 , & [ kw:: Trait ] )
618
+ // unsafe auto trait
619
+ || self . check_keyword ( kw:: Unsafe ) && self . is_keyword_ahead ( 1 , & [ kw:: Trait , kw:: Auto ] )
620
+ }
621
+
622
+ /// Parses `unsafe? auto? trait Foo { ... }` or `trait Foo = Bar;`.
623
+ fn parse_item_trait ( & mut self , attrs : & mut Vec < Attribute > , lo : Span ) -> PResult < ' a , ItemInfo > {
624
+ let unsafety = self . parse_unsafety ( ) ;
619
625
// Parse optional `auto` prefix.
620
626
let is_auto = if self . eat_keyword ( kw:: Auto ) { IsAuto :: Yes } else { IsAuto :: No } ;
621
627
@@ -1179,7 +1185,7 @@ impl<'a> Parser<'a> {
1179
1185
Ok ( ( class_name, ItemKind :: Union ( vdata, generics) ) )
1180
1186
}
1181
1187
1182
- pub ( super ) fn is_union_item ( & self ) -> bool {
1188
+ fn is_union_item ( & self ) -> bool {
1183
1189
self . token . is_keyword ( kw:: Union )
1184
1190
&& self . look_ahead ( 1 , |t| t. is_ident ( ) && !t. is_reserved_ident ( ) )
1185
1191
}
@@ -1362,7 +1368,7 @@ impl<'a> Parser<'a> {
1362
1368
}
1363
1369
1364
1370
/// Is this unambiguously the start of a `macro_rules! foo` item defnition?
1365
- pub ( super ) fn is_macro_rules_item ( & mut self ) -> bool {
1371
+ fn is_macro_rules_item ( & mut self ) -> bool {
1366
1372
self . check_keyword ( sym:: macro_rules)
1367
1373
&& self . look_ahead ( 1 , |t| * t == token:: Not )
1368
1374
&& self . look_ahead ( 2 , |t| t. is_ident ( ) )
0 commit comments