Skip to content

Commit 69cb057

Browse files
committed
Migrate check_foreign_kind_bodyless and fix prev
1 parent 92b96e6 commit 69cb057

File tree

3 files changed

+50
-20
lines changed

3 files changed

+50
-20
lines changed

compiler/rustc_ast_passes/src/ast_validation.rs

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -432,24 +432,14 @@ impl<'a> AstValidator<'a> {
432432
}
433433
}
434434

435-
fn check_foreign_kind_bodyless(&self, ident: Ident, kind: &str, body: Option<Span>) {
435+
fn check_foreign_kind_bodyless<D>(&self, ident: Ident, create_diag: impl FnOnce(Span, Span, Span) -> D, body: Option<Span>)
436+
where
437+
D: SessionDiagnostic<'a>,
438+
{
436439
let Some(body) = body else {
437440
return;
438441
};
439-
self.err_handler()
440-
.struct_span_err(ident.span, &format!("incorrect `{}` inside `extern` block", kind))
441-
.span_label(ident.span, "cannot have a body")
442-
.span_label(body, "the invalid body")
443-
.span_label(
444-
self.current_extern_span(),
445-
format!(
446-
"`extern` blocks define existing foreign {0}s and {0}s \
447-
inside of them cannot have a body",
448-
kind
449-
),
450-
)
451-
.note(MORE_EXTERN)
452-
.emit();
442+
self.session.emit_err(create_diag(ident.span, body, self.current_extern_span()));
453443
}
454444

455445
/// An `fn` in `extern { ... }` cannot have a body `{ ... }`.
@@ -1230,13 +1220,13 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
12301220
..
12311221
}) => {
12321222
self.check_defaultness(fi.span, *defaultness);
1233-
self.check_foreign_kind_bodyless(fi.ident, "type", ty.as_ref().map(|b| b.span));
1223+
self.check_foreign_kind_bodyless(fi.ident, |span, body_span, extern_span| ForeignTyWithBody { span, body_span, extern_span }, ty.as_ref().map(|b| b.span));
12341224
self.check_type_no_bounds(bounds, |span| ForeignTyWithBound { span });
12351225
self.check_foreign_ty_genericless(generics, where_clauses.0.1);
12361226
self.check_foreign_item_ascii_only(fi.ident);
12371227
}
12381228
ForeignItemKind::Static(_, _, body) => {
1239-
self.check_foreign_kind_bodyless(fi.ident, "static", body.as_ref().map(|b| b.span));
1229+
self.check_foreign_kind_bodyless(fi.ident, |span, body_span, extern_span| ForeignStaticWithBody { span, body_span, extern_span }, body.as_ref().map(|b| b.span));
12401230
self.check_foreign_item_ascii_only(fi.ident);
12411231
}
12421232
ForeignItemKind::MacCall(..) => {}

compiler/rustc_ast_passes/src/errors.rs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ pub struct ForeignTyWithGenericParam {
253253
#[primary_span]
254254
#[suggestion(code = "", applicability = "maybe-incorrect")]
255255
pub span: Span,
256-
#[label]
256+
#[label(ast_passes::extern_block_label)]
257257
pub extern_span: Span,
258258
}
259259

@@ -264,6 +264,30 @@ pub struct ForeignTyWithWhereClause {
264264
#[primary_span]
265265
#[suggestion(code = "", applicability = "maybe-incorrect")]
266266
pub span: Span,
267-
#[label]
267+
#[label(ast_passes::extern_block_label)]
268+
pub extern_span: Span,
269+
}
270+
271+
#[derive(SessionDiagnostic)]
272+
#[diag(ast_passes::foreign_ty_with_body)]
273+
#[note(ast_passes::more_extern_note)]
274+
pub struct ForeignTyWithBody {
275+
#[primary_span]
276+
pub span: Span,
277+
#[label(ast_passes::body_label)]
278+
pub body_span: Span,
279+
#[label(ast_passes::extern_block_label)]
280+
pub extern_span: Span,
281+
}
282+
283+
#[derive(SessionDiagnostic)]
284+
#[diag(ast_passes::foreign_static_with_body)]
285+
#[note(ast_passes::more_extern_note)]
286+
pub struct ForeignStaticWithBody {
287+
#[primary_span]
288+
pub span: Span,
289+
#[label(ast_passes::body_label)]
290+
pub body_span: Span,
291+
#[label(ast_passes::extern_block_label)]
268292
pub extern_span: Span,
269293
}

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
ast_passes_more_extern_note =
1+
-ast_passes_more_extern =
22
for more information, visit https://doc.rust-lang.org/std/keyword.extern.html
33
44
ast_passes_forbidden_let =
@@ -107,8 +107,24 @@ ast_passes_foreign_ty_with_generic_param =
107107
`type`s inside `extern` blocks cannot have generic parameters
108108
.suggestion = remove the generic parameters
109109
.extern_block_label = `extern` block begins here
110+
.more_extern_note = { -ast_passes_more_extern }
110111
111112
ast_passes_foreign_ty_with_where_clause =
112113
`type`s inside `extern` blocks cannot have `where` clauses
113114
.suggestion = remove the `where` clause
114115
.extern_block_label = `extern` block begins here
116+
.more_extern_note = { -ast_passes_more_extern }
117+
118+
ast_passes_foreign_ty_with_body =
119+
incorrect `type` inside `extern` block
120+
.label = cannot have a body
121+
.body_label = the invalid body
122+
.extern_block_label = `extern` blocks define existing foreign types and types inside of them cannot have a body
123+
.more_extern_note = { -ast_passes_more_extern }
124+
125+
ast_passes_foreign_static_with_body =
126+
incorrect `static` inside `extern` block
127+
.label = cannot have a body
128+
.body_label = the invalid body
129+
.extern_block_label = `extern` blocks define existing foreign statics and statics inside of them cannot have a body
130+
.more_extern_note = { -ast_passes_more_extern }

0 commit comments

Comments
 (0)