Repository navigation
Suggest keyword order for extern "C" const unsafe fn
#162204
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
rust-bors
merged 2 commits into
rust-lang:main
from
KevinA-cpu:extern-const-unsafe-does-not-trigger-note-issue-140171
Sep 15, 2026
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
tests/ui/parser/issues/issue-87217-keyword-order/wrong-const-async-unsafe-abi.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| // Test for issue https://github.com/rust-lang/rust/issues/140171 | ||
| // There is an order to respect for keywords before a function: | ||
| // `<visibility>, const, async, unsafe, extern, "<ABI>"` | ||
| // | ||
| // This test ensures the compiler is helpful about them being misplaced. | ||
| //@ edition:2018 | ||
|
|
||
| extern "C" const async unsafe fn b() {} | ||
| //~^ ERROR expected `fn`, found keyword `const` | ||
| //~| NOTE expected `fn` | ||
| //~| HELP `const` must come before `extern "C"` | ||
| //~| SUGGESTION const extern "C" | ||
| //~| NOTE keyword order for functions declaration is `pub`, `default`, `const`, `async`, `unsafe`, `extern` | ||
|
|
||
| fn main() {} |
15 changes: 15 additions & 0 deletions
15
tests/ui/parser/issues/issue-87217-keyword-order/wrong-const-async-unsafe-abi.stderr
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| error: expected `fn`, found keyword `const` | ||
| --> $DIR/wrong-const-async-unsafe-abi.rs:8:12 | ||
| | | ||
| LL | extern "C" const async unsafe fn b() {} | ||
| | ^^^^^ expected `fn` | ||
| | | ||
| = note: keyword order for functions declaration is `pub`, `default`, `const`, `async`, `unsafe`, `extern` | ||
| help: `const` must come before `extern "C"` | ||
| | | ||
| LL - extern "C" const async unsafe fn b() {} | ||
| LL + const extern "C" async unsafe fn b() {} | ||
| | | ||
|
|
||
| error: aborting due to 1 previous error | ||
|
|
15 changes: 15 additions & 0 deletions
15
tests/ui/parser/issues/issue-87217-keyword-order/wrong-const-unsafe-abi.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| // Test for issue https://github.com/rust-lang/rust/issues/140171 | ||
| // There is an order to respect for keywords before a function: | ||
| // `<visibility>, const, async, unsafe, extern, "<ABI>"` | ||
| // | ||
| // This test ensures the compiler is helpful about them being misplaced. | ||
| //@ edition:2018 | ||
|
|
||
| extern "C" const unsafe fn a() {} | ||
| //~^ ERROR expected `fn`, found keyword `const` | ||
| //~| NOTE expected `fn` | ||
| //~| HELP `const` must come before `extern "C"` | ||
| //~| SUGGESTION const extern "C" | ||
| //~| NOTE keyword order for functions declaration is `pub`, `default`, `const`, `async`, `unsafe`, `extern` | ||
|
|
||
| fn main() {} |
15 changes: 15 additions & 0 deletions
15
tests/ui/parser/issues/issue-87217-keyword-order/wrong-const-unsafe-abi.stderr
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| error: expected `fn`, found keyword `const` | ||
| --> $DIR/wrong-const-unsafe-abi.rs:8:12 | ||
| | | ||
| LL | extern "C" const unsafe fn a() {} | ||
| | ^^^^^ expected `fn` | ||
| | | ||
| = note: keyword order for functions declaration is `pub`, `default`, `const`, `async`, `unsafe`, `extern` | ||
| help: `const` must come before `extern "C"` | ||
| | | ||
| LL - extern "C" const unsafe fn a() {} | ||
| LL + const extern "C" unsafe fn a() {} | ||
| | | ||
|
|
||
| error: aborting due to 1 previous error | ||
|
|
16 changes: 16 additions & 0 deletions
16
tests/ui/parser/issues/issue-87217-keyword-order/wrong-pub-const-unsafe-abi.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| // Test for issue https://github.com/rust-lang/rust/issues/140171 | ||
| // There is an order to respect for keywords before a function: | ||
| // `<visibility>, const, async, unsafe, extern, "<ABI>"` | ||
| // | ||
| // This test ensures the compiler is helpful about them being misplaced. | ||
| //@ edition:2018 | ||
|
|
||
| unsafe extern "C" { static errno: i32; } | ||
|
|
||
| extern "C" pub const unsafe fn c() {} | ||
| //~^ ERROR expected `fn`, found keyword `pub` | ||
| //~| NOTE expected `fn` | ||
| //~| HELP visibility `pub` must come before `extern "C"` | ||
| //~| SUGGESTION pub extern "C" | ||
|
|
||
| fn main() {} |
14 changes: 14 additions & 0 deletions
14
tests/ui/parser/issues/issue-87217-keyword-order/wrong-pub-const-unsafe-abi.stderr
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| error: expected `fn`, found keyword `pub` | ||
| --> $DIR/wrong-pub-const-unsafe-abi.rs:10:12 | ||
| | | ||
| LL | extern "C" pub const unsafe fn c() {} | ||
| | ^^^ expected `fn` | ||
| | | ||
| help: visibility `pub` must come before `extern "C"` | ||
| | | ||
| LL - extern "C" pub const unsafe fn c() {} | ||
| LL + pub extern "C" const unsafe fn c() {} | ||
| | | ||
|
|
||
| error: aborting due to 1 previous error | ||
|
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
help me understand the fix, per my try current nightly could give correct diagnostic for:
so the pr is fixing the issue that caused by add some more keyword before
fn?is not giving the correct suggest because of the extra
async unsafe?View changes since the review
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @chenyukang , thanks for the review
So in check_fn_front_matter, there is this set of conditions above my changes on how to identify whether it's an
fnor not:Here the code claims that we can confidently know it's an
fnif:fn$qualand next token isfn$qual, next token is another$qualso someone is definitely trying to write afnbecause I suppose onlyfncan have two qualsYou can also write
fnafterextern ABIbut the current code doesn't follow the samefnchecks as the code above it - I simply add that in for this issueyes, exactly,
extern ABI fnhave not check for two quals when identifying fn.I agree that check_fn_front_matter is currently very hard to understand due to all the
||I can try to make it easier to read with if-else if you want