@@ -31,6 +31,12 @@ enum OuterAttributeType {
31
31
Attribute ,
32
32
}
33
33
34
+ #[ derive( Clone , Copy , PartialEq , Eq ) ]
35
+ pub enum AllowLeadingUnsafe {
36
+ Yes ,
37
+ No ,
38
+ }
39
+
34
40
impl < ' a > Parser < ' a > {
35
41
/// Parses attributes that appear before an item.
36
42
pub ( super ) fn parse_outer_attributes ( & mut self ) -> PResult < ' a , AttrWrapper > {
@@ -332,7 +338,7 @@ impl<'a> Parser<'a> {
332
338
333
339
/// Parses `cfg_attr(pred, attr_item_list)` where `attr_item_list` is comma-delimited.
334
340
pub fn parse_cfg_attr ( & mut self ) -> PResult < ' a , ( ast:: MetaItem , Vec < ( ast:: AttrItem , Span ) > ) > {
335
- let cfg_predicate = self . parse_meta_item ( ) ?;
341
+ let cfg_predicate = self . parse_meta_item ( AllowLeadingUnsafe :: No ) ?;
336
342
self . expect ( & token:: Comma ) ?;
337
343
338
344
// Presumably, the majority of the time there will only be one attr.
@@ -368,7 +374,10 @@ impl<'a> Parser<'a> {
368
374
/// MetaItem = SimplePath ( '=' UNSUFFIXED_LIT | '(' MetaSeq? ')' )? ;
369
375
/// MetaSeq = MetaItemInner (',' MetaItemInner)* ','? ;
370
376
/// ```
371
- pub fn parse_meta_item ( & mut self ) -> PResult < ' a , ast:: MetaItem > {
377
+ pub fn parse_meta_item (
378
+ & mut self ,
379
+ unsafe_allowed : AllowLeadingUnsafe ,
380
+ ) -> PResult < ' a , ast:: MetaItem > {
372
381
// We can't use `maybe_whole` here because it would bump in the `None`
373
382
// case, which we don't want.
374
383
if let token:: Interpolated ( nt) = & self . token . kind
@@ -384,7 +393,11 @@ impl<'a> Parser<'a> {
384
393
}
385
394
386
395
let lo = self . token . span ;
387
- let is_unsafe = self . eat_keyword ( kw:: Unsafe ) ;
396
+ let is_unsafe = if unsafe_allowed == AllowLeadingUnsafe :: Yes {
397
+ self . eat_keyword ( kw:: Unsafe )
398
+ } else {
399
+ false
400
+ } ;
388
401
let unsafety = if is_unsafe {
389
402
let unsafe_span = self . prev_token . span ;
390
403
self . psess . gated_spans . gate ( sym:: unsafe_attributes, unsafe_span) ;
@@ -427,7 +440,7 @@ impl<'a> Parser<'a> {
427
440
Err ( err) => err. cancel ( ) , // we provide a better error below
428
441
}
429
442
430
- match self . parse_meta_item ( ) {
443
+ match self . parse_meta_item ( AllowLeadingUnsafe :: No ) {
431
444
Ok ( mi) => return Ok ( ast:: NestedMetaItem :: MetaItem ( mi) ) ,
432
445
Err ( err) => err. cancel ( ) , // we provide a better error below
433
446
}
0 commit comments