From bdc317786856c3a49dfa9a35a25037ce1872440e Mon Sep 17 00:00:00 2001 From: Takayuki Maeda Date: Fri, 11 Mar 2022 16:15:57 +0900 Subject: [PATCH 1/2] suggest using double colon when using single colon in struct field type path --- compiler/rustc_parse/src/parser/item.rs | 10 ++++++ ...turct-field-type-including-single-colon.rs | 20 +++++++++++ ...t-field-type-including-single-colon.stderr | 36 +++++++++++++++++++ 3 files changed, 66 insertions(+) create mode 100644 src/test/ui/suggestions/sturct-field-type-including-single-colon.rs create mode 100644 src/test/ui/suggestions/sturct-field-type-including-single-colon.stderr diff --git a/compiler/rustc_parse/src/parser/item.rs b/compiler/rustc_parse/src/parser/item.rs index 5db1e4e0523ff..423ce7c354c3f 100644 --- a/compiler/rustc_parse/src/parser/item.rs +++ b/compiler/rustc_parse/src/parser/item.rs @@ -1534,6 +1534,16 @@ impl<'a> Parser<'a> { let name = self.parse_field_ident(adt_ty, lo)?; self.expect_field_ty_separator()?; let ty = self.parse_ty()?; + if self.token.kind == token::Colon && self.look_ahead(1, |tok| tok.kind != token::Colon) { + self.struct_span_err(self.token.span, "found single colon in a struct field type path") + .span_suggestion_verbose( + self.token.span, + "maybe you meant to write a path separator here", + "::".to_string(), + Applicability::MaybeIncorrect, + ) + .emit(); + } if self.token.kind == token::Eq { self.bump(); let const_expr = self.parse_anon_const_expr()?; diff --git a/src/test/ui/suggestions/sturct-field-type-including-single-colon.rs b/src/test/ui/suggestions/sturct-field-type-including-single-colon.rs new file mode 100644 index 0000000000000..b7ad6d996f1ab --- /dev/null +++ b/src/test/ui/suggestions/sturct-field-type-including-single-colon.rs @@ -0,0 +1,20 @@ +mod foo { + struct A; + mod bar { + struct B; + } +} + +struct Foo { + a: foo:A, + //~^ ERROR found single colon in a struct field type path + //~| expected `,`, or `}`, found `:` +} + +struct Bar { + b: foo::bar:B, + //~^ ERROR found single colon in a struct field type path + //~| expected `,`, or `}`, found `:` +} + +fn main() {} diff --git a/src/test/ui/suggestions/sturct-field-type-including-single-colon.stderr b/src/test/ui/suggestions/sturct-field-type-including-single-colon.stderr new file mode 100644 index 0000000000000..7566ca23472a9 --- /dev/null +++ b/src/test/ui/suggestions/sturct-field-type-including-single-colon.stderr @@ -0,0 +1,36 @@ +error: found single colon in a struct field type path + --> $DIR/sturct-field-type-including-single-colon.rs:9:11 + | +LL | a: foo:A, + | ^ + | +help: maybe you meant to write a path separator here + | +LL | a: foo::A, + | ~~ + +error: expected `,`, or `}`, found `:` + --> $DIR/sturct-field-type-including-single-colon.rs:9:11 + | +LL | a: foo:A, + | ^ + +error: found single colon in a struct field type path + --> $DIR/sturct-field-type-including-single-colon.rs:15:16 + | +LL | b: foo::bar:B, + | ^ + | +help: maybe you meant to write a path separator here + | +LL | b: foo::bar::B, + | ~~ + +error: expected `,`, or `}`, found `:` + --> $DIR/sturct-field-type-including-single-colon.rs:15:16 + | +LL | b: foo::bar:B, + | ^ + +error: aborting due to 4 previous errors + From 813f00dd4fa52eda78bf6b96a3f7bdd82e0d82ae Mon Sep 17 00:00:00 2001 From: Takayuki Maeda Date: Fri, 11 Mar 2022 21:26:06 +0900 Subject: [PATCH 2/2] fix a suggestion message --- compiler/rustc_parse/src/parser/item.rs | 2 +- ...s => struct-field-type-including-single-colon.rs} | 0 ... struct-field-type-including-single-colon.stderr} | 12 ++++++------ 3 files changed, 7 insertions(+), 7 deletions(-) rename src/test/ui/suggestions/{sturct-field-type-including-single-colon.rs => struct-field-type-including-single-colon.rs} (100%) rename src/test/ui/suggestions/{sturct-field-type-including-single-colon.stderr => struct-field-type-including-single-colon.stderr} (60%) diff --git a/compiler/rustc_parse/src/parser/item.rs b/compiler/rustc_parse/src/parser/item.rs index 423ce7c354c3f..ec89301c1e22e 100644 --- a/compiler/rustc_parse/src/parser/item.rs +++ b/compiler/rustc_parse/src/parser/item.rs @@ -1538,7 +1538,7 @@ impl<'a> Parser<'a> { self.struct_span_err(self.token.span, "found single colon in a struct field type path") .span_suggestion_verbose( self.token.span, - "maybe you meant to write a path separator here", + "write a path separator here", "::".to_string(), Applicability::MaybeIncorrect, ) diff --git a/src/test/ui/suggestions/sturct-field-type-including-single-colon.rs b/src/test/ui/suggestions/struct-field-type-including-single-colon.rs similarity index 100% rename from src/test/ui/suggestions/sturct-field-type-including-single-colon.rs rename to src/test/ui/suggestions/struct-field-type-including-single-colon.rs diff --git a/src/test/ui/suggestions/sturct-field-type-including-single-colon.stderr b/src/test/ui/suggestions/struct-field-type-including-single-colon.stderr similarity index 60% rename from src/test/ui/suggestions/sturct-field-type-including-single-colon.stderr rename to src/test/ui/suggestions/struct-field-type-including-single-colon.stderr index 7566ca23472a9..189759d64fc4e 100644 --- a/src/test/ui/suggestions/sturct-field-type-including-single-colon.stderr +++ b/src/test/ui/suggestions/struct-field-type-including-single-colon.stderr @@ -1,33 +1,33 @@ error: found single colon in a struct field type path - --> $DIR/sturct-field-type-including-single-colon.rs:9:11 + --> $DIR/struct-field-type-including-single-colon.rs:9:11 | LL | a: foo:A, | ^ | -help: maybe you meant to write a path separator here +help: write a path separator here | LL | a: foo::A, | ~~ error: expected `,`, or `}`, found `:` - --> $DIR/sturct-field-type-including-single-colon.rs:9:11 + --> $DIR/struct-field-type-including-single-colon.rs:9:11 | LL | a: foo:A, | ^ error: found single colon in a struct field type path - --> $DIR/sturct-field-type-including-single-colon.rs:15:16 + --> $DIR/struct-field-type-including-single-colon.rs:15:16 | LL | b: foo::bar:B, | ^ | -help: maybe you meant to write a path separator here +help: write a path separator here | LL | b: foo::bar::B, | ~~ error: expected `,`, or `}`, found `:` - --> $DIR/sturct-field-type-including-single-colon.rs:15:16 + --> $DIR/struct-field-type-including-single-colon.rs:15:16 | LL | b: foo::bar:B, | ^