@@ -34,7 +34,7 @@ impl<'a> Parser<'a> {
34
34
35
35
/// Parses a `mod <foo> { ... }` or `mod <foo>;` item.
36
36
fn parse_item_mod ( & mut self , attrs : & mut AttrVec ) -> PResult < ' a , ItemInfo > {
37
- let unsafety = self . parse_unsafety ( ) ;
37
+ let unsafety = self . parse_unsafety ( false ) ;
38
38
self . expect_keyword ( kw:: Mod ) ?;
39
39
let id = self . parse_ident ( ) ?;
40
40
let mod_kind = if self . eat ( & token:: Semi ) {
@@ -215,14 +215,14 @@ impl<'a> Parser<'a> {
215
215
kw_case_insensitive : bool ,
216
216
) -> PResult < ' a , Option < ItemInfo > > {
217
217
let def_final = def == & Defaultness :: Final ;
218
- let mut def = || mem:: replace ( def, Defaultness :: Final ) ;
218
+ let mut def_ = || mem:: replace ( def, Defaultness :: Final ) ;
219
219
220
220
let info = if self . eat_keyword_case ( kw:: Use , kw_case_insensitive) {
221
221
self . parse_use_item ( ) ?
222
- } else if self . check_fn_front_matter ( def_final) {
222
+ } else if self . check_fn_front_matter ( def_final, kw_case_insensitive ) {
223
223
// FUNCTION ITEM
224
224
let ( ident, sig, generics, body) = self . parse_fn ( attrs, fn_parse_mode, lo, vis) ?;
225
- ( ident, ItemKind :: Fn ( Box :: new ( Fn { defaultness : def ( ) , sig, generics, body } ) ) )
225
+ ( ident, ItemKind :: Fn ( Box :: new ( Fn { defaultness : def_ ( ) , sig, generics, body } ) ) )
226
226
} else if self . eat_keyword ( kw:: Extern ) {
227
227
if self . eat_keyword ( kw:: Crate ) {
228
228
// EXTERN CRATE
@@ -233,7 +233,7 @@ impl<'a> Parser<'a> {
233
233
}
234
234
} else if self . is_unsafe_foreign_mod ( ) {
235
235
// EXTERN BLOCK
236
- let unsafety = self . parse_unsafety ( ) ;
236
+ let unsafety = self . parse_unsafety ( false ) ;
237
237
self . expect_keyword ( kw:: Extern ) ?;
238
238
self . parse_item_foreign_mod ( attrs, unsafety) ?
239
239
} else if self . is_static_global ( ) {
@@ -242,15 +242,15 @@ impl<'a> Parser<'a> {
242
242
let m = self . parse_mutability ( ) ;
243
243
let ( ident, ty, expr) = self . parse_item_global ( Some ( m) ) ?;
244
244
( ident, ItemKind :: Static ( ty, m, expr) )
245
- } else if let Const :: Yes ( const_span) = self . parse_constness ( ) {
245
+ } else if let Const :: Yes ( const_span) = self . parse_constness ( false ) {
246
246
// CONST ITEM
247
247
if self . token . is_keyword ( kw:: Impl ) {
248
248
// recover from `const impl`, suggest `impl const`
249
- self . recover_const_impl ( const_span, attrs, def ( ) ) ?
249
+ self . recover_const_impl ( const_span, attrs, def_ ( ) ) ?
250
250
} else {
251
251
self . recover_const_mut ( const_span) ;
252
252
let ( ident, ty, expr) = self . parse_item_global ( None ) ?;
253
- ( ident, ItemKind :: Const ( def ( ) , ty, expr) )
253
+ ( ident, ItemKind :: Const ( def_ ( ) , ty, expr) )
254
254
}
255
255
} else if self . check_keyword ( kw:: Trait ) || self . check_auto_or_unsafe_trait_item ( ) {
256
256
// TRAIT ITEM
@@ -259,15 +259,15 @@ impl<'a> Parser<'a> {
259
259
|| self . check_keyword ( kw:: Unsafe ) && self . is_keyword_ahead ( 1 , & [ kw:: Impl ] )
260
260
{
261
261
// IMPL ITEM
262
- self . parse_item_impl ( attrs, def ( ) ) ?
262
+ self . parse_item_impl ( attrs, def_ ( ) ) ?
263
263
} else if self . check_keyword ( kw:: Mod )
264
264
|| self . check_keyword ( kw:: Unsafe ) && self . is_keyword_ahead ( 1 , & [ kw:: Mod ] )
265
265
{
266
266
// MODULE ITEM
267
267
self . parse_item_mod ( attrs) ?
268
268
} else if self . eat_keyword ( kw:: Type ) {
269
269
// TYPE ITEM
270
- self . parse_type_alias ( def ( ) ) ?
270
+ self . parse_type_alias ( def_ ( ) ) ?
271
271
} else if self . eat_keyword ( kw:: Enum ) {
272
272
// ENUM ITEM
273
273
self . parse_item_enum ( ) ?
@@ -295,16 +295,10 @@ impl<'a> Parser<'a> {
295
295
self . recover_missing_kw_before_item ( ) ?;
296
296
return Ok ( None ) ;
297
297
} else if self . isnt_macro_invocation ( ) && !kw_case_insensitive {
298
+ _ = def_;
299
+
298
300
// Recover wrong cased keywords
299
- return self . parse_item_kind (
300
- attrs,
301
- macros_allowed,
302
- lo,
303
- vis,
304
- & mut def ( ) ,
305
- fn_parse_mode,
306
- true ,
307
- ) ;
301
+ return self . parse_item_kind ( attrs, macros_allowed, lo, vis, def, fn_parse_mode, true ) ;
308
302
} else if macros_allowed && self . check_path ( ) {
309
303
// MACRO INVOCATION ITEM
310
304
( Ident :: empty ( ) , ItemKind :: MacCall ( P ( self . parse_item_macro ( vis) ?) ) )
@@ -557,7 +551,7 @@ impl<'a> Parser<'a> {
557
551
attrs : & mut AttrVec ,
558
552
defaultness : Defaultness ,
559
553
) -> PResult < ' a , ItemInfo > {
560
- let unsafety = self . parse_unsafety ( ) ;
554
+ let unsafety = self . parse_unsafety ( false ) ;
561
555
self . expect_keyword ( kw:: Impl ) ?;
562
556
563
557
// First, parse generic parameters if necessary.
@@ -571,7 +565,7 @@ impl<'a> Parser<'a> {
571
565
generics
572
566
} ;
573
567
574
- let constness = self . parse_constness ( ) ;
568
+ let constness = self . parse_constness ( false ) ;
575
569
if let Const :: Yes ( span) = constness {
576
570
self . sess . gated_spans . gate ( sym:: const_trait_impl, span) ;
577
571
}
@@ -815,7 +809,7 @@ impl<'a> Parser<'a> {
815
809
816
810
/// Parses `unsafe? auto? trait Foo { ... }` or `trait Foo = Bar;`.
817
811
fn parse_item_trait ( & mut self , attrs : & mut AttrVec , lo : Span ) -> PResult < ' a , ItemInfo > {
818
- let unsafety = self . parse_unsafety ( ) ;
812
+ let unsafety = self . parse_unsafety ( false ) ;
819
813
// Parse optional `auto` prefix.
820
814
let is_auto = if self . eat_keyword ( kw:: Auto ) { IsAuto :: Yes } else { IsAuto :: No } ;
821
815
@@ -1764,7 +1758,7 @@ impl<'a> Parser<'a> {
1764
1758
let ( ident, is_raw) = self . ident_or_err ( ) ?;
1765
1759
if !is_raw && ident. is_reserved ( ) {
1766
1760
let snapshot = self . create_snapshot_for_diagnostic ( ) ;
1767
- let err = if self . check_fn_front_matter ( false ) {
1761
+ let err = if self . check_fn_front_matter ( false , false ) {
1768
1762
let inherited_vis = Visibility {
1769
1763
span : rustc_span:: DUMMY_SP ,
1770
1764
kind : VisibilityKind :: Inherited ,
@@ -2153,7 +2147,11 @@ impl<'a> Parser<'a> {
2153
2147
///
2154
2148
/// `check_pub` adds additional `pub` to the checks in case users place it
2155
2149
/// wrongly, can be used to ensure `pub` never comes after `default`.
2156
- pub ( super ) fn check_fn_front_matter ( & mut self , check_pub : bool ) -> bool {
2150
+ pub ( super ) fn check_fn_front_matter (
2151
+ & mut self ,
2152
+ check_pub : bool ,
2153
+ kw_case_insensitive : bool ,
2154
+ ) -> bool {
2157
2155
// We use an over-approximation here.
2158
2156
// `const const`, `fn const` won't parse, but we're not stepping over other syntax either.
2159
2157
// `pub` is added in case users got confused with the ordering like `async pub fn`,
@@ -2163,23 +2161,30 @@ impl<'a> Parser<'a> {
2163
2161
} else {
2164
2162
& [ kw:: Const , kw:: Async , kw:: Unsafe , kw:: Extern ]
2165
2163
} ;
2166
- self . check_keyword ( kw:: Fn ) // Definitely an `fn`.
2164
+ self . check_keyword_case ( kw:: Fn , kw_case_insensitive ) // Definitely an `fn`.
2167
2165
// `$qual fn` or `$qual $qual`:
2168
- || quals. iter ( ) . any ( |& kw| self . check_keyword ( kw) )
2166
+ || quals. iter ( ) . any ( |& kw| self . check_keyword_case ( kw, kw_case_insensitive ) )
2169
2167
&& self . look_ahead ( 1 , |t| {
2170
2168
// `$qual fn`, e.g. `const fn` or `async fn`.
2171
- t. is_keyword ( kw:: Fn )
2169
+ t. is_keyword_case ( kw:: Fn , kw_case_insensitive )
2172
2170
// Two qualifiers `$qual $qual` is enough, e.g. `async unsafe`.
2173
- || t. is_non_raw_ident_where ( |i| quals. contains ( & i. name )
2174
- // Rule out 2015 `const async: T = val`.
2175
- && i. is_reserved ( )
2171
+ || (
2172
+ (
2173
+ t. is_non_raw_ident_where ( |i|
2174
+ quals. contains ( & i. name )
2175
+ // Rule out 2015 `const async: T = val`.
2176
+ && i. is_reserved ( )
2177
+ )
2178
+ || kw_case_insensitive
2179
+ && t. is_non_raw_ident_where ( |i| quals. iter ( ) . any ( |qual| qual. as_str ( ) == i. name . as_str ( ) . to_lowercase ( ) ) )
2180
+ )
2176
2181
// Rule out unsafe extern block.
2177
2182
&& !self . is_unsafe_foreign_mod ( ) )
2178
2183
} )
2179
2184
// `extern ABI fn`
2180
- || self . check_keyword ( kw:: Extern )
2185
+ || self . check_keyword_case ( kw:: Extern , kw_case_insensitive )
2181
2186
&& self . look_ahead ( 1 , |t| t. can_begin_literal_maybe_minus ( ) )
2182
- && self . look_ahead ( 2 , |t| t. is_keyword ( kw:: Fn ) )
2187
+ && self . look_ahead ( 2 , |t| t. is_keyword_case ( kw:: Fn , kw_case_insensitive ) )
2183
2188
}
2184
2189
2185
2190
/// Parses all the "front matter" (or "qualifiers") for a `fn` declaration,
@@ -2195,22 +2200,22 @@ impl<'a> Parser<'a> {
2195
2200
/// `Visibility::Inherited` when no visibility is known.
2196
2201
pub ( super ) fn parse_fn_front_matter ( & mut self , orig_vis : & Visibility ) -> PResult < ' a , FnHeader > {
2197
2202
let sp_start = self . token . span ;
2198
- let constness = self . parse_constness ( ) ;
2203
+ let constness = self . parse_constness ( true ) ;
2199
2204
2200
2205
let async_start_sp = self . token . span ;
2201
- let asyncness = self . parse_asyncness ( ) ;
2206
+ let asyncness = self . parse_asyncness ( true ) ;
2202
2207
2203
2208
let unsafe_start_sp = self . token . span ;
2204
- let unsafety = self . parse_unsafety ( ) ;
2209
+ let unsafety = self . parse_unsafety ( true ) ;
2205
2210
2206
2211
let ext_start_sp = self . token . span ;
2207
- let ext = self . parse_extern ( ) ;
2212
+ let ext = self . parse_extern ( true ) ;
2208
2213
2209
2214
if let Async :: Yes { span, .. } = asyncness {
2210
2215
self . ban_async_in_2015 ( span) ;
2211
2216
}
2212
2217
2213
- if !self . eat_keyword ( kw:: Fn ) {
2218
+ if !self . eat_keyword_case ( kw:: Fn , true ) {
2214
2219
// It is possible for `expect_one_of` to recover given the contents of
2215
2220
// `self.expected_tokens`, therefore, do not use `self.unexpected()` which doesn't
2216
2221
// account for this.
0 commit comments