Skip to content

Commit

Permalink
Fix bad replacement for unsafe extern block suggestion
Browse files Browse the repository at this point in the history
  • Loading branch information
chenyukang committed Jun 26, 2024
1 parent c3d7fb3 commit 0addda6
Show file tree
Hide file tree
Showing 10 changed files with 75 additions and 26 deletions.
2 changes: 1 addition & 1 deletion compiler/rustc_ast_passes/src/ast_validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,7 @@ impl<'a> AstValidator<'a> {
{
self.dcx().emit_err(errors::InvalidSafetyOnExtern {
item_span: span,
block: self.current_extern_span(),
block: self.current_extern_span().shrink_to_lo(),
});
}
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_ast_passes/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ pub enum ExternBlockSuggestion {
pub struct InvalidSafetyOnExtern {
#[primary_span]
pub item_span: Span,
#[suggestion(code = "", applicability = "maybe-incorrect")]
#[suggestion(code = "unsafe ", applicability = "machine-applicable", style = "verbose")]
pub block: Span,
}

Expand Down
16 changes: 10 additions & 6 deletions tests/ui/parser/fn-header-semantic-fail.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,13 @@ LL | async fn fe1();
error: items in unadorned `extern` blocks cannot have safety qualifiers
--> $DIR/fn-header-semantic-fail.rs:47:9
|
LL | extern "C" {
| ---------- help: add unsafe to this `extern` block
LL | async fn fe1();
LL | unsafe fn fe2();
| ^^^^^^^^^^^^^^^^
|
help: add unsafe to this `extern` block
|
LL | unsafe extern "C" {
| ++++++

error: functions in `extern` blocks cannot have qualifiers
--> $DIR/fn-header-semantic-fail.rs:48:9
Expand Down Expand Up @@ -135,11 +137,13 @@ LL | const async unsafe extern "C" fn fe5();
error: items in unadorned `extern` blocks cannot have safety qualifiers
--> $DIR/fn-header-semantic-fail.rs:50:9
|
LL | extern "C" {
| ---------- help: add unsafe to this `extern` block
...
LL | const async unsafe extern "C" fn fe5();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: add unsafe to this `extern` block
|
LL | unsafe extern "C" {
| ++++++

error: functions cannot be both `const` and `async`
--> $DIR/fn-header-semantic-fail.rs:50:9
Expand Down
8 changes: 5 additions & 3 deletions tests/ui/parser/no-const-fn-in-extern-block.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ LL | const unsafe fn bar();
error: items in unadorned `extern` blocks cannot have safety qualifiers
--> $DIR/no-const-fn-in-extern-block.rs:4:5
|
LL | extern "C" {
| ---------- help: add unsafe to this `extern` block
...
LL | const unsafe fn bar();
| ^^^^^^^^^^^^^^^^^^^^^^
|
help: add unsafe to this `extern` block
|
LL | unsafe extern "C" {
| ++++++

error: aborting due to 3 previous errors

8 changes: 5 additions & 3 deletions tests/ui/parser/unsafe-foreign-mod-2.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ LL | extern "C" unsafe {
error: items in unadorned `extern` blocks cannot have safety qualifiers
--> $DIR/unsafe-foreign-mod-2.rs:4:5
|
LL | extern "C" unsafe {
| ----------------- help: add unsafe to this `extern` block
...
LL | unsafe fn foo();
| ^^^^^^^^^^^^^^^^
|
help: add unsafe to this `extern` block
|
LL | unsafe extern "C" unsafe {
| ++++++

error: aborting due to 3 previous errors

Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
error: items in unadorned `extern` blocks cannot have safety qualifiers
--> $DIR/safe-unsafe-on-unadorned-extern-block.rs:10:5
|
LL | extern "C" {
| ---------- help: add unsafe to this `extern` block
LL |
LL | safe static TEST1: i32;
| ^^^^^^^^^^^^^^^^^^^^^^^
|
help: add unsafe to this `extern` block
|
LL | unsafe extern "C" {
| ++++++

error: items in unadorned `extern` blocks cannot have safety qualifiers
--> $DIR/safe-unsafe-on-unadorned-extern-block.rs:12:5
|
LL | extern "C" {
| ---------- help: add unsafe to this `extern` block
...
LL | safe fn test1(i: i32);
| ^^^^^^^^^^^^^^^^^^^^^^
|
help: add unsafe to this `extern` block
|
LL | unsafe extern "C" {
| ++++++

error: aborting due to 2 previous errors

Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,24 @@ LL | | }
error: items in unadorned `extern` blocks cannot have safety qualifiers
--> $DIR/safe-unsafe-on-unadorned-extern-block.rs:10:5
|
LL | extern "C" {
| ---------- help: add unsafe to this `extern` block
LL |
LL | safe static TEST1: i32;
| ^^^^^^^^^^^^^^^^^^^^^^^
|
help: add unsafe to this `extern` block
|
LL | unsafe extern "C" {
| ++++++

error: items in unadorned `extern` blocks cannot have safety qualifiers
--> $DIR/safe-unsafe-on-unadorned-extern-block.rs:12:5
|
LL | extern "C" {
| ---------- help: add unsafe to this `extern` block
...
LL | safe fn test1(i: i32);
| ^^^^^^^^^^^^^^^^^^^^^^
|
help: add unsafe to this `extern` block
|
LL | unsafe extern "C" {
| ++++++

error: aborting due to 3 previous errors

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//@ run-rustfix

#![feature(unsafe_extern_blocks)]
#![allow(dead_code)]

unsafe extern "C" {
unsafe fn foo(); //~ ERROR items in unadorned `extern` blocks cannot have safety qualifiers
}

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//@ run-rustfix

#![feature(unsafe_extern_blocks)]
#![allow(dead_code)]

extern "C" {
unsafe fn foo(); //~ ERROR items in unadorned `extern` blocks cannot have safety qualifiers
}

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
error: items in unadorned `extern` blocks cannot have safety qualifiers
--> $DIR/unsafe-on-extern-block-issue-126756.rs:7:5
|
LL | unsafe fn foo();
| ^^^^^^^^^^^^^^^^
|
help: add unsafe to this `extern` block
|
LL | unsafe extern "C" {
| ++++++

error: aborting due to 1 previous error

0 comments on commit 0addda6

Please sign in to comment.