diff options
| author | Maybe Waffle <waffle.lapkin@gmail.com> | 2022-08-05 15:41:42 +0400 |
|---|---|---|
| committer | Maybe Waffle <waffle.lapkin@gmail.com> | 2022-08-05 16:19:28 +0400 |
| commit | 743ad07c4b267bc5806a455b51ff07b3012591cc (patch) | |
| tree | 40f86b5588e7a49c9fb7ed4d8c603c7f8bbd9352 /compiler | |
| parent | 1f5d8d49eb6111931091f700d07518cd2b80bc18 (diff) | |
| download | rust-743ad07c4b267bc5806a455b51ff07b3012591cc.tar.gz rust-743ad07c4b267bc5806a455b51ff07b3012591cc.zip | |
Improve diagnostics for `const a: = expr;`
Diffstat (limited to 'compiler')
| -rw-r--r-- | compiler/rustc_parse/src/parser/item.rs | 30 | ||||
| -rw-r--r-- | compiler/rustc_typeck/src/collect/type_of.rs | 5 |
2 files changed, 23 insertions, 12 deletions
diff --git a/compiler/rustc_parse/src/parser/item.rs b/compiler/rustc_parse/src/parser/item.rs index 2c1e5807aa7..0d175c5c893 100644 --- a/compiler/rustc_parse/src/parser/item.rs +++ b/compiler/rustc_parse/src/parser/item.rs @@ -1179,10 +1179,11 @@ impl<'a> Parser<'a> { // Parse the type of a `const` or `static mut?` item. // That is, the `":" $ty` fragment. - let ty = if self.eat(&token::Colon) { - self.parse_ty()? - } else { - self.recover_missing_const_type(id, m) + let ty = match (self.eat(&token::Colon), self.check(&token::Eq) | self.check(&token::Semi)) + { + // If there wasn't a `:` or the colon was followed by a `=` or `;` recover a missing type. + (true, false) => self.parse_ty()?, + (colon, _) => self.recover_missing_const_type(colon, m), }; let expr = if self.eat(&token::Eq) { Some(self.parse_expr()?) } else { None }; @@ -1190,9 +1191,9 @@ impl<'a> Parser<'a> { Ok((id, ty, expr)) } - /// We were supposed to parse `:` but the `:` was missing. + /// We were supposed to parse `":" $ty` but the `:` or the type was missing. /// This means that the type is missing. - fn recover_missing_const_type(&mut self, id: Ident, m: Option<Mutability>) -> P<Ty> { + fn recover_missing_const_type(&mut self, colon_present: bool, m: Option<Mutability>) -> P<Ty> { // Construct the error and stash it away with the hope // that typeck will later enrich the error with a type. let kind = match m { @@ -1200,18 +1201,25 @@ impl<'a> Parser<'a> { Some(Mutability::Not) => "static", None => "const", }; - let mut err = self.struct_span_err(id.span, &format!("missing type for `{kind}` item")); + + let colon = match colon_present { + true => "", + false => ":", + }; + + let span = self.prev_token.span.shrink_to_hi(); + let mut err = self.struct_span_err(span, &format!("missing type for `{kind}` item")); err.span_suggestion( - id.span, + span, "provide a type for the item", - format!("{id}: <type>"), + format!("{colon} <type>"), Applicability::HasPlaceholders, ); - err.stash(id.span, StashKey::ItemNoType); + err.stash(span, StashKey::ItemNoType); // The user intended that the type be inferred, // so treat this as if the user wrote e.g. `const A: _ = expr;`. - P(Ty { kind: TyKind::Infer, span: id.span, id: ast::DUMMY_NODE_ID, tokens: None }) + P(Ty { kind: TyKind::Infer, span, id: ast::DUMMY_NODE_ID, tokens: None }) } /// Parses an enum declaration. diff --git a/compiler/rustc_typeck/src/collect/type_of.rs b/compiler/rustc_typeck/src/collect/type_of.rs index 534ddfa9531..f1dbe64f13a 100644 --- a/compiler/rustc_typeck/src/collect/type_of.rs +++ b/compiler/rustc_typeck/src/collect/type_of.rs @@ -801,6 +801,9 @@ fn infer_placeholder_type<'a>( match tcx.sess.diagnostic().steal_diagnostic(span, StashKey::ItemNoType) { Some(mut err) => { if !ty.references_error() { + // Only suggest adding `:` if it was missing (and suggested by parsing diagnostic) + let colon = if span == item_ident.span.shrink_to_hi() { ":" } else { "" }; + // The parser provided a sub-optimal `HasPlaceholders` suggestion for the type. // We are typeck and have the real type, so remove that and suggest the actual type. // FIXME(eddyb) this looks like it should be functionality on `Diagnostic`. @@ -816,7 +819,7 @@ fn infer_placeholder_type<'a>( err.span_suggestion( span, &format!("provide a type for the {item}", item = kind), - format!("{}: {}", item_ident, sugg_ty), + format!("{colon} {sugg_ty}"), Applicability::MachineApplicable, ); } else { |
