Skip to content

Commit 04a9c10

Browse files
committed
Fix ICE when misplaced visibility cannot be properly parsed
1 parent c5e344f commit 04a9c10

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

compiler/rustc_parse/src/parser/item.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -1791,7 +1791,13 @@ impl<'a> Parser<'a> {
17911791
if self.check_keyword(kw::Pub) {
17921792
let sp = sp_start.to(self.prev_token.span);
17931793
if let Ok(snippet) = self.span_to_snippet(sp) {
1794-
let vis = self.parse_visibility(FollowedByType::No)?;
1794+
let vis = match self.parse_visibility(FollowedByType::No) {
1795+
Ok(v) => v,
1796+
Err(mut d) => {
1797+
d.cancel();
1798+
return Err(err);
1799+
}
1800+
};
17951801
let vs = pprust::vis_to_string(&vis);
17961802
let vs = vs.trim_end();
17971803
err.span_suggestion(

src/test/ui/parser/issue-86895.rs

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const pub () {}
2+
//~^ ERROR expected one of `async`, `extern`, `fn`, or `unsafe`
3+
pub fn main() {}

src/test/ui/parser/issue-86895.stderr

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error: expected one of `async`, `extern`, `fn`, or `unsafe`, found keyword `pub`
2+
--> $DIR/issue-86895.rs:1:7
3+
|
4+
LL | const pub () {}
5+
| ^^^ expected one of `async`, `extern`, `fn`, or `unsafe`
6+
7+
error: aborting due to previous error
8+

0 commit comments

Comments
 (0)