diff --git a/compiler/rustc_parse/src/errors.rs b/compiler/rustc_parse/src/errors.rs index a0b6321a6c43..2d9e0a857bf5 100644 --- a/compiler/rustc_parse/src/errors.rs +++ b/compiler/rustc_parse/src/errors.rs @@ -4639,3 +4639,28 @@ pub(crate) struct ReservedMultihashLint { )] pub suggestion: Span, } + +#[derive(Subdiagnostic)] +#[suggestion( + "if you meant to write a path, use a double colon:", + code = "::", + applicability = "maybe-incorrect" +)] +pub(crate) struct UseDoubleColonSuggestion { + #[primary_span] + pub colon: Span, +} + +#[derive(Subdiagnostic)] +#[multipart_suggestion( + "if you meant to create a regular struct, use curly braces:", + applicability = "maybe-incorrect" +)] +pub(crate) struct UseRegularStructSuggestion { + #[suggestion_part(code = "{{")] + pub open: Span, + #[suggestion_part(code = "}}")] + pub close: Span, + #[suggestion_part(code = "")] + pub semicolon: Option, +} diff --git a/compiler/rustc_parse/src/parser/item.rs b/compiler/rustc_parse/src/parser/item.rs index bed03800d3f4..3f6429c6a60f 100644 --- a/compiler/rustc_parse/src/parser/item.rs +++ b/compiler/rustc_parse/src/parser/item.rs @@ -23,7 +23,10 @@ AllowConstBlockItems, AttrWrapper, ExpKeywordPair, ExpTokenPair, FollowedByType, ForceCollect, Parser, PathStyle, Recovered, Trailing, UsePreAttrPos, }; -use crate::errors::{self, FnPointerCannotBeAsync, FnPointerCannotBeConst, MacroExpandsToAdtField}; +use crate::errors::{ + self, FnPointerCannotBeAsync, FnPointerCannotBeConst, MacroExpandsToAdtField, + UseDoubleColonSuggestion, UseRegularStructSuggestion, +}; use crate::exp; impl<'a> Parser<'a> { @@ -2084,10 +2087,11 @@ fn parse_unsafe_field(&mut self) -> Safety { Safety::Default } } - + /// This is the case where we find `struct Foo(T) where T: Copy;` + /// Unit like structs are handled in parse_item_struct function pub(super) fn parse_tuple_struct_body(&mut self) -> PResult<'a, ThinVec> { - // This is the case where we find `struct Foo(T) where T: Copy;` - // Unit like structs are handled in parse_item_struct function + let openparen_span = self.token.span; + let mut encountered_colon = false; self.parse_paren_comma_seq(|p| { let attrs = p.parse_outer_attributes()?; p.collect_tokens(None, attrs, ForceCollect::No, |p, attrs| { @@ -2109,6 +2113,8 @@ pub(super) fn parse_tuple_struct_body(&mut self) -> PResult<'a, ThinVec PResult<'a, ThinVec $DIR/field-name-in-tuple-struct.rs:3:13 + | +LL | struct Foo(a:u8,b:u8); + | ^ expected one of 7 possible tokens + | +help: if you meant to write a path, use a double colon: + | +LL | struct Foo(a::u8,b:u8); + | + +help: if you meant to create a regular struct, use curly braces: + | +LL - struct Foo(a:u8,b:u8); +LL + struct Foo{a:u8,b:u8} + | + +error: aborting due to 1 previous error +