-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Improve diagnostics for wrongly ordered keywords in function declaration #87235
Merged
Merged
Changes from all commits
Commits
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
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
11 changes: 11 additions & 0 deletions
11
src/test/ui/parser/issue-87217-keyword-order/const-async-const.rs
This file contains 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,11 @@ | ||
// edition:2018 | ||
|
||
// Test that even when `const` is already present, the proposed fix is `const const async`, | ||
// like for `pub pub`. | ||
|
||
const async const fn test() {} | ||
//~^ ERROR expected one of `extern`, `fn`, or `unsafe`, found keyword `const` | ||
//~| NOTE expected one of `extern`, `fn`, or `unsafe` | ||
//~| HELP `const` must come before `async` | ||
//~| SUGGESTION const async | ||
//~| NOTE keyword order for functions declaration is `default`, `pub`, `const`, `async`, `unsafe`, `extern` |
13 changes: 13 additions & 0 deletions
13
src/test/ui/parser/issue-87217-keyword-order/const-async-const.stderr
This file contains 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,13 @@ | ||
error: expected one of `extern`, `fn`, or `unsafe`, found keyword `const` | ||
--> $DIR/const-async-const.rs:6:13 | ||
| | ||
LL | const async const fn test() {} | ||
| ------^^^^^ | ||
| | | | ||
| | expected one of `extern`, `fn`, or `unsafe` | ||
| help: `const` must come before `async`: `const async` | ||
| | ||
= note: keyword order for functions declaration is `default`, `pub`, `const`, `async`, `unsafe`, `extern` | ||
|
||
error: aborting due to previous error | ||
|
14 changes: 14 additions & 0 deletions
14
src/test/ui/parser/issue-87217-keyword-order/several-kw-jump.rs
This file contains 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 @@ | ||
// edition:2018 | ||
|
||
// 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. | ||
// Visibilities are tested elsewhere. | ||
|
||
async unsafe const fn test() {} | ||
//~^ ERROR expected one of `extern` or `fn`, found keyword `const` | ||
//~| NOTE expected one of `extern` or `fn` | ||
//~| HELP `const` must come before `async unsafe` | ||
//~| SUGGESTION const async unsafe | ||
//~| NOTE keyword order for functions declaration is `default`, `pub`, `const`, `async`, `unsafe`, `extern` |
13 changes: 13 additions & 0 deletions
13
src/test/ui/parser/issue-87217-keyword-order/several-kw-jump.stderr
This file contains 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,13 @@ | ||
error: expected one of `extern` or `fn`, found keyword `const` | ||
--> $DIR/several-kw-jump.rs:9:14 | ||
| | ||
LL | async unsafe const fn test() {} | ||
| -------------^^^^^ | ||
| | | | ||
| | expected one of `extern` or `fn` | ||
| help: `const` must come before `async unsafe`: `const async unsafe` | ||
| | ||
= note: keyword order for functions declaration is `default`, `pub`, `const`, `async`, `unsafe`, `extern` | ||
|
||
error: aborting due to previous error | ||
|
14 changes: 14 additions & 0 deletions
14
src/test/ui/parser/issue-87217-keyword-order/wrong-async.rs
This file contains 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 @@ | ||
// edition:2018 | ||
|
||
// 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. | ||
// Visibilities are tested elsewhere. | ||
|
||
unsafe async fn test() {} | ||
//~^ ERROR expected one of `extern` or `fn`, found keyword `async` | ||
//~| NOTE expected one of `extern` or `fn` | ||
//~| HELP `async` must come before `unsafe` | ||
//~| SUGGESTION async unsafe | ||
//~| NOTE keyword order for functions declaration is `default`, `pub`, `const`, `async`, `unsafe`, `extern` |
13 changes: 13 additions & 0 deletions
13
src/test/ui/parser/issue-87217-keyword-order/wrong-async.stderr
This file contains 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,13 @@ | ||
error: expected one of `extern` or `fn`, found keyword `async` | ||
--> $DIR/wrong-async.rs:9:8 | ||
| | ||
LL | unsafe async fn test() {} | ||
| -------^^^^^ | ||
| | | | ||
| | expected one of `extern` or `fn` | ||
| help: `async` must come before `unsafe`: `async unsafe` | ||
| | ||
= note: keyword order for functions declaration is `default`, `pub`, `const`, `async`, `unsafe`, `extern` | ||
|
||
error: aborting due to previous error | ||
|
14 changes: 14 additions & 0 deletions
14
src/test/ui/parser/issue-87217-keyword-order/wrong-const.rs
This file contains 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 @@ | ||
// edition:2018 | ||
|
||
// 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. | ||
// Visibilities are tested elsewhere. | ||
|
||
unsafe const fn test() {} | ||
//~^ ERROR expected one of `extern` or `fn`, found keyword `const` | ||
//~| NOTE expected one of `extern` or `fn` | ||
//~| HELP `const` must come before `unsafe` | ||
//~| SUGGESTION const unsafe | ||
//~| NOTE keyword order for functions declaration is `default`, `pub`, `const`, `async`, `unsafe`, `extern` |
13 changes: 13 additions & 0 deletions
13
src/test/ui/parser/issue-87217-keyword-order/wrong-const.stderr
This file contains 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,13 @@ | ||
error: expected one of `extern` or `fn`, found keyword `const` | ||
--> $DIR/wrong-const.rs:9:8 | ||
| | ||
LL | unsafe const fn test() {} | ||
| -------^^^^^ | ||
| | | | ||
| | expected one of `extern` or `fn` | ||
| help: `const` must come before `unsafe`: `const unsafe` | ||
| | ||
= note: keyword order for functions declaration is `default`, `pub`, `const`, `async`, `unsafe`, `extern` | ||
|
||
error: aborting due to previous error | ||
|
14 changes: 14 additions & 0 deletions
14
src/test/ui/parser/issue-87217-keyword-order/wrong-unsafe.rs
This file contains 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 @@ | ||
// edition:2018 | ||
|
||
// 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. | ||
// Visibilities are tested elsewhere. | ||
|
||
extern unsafe fn test() {} | ||
//~^ ERROR expected `fn`, found keyword `unsafe` | ||
//~| NOTE expected `fn` | ||
//~| HELP `unsafe` must come before `extern` | ||
//~| SUGGESTION unsafe extern | ||
//~| NOTE keyword order for functions declaration is `default`, `pub`, `const`, `async`, `unsafe`, `extern` |
13 changes: 13 additions & 0 deletions
13
src/test/ui/parser/issue-87217-keyword-order/wrong-unsafe.stderr
This file contains 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,13 @@ | ||
error: expected `fn`, found keyword `unsafe` | ||
--> $DIR/wrong-unsafe.rs:9:8 | ||
| | ||
LL | extern unsafe fn test() {} | ||
| -------^^^^^^ | ||
| | | | ||
| | expected `fn` | ||
| help: `unsafe` must come before `extern`: `unsafe extern` | ||
| | ||
= note: keyword order for functions declaration is `default`, `pub`, `const`, `async`, `unsafe`, `extern` | ||
|
||
error: aborting due to previous error | ||
|
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.
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.
If I'm reading this correctly, we will trigger the diagnostic here below for code that has duplicate keywords, such as e.g. in
and I believe that the machine applicable suggestion will suggest a
const const async fn
which is invalid.It may not be critical to fix this now in this PR, but could you please at least add a test for that case?
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.
You are right, I will add a test for it.
pub pub fn test() {}
already behaves like that and has aMachineApplicable
fix, I think it is because it is expected to sometimes misplace keywords but repeating them is not something that happens often so it should be okay ? I can try to fix it in this PR or open another issue+PR combo, I don't know what's best hereThere 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.
I don't believe this must be fixed in this PR, but I would consider that a bug.