Skip to content

Commit f25463e

Browse files
committed
Fix the issue of wrong diagnosis for extern pub fn
1 parent b7bc6f8 commit f25463e

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

compiler/rustc_parse/src/parser/item.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -2182,7 +2182,11 @@ impl<'a> Parser<'a> {
21822182
// `extern ABI fn`
21832183
|| self.check_keyword_case(kw::Extern, case)
21842184
&& self.look_ahead(1, |t| t.can_begin_literal_maybe_minus())
2185-
&& self.look_ahead(2, |t| t.is_keyword_case(kw::Fn, case))
2185+
&& (self.look_ahead(2, |t| t.is_keyword_case(kw::Fn, case)) ||
2186+
// this branch is only for better diagnostic in later, `pub` is not allowed here
2187+
(self.may_recover()
2188+
&& self.look_ahead(2, |t| t.is_keyword(kw::Pub))
2189+
&& self.look_ahead(3, |t| t.is_keyword_case(kw::Fn, case))))
21862190
}
21872191

21882192
/// Parses all the "front matter" (or "qualifiers") for a `fn` declaration,

tests/ui/parser/issue-113342.rs

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#[link(name = "my_c_library")]
2+
extern "C" {
3+
fn my_c_function(x: i32) -> bool;
4+
}
5+
6+
#[no_mangle]
7+
extern "C" pub fn id(x: i32) -> i32 { x } //~ ERROR expected `fn`, found keyword `pub`
8+
9+
fn main() {}

tests/ui/parser/issue-113342.stderr

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error: expected `fn`, found keyword `pub`
2+
--> $DIR/issue-113342.rs:7:12
3+
|
4+
LL | extern "C" pub fn id(x: i32) -> i32 { x }
5+
| -----------^^^
6+
| | |
7+
| | expected `fn`
8+
| help: visibility `pub` must come before `extern "C"`: `pub extern "C"`
9+
10+
error: aborting due to previous error
11+

0 commit comments

Comments
 (0)