Rollup merge of #131550 - compiler-errors:extern-diags, r=spastorino

Make some tweaks to extern block diagnostics

Self-explanatory. See the diagnostic changes; I hope they make them a bit more descriptive.

r? spastorino
This commit is contained in:
Matthias Krüger 2024-10-14 17:06:38 +02:00 committed by GitHub
commit b8cdca8cce
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
16 changed files with 62 additions and 58 deletions

View File

@ -62,12 +62,12 @@ ast_passes_equality_in_where = equality constraints are not yet supported in `wh
ast_passes_extern_block_suggestion = if you meant to declare an externally defined function, use an `extern` block
ast_passes_extern_fn_qualifiers = functions in `extern` blocks cannot have qualifiers
ast_passes_extern_fn_qualifiers = functions in `extern` blocks cannot have `{$kw}` qualifier
.label = in this `extern` block
.suggestion = remove this qualifier
.suggestion = remove the `{$kw}` qualifier
ast_passes_extern_invalid_safety = items in unadorned `extern` blocks cannot have safety qualifiers
.suggestion = add unsafe to this `extern` block
ast_passes_extern_invalid_safety = items in `extern` blocks without an `unsafe` qualifier cannot have safety qualifiers
.suggestion = add `unsafe` to this `extern` block
ast_passes_extern_item_ascii = items in `extern` blocks cannot use non-ascii identifiers
.label = in this `extern` block

View File

@ -524,21 +524,24 @@ fn check_foreign_fn_headerless(
// Deconstruct to ensure exhaustiveness
FnHeader { safety: _, coroutine_kind, constness, ext }: FnHeader,
) {
let report_err = |span| {
self.dcx()
.emit_err(errors::FnQualifierInExtern { span, block: self.current_extern_span() });
let report_err = |span, kw| {
self.dcx().emit_err(errors::FnQualifierInExtern {
span,
kw,
block: self.current_extern_span(),
});
};
match coroutine_kind {
Some(knd) => report_err(knd.span()),
Some(kind) => report_err(kind.span(), kind.as_str()),
None => (),
}
match constness {
Const::Yes(span) => report_err(span),
Const::Yes(span) => report_err(span, "const"),
Const::No => (),
}
match ext {
Extern::None => (),
Extern::Implicit(span) | Extern::Explicit(_, span) => report_err(span),
Extern::Implicit(span) | Extern::Explicit(_, span) => report_err(span, "extern"),
}
}

View File

@ -295,6 +295,7 @@ pub(crate) struct FnQualifierInExtern {
pub span: Span,
#[label]
pub block: Span,
pub kw: &'static str,
}
#[derive(Diagnostic)]

View File

@ -2,7 +2,7 @@
//@ is "$.index[*][?(@.name=='f1')].inner.function.header.is_unsafe" true
pub fn f1();
// items in unadorned `extern` blocks cannot have safety qualifiers
// items in `extern` blocks without an `unsafe` qualifier cannot have safety qualifiers
}
unsafe extern "C" {

View File

@ -2,7 +2,7 @@
extern {
async fn L() { //~ ERROR: incorrect function inside `extern` block
//~^ ERROR: functions in `extern` blocks cannot have qualifiers
//~^ ERROR: functions in `extern` blocks cannot have `async` qualifier
async fn M() {}
}
}

View File

@ -15,13 +15,13 @@ LL | | }
= help: you might have meant to write a function accessible through FFI, which can be done by writing `extern fn` outside of the `extern` block
= note: for more information, visit https://doc.rust-lang.org/std/keyword.extern.html
error: functions in `extern` blocks cannot have qualifiers
error: functions in `extern` blocks cannot have `async` qualifier
--> $DIR/issue-95829.rs:4:5
|
LL | extern {
| ------ in this `extern` block
LL | async fn L() {
| ^^^^^ help: remove this qualifier
| ^^^^^ help: remove the `async` qualifier
error: aborting due to 2 previous errors

View File

@ -41,15 +41,15 @@ extern "C" fn fi4() {} // OK.
}
extern "C" {
async fn fe1(); //~ ERROR functions in `extern` blocks cannot have qualifiers
unsafe fn fe2(); //~ ERROR items in unadorned `extern` blocks cannot have safety qualifiers
const fn fe3(); //~ ERROR functions in `extern` blocks cannot have qualifiers
extern "C" fn fe4(); //~ ERROR functions in `extern` blocks cannot have qualifiers
async fn fe1(); //~ ERROR functions in `extern` blocks cannot
unsafe fn fe2(); //~ ERROR items in `extern` blocks without an `unsafe` qualifier cannot
const fn fe3(); //~ ERROR functions in `extern` blocks cannot
extern "C" fn fe4(); //~ ERROR functions in `extern` blocks cannot
const async unsafe extern "C" fn fe5();
//~^ ERROR functions in `extern` blocks
//~| ERROR functions in `extern` blocks
//~| ERROR functions in `extern` blocks
//~| ERROR functions cannot be both `const` and `async`
//~| ERROR items in unadorned `extern` blocks cannot have safety qualifiers
//~| ERROR items in `extern` blocks without an `unsafe` qualifier cannot have
}
}

View File

@ -70,77 +70,77 @@ LL | const async unsafe extern "C" fn fi5() {}
| | `async` because of this
| `const` because of this
error: functions in `extern` blocks cannot have qualifiers
error: functions in `extern` blocks cannot have `async` qualifier
--> $DIR/fn-header-semantic-fail.rs:44:9
|
LL | extern "C" {
| ---------- in this `extern` block
LL | async fn fe1();
| ^^^^^ help: remove this qualifier
| ^^^^^ help: remove the `async` qualifier
error: items in unadorned `extern` blocks cannot have safety qualifiers
error: items in `extern` blocks without an `unsafe` qualifier cannot have safety qualifiers
--> $DIR/fn-header-semantic-fail.rs:45:9
|
LL | unsafe fn fe2();
| ^^^^^^^^^^^^^^^^
|
help: add unsafe to this `extern` block
help: add `unsafe` to this `extern` block
|
LL | unsafe extern "C" {
| ++++++
error: functions in `extern` blocks cannot have qualifiers
error: functions in `extern` blocks cannot have `const` qualifier
--> $DIR/fn-header-semantic-fail.rs:46:9
|
LL | extern "C" {
| ---------- in this `extern` block
...
LL | const fn fe3();
| ^^^^^ help: remove this qualifier
| ^^^^^ help: remove the `const` qualifier
error: functions in `extern` blocks cannot have qualifiers
error: functions in `extern` blocks cannot have `extern` qualifier
--> $DIR/fn-header-semantic-fail.rs:47:9
|
LL | extern "C" {
| ---------- in this `extern` block
...
LL | extern "C" fn fe4();
| ^^^^^^^^^^ help: remove this qualifier
| ^^^^^^^^^^ help: remove the `extern` qualifier
error: functions in `extern` blocks cannot have qualifiers
error: functions in `extern` blocks cannot have `async` qualifier
--> $DIR/fn-header-semantic-fail.rs:48:15
|
LL | extern "C" {
| ---------- in this `extern` block
...
LL | const async unsafe extern "C" fn fe5();
| ^^^^^ help: remove this qualifier
| ^^^^^ help: remove the `async` qualifier
error: functions in `extern` blocks cannot have qualifiers
error: functions in `extern` blocks cannot have `const` qualifier
--> $DIR/fn-header-semantic-fail.rs:48:9
|
LL | extern "C" {
| ---------- in this `extern` block
...
LL | const async unsafe extern "C" fn fe5();
| ^^^^^ help: remove this qualifier
| ^^^^^ help: remove the `const` qualifier
error: functions in `extern` blocks cannot have qualifiers
error: functions in `extern` blocks cannot have `extern` qualifier
--> $DIR/fn-header-semantic-fail.rs:48:28
|
LL | extern "C" {
| ---------- in this `extern` block
...
LL | const async unsafe extern "C" fn fe5();
| ^^^^^^^^^^ help: remove this qualifier
| ^^^^^^^^^^ help: remove the `extern` qualifier
error: items in unadorned `extern` blocks cannot have safety qualifiers
error: items in `extern` blocks without an `unsafe` qualifier cannot have safety qualifiers
--> $DIR/fn-header-semantic-fail.rs:48:9
|
LL | const async unsafe extern "C" fn fe5();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: add unsafe to this `extern` block
help: add `unsafe` to this `extern` block
|
LL | unsafe extern "C" {
| ++++++

View File

@ -1,9 +1,9 @@
extern "C" {
const fn foo();
//~^ ERROR functions in `extern` blocks cannot have qualifiers
//~^ ERROR functions in `extern` blocks cannot
const unsafe fn bar();
//~^ ERROR functions in `extern` blocks cannot have qualifiers
//~| ERROR items in unadorned `extern` blocks cannot have safety qualifiers
//~^ ERROR functions in `extern` blocks cannot
//~| ERROR items in `extern` blocks without an `unsafe` qualifier cannot
}
fn main() {}

View File

@ -1,27 +1,27 @@
error: functions in `extern` blocks cannot have qualifiers
error: functions in `extern` blocks cannot have `const` qualifier
--> $DIR/no-const-fn-in-extern-block.rs:2:5
|
LL | extern "C" {
| ---------- in this `extern` block
LL | const fn foo();
| ^^^^^ help: remove this qualifier
| ^^^^^ help: remove the `const` qualifier
error: functions in `extern` blocks cannot have qualifiers
error: functions in `extern` blocks cannot have `const` qualifier
--> $DIR/no-const-fn-in-extern-block.rs:4:5
|
LL | extern "C" {
| ---------- in this `extern` block
...
LL | const unsafe fn bar();
| ^^^^^ help: remove this qualifier
| ^^^^^ help: remove the `const` qualifier
error: items in unadorned `extern` blocks cannot have safety qualifiers
error: items in `extern` blocks without an `unsafe` qualifier cannot have safety qualifiers
--> $DIR/no-const-fn-in-extern-block.rs:4:5
|
LL | const unsafe fn bar();
| ^^^^^^^^^^^^^^^^^^^^^^
|
help: add unsafe to this `extern` block
help: add `unsafe` to this `extern` block
|
LL | unsafe extern "C" {
| ++++++

View File

@ -1,21 +1,21 @@
error: items in unadorned `extern` blocks cannot have safety qualifiers
error: items in `extern` blocks without an `unsafe` qualifier cannot have safety qualifiers
--> $DIR/safe-unsafe-on-unadorned-extern-block.rs:8:5
|
LL | safe static TEST1: i32;
| ^^^^^^^^^^^^^^^^^^^^^^^
|
help: add unsafe to this `extern` block
help: add `unsafe` to this `extern` block
|
LL | unsafe extern "C" {
| ++++++
error: items in unadorned `extern` blocks cannot have safety qualifiers
error: items in `extern` blocks without an `unsafe` qualifier cannot have safety qualifiers
--> $DIR/safe-unsafe-on-unadorned-extern-block.rs:10:5
|
LL | safe fn test1(i: i32);
| ^^^^^^^^^^^^^^^^^^^^^^
|
help: add unsafe to this `extern` block
help: add `unsafe` to this `extern` block
|
LL | unsafe extern "C" {
| ++++++

View File

@ -10,24 +10,24 @@ LL | |
LL | | }
| |_^
error: items in unadorned `extern` blocks cannot have safety qualifiers
error: items in `extern` blocks without an `unsafe` qualifier cannot have safety qualifiers
--> $DIR/safe-unsafe-on-unadorned-extern-block.rs:8:5
|
LL | safe static TEST1: i32;
| ^^^^^^^^^^^^^^^^^^^^^^^
|
help: add unsafe to this `extern` block
help: add `unsafe` to this `extern` block
|
LL | unsafe extern "C" {
| ++++++
error: items in unadorned `extern` blocks cannot have safety qualifiers
error: items in `extern` blocks without an `unsafe` qualifier cannot have safety qualifiers
--> $DIR/safe-unsafe-on-unadorned-extern-block.rs:10:5
|
LL | safe fn test1(i: i32);
| ^^^^^^^^^^^^^^^^^^^^^^
|
help: add unsafe to this `extern` block
help: add `unsafe` to this `extern` block
|
LL | unsafe extern "C" {
| ++++++

View File

@ -6,9 +6,9 @@
extern "C" {
//[edition2024]~^ ERROR extern blocks must be unsafe
safe static TEST1: i32;
//~^ ERROR items in unadorned `extern` blocks cannot have safety qualifiers
//~^ ERROR items in `extern` blocks without an `unsafe` qualifier cannot have safety qualifiers
safe fn test1(i: i32);
//~^ ERROR items in unadorned `extern` blocks cannot have safety qualifiers
//~^ ERROR items in `extern` blocks without an `unsafe` qualifier cannot have safety qualifiers
}
fn test2() {

View File

@ -3,7 +3,7 @@
#![allow(dead_code)]
unsafe extern "C" {
unsafe fn foo(); //~ ERROR items in unadorned `extern` blocks cannot have safety qualifiers
unsafe fn foo(); //~ ERROR items in `extern` blocks without an `unsafe` qualifier cannot have safety qualifiers
}
fn main() {}

View File

@ -3,7 +3,7 @@
#![allow(dead_code)]
extern "C" {
unsafe fn foo(); //~ ERROR items in unadorned `extern` blocks cannot have safety qualifiers
unsafe fn foo(); //~ ERROR items in `extern` blocks without an `unsafe` qualifier cannot have safety qualifiers
}
fn main() {}

View File

@ -1,10 +1,10 @@
error: items in unadorned `extern` blocks cannot have safety qualifiers
error: items in `extern` blocks without an `unsafe` qualifier cannot have safety qualifiers
--> $DIR/unsafe-on-extern-block-issue-126756.rs:6:5
|
LL | unsafe fn foo();
| ^^^^^^^^^^^^^^^^
|
help: add unsafe to this `extern` block
help: add `unsafe` to this `extern` block
|
LL | unsafe extern "C" {
| ++++++