From 0757d5f83f4bc4faca896c85932cf1e4a0cd6031 Mon Sep 17 00:00:00 2001 From: Xiretza Date: Thu, 6 Oct 2022 18:35:53 +0200 Subject: [PATCH] Fix condition for "missing `struct`" diagnostic on tuple structs The check previously matched this, and suggested adding a missing `struct`: pub Foo(...): It was probably intended to match this instead (semicolon instead of colon): pub Foo(...); --- compiler/rustc_parse/src/parser/item.rs | 2 +- tests/ui/pub/pub-ident-fn-3.rs | 2 +- tests/ui/pub/pub-ident-fn-3.stderr | 9 +++++++-- tests/ui/pub/pub-ident-fn-or-struct-2.rs | 2 +- tests/ui/pub/pub-ident-fn-or-struct-2.stderr | 9 +++++++-- tests/ui/pub/pub-ident-struct-4.fixed | 6 ++++++ tests/ui/pub/pub-ident-struct-4.rs | 6 ++++++ tests/ui/pub/pub-ident-struct-4.stderr | 13 +++++++++++++ 8 files changed, 42 insertions(+), 7 deletions(-) create mode 100644 tests/ui/pub/pub-ident-struct-4.fixed create mode 100644 tests/ui/pub/pub-ident-struct-4.rs create mode 100644 tests/ui/pub/pub-ident-struct-4.stderr diff --git a/compiler/rustc_parse/src/parser/item.rs b/compiler/rustc_parse/src/parser/item.rs index d72c7d8cabc..32ffc9306f2 100644 --- a/compiler/rustc_parse/src/parser/item.rs +++ b/compiler/rustc_parse/src/parser/item.rs @@ -412,7 +412,7 @@ impl<'a> Parser<'a> { } else if self.check(&token::OpenDelim(Delimiter::Brace)) { self.bump(); // `{` ("fn", kw_name, false) - } else if self.check(&token::Colon) { + } else if self.check(&token::Semi) { let kw = "struct"; (kw, kw, false) } else { diff --git a/tests/ui/pub/pub-ident-fn-3.rs b/tests/ui/pub/pub-ident-fn-3.rs index fdbea7cf487..50db4039d4f 100644 --- a/tests/ui/pub/pub-ident-fn-3.rs +++ b/tests/ui/pub/pub-ident-fn-3.rs @@ -2,7 +2,7 @@ mod foo { pub bar(); - //~^ ERROR missing `fn` or `struct` for function or struct definition + //~^ ERROR missing `struct` for struct definition } fn main() {} diff --git a/tests/ui/pub/pub-ident-fn-3.stderr b/tests/ui/pub/pub-ident-fn-3.stderr index 6d3d4e592c8..23eadec1193 100644 --- a/tests/ui/pub/pub-ident-fn-3.stderr +++ b/tests/ui/pub/pub-ident-fn-3.stderr @@ -1,8 +1,13 @@ -error: missing `fn` or `struct` for function or struct definition +error: missing `struct` for struct definition --> $DIR/pub-ident-fn-3.rs:4:8 | LL | pub bar(); - | ---^--- help: if you meant to call a macro, try: `bar!` + | ^ + | +help: add `struct` here to parse `bar` as a public struct + | +LL | pub struct bar(); + | ++++++ error: aborting due to previous error diff --git a/tests/ui/pub/pub-ident-fn-or-struct-2.rs b/tests/ui/pub/pub-ident-fn-or-struct-2.rs index 8f67cdd2933..dfa6cf2ee1e 100644 --- a/tests/ui/pub/pub-ident-fn-or-struct-2.rs +++ b/tests/ui/pub/pub-ident-fn-or-struct-2.rs @@ -1,4 +1,4 @@ pub S(); -//~^ ERROR missing `fn` or `struct` for function or struct definition +//~^ ERROR missing `struct` for struct definition fn main() {} diff --git a/tests/ui/pub/pub-ident-fn-or-struct-2.stderr b/tests/ui/pub/pub-ident-fn-or-struct-2.stderr index 047e66b18d8..b7a9bc0a7db 100644 --- a/tests/ui/pub/pub-ident-fn-or-struct-2.stderr +++ b/tests/ui/pub/pub-ident-fn-or-struct-2.stderr @@ -1,8 +1,13 @@ -error: missing `fn` or `struct` for function or struct definition +error: missing `struct` for struct definition --> $DIR/pub-ident-fn-or-struct-2.rs:1:4 | LL | pub S(); - | ---^- help: if you meant to call a macro, try: `S!` + | ^ + | +help: add `struct` here to parse `S` as a public struct + | +LL | pub struct S(); + | ++++++ error: aborting due to previous error diff --git a/tests/ui/pub/pub-ident-struct-4.fixed b/tests/ui/pub/pub-ident-struct-4.fixed new file mode 100644 index 00000000000..b49fa678e1b --- /dev/null +++ b/tests/ui/pub/pub-ident-struct-4.fixed @@ -0,0 +1,6 @@ +// run-rustfix + +pub struct T(String); +//~^ ERROR missing `struct` for struct definition + +fn main() {} diff --git a/tests/ui/pub/pub-ident-struct-4.rs b/tests/ui/pub/pub-ident-struct-4.rs new file mode 100644 index 00000000000..20bc94b0acb --- /dev/null +++ b/tests/ui/pub/pub-ident-struct-4.rs @@ -0,0 +1,6 @@ +// run-rustfix + +pub T(String); +//~^ ERROR missing `struct` for struct definition + +fn main() {} diff --git a/tests/ui/pub/pub-ident-struct-4.stderr b/tests/ui/pub/pub-ident-struct-4.stderr new file mode 100644 index 00000000000..90c7138e5ce --- /dev/null +++ b/tests/ui/pub/pub-ident-struct-4.stderr @@ -0,0 +1,13 @@ +error: missing `struct` for struct definition + --> $DIR/pub-ident-struct-4.rs:3:4 + | +LL | pub T(String); + | ^ + | +help: add `struct` here to parse `T` as a public struct + | +LL | pub struct T(String); + | ++++++ + +error: aborting due to previous error +