Skip to content

Commit a24f5ec

Browse files
authored
Unrolled build for rust-lang#123397
Rollup merge of rust-lang#123397 - krtab:foreign_fn_qualif_diag, r=petrochenkov Fix diagnostic for qualifier in extern block Closes: rust-lang#123306
2 parents ca7d34e + 109daa2 commit a24f5ec

10 files changed

+91
-70
lines changed

compiler/rustc_ast/src/ast.rs

+8
Original file line numberDiff line numberDiff line change
@@ -2484,6 +2484,14 @@ pub enum CoroutineKind {
24842484
}
24852485

24862486
impl CoroutineKind {
2487+
pub fn span(self) -> Span {
2488+
match self {
2489+
CoroutineKind::Async { span, .. } => span,
2490+
CoroutineKind::Gen { span, .. } => span,
2491+
CoroutineKind::AsyncGen { span, .. } => span,
2492+
}
2493+
}
2494+
24872495
pub fn is_async(self) -> bool {
24882496
matches!(self, CoroutineKind::Async { .. })
24892497
}

compiler/rustc_ast_passes/messages.ftl

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ ast_passes_extern_block_suggestion = if you meant to declare an externally defin
6868
6969
ast_passes_extern_fn_qualifiers = functions in `extern` blocks cannot have qualifiers
7070
.label = in this `extern` block
71-
.suggestion = remove the qualifiers
71+
.suggestion = remove this qualifier
7272
7373
ast_passes_extern_item_ascii = items in `extern` blocks cannot use non-ascii identifiers
7474
.label = in this `extern` block

compiler/rustc_ast_passes/src/ast_validation.rs

+24-5
Original file line numberDiff line numberDiff line change
@@ -514,13 +514,32 @@ impl<'a> AstValidator<'a> {
514514
}
515515

516516
/// An `fn` in `extern { ... }` cannot have qualifiers, e.g. `async fn`.
517-
fn check_foreign_fn_headerless(&self, ident: Ident, span: Span, header: FnHeader) {
518-
if header.has_qualifiers() {
517+
fn check_foreign_fn_headerless(
518+
&self,
519+
// Deconstruct to ensure exhaustiveness
520+
FnHeader { unsafety, coroutine_kind, constness, ext }: FnHeader,
521+
) {
522+
let report_err = |span| {
519523
self.dcx().emit_err(errors::FnQualifierInExtern {
520-
span: ident.span,
524+
span: span,
521525
block: self.current_extern_span(),
522-
sugg_span: span.until(ident.span.shrink_to_lo()),
523526
});
527+
};
528+
match unsafety {
529+
Unsafe::Yes(span) => report_err(span),
530+
Unsafe::No => (),
531+
}
532+
match coroutine_kind {
533+
Some(knd) => report_err(knd.span()),
534+
None => (),
535+
}
536+
match constness {
537+
Const::Yes(span) => report_err(span),
538+
Const::No => (),
539+
}
540+
match ext {
541+
Extern::None => (),
542+
Extern::Implicit(span) | Extern::Explicit(_, span) => report_err(span),
524543
}
525544
}
526545

@@ -1145,7 +1164,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
11451164
ForeignItemKind::Fn(box Fn { defaultness, sig, body, .. }) => {
11461165
self.check_defaultness(fi.span, *defaultness);
11471166
self.check_foreign_fn_bodyless(fi.ident, body.as_deref());
1148-
self.check_foreign_fn_headerless(fi.ident, fi.span, sig.header);
1167+
self.check_foreign_fn_headerless(sig.header);
11491168
self.check_foreign_item_ascii_only(fi.ident);
11501169
}
11511170
ForeignItemKind::TyAlias(box TyAlias {

compiler/rustc_ast_passes/src/errors.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -270,11 +270,10 @@ pub struct FnBodyInExtern {
270270
#[diag(ast_passes_extern_fn_qualifiers)]
271271
pub struct FnQualifierInExtern {
272272
#[primary_span]
273+
#[suggestion(code = "", applicability = "maybe-incorrect")]
273274
pub span: Span,
274275
#[label]
275276
pub block: Span,
276-
#[suggestion(code = "fn ", applicability = "maybe-incorrect", style = "verbose")]
277-
pub sugg_span: Span,
278277
}
279278

280279
#[derive(Diagnostic)]

tests/ui/extern/issue-95829.stderr

+2-7
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,12 @@ LL | | }
1616
= note: for more information, visit https://doc.rust-lang.org/std/keyword.extern.html
1717

1818
error: functions in `extern` blocks cannot have qualifiers
19-
--> $DIR/issue-95829.rs:4:14
19+
--> $DIR/issue-95829.rs:4:5
2020
|
2121
LL | extern {
2222
| ------ in this `extern` block
2323
LL | async fn L() {
24-
| ^
25-
|
26-
help: remove the qualifiers
27-
|
28-
LL | fn L() {
29-
| ~~
24+
| ^^^^^ help: remove this qualifier
3025

3126
error: aborting due to 2 previous errors
3227

tests/ui/parser/fn-header-semantic-fail.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ fn main() {
4848
const fn fe3(); //~ ERROR functions in `extern` blocks cannot have qualifiers
4949
extern "C" fn fe4(); //~ ERROR functions in `extern` blocks cannot have qualifiers
5050
const async unsafe extern "C" fn fe5(); //~ ERROR functions in `extern` blocks
51-
//~^ ERROR functions cannot be both `const` and `async`
51+
//~| ERROR functions in `extern` blocks
52+
//~| ERROR functions in `extern` blocks
53+
//~| ERROR functions in `extern` blocks
54+
//~| ERROR functions cannot be both `const` and `async`
5255
}
5356
}

tests/ui/parser/fn-header-semantic-fail.stderr

+35-33
Original file line numberDiff line numberDiff line change
@@ -71,73 +71,75 @@ LL | const async unsafe extern "C" fn fi5() {}
7171
| `const` because of this
7272

7373
error: functions in `extern` blocks cannot have qualifiers
74-
--> $DIR/fn-header-semantic-fail.rs:46:18
74+
--> $DIR/fn-header-semantic-fail.rs:46:9
7575
|
7676
LL | extern "C" {
7777
| ---------- in this `extern` block
7878
LL | async fn fe1();
79-
| ^^^
80-
|
81-
help: remove the qualifiers
82-
|
83-
LL | fn fe1();
84-
| ~~
79+
| ^^^^^ help: remove this qualifier
8580

8681
error: functions in `extern` blocks cannot have qualifiers
87-
--> $DIR/fn-header-semantic-fail.rs:47:19
82+
--> $DIR/fn-header-semantic-fail.rs:47:9
8883
|
8984
LL | extern "C" {
9085
| ---------- in this `extern` block
9186
LL | async fn fe1();
9287
LL | unsafe fn fe2();
93-
| ^^^
94-
|
95-
help: remove the qualifiers
96-
|
97-
LL | fn fe2();
98-
| ~~
88+
| ^^^^^^ help: remove this qualifier
9989

10090
error: functions in `extern` blocks cannot have qualifiers
101-
--> $DIR/fn-header-semantic-fail.rs:48:18
91+
--> $DIR/fn-header-semantic-fail.rs:48:9
10292
|
10393
LL | extern "C" {
10494
| ---------- in this `extern` block
10595
...
10696
LL | const fn fe3();
107-
| ^^^
108-
|
109-
help: remove the qualifiers
110-
|
111-
LL | fn fe3();
112-
| ~~
97+
| ^^^^^ help: remove this qualifier
11398

11499
error: functions in `extern` blocks cannot have qualifiers
115-
--> $DIR/fn-header-semantic-fail.rs:49:23
100+
--> $DIR/fn-header-semantic-fail.rs:49:9
116101
|
117102
LL | extern "C" {
118103
| ---------- in this `extern` block
119104
...
120105
LL | extern "C" fn fe4();
121-
| ^^^
122-
|
123-
help: remove the qualifiers
106+
| ^^^^^^^^^^ help: remove this qualifier
107+
108+
error: functions in `extern` blocks cannot have qualifiers
109+
--> $DIR/fn-header-semantic-fail.rs:50:21
124110
|
125-
LL | fn fe4();
126-
| ~~
111+
LL | extern "C" {
112+
| ---------- in this `extern` block
113+
...
114+
LL | const async unsafe extern "C" fn fe5();
115+
| ^^^^^^ help: remove this qualifier
127116

128117
error: functions in `extern` blocks cannot have qualifiers
129-
--> $DIR/fn-header-semantic-fail.rs:50:42
118+
--> $DIR/fn-header-semantic-fail.rs:50:15
130119
|
131120
LL | extern "C" {
132121
| ---------- in this `extern` block
133122
...
134123
LL | const async unsafe extern "C" fn fe5();
135-
| ^^^
124+
| ^^^^^ help: remove this qualifier
125+
126+
error: functions in `extern` blocks cannot have qualifiers
127+
--> $DIR/fn-header-semantic-fail.rs:50:9
136128
|
137-
help: remove the qualifiers
129+
LL | extern "C" {
130+
| ---------- in this `extern` block
131+
...
132+
LL | const async unsafe extern "C" fn fe5();
133+
| ^^^^^ help: remove this qualifier
134+
135+
error: functions in `extern` blocks cannot have qualifiers
136+
--> $DIR/fn-header-semantic-fail.rs:50:28
138137
|
139-
LL | fn fe5();
140-
| ~~
138+
LL | extern "C" {
139+
| ---------- in this `extern` block
140+
...
141+
LL | const async unsafe extern "C" fn fe5();
142+
| ^^^^^^^^^^ help: remove this qualifier
141143

142144
error: functions cannot be both `const` and `async`
143145
--> $DIR/fn-header-semantic-fail.rs:50:9
@@ -148,6 +150,6 @@ LL | const async unsafe extern "C" fn fe5();
148150
| | `async` because of this
149151
| `const` because of this
150152

151-
error: aborting due to 14 previous errors
153+
error: aborting due to 17 previous errors
152154

153155
For more information about this error, try `rustc --explain E0379`.

tests/ui/parser/no-const-fn-in-extern-block.rs

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ extern "C" {
33
//~^ ERROR functions in `extern` blocks cannot have qualifiers
44
const unsafe fn bar();
55
//~^ ERROR functions in `extern` blocks cannot have qualifiers
6+
//~| ERROR functions in `extern` blocks cannot have qualifiers
67
}
78

89
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,28 @@
11
error: functions in `extern` blocks cannot have qualifiers
2-
--> $DIR/no-const-fn-in-extern-block.rs:2:14
2+
--> $DIR/no-const-fn-in-extern-block.rs:2:5
33
|
44
LL | extern "C" {
55
| ---------- in this `extern` block
66
LL | const fn foo();
7-
| ^^^
8-
|
9-
help: remove the qualifiers
10-
|
11-
LL | fn foo();
12-
| ~~
7+
| ^^^^^ help: remove this qualifier
138

149
error: functions in `extern` blocks cannot have qualifiers
15-
--> $DIR/no-const-fn-in-extern-block.rs:4:21
10+
--> $DIR/no-const-fn-in-extern-block.rs:4:11
1611
|
1712
LL | extern "C" {
1813
| ---------- in this `extern` block
1914
...
2015
LL | const unsafe fn bar();
21-
| ^^^
22-
|
23-
help: remove the qualifiers
16+
| ^^^^^^ help: remove this qualifier
17+
18+
error: functions in `extern` blocks cannot have qualifiers
19+
--> $DIR/no-const-fn-in-extern-block.rs:4:5
2420
|
25-
LL | fn bar();
26-
| ~~
21+
LL | extern "C" {
22+
| ---------- in this `extern` block
23+
...
24+
LL | const unsafe fn bar();
25+
| ^^^^^ help: remove this qualifier
2726

28-
error: aborting due to 2 previous errors
27+
error: aborting due to 3 previous errors
2928

tests/ui/parser/unsafe-foreign-mod-2.stderr

+2-7
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,13 @@ LL | extern "C" unsafe {
1111
| ^^^^^^
1212

1313
error: functions in `extern` blocks cannot have qualifiers
14-
--> $DIR/unsafe-foreign-mod-2.rs:4:15
14+
--> $DIR/unsafe-foreign-mod-2.rs:4:5
1515
|
1616
LL | extern "C" unsafe {
1717
| ----------------- in this `extern` block
1818
...
1919
LL | unsafe fn foo();
20-
| ^^^
21-
|
22-
help: remove the qualifiers
23-
|
24-
LL | fn foo();
25-
| ~~
20+
| ^^^^^^ help: remove this qualifier
2621

2722
error: aborting due to 3 previous errors
2823

0 commit comments

Comments
 (0)