about summary refs log tree commit diff
diff options
context:
space:
mode:
authorYuki Okushi <jtitor@2k36.org>2022-08-25 08:50:56 +0900
committerGitHub <noreply@github.com>2022-08-25 08:50:56 +0900
commited8cfc86a2a6541f8f998ab45e889fd3f2371cbe (patch)
treec4f06237465b59fa226b8163134cd4a80f032e9b
parentb3f178350a44095e71306789da441ed785fae28c (diff)
parent351acc87f57d05847e95b1863a9c5c7e953e162c (diff)
downloadrust-ed8cfc86a2a6541f8f998ab45e889fd3f2371cbe.tar.gz
rust-ed8cfc86a2a6541f8f998ab45e889fd3f2371cbe.zip
Rollup merge of #100188 - chenyukang:fix-issue-100165, r=estebank
Parser will not suggest invalid expression when use public

Fixes #100165
-rw-r--r--compiler/rustc_parse/src/parser/diagnostics.rs24
-rw-r--r--compiler/rustc_span/src/symbol.rs1
-rw-r--r--src/test/ui/parser/public-instead-of-pub-1.fixed11
-rw-r--r--src/test/ui/parser/public-instead-of-pub-1.rs11
-rw-r--r--src/test/ui/parser/public-instead-of-pub-1.stderr13
-rw-r--r--src/test/ui/parser/public-instead-of-pub-2.rs7
-rw-r--r--src/test/ui/parser/public-instead-of-pub-2.stderr8
7 files changed, 65 insertions, 10 deletions
diff --git a/compiler/rustc_parse/src/parser/diagnostics.rs b/compiler/rustc_parse/src/parser/diagnostics.rs
index b8884dd32d6..f2d79ce756c 100644
--- a/compiler/rustc_parse/src/parser/diagnostics.rs
+++ b/compiler/rustc_parse/src/parser/diagnostics.rs
@@ -22,7 +22,7 @@ use rustc_errors::{
 use rustc_errors::{pluralize, struct_span_err, Diagnostic, ErrorGuaranteed};
 use rustc_macros::{SessionDiagnostic, SessionSubdiagnostic};
 use rustc_span::source_map::Spanned;
-use rustc_span::symbol::{kw, Ident};
+use rustc_span::symbol::{kw, sym, Ident};
 use rustc_span::{Span, SpanSnippetError, DUMMY_SP};
 use std::ops::{Deref, DerefMut};
 
@@ -977,15 +977,6 @@ impl<'a> Parser<'a> {
         let mut err = self.struct_span_err(self.token.span, &msg_exp);
 
         if let TokenKind::Ident(symbol, _) = &self.prev_token.kind {
-            if symbol.as_str() == "public" {
-                err.span_suggestion_short(
-                    self.prev_token.span,
-                    "write `pub` instead of `public` to make the item public",
-                    "pub",
-                    appl,
-                );
-            }
-
             if ["def", "fun", "func", "function"].contains(&symbol.as_str()) {
                 err.span_suggestion_short(
                     self.prev_token.span,
@@ -996,6 +987,19 @@ impl<'a> Parser<'a> {
             }
         }
 
+        // `pub` may be used for an item or `pub(crate)`
+        if self.prev_token.is_ident_named(sym::public)
+            && (self.token.can_begin_item()
+                || self.token.kind == TokenKind::OpenDelim(Delimiter::Parenthesis))
+        {
+            err.span_suggestion_short(
+                self.prev_token.span,
+                "write `pub` instead of `public` to make the item public",
+                "pub",
+                appl,
+            );
+        }
+
         // Add suggestion for a missing closing angle bracket if '>' is included in expected_tokens
         // there are unclosed angle brackets
         if self.unmatched_angle_bracket_count > 0
diff --git a/compiler/rustc_span/src/symbol.rs b/compiler/rustc_span/src/symbol.rs
index e4bbbbf83cb..5085e3aedfd 100644
--- a/compiler/rustc_span/src/symbol.rs
+++ b/compiler/rustc_span/src/symbol.rs
@@ -1119,6 +1119,7 @@ symbols! {
         ptr_offset_from_unsigned,
         pub_macro_rules,
         pub_restricted,
+        public,
         pure,
         pushpop_unsafe,
         qreg,
diff --git a/src/test/ui/parser/public-instead-of-pub-1.fixed b/src/test/ui/parser/public-instead-of-pub-1.fixed
new file mode 100644
index 00000000000..a4fa68ba5cc
--- /dev/null
+++ b/src/test/ui/parser/public-instead-of-pub-1.fixed
@@ -0,0 +1,11 @@
+// Checks what happens when `public` is used instead of the correct, `pub`
+// run-rustfix
+
+pub enum Test {
+    //~^ ERROR expected one of `!` or `::`, found keyword `enum`
+    //~^^ HELP write `pub` instead of `public` to make the item public
+    A,
+    B,
+}
+
+fn main() { }
diff --git a/src/test/ui/parser/public-instead-of-pub-1.rs b/src/test/ui/parser/public-instead-of-pub-1.rs
new file mode 100644
index 00000000000..43565c9b1d2
--- /dev/null
+++ b/src/test/ui/parser/public-instead-of-pub-1.rs
@@ -0,0 +1,11 @@
+// Checks what happens when `public` is used instead of the correct, `pub`
+// run-rustfix
+
+public enum Test {
+    //~^ ERROR expected one of `!` or `::`, found keyword `enum`
+    //~^^ HELP write `pub` instead of `public` to make the item public
+    A,
+    B,
+}
+
+fn main() { }
diff --git a/src/test/ui/parser/public-instead-of-pub-1.stderr b/src/test/ui/parser/public-instead-of-pub-1.stderr
new file mode 100644
index 00000000000..795a5bcf5df
--- /dev/null
+++ b/src/test/ui/parser/public-instead-of-pub-1.stderr
@@ -0,0 +1,13 @@
+error: expected one of `!` or `::`, found keyword `enum`
+  --> $DIR/public-instead-of-pub-1.rs:4:8
+   |
+LL | public enum Test {
+   |        ^^^^ expected one of `!` or `::`
+   |
+help: write `pub` instead of `public` to make the item public
+   |
+LL | pub enum Test {
+   | ~~~
+
+error: aborting due to previous error
+
diff --git a/src/test/ui/parser/public-instead-of-pub-2.rs b/src/test/ui/parser/public-instead-of-pub-2.rs
new file mode 100644
index 00000000000..8a43c361e05
--- /dev/null
+++ b/src/test/ui/parser/public-instead-of-pub-2.rs
@@ -0,0 +1,7 @@
+// Checks what happens when `public` is used instead of the correct, `pub`
+// Won't give help message for this case
+
+public let x = 1;
+//~^ ERROR expected one of `!` or `::`, found keyword `let`
+
+fn main() { }
diff --git a/src/test/ui/parser/public-instead-of-pub-2.stderr b/src/test/ui/parser/public-instead-of-pub-2.stderr
new file mode 100644
index 00000000000..efe225656fd
--- /dev/null
+++ b/src/test/ui/parser/public-instead-of-pub-2.stderr
@@ -0,0 +1,8 @@
+error: expected one of `!` or `::`, found keyword `let`
+  --> $DIR/public-instead-of-pub-2.rs:4:8
+   |
+LL | public let x = 1;
+   |        ^^^ expected one of `!` or `::`
+
+error: aborting due to previous error
+