Skip to content

Commit

Permalink
Rollup merge of #113350 - chenyukang:yukang-fix-113342-parser, r=comp…
Browse files Browse the repository at this point in the history
…iler-errors

Fix the issue of wrong diagnosis for extern pub fn

Fixes #113342
  • Loading branch information
fee1-dead authored Jul 6, 2023
2 parents 1830b80 + f25463e commit 2bc0ae3
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
6 changes: 5 additions & 1 deletion compiler/rustc_parse/src/parser/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2182,7 +2182,11 @@ impl<'a> Parser<'a> {
// `extern ABI fn`
|| self.check_keyword_case(kw::Extern, case)
&& self.look_ahead(1, |t| t.can_begin_literal_maybe_minus())
&& self.look_ahead(2, |t| t.is_keyword_case(kw::Fn, case))
&& (self.look_ahead(2, |t| t.is_keyword_case(kw::Fn, case)) ||
// this branch is only for better diagnostic in later, `pub` is not allowed here
(self.may_recover()
&& self.look_ahead(2, |t| t.is_keyword(kw::Pub))
&& self.look_ahead(3, |t| t.is_keyword_case(kw::Fn, case))))
}

/// Parses all the "front matter" (or "qualifiers") for a `fn` declaration,
Expand Down
9 changes: 9 additions & 0 deletions tests/ui/parser/issue-113342.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#[link(name = "my_c_library")]
extern "C" {
fn my_c_function(x: i32) -> bool;
}

#[no_mangle]
extern "C" pub fn id(x: i32) -> i32 { x } //~ ERROR expected `fn`, found keyword `pub`

fn main() {}
11 changes: 11 additions & 0 deletions tests/ui/parser/issue-113342.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
error: expected `fn`, found keyword `pub`
--> $DIR/issue-113342.rs:7:12
|
LL | extern "C" pub fn id(x: i32) -> i32 { x }
| -----------^^^
| | |
| | expected `fn`
| help: visibility `pub` must come before `extern "C"`: `pub extern "C"`

error: aborting due to previous error

0 comments on commit 2bc0ae3

Please sign in to comment.