From 04a9c10fc27fcf7c4a4415401c7653a361177631 Mon Sep 17 00:00:00 2001 From: Ryan Levick Date: Wed, 7 Jul 2021 14:25:40 +0200 Subject: [PATCH] Fix ICE when misplaced visibility cannot be properly parsed --- compiler/rustc_parse/src/parser/item.rs | 8 +++++++- src/test/ui/parser/issue-86895.rs | 3 +++ src/test/ui/parser/issue-86895.stderr | 8 ++++++++ 3 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 src/test/ui/parser/issue-86895.rs create mode 100644 src/test/ui/parser/issue-86895.stderr diff --git a/compiler/rustc_parse/src/parser/item.rs b/compiler/rustc_parse/src/parser/item.rs index 2daa9e2485b1b..2ce63d011f438 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 0000000000000..4cd09843107dd --- /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 0000000000000..575d857c0ed4f --- /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 +