Skip to content

Commit 79ad624

Browse files
committed
Migrate ForeignFnWith(Body/Qualifier) and fix prev
1 parent 8384368 commit 79ad624

File tree

3 files changed

+50
-33
lines changed

3 files changed

+50
-33
lines changed

compiler/rustc_ast_passes/src/ast_validation.rs

Lines changed: 10 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,6 @@ use std::ops::{Deref, DerefMut};
3030

3131
use crate::errors::*;
3232

33-
const MORE_EXTERN: &str =
34-
"for more information, visit https://doc.rust-lang.org/std/keyword.extern.html";
35-
3633
/// Is `self` allowed semantically as the first parameter in an `FnDecl`?
3734
enum SelfSemantic {
3835
Yes,
@@ -451,26 +448,11 @@ impl<'a> AstValidator<'a> {
451448
let Some(body) = body else {
452449
return;
453450
};
454-
self.err_handler()
455-
.struct_span_err(ident.span, "incorrect function inside `extern` block")
456-
.span_label(ident.span, "cannot have a body")
457-
.span_suggestion(
458-
body.span,
459-
"remove the invalid body",
460-
";",
461-
Applicability::MaybeIncorrect,
462-
)
463-
.help(
464-
"you might have meant to write a function accessible through FFI, \
465-
which can be done by writing `extern fn` outside of the `extern` block",
466-
)
467-
.span_label(
468-
self.current_extern_span(),
469-
"`extern` blocks define existing foreign functions and functions \
470-
inside of them cannot have a body",
471-
)
472-
.note(MORE_EXTERN)
473-
.emit();
451+
self.session.emit_err(ForeignFnWithBody {
452+
span: ident.span,
453+
body_span: body.span,
454+
extern_span: self.current_extern_span(),
455+
});
474456
}
475457

476458
fn current_extern_span(&self) -> Span {
@@ -480,16 +462,11 @@ impl<'a> AstValidator<'a> {
480462
/// An `fn` in `extern { ... }` cannot have qualifiers, e.g. `async fn`.
481463
fn check_foreign_fn_headerless(&self, ident: Ident, span: Span, header: FnHeader) {
482464
if header.has_qualifiers() {
483-
self.err_handler()
484-
.struct_span_err(ident.span, "functions in `extern` blocks cannot have qualifiers")
485-
.span_label(self.current_extern_span(), "in this `extern` block")
486-
.span_suggestion_verbose(
487-
span.until(ident.span.shrink_to_lo()),
488-
"remove the qualifiers",
489-
"fn ",
490-
Applicability::MaybeIncorrect,
491-
)
492-
.emit();
465+
self.session.emit_err(ForeignFnWithQualifier {
466+
span: ident.span,
467+
extern_span: self.current_extern_span(),
468+
replace_span: span.until(ident.span.shrink_to_lo()),
469+
});
493470
}
494471
}
495472

compiler/rustc_ast_passes/src/errors.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,7 @@ pub struct ForeignTyWithWhereClause {
273273
#[note(ast_passes::more_extern_note)]
274274
pub struct ForeignTyWithBody {
275275
#[primary_span]
276+
#[label]
276277
pub span: Span,
277278
#[label(ast_passes::body_label)]
278279
pub body_span: Span,
@@ -285,9 +286,35 @@ pub struct ForeignTyWithBody {
285286
#[note(ast_passes::more_extern_note)]
286287
pub struct ForeignStaticWithBody {
287288
#[primary_span]
289+
#[label]
288290
pub span: Span,
289291
#[label(ast_passes::body_label)]
290292
pub body_span: Span,
291293
#[label(ast_passes::extern_block_label)]
292294
pub extern_span: Span,
293295
}
296+
297+
#[derive(SessionDiagnostic)]
298+
#[diag(ast_passes::foreign_fn_with_body)]
299+
#[help]
300+
#[note(ast_passes::more_extern_note)]
301+
pub struct ForeignFnWithBody {
302+
#[primary_span]
303+
#[label]
304+
pub span: Span,
305+
#[suggestion(code = ";", applicability = "maybe-incorrect")]
306+
pub body_span: Span,
307+
#[label(ast_passes::extern_block_label)]
308+
pub extern_span: Span,
309+
}
310+
311+
#[derive(SessionDiagnostic)]
312+
#[diag(ast_passes::foreign_fn_with_qualifier)]
313+
pub struct ForeignFnWithQualifier {
314+
#[primary_span]
315+
pub span: Span,
316+
#[label(ast_passes::extern_block_label)]
317+
pub extern_span: Span,
318+
#[suggestion_verbose(code = "fn ", applicability = "maybe-incorrect")]
319+
pub replace_span: Span,
320+
}

compiler/rustc_error_messages/locales/en-US/ast_passes.ftl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,3 +128,16 @@ ast_passes_foreign_static_with_body =
128128
.body_label = the invalid body
129129
.extern_block_label = `extern` blocks define existing foreign statics and statics inside of them cannot have a body
130130
.more_extern_note = { -ast_passes_more_extern }
131+
132+
ast_passes_foreign_fn_with_body =
133+
incorrect function inside `extern` block
134+
.label = cannot have a body
135+
.suggestion = remove the invalid body
136+
.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
137+
.extern_block_label = `extern` blocks define existing foreign functions and functions inside of them cannot have a body
138+
.more_extern_note = { -ast_passes_more_extern }
139+
140+
ast_passes_foreign_fn_with_qualifier =
141+
functions in `extern` blocks cannot have qualifiers
142+
.extern_block_label = in this `extern` block
143+
.suggestion = remove the qualifiers

0 commit comments

Comments
 (0)