@@ -43,6 +43,15 @@ impl<'a> Parser<'a> {
43
43
fn parse_ty_param ( & mut self , preceding_attrs : AttrVec ) -> PResult < ' a , GenericParam > {
44
44
let ident = self . parse_ident ( ) ?;
45
45
46
+ // We might have a typo'd `Const` that was parsed as a type parameter.
47
+ if self . may_recover ( )
48
+ && ident. name . as_str ( ) . to_ascii_lowercase ( ) == kw:: Const . as_str ( )
49
+ && self . check_ident ( )
50
+ // `Const` followed by IDENT
51
+ {
52
+ return Ok ( self . recover_const_param_with_mistyped_const ( preceding_attrs, ident) ?) ;
53
+ }
54
+
46
55
// Parse optional colon and param bounds.
47
56
let mut colon_span = None ;
48
57
let bounds = if self . eat ( & token:: Colon ) {
@@ -120,6 +129,41 @@ impl<'a> Parser<'a> {
120
129
} )
121
130
}
122
131
132
+ pub ( crate ) fn recover_const_param_with_mistyped_const (
133
+ & mut self ,
134
+ preceding_attrs : AttrVec ,
135
+ mistyped_const_ident : Ident ,
136
+ ) -> PResult < ' a , GenericParam > {
137
+ let ident = self . parse_ident ( ) ?;
138
+ self . expect ( & token:: Colon ) ?;
139
+ let ty = self . parse_ty ( ) ?;
140
+
141
+ // Parse optional const generics default value.
142
+ let default = if self . eat ( & token:: Eq ) { Some ( self . parse_const_arg ( ) ?) } else { None } ;
143
+
144
+ let mut err = self . struct_span_err (
145
+ mistyped_const_ident. span ,
146
+ format ! ( "`const` keyword was mistyped as `{}`" , mistyped_const_ident. as_str( ) ) ,
147
+ ) ;
148
+ err. span_suggestion_verbose (
149
+ mistyped_const_ident. span ,
150
+ "use the `const` keyword" ,
151
+ kw:: Const . as_str ( ) ,
152
+ Applicability :: MachineApplicable ,
153
+ ) ;
154
+ err. emit ( ) ;
155
+
156
+ Ok ( GenericParam {
157
+ ident,
158
+ id : ast:: DUMMY_NODE_ID ,
159
+ attrs : preceding_attrs,
160
+ bounds : Vec :: new ( ) ,
161
+ kind : GenericParamKind :: Const { ty, kw_span : mistyped_const_ident. span , default } ,
162
+ is_placeholder : false ,
163
+ colon_span : None ,
164
+ } )
165
+ }
166
+
123
167
/// Parses a (possibly empty) list of lifetime and type parameters, possibly including
124
168
/// a trailing comma and erroneous trailing attributes.
125
169
pub ( super ) fn parse_generic_params ( & mut self ) -> PResult < ' a , ThinVec < ast:: GenericParam > > {
0 commit comments