@@ -1179,39 +1179,47 @@ impl<'a> Parser<'a> {
1179
1179
1180
1180
// Parse the type of a `const` or `static mut?` item.
1181
1181
// That is, the `":" $ty` fragment.
1182
- let ty = if self . eat ( & token:: Colon ) {
1183
- self . parse_ty ( ) ?
1184
- } else {
1185
- self . recover_missing_const_type ( id, m)
1182
+ let ty = match ( self . eat ( & token:: Colon ) , self . check ( & token:: Eq ) | self . check ( & token:: Semi ) )
1183
+ {
1184
+ // If there wasn't a `:` or the colon was followed by a `=` or `;` recover a missing type.
1185
+ ( true , false ) => self . parse_ty ( ) ?,
1186
+ ( colon, _) => self . recover_missing_const_type ( colon, m) ,
1186
1187
} ;
1187
1188
1188
1189
let expr = if self . eat ( & token:: Eq ) { Some ( self . parse_expr ( ) ?) } else { None } ;
1189
1190
self . expect_semi ( ) ?;
1190
1191
Ok ( ( id, ty, expr) )
1191
1192
}
1192
1193
1193
- /// We were supposed to parse `: ` but the `:` was missing.
1194
+ /// We were supposed to parse `":" $ty ` but the `:` or the type was missing.
1194
1195
/// This means that the type is missing.
1195
- fn recover_missing_const_type ( & mut self , id : Ident , m : Option < Mutability > ) -> P < Ty > {
1196
+ fn recover_missing_const_type ( & mut self , colon_present : bool , m : Option < Mutability > ) -> P < Ty > {
1196
1197
// Construct the error and stash it away with the hope
1197
1198
// that typeck will later enrich the error with a type.
1198
1199
let kind = match m {
1199
1200
Some ( Mutability :: Mut ) => "static mut" ,
1200
1201
Some ( Mutability :: Not ) => "static" ,
1201
1202
None => "const" ,
1202
1203
} ;
1203
- let mut err = self . struct_span_err ( id. span , & format ! ( "missing type for `{kind}` item" ) ) ;
1204
+
1205
+ let colon = match colon_present {
1206
+ true => "" ,
1207
+ false => ":" ,
1208
+ } ;
1209
+
1210
+ let span = self . prev_token . span . shrink_to_hi ( ) ;
1211
+ let mut err = self . struct_span_err ( span, & format ! ( "missing type for `{kind}` item" ) ) ;
1204
1212
err. span_suggestion (
1205
- id . span ,
1213
+ span,
1206
1214
"provide a type for the item" ,
1207
- format ! ( "{id}: <type>" ) ,
1215
+ format ! ( "{colon} <type>" ) ,
1208
1216
Applicability :: HasPlaceholders ,
1209
1217
) ;
1210
- err. stash ( id . span , StashKey :: ItemNoType ) ;
1218
+ err. stash ( span, StashKey :: ItemNoType ) ;
1211
1219
1212
1220
// The user intended that the type be inferred,
1213
1221
// so treat this as if the user wrote e.g. `const A: _ = expr;`.
1214
- P ( Ty { kind : TyKind :: Infer , span : id . span , id : ast:: DUMMY_NODE_ID , tokens : None } )
1222
+ P ( Ty { kind : TyKind :: Infer , span, id : ast:: DUMMY_NODE_ID , tokens : None } )
1215
1223
}
1216
1224
1217
1225
/// Parses an enum declaration.
0 commit comments