about summary refs log tree commit diff
diff options
context:
space:
mode:
authoryukang <moorekang@gmail.com>2024-06-26 08:44:59 +0800
committeryukang <moorekang@gmail.com>2024-06-26 08:50:50 +0800
commit0addda65788642c9967cd151eff953be1ddee32f (patch)
treef44d667dd8f7f0daff5d941c7268a2f46ac75b3e
parentc3d7fb398569407350abe044e786bc7890c90397 (diff)
downloadrust-0addda65788642c9967cd151eff953be1ddee32f.tar.gz
rust-0addda65788642c9967cd151eff953be1ddee32f.zip
Fix bad replacement for unsafe extern block suggestion
-rw-r--r--compiler/rustc_ast_passes/src/ast_validation.rs2
-rw-r--r--compiler/rustc_ast_passes/src/errors.rs2
-rw-r--r--tests/ui/parser/fn-header-semantic-fail.stderr16
-rw-r--r--tests/ui/parser/no-const-fn-in-extern-block.stderr8
-rw-r--r--tests/ui/parser/unsafe-foreign-mod-2.stderr8
-rw-r--r--tests/ui/rust-2024/unsafe-extern-blocks/safe-unsafe-on-unadorned-extern-block.edition2021.stderr16
-rw-r--r--tests/ui/rust-2024/unsafe-extern-blocks/safe-unsafe-on-unadorned-extern-block.edition2024.stderr16
-rw-r--r--tests/ui/rust-2024/unsafe-extern-blocks/unsafe-on-extern-block-issue-126756.fixed10
-rw-r--r--tests/ui/rust-2024/unsafe-extern-blocks/unsafe-on-extern-block-issue-126756.rs10
-rw-r--r--tests/ui/rust-2024/unsafe-extern-blocks/unsafe-on-extern-block-issue-126756.stderr13
10 files changed, 75 insertions, 26 deletions
diff --git a/compiler/rustc_ast_passes/src/ast_validation.rs b/compiler/rustc_ast_passes/src/ast_validation.rs
index 79717c969d7..6cff5392f95 100644
--- a/compiler/rustc_ast_passes/src/ast_validation.rs
+++ b/compiler/rustc_ast_passes/src/ast_validation.rs
@@ -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(),
                     });
                 }
             }
diff --git a/compiler/rustc_ast_passes/src/errors.rs b/compiler/rustc_ast_passes/src/errors.rs
index 96c476b271c..965d8fac712 100644
--- a/compiler/rustc_ast_passes/src/errors.rs
+++ b/compiler/rustc_ast_passes/src/errors.rs
@@ -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,
 }
 
diff --git a/tests/ui/parser/fn-header-semantic-fail.stderr b/tests/ui/parser/fn-header-semantic-fail.stderr
index 29404f1793b..b519ddbe2b4 100644
--- a/tests/ui/parser/fn-header-semantic-fail.stderr
+++ b/tests/ui/parser/fn-header-semantic-fail.stderr
@@ -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
@@ -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
diff --git a/tests/ui/parser/no-const-fn-in-extern-block.stderr b/tests/ui/parser/no-const-fn-in-extern-block.stderr
index f575acc839d..8c23824a708 100644
--- a/tests/ui/parser/no-const-fn-in-extern-block.stderr
+++ b/tests/ui/parser/no-const-fn-in-extern-block.stderr
@@ -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
 
diff --git a/tests/ui/parser/unsafe-foreign-mod-2.stderr b/tests/ui/parser/unsafe-foreign-mod-2.stderr
index e59352395ed..77a383d5efa 100644
--- a/tests/ui/parser/unsafe-foreign-mod-2.stderr
+++ b/tests/ui/parser/unsafe-foreign-mod-2.stderr
@@ -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
 
diff --git a/tests/ui/rust-2024/unsafe-extern-blocks/safe-unsafe-on-unadorned-extern-block.edition2021.stderr b/tests/ui/rust-2024/unsafe-extern-blocks/safe-unsafe-on-unadorned-extern-block.edition2021.stderr
index 411cf48b486..e90613357b1 100644
--- a/tests/ui/rust-2024/unsafe-extern-blocks/safe-unsafe-on-unadorned-extern-block.edition2021.stderr
+++ b/tests/ui/rust-2024/unsafe-extern-blocks/safe-unsafe-on-unadorned-extern-block.edition2021.stderr
@@ -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
 
diff --git a/tests/ui/rust-2024/unsafe-extern-blocks/safe-unsafe-on-unadorned-extern-block.edition2024.stderr b/tests/ui/rust-2024/unsafe-extern-blocks/safe-unsafe-on-unadorned-extern-block.edition2024.stderr
index b634adc2999..1207ee158cc 100644
--- a/tests/ui/rust-2024/unsafe-extern-blocks/safe-unsafe-on-unadorned-extern-block.edition2024.stderr
+++ b/tests/ui/rust-2024/unsafe-extern-blocks/safe-unsafe-on-unadorned-extern-block.edition2024.stderr
@@ -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
 
diff --git a/tests/ui/rust-2024/unsafe-extern-blocks/unsafe-on-extern-block-issue-126756.fixed b/tests/ui/rust-2024/unsafe-extern-blocks/unsafe-on-extern-block-issue-126756.fixed
new file mode 100644
index 00000000000..2ff595cc44d
--- /dev/null
+++ b/tests/ui/rust-2024/unsafe-extern-blocks/unsafe-on-extern-block-issue-126756.fixed
@@ -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() {}
diff --git a/tests/ui/rust-2024/unsafe-extern-blocks/unsafe-on-extern-block-issue-126756.rs b/tests/ui/rust-2024/unsafe-extern-blocks/unsafe-on-extern-block-issue-126756.rs
new file mode 100644
index 00000000000..6fe43f7a5b4
--- /dev/null
+++ b/tests/ui/rust-2024/unsafe-extern-blocks/unsafe-on-extern-block-issue-126756.rs
@@ -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() {}
diff --git a/tests/ui/rust-2024/unsafe-extern-blocks/unsafe-on-extern-block-issue-126756.stderr b/tests/ui/rust-2024/unsafe-extern-blocks/unsafe-on-extern-block-issue-126756.stderr
new file mode 100644
index 00000000000..05d23d001ad
--- /dev/null
+++ b/tests/ui/rust-2024/unsafe-extern-blocks/unsafe-on-extern-block-issue-126756.stderr
@@ -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
+