@@ -8,6 +8,7 @@ use rustc_ast::ast::*;
8
8
use rustc_ast:: ptr:: P ;
9
9
use rustc_ast:: token:: { self , Delimiter , TokenKind } ;
10
10
use rustc_ast:: tokenstream:: { DelimSpan , TokenStream , TokenTree } ;
11
+ use rustc_ast:: util:: case:: Case ;
11
12
use rustc_ast:: { self as ast, AttrVec , Attribute , DUMMY_NODE_ID } ;
12
13
use rustc_ast:: { Async , Const , Defaultness , IsAuto , Mutability , Unsafe , UseTree , UseTreeKind } ;
13
14
use rustc_ast:: { BindingAnnotation , Block , FnDecl , FnSig , Param , SelfKind } ;
@@ -34,7 +35,7 @@ impl<'a> Parser<'a> {
34
35
35
36
/// Parses a `mod <foo> { ... }` or `mod <foo>;` item.
36
37
fn parse_item_mod ( & mut self , attrs : & mut AttrVec ) -> PResult < ' a , ItemInfo > {
37
- let unsafety = self . parse_unsafety ( ) ;
38
+ let unsafety = self . parse_unsafety ( Case :: Sensitive ) ;
38
39
self . expect_keyword ( kw:: Mod ) ?;
39
40
let id = self . parse_ident ( ) ?;
40
41
let mod_kind = if self . eat ( & token:: Semi ) {
@@ -143,8 +144,15 @@ impl<'a> Parser<'a> {
143
144
let lo = self . token . span ;
144
145
let vis = self . parse_visibility ( FollowedByType :: No ) ?;
145
146
let mut def = self . parse_defaultness ( ) ;
146
- let kind =
147
- self . parse_item_kind ( & mut attrs, mac_allowed, lo, & vis, & mut def, fn_parse_mode) ?;
147
+ let kind = self . parse_item_kind (
148
+ & mut attrs,
149
+ mac_allowed,
150
+ lo,
151
+ & vis,
152
+ & mut def,
153
+ fn_parse_mode,
154
+ Case :: Sensitive ,
155
+ ) ?;
148
156
if let Some ( ( ident, kind) ) = kind {
149
157
self . error_on_unconsumed_default ( def, & kind) ;
150
158
let span = lo. to ( self . prev_token . span ) ;
@@ -205,16 +213,17 @@ impl<'a> Parser<'a> {
205
213
vis : & Visibility ,
206
214
def : & mut Defaultness ,
207
215
fn_parse_mode : FnParseMode ,
216
+ case : Case ,
208
217
) -> PResult < ' a , Option < ItemInfo > > {
209
218
let def_final = def == & Defaultness :: Final ;
210
- let mut def = || mem:: replace ( def, Defaultness :: Final ) ;
219
+ let mut def_ = || mem:: replace ( def, Defaultness :: Final ) ;
211
220
212
- let info = if self . eat_keyword ( kw:: Use ) {
221
+ let info = if self . eat_keyword_case ( kw:: Use , case ) {
213
222
self . parse_use_item ( ) ?
214
- } else if self . check_fn_front_matter ( def_final) {
223
+ } else if self . check_fn_front_matter ( def_final, case ) {
215
224
// FUNCTION ITEM
216
225
let ( ident, sig, generics, body) = self . parse_fn ( attrs, fn_parse_mode, lo, vis) ?;
217
- ( ident, ItemKind :: Fn ( Box :: new ( Fn { defaultness : def ( ) , sig, generics, body } ) ) )
226
+ ( ident, ItemKind :: Fn ( Box :: new ( Fn { defaultness : def_ ( ) , sig, generics, body } ) ) )
218
227
} else if self . eat_keyword ( kw:: Extern ) {
219
228
if self . eat_keyword ( kw:: Crate ) {
220
229
// EXTERN CRATE
@@ -225,7 +234,7 @@ impl<'a> Parser<'a> {
225
234
}
226
235
} else if self . is_unsafe_foreign_mod ( ) {
227
236
// EXTERN BLOCK
228
- let unsafety = self . parse_unsafety ( ) ;
237
+ let unsafety = self . parse_unsafety ( Case :: Sensitive ) ;
229
238
self . expect_keyword ( kw:: Extern ) ?;
230
239
self . parse_item_foreign_mod ( attrs, unsafety) ?
231
240
} else if self . is_static_global ( ) {
@@ -234,15 +243,15 @@ impl<'a> Parser<'a> {
234
243
let m = self . parse_mutability ( ) ;
235
244
let ( ident, ty, expr) = self . parse_item_global ( Some ( m) ) ?;
236
245
( ident, ItemKind :: Static ( ty, m, expr) )
237
- } else if let Const :: Yes ( const_span) = self . parse_constness ( ) {
246
+ } else if let Const :: Yes ( const_span) = self . parse_constness ( Case :: Sensitive ) {
238
247
// CONST ITEM
239
248
if self . token . is_keyword ( kw:: Impl ) {
240
249
// recover from `const impl`, suggest `impl const`
241
- self . recover_const_impl ( const_span, attrs, def ( ) ) ?
250
+ self . recover_const_impl ( const_span, attrs, def_ ( ) ) ?
242
251
} else {
243
252
self . recover_const_mut ( const_span) ;
244
253
let ( ident, ty, expr) = self . parse_item_global ( None ) ?;
245
- ( ident, ItemKind :: Const ( def ( ) , ty, expr) )
254
+ ( ident, ItemKind :: Const ( def_ ( ) , ty, expr) )
246
255
}
247
256
} else if self . check_keyword ( kw:: Trait ) || self . check_auto_or_unsafe_trait_item ( ) {
248
257
// TRAIT ITEM
@@ -251,15 +260,15 @@ impl<'a> Parser<'a> {
251
260
|| self . check_keyword ( kw:: Unsafe ) && self . is_keyword_ahead ( 1 , & [ kw:: Impl ] )
252
261
{
253
262
// IMPL ITEM
254
- self . parse_item_impl ( attrs, def ( ) ) ?
263
+ self . parse_item_impl ( attrs, def_ ( ) ) ?
255
264
} else if self . check_keyword ( kw:: Mod )
256
265
|| self . check_keyword ( kw:: Unsafe ) && self . is_keyword_ahead ( 1 , & [ kw:: Mod ] )
257
266
{
258
267
// MODULE ITEM
259
268
self . parse_item_mod ( attrs) ?
260
269
} else if self . eat_keyword ( kw:: Type ) {
261
270
// TYPE ITEM
262
- self . parse_type_alias ( def ( ) ) ?
271
+ self . parse_type_alias ( def_ ( ) ) ?
263
272
} else if self . eat_keyword ( kw:: Enum ) {
264
273
// ENUM ITEM
265
274
self . parse_item_enum ( ) ?
@@ -286,6 +295,19 @@ impl<'a> Parser<'a> {
286
295
} else if self . isnt_macro_invocation ( ) && vis. kind . is_pub ( ) {
287
296
self . recover_missing_kw_before_item ( ) ?;
288
297
return Ok ( None ) ;
298
+ } else if self . isnt_macro_invocation ( ) && case == Case :: Sensitive {
299
+ _ = def_;
300
+
301
+ // Recover wrong cased keywords
302
+ return self . parse_item_kind (
303
+ attrs,
304
+ macros_allowed,
305
+ lo,
306
+ vis,
307
+ def,
308
+ fn_parse_mode,
309
+ Case :: Insensitive ,
310
+ ) ;
289
311
} else if macros_allowed && self . check_path ( ) {
290
312
// MACRO INVOCATION ITEM
291
313
( Ident :: empty ( ) , ItemKind :: MacCall ( P ( self . parse_item_macro ( vis) ?) ) )
@@ -538,7 +560,7 @@ impl<'a> Parser<'a> {
538
560
attrs : & mut AttrVec ,
539
561
defaultness : Defaultness ,
540
562
) -> PResult < ' a , ItemInfo > {
541
- let unsafety = self . parse_unsafety ( ) ;
563
+ let unsafety = self . parse_unsafety ( Case :: Sensitive ) ;
542
564
self . expect_keyword ( kw:: Impl ) ?;
543
565
544
566
// First, parse generic parameters if necessary.
@@ -552,7 +574,7 @@ impl<'a> Parser<'a> {
552
574
generics
553
575
} ;
554
576
555
- let constness = self . parse_constness ( ) ;
577
+ let constness = self . parse_constness ( Case :: Sensitive ) ;
556
578
if let Const :: Yes ( span) = constness {
557
579
self . sess . gated_spans . gate ( sym:: const_trait_impl, span) ;
558
580
}
@@ -796,7 +818,7 @@ impl<'a> Parser<'a> {
796
818
797
819
/// Parses `unsafe? auto? trait Foo { ... }` or `trait Foo = Bar;`.
798
820
fn parse_item_trait ( & mut self , attrs : & mut AttrVec , lo : Span ) -> PResult < ' a , ItemInfo > {
799
- let unsafety = self . parse_unsafety ( ) ;
821
+ let unsafety = self . parse_unsafety ( Case :: Sensitive ) ;
800
822
// Parse optional `auto` prefix.
801
823
let is_auto = if self . eat_keyword ( kw:: Auto ) { IsAuto :: Yes } else { IsAuto :: No } ;
802
824
@@ -1762,7 +1784,7 @@ impl<'a> Parser<'a> {
1762
1784
let ( ident, is_raw) = self . ident_or_err ( ) ?;
1763
1785
if !is_raw && ident. is_reserved ( ) {
1764
1786
let snapshot = self . create_snapshot_for_diagnostic ( ) ;
1765
- let err = if self . check_fn_front_matter ( false ) {
1787
+ let err = if self . check_fn_front_matter ( false , Case :: Sensitive ) {
1766
1788
let inherited_vis = Visibility {
1767
1789
span : rustc_span:: DUMMY_SP ,
1768
1790
kind : VisibilityKind :: Inherited ,
@@ -2172,7 +2194,7 @@ impl<'a> Parser<'a> {
2172
2194
///
2173
2195
/// `check_pub` adds additional `pub` to the checks in case users place it
2174
2196
/// wrongly, can be used to ensure `pub` never comes after `default`.
2175
- pub ( super ) fn check_fn_front_matter ( & mut self , check_pub : bool ) -> bool {
2197
+ pub ( super ) fn check_fn_front_matter ( & mut self , check_pub : bool , case : Case ) -> bool {
2176
2198
// We use an over-approximation here.
2177
2199
// `const const`, `fn const` won't parse, but we're not stepping over other syntax either.
2178
2200
// `pub` is added in case users got confused with the ordering like `async pub fn`,
@@ -2182,23 +2204,30 @@ impl<'a> Parser<'a> {
2182
2204
} else {
2183
2205
& [ kw:: Const , kw:: Async , kw:: Unsafe , kw:: Extern ]
2184
2206
} ;
2185
- self . check_keyword ( kw:: Fn ) // Definitely an `fn`.
2207
+ self . check_keyword_case ( kw:: Fn , case ) // Definitely an `fn`.
2186
2208
// `$qual fn` or `$qual $qual`:
2187
- || quals. iter ( ) . any ( |& kw| self . check_keyword ( kw) )
2209
+ || quals. iter ( ) . any ( |& kw| self . check_keyword_case ( kw, case ) )
2188
2210
&& self . look_ahead ( 1 , |t| {
2189
2211
// `$qual fn`, e.g. `const fn` or `async fn`.
2190
- t. is_keyword ( kw:: Fn )
2212
+ t. is_keyword_case ( kw:: Fn , case )
2191
2213
// Two qualifiers `$qual $qual` is enough, e.g. `async unsafe`.
2192
- || t. is_non_raw_ident_where ( |i| quals. contains ( & i. name )
2193
- // Rule out 2015 `const async: T = val`.
2194
- && i. is_reserved ( )
2214
+ || (
2215
+ (
2216
+ t. is_non_raw_ident_where ( |i|
2217
+ quals. contains ( & i. name )
2218
+ // Rule out 2015 `const async: T = val`.
2219
+ && i. is_reserved ( )
2220
+ )
2221
+ || case == Case :: Insensitive
2222
+ && t. is_non_raw_ident_where ( |i| quals. iter ( ) . any ( |qual| qual. as_str ( ) == i. name . as_str ( ) . to_lowercase ( ) ) )
2223
+ )
2195
2224
// Rule out unsafe extern block.
2196
2225
&& !self . is_unsafe_foreign_mod ( ) )
2197
2226
} )
2198
2227
// `extern ABI fn`
2199
- || self . check_keyword ( kw:: Extern )
2228
+ || self . check_keyword_case ( kw:: Extern , case )
2200
2229
&& self . look_ahead ( 1 , |t| t. can_begin_literal_maybe_minus ( ) )
2201
- && self . look_ahead ( 2 , |t| t. is_keyword ( kw:: Fn ) )
2230
+ && self . look_ahead ( 2 , |t| t. is_keyword_case ( kw:: Fn , case ) )
2202
2231
}
2203
2232
2204
2233
/// Parses all the "front matter" (or "qualifiers") for a `fn` declaration,
@@ -2214,22 +2243,22 @@ impl<'a> Parser<'a> {
2214
2243
/// `Visibility::Inherited` when no visibility is known.
2215
2244
pub ( super ) fn parse_fn_front_matter ( & mut self , orig_vis : & Visibility ) -> PResult < ' a , FnHeader > {
2216
2245
let sp_start = self . token . span ;
2217
- let constness = self . parse_constness ( ) ;
2246
+ let constness = self . parse_constness ( Case :: Insensitive ) ;
2218
2247
2219
2248
let async_start_sp = self . token . span ;
2220
- let asyncness = self . parse_asyncness ( ) ;
2249
+ let asyncness = self . parse_asyncness ( Case :: Insensitive ) ;
2221
2250
2222
2251
let unsafe_start_sp = self . token . span ;
2223
- let unsafety = self . parse_unsafety ( ) ;
2252
+ let unsafety = self . parse_unsafety ( Case :: Insensitive ) ;
2224
2253
2225
2254
let ext_start_sp = self . token . span ;
2226
- let ext = self . parse_extern ( ) ;
2255
+ let ext = self . parse_extern ( Case :: Insensitive ) ;
2227
2256
2228
2257
if let Async :: Yes { span, .. } = asyncness {
2229
2258
self . ban_async_in_2015 ( span) ;
2230
2259
}
2231
2260
2232
- if !self . eat_keyword ( kw:: Fn ) {
2261
+ if !self . eat_keyword_case ( kw:: Fn , Case :: Insensitive ) {
2233
2262
// It is possible for `expect_one_of` to recover given the contents of
2234
2263
// `self.expected_tokens`, therefore, do not use `self.unexpected()` which doesn't
2235
2264
// account for this.
0 commit comments