@@ -1179,39 +1179,47 @@ impl<'a> Parser<'a> {
11791179
11801180 // Parse the type of a `const` or `static mut?` item.
11811181 // 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) ,
11861187 } ;
11871188
11881189 let expr = if self . eat ( & token:: Eq ) { Some ( self . parse_expr ( ) ?) } else { None } ;
11891190 self . expect_semi ( ) ?;
11901191 Ok ( ( id, ty, expr) )
11911192 }
11921193
1193- /// We were supposed to parse `: ` but the `:` was missing.
1194+ /// We were supposed to parse `":" $ty ` but the `:` or the type was missing.
11941195 /// 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 > {
11961197 // Construct the error and stash it away with the hope
11971198 // that typeck will later enrich the error with a type.
11981199 let kind = match m {
11991200 Some ( Mutability :: Mut ) => "static mut" ,
12001201 Some ( Mutability :: Not ) => "static" ,
12011202 None => "const" ,
12021203 } ;
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" ) ) ;
12041212 err. span_suggestion (
1205- id . span ,
1213+ span,
12061214 "provide a type for the item" ,
1207- format ! ( "{id}: <type>" ) ,
1215+ format ! ( "{colon} <type>" ) ,
12081216 Applicability :: HasPlaceholders ,
12091217 ) ;
1210- err. stash ( id . span , StashKey :: ItemNoType ) ;
1218+ err. stash ( span, StashKey :: ItemNoType ) ;
12111219
12121220 // The user intended that the type be inferred,
12131221 // 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 } )
12151223 }
12161224
12171225 /// Parses an enum declaration.
0 commit comments