-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Add targeted suggestion for incompatible {un,}safe
keywords when parsing fn-frontmatter-like fragments
#133618
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
Closed
+144
−4
Closed
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 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
31 changes: 31 additions & 0 deletions
31
tests/ui/parser/incompatible-safe-unsafe-keywords-extern-block-1.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,31 @@ | ||
//! Check that we don't try to suggest reordering incompatible keywords `safe` and `unsafe` when | ||
//! parsing things that looks like fn frontmatter/extern blocks. | ||
//! | ||
//! # Context | ||
//! | ||
//! Previously, there was some recovery logic related to misplaced keywords (e.g. `safe` and | ||
//! `unsafe`) when we tried to parse fn frontmatter (this is what happens when trying to parse | ||
//! something malformed like `unsafe safe extern {}` or `safe unsafe extern {}`). Unfortunately, the | ||
//! recovery logic only really handled duplicate keywords or misplaced keywords. This meant that | ||
//! incompatible keywords like {`unsafe`, `safe`} when used together produces some funny suggestion | ||
//! e.g. | ||
//! | ||
//! ```text | ||
//! help: `unsafe` must come before `safe`: `unsafe safe` | ||
//! ``` | ||
//! | ||
//! and then if you applied that suggestion, another suggestion in the recovery logic will tell you | ||
//! to flip it back, ad infinitum. | ||
//! | ||
//! # References | ||
//! | ||
//! See <https://github.com/rust-lang/rust/issues/133586>. | ||
//! | ||
//! See `incompatible-safe-unsafe-keywords-extern-block-2.rs` for the `safe unsafe extern {}` | ||
//! version. | ||
#![crate_type = "lib"] | ||
|
||
safe unsafe extern {} | ||
//~^ ERROR expected one of `extern` or `fn`, found keyword `unsafe` | ||
//~| NOTE expected one of `extern` or `fn` | ||
//~| HELP `safe` and `unsafe` are incompatible, use only one of the keywords |
14 changes: 14 additions & 0 deletions
14
tests/ui/parser/incompatible-safe-unsafe-keywords-extern-block-1.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 one of `extern` or `fn`, found keyword `unsafe` | ||
--> $DIR/incompatible-safe-unsafe-keywords-extern-block-1.rs:28:6 | ||
| | ||
LL | safe unsafe extern {} | ||
| ^^^^^^ expected one of `extern` or `fn` | ||
| | ||
help: `safe` and `unsafe` are incompatible, use only one of the keywords | ||
--> $DIR/incompatible-safe-unsafe-keywords-extern-block-1.rs:28:1 | ||
| | ||
LL | safe unsafe extern {} | ||
| ^^^^^^^^^^^ | ||
|
||
error: aborting due to 1 previous error | ||
|
15 changes: 15 additions & 0 deletions
15
tests/ui/parser/incompatible-safe-unsafe-keywords-extern-block-2.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 @@ | ||
//! Check that we don't try to suggest reordering incompatible keywords `safe` and `unsafe` when | ||
//! parsing things that looks like fn frontmatter/extern blocks. | ||
//! | ||
//! # References | ||
//! | ||
//! See <https://github.com/rust-lang/rust/issues/133586>. | ||
//! | ||
//! See `incompatible-safe-unsafe-keywords-extern-block-1.rs` for the `unsafe safqe extern {}` | ||
//! version. | ||
#![crate_type = "lib"] | ||
|
||
unsafe safe extern {} | ||
//~^ ERROR expected one of `extern` or `fn`, found `safe` | ||
//~| NOTE expected one of `extern` or `fn` | ||
//~| HELP `unsafe` and `safe` are incompatible, use only one of the keywords |
14 changes: 14 additions & 0 deletions
14
tests/ui/parser/incompatible-safe-unsafe-keywords-extern-block-2.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 one of `extern` or `fn`, found `safe` | ||
--> $DIR/incompatible-safe-unsafe-keywords-extern-block-2.rs:12:8 | ||
| | ||
LL | unsafe safe extern {} | ||
| ^^^^ expected one of `extern` or `fn` | ||
| | ||
help: `unsafe` and `safe` are incompatible, use only one of the keywords | ||
--> $DIR/incompatible-safe-unsafe-keywords-extern-block-2.rs:12:1 | ||
| | ||
LL | unsafe safe extern {} | ||
| ^^^^^^^^^^^ | ||
|
||
error: aborting due to 1 previous error | ||
|
11 changes: 11 additions & 0 deletions
11
tests/ui/parser/incompatible-safe-unsafe-keywords-extern-fn-in-extern-block.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,11 @@ | ||
//! Check that we emit a targeted suggestion for an extern fn that uses incompatible `safe` and | ||
//! `unsafe` keywords. | ||
#![crate_type = "lib"] | ||
|
||
unsafe extern { | ||
//~^ NOTE while parsing this item list starting here | ||
pub safe unsafe extern fn foo() {} | ||
//~^ ERROR expected one of `extern` or `fn`, found keyword `unsafe` | ||
//~| NOTE expected one of `extern` or `fn` | ||
//~| `safe` and `unsafe` are incompatible, use only one of the keywords | ||
} //~ NOTE the item list ends here |
20 changes: 20 additions & 0 deletions
20
tests/ui/parser/incompatible-safe-unsafe-keywords-extern-fn-in-extern-block.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,20 @@ | ||
error: expected one of `extern` or `fn`, found keyword `unsafe` | ||
--> $DIR/incompatible-safe-unsafe-keywords-extern-fn-in-extern-block.rs:7:14 | ||
| | ||
LL | unsafe extern { | ||
| - while parsing this item list starting here | ||
LL | | ||
LL | pub safe unsafe extern fn foo() {} | ||
| ^^^^^^ expected one of `extern` or `fn` | ||
... | ||
LL | } | ||
| - the item list ends here | ||
| | ||
help: `safe` and `unsafe` are incompatible, use only one of the keywords | ||
--> $DIR/incompatible-safe-unsafe-keywords-extern-fn-in-extern-block.rs:7:9 | ||
| | ||
LL | pub safe unsafe extern fn foo() {} | ||
| ^^^^^^^^^^^ | ||
|
||
error: aborting due to 1 previous error | ||
|
8 changes: 8 additions & 0 deletions
8
tests/ui/parser/incompatible-safe-unsafe-keywords-extern-fn.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,8 @@ | ||
//! Check that we emit a targeted suggestion for an extern fn that uses incompatible `safe` and | ||
//! `unsafe` keywords. | ||
#![crate_type = "lib"] | ||
|
||
pub safe unsafe extern fn foo() {} | ||
//~^ ERROR expected one of `extern` or `fn`, found keyword `unsafe` | ||
//~| NOTE expected one of `extern` or `fn` | ||
//~| `safe` and `unsafe` are incompatible, use only one of the keywords |
14 changes: 14 additions & 0 deletions
14
tests/ui/parser/incompatible-safe-unsafe-keywords-extern-fn.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 one of `extern` or `fn`, found keyword `unsafe` | ||
--> $DIR/incompatible-safe-unsafe-keywords-extern-fn.rs:5:10 | ||
| | ||
LL | pub safe unsafe extern fn foo() {} | ||
| ^^^^^^ expected one of `extern` or `fn` | ||
| | ||
help: `safe` and `unsafe` are incompatible, use only one of the keywords | ||
--> $DIR/incompatible-safe-unsafe-keywords-extern-fn.rs:5:5 | ||
| | ||
LL | pub safe unsafe extern fn foo() {} | ||
| ^^^^^^^^^^^ | ||
|
||
error: aborting due to 1 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.
Remark: this is more of a band-aid fix, because this recovery code path is taken when we just so happen to be trying to parse anything that's fn-frontmatter-like that does not immediately include an
fn
keyword (as part of trying to parse item shaped things). So it's kinda best-effort anyway.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.
safe isnt supposed to be incompatible with unsafe in this context; safe is not valid in this context
so this error should be for the extern block
this is atleast what i understand
to sum up:
The desired output: