Rollup merge of #126973 - chenyukang:yukang-fix-126756-unsafe-suggestion-error, r=spastorino

Fix bad replacement for unsafe extern block suggestion

Fixes #126756

r? ``@spastorino``

link #123743
This commit is contained in:
Matthias Krüger 2024-06-26 07:50:21 +02:00 committed by GitHub
commit b2720867f1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 75 additions and 26 deletions

View file

@ -464,7 +464,7 @@ fn check_item_safety(&self, span: Span, safety: Safety) {
{
self.dcx().emit_err(errors::InvalidSafetyOnExtern {
item_span: span,
block: self.current_extern_span(),
block: self.current_extern_span().shrink_to_lo(),
});
}
}

View file

@ -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,
}

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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() {}

View file

@ -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() {}

View file

@ -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