about summary refs log tree commit diff
diff options
context:
space:
mode:
authorYuki Okushi <jtitor@2k36.org>2021-07-08 10:44:34 +0900
committerGitHub <noreply@github.com>2021-07-08 10:44:34 +0900
commit463301aa5a369609d598d2c66902c47a3b1efb51 (patch)
tree84731637e3c3919de60845c7def7f7b69885d3fc
parent89638a1ddc07a1da91b293a0e359557bf061c12f (diff)
parent04a9c10fc27fcf7c4a4415401c7653a361177631 (diff)
downloadrust-463301aa5a369609d598d2c66902c47a3b1efb51.tar.gz
rust-463301aa5a369609d598d2c66902c47a3b1efb51.zip
Rollup merge of #86932 - rylev:fix-ice-86895, r=estebank
Fix ICE when misplaced visibility cannot be properly parsed

Fixes #86895

The issue was that a failure to parse the visibility was causing the original error to be dropped before being emitted.

The resulting error isn't quite as nice as when the visibility is parsed properly, but I'm not sure which error to prioritize here. Displaying both errors might be too confusing.

r? ```@estebank```
-rw-r--r--compiler/rustc_parse/src/parser/item.rs8
-rw-r--r--src/test/ui/parser/issue-86895.rs3
-rw-r--r--src/test/ui/parser/issue-86895.stderr8
3 files changed, 18 insertions, 1 deletions
diff --git a/compiler/rustc_parse/src/parser/item.rs b/compiler/rustc_parse/src/parser/item.rs
index 2daa9e2485b..2ce63d011f4 100644
--- a/compiler/rustc_parse/src/parser/item.rs
+++ b/compiler/rustc_parse/src/parser/item.rs
@@ -1791,7 +1791,13 @@ impl<'a> Parser<'a> {
                     if self.check_keyword(kw::Pub) {
                         let sp = sp_start.to(self.prev_token.span);
                         if let Ok(snippet) = self.span_to_snippet(sp) {
-                            let vis = self.parse_visibility(FollowedByType::No)?;
+                            let vis = match self.parse_visibility(FollowedByType::No) {
+                                Ok(v) => v,
+                                Err(mut d) => {
+                                    d.cancel();
+                                    return Err(err);
+                                }
+                            };
                             let vs = pprust::vis_to_string(&vis);
                             let vs = vs.trim_end();
                             err.span_suggestion(
diff --git a/src/test/ui/parser/issue-86895.rs b/src/test/ui/parser/issue-86895.rs
new file mode 100644
index 00000000000..4cd09843107
--- /dev/null
+++ b/src/test/ui/parser/issue-86895.rs
@@ -0,0 +1,3 @@
+const pub () {}
+//~^ ERROR expected one of `async`, `extern`, `fn`, or `unsafe`
+pub fn main() {}
diff --git a/src/test/ui/parser/issue-86895.stderr b/src/test/ui/parser/issue-86895.stderr
new file mode 100644
index 00000000000..575d857c0ed
--- /dev/null
+++ b/src/test/ui/parser/issue-86895.stderr
@@ -0,0 +1,8 @@
+error: expected one of `async`, `extern`, `fn`, or `unsafe`, found keyword `pub`
+  --> $DIR/issue-86895.rs:1:7
+   |
+LL | const pub () {}
+   |       ^^^ expected one of `async`, `extern`, `fn`, or `unsafe`
+
+error: aborting due to previous error
+