Skip to content

Commit 2aea631

Browse files
committed
Migrate ForeignItemNonAscii, ForbiddenCVarArgs, UnnamedAssocConst, NomangleItemNonAscii
1 parent 79ad624 commit 2aea631

File tree

3 files changed

+78
-45
lines changed

3 files changed

+78
-45
lines changed

compiler/rustc_ast_passes/src/ast_validation.rs

Lines changed: 13 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -473,18 +473,10 @@ impl<'a> AstValidator<'a> {
473473
/// An item in `extern { ... }` cannot use non-ascii identifier.
474474
fn check_foreign_item_ascii_only(&self, ident: Ident) {
475475
if !ident.as_str().is_ascii() {
476-
let n = 83942;
477-
self.err_handler()
478-
.struct_span_err(
479-
ident.span,
480-
"items in `extern` blocks cannot use non-ascii identifiers",
481-
)
482-
.span_label(self.current_extern_span(), "in this `extern` block")
483-
.note(&format!(
484-
"this limitation may be lifted in the future; see issue #{} <https://github.com/rust-lang/rust/issues/{}> for more information",
485-
n, n,
486-
))
487-
.emit();
476+
self.session.emit_err(ForeignItemNonAscii {
477+
span: ident.span,
478+
extern_span: self.current_extern_span(),
479+
});
488480
}
489481
}
490482

@@ -507,53 +499,34 @@ impl<'a> AstValidator<'a> {
507499

508500
for Param { ty, span, .. } in &fk.decl().inputs {
509501
if let TyKind::CVarArgs = ty.kind {
510-
self.err_handler()
511-
.struct_span_err(
512-
*span,
513-
"only foreign or `unsafe extern \"C\"` functions may be C-variadic",
514-
)
515-
.emit();
502+
self.session.emit_err(ForbiddenCVarArgs { span: *span });
516503
}
517504
}
518505
}
519506

520-
fn check_item_named(&self, ident: Ident, kind: &str) {
507+
fn check_item_named<D>(&self, ident: Ident, create_diag: impl FnOnce(Span) -> D)
508+
where
509+
D: SessionDiagnostic<'a>,
510+
{
521511
if ident.name != kw::Underscore {
522512
return;
523513
}
524-
self.err_handler()
525-
.struct_span_err(ident.span, &format!("`{}` items in this context need a name", kind))
526-
.span_label(ident.span, format!("`_` is not a valid name for this `{}` item", kind))
527-
.emit();
514+
self.session.emit_err(create_diag(ident.span));
528515
}
529516

530517
fn check_nomangle_item_asciionly(&self, ident: Ident, item_span: Span) {
531518
if ident.name.as_str().is_ascii() {
532519
return;
533520
}
534521
let head_span = self.session.source_map().guess_head_span(item_span);
535-
struct_span_err!(
536-
self.session,
537-
head_span,
538-
E0754,
539-
"`#[no_mangle]` requires ASCII identifier"
540-
)
541-
.emit();
522+
self.session.emit_err(NomangleItemNonAscii { span: head_span });
542523
}
543524

544525
fn check_mod_file_item_asciionly(&self, ident: Ident) {
545526
if ident.name.as_str().is_ascii() {
546527
return;
547528
}
548-
struct_span_err!(
549-
self.session,
550-
ident.span,
551-
E0754,
552-
"trying to load file for module `{}` with non-ascii identifier name",
553-
ident.name
554-
)
555-
.help("consider using `#[path]` attribute to specify filesystem path")
556-
.emit();
529+
self.session.emit_err(ModFileItemNonAscii { span: ident.span, name: ident.name });
557530
}
558531

559532
fn deny_generic_params(&self, generics: &Generics, ident_span: Span) {
@@ -1537,7 +1510,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
15371510
}
15381511

15391512
if let AssocItemKind::Const(..) = item.kind {
1540-
self.check_item_named(item.ident, "const");
1513+
self.check_item_named(item.ident, |span| UnnamedAssocConst { span });
15411514
}
15421515

15431516
match &item.kind {

compiler/rustc_ast_passes/src/errors.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,3 +318,44 @@ pub struct ForeignFnWithQualifier {
318318
#[suggestion_verbose(code = "fn ", applicability = "maybe-incorrect")]
319319
pub replace_span: Span,
320320
}
321+
322+
#[derive(SessionDiagnostic)]
323+
#[diag(ast_passes::foreign_item_non_ascii)]
324+
#[note]
325+
pub struct ForeignItemNonAscii {
326+
#[primary_span]
327+
pub span: Span,
328+
#[label(ast_passes::extern_block_label)]
329+
pub extern_span: Span,
330+
}
331+
332+
#[derive(SessionDiagnostic)]
333+
#[diag(ast_passes::forbidden_c_var_args)]
334+
pub struct ForbiddenCVarArgs {
335+
#[primary_span]
336+
pub span: Span,
337+
}
338+
339+
#[derive(SessionDiagnostic)]
340+
#[diag(ast_passes::unnamed_assoc_const)]
341+
pub struct UnnamedAssocConst {
342+
#[primary_span]
343+
#[label]
344+
pub span: Span,
345+
}
346+
347+
#[derive(SessionDiagnostic)]
348+
#[diag(ast_passes::nomangle_item_non_ascii, code = "E0754")]
349+
pub struct NomangleItemNonAscii {
350+
#[primary_span]
351+
pub span: Span,
352+
}
353+
354+
#[derive(SessionDiagnostic)]
355+
#[diag(ast_passes::mod_file_item_non_ascii, code = "E0754")]
356+
#[help]
357+
pub struct ModFileItemNonAscii {
358+
#[primary_span]
359+
pub span: Span,
360+
pub name: Symbol,
361+
}

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

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -107,37 +107,56 @@ 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 }
110+
.more_extern_note = {-ast_passes_more_extern}
111111
112112
ast_passes_foreign_ty_with_where_clause =
113113
`type`s inside `extern` blocks cannot have `where` clauses
114114
.suggestion = remove the `where` clause
115115
.extern_block_label = `extern` block begins here
116-
.more_extern_note = { -ast_passes_more_extern }
116+
.more_extern_note = {-ast_passes_more_extern}
117117
118118
ast_passes_foreign_ty_with_body =
119119
incorrect `type` inside `extern` block
120120
.label = cannot have a body
121121
.body_label = the invalid body
122122
.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 }
123+
.more_extern_note = {-ast_passes_more_extern}
124124
125125
ast_passes_foreign_static_with_body =
126126
incorrect `static` inside `extern` block
127127
.label = cannot have a 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
130-
.more_extern_note = { -ast_passes_more_extern }
130+
.more_extern_note = {-ast_passes_more_extern}
131131
132132
ast_passes_foreign_fn_with_body =
133133
incorrect function inside `extern` block
134134
.label = cannot have a body
135135
.suggestion = remove the invalid body
136136
.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
137137
.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 }
138+
.more_extern_note = {-ast_passes_more_extern}
139139
140140
ast_passes_foreign_fn_with_qualifier =
141141
functions in `extern` blocks cannot have qualifiers
142142
.extern_block_label = in this `extern` block
143143
.suggestion = remove the qualifiers
144+
145+
ast_passes_foreign_item_non_ascii =
146+
items in `extern` blocks cannot use non-ascii identifiers
147+
.extern_block_label = in this `extern` block
148+
.note = this limitation may be lifted in the future; see issue #83942 <https://github.com/rust-lang/rust/issues/83942> for more information
149+
150+
ast_passes_forbidden_c_var_args =
151+
only foreign or `unsafe extern "C"` functions may be C-variadic
152+
153+
ast_passes_unnamed_assoc_const =
154+
`const` items in this context need a name
155+
.label = `_` is not a valid name for this `const` item
156+
157+
ast_passes_nomangle_item_non_ascii =
158+
`#[no_mangle]` requires ASCII identifier
159+
160+
ast_passes_mod_file_item_non_ascii =
161+
trying to load file for module `{$name}` with non-ascii identifier name
162+
.help = consider using `#[path]` attribute to specify filesystem path

0 commit comments

Comments
 (0)