Skip to content

Commit bdaa0c9

Browse files
committed
Use diagnostic structs instead of manual emitted errors
1 parent 624ce6f commit bdaa0c9

File tree

3 files changed

+45
-21
lines changed

3 files changed

+45
-21
lines changed

compiler/rustc_ast_passes/messages.ftl

+12
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
ast_passes_anon_struct_or_union_not_allowed =
2+
anonymous {$struct_or_union}s are not allowed outside of unnamed struct or union fields
3+
.label = anonymous {$struct_or_union} declared here
4+
15
ast_passes_assoc_const_without_body =
26
associated constant in `impl` without body
37
.suggestion = provide a definition for the constant
@@ -162,6 +166,14 @@ ast_passes_inherent_cannot_be = inherent impls cannot be {$annotation}
162166
ast_passes_invalid_label =
163167
invalid label name `{$name}`
164168
169+
ast_passes_invalid_unnamed_field =
170+
unnamed fields are not allowed outside of structs or unions
171+
.label = unnamed field declared here
172+
173+
ast_passes_invalid_unnamed_field_ty =
174+
unnamed fields can only have struct or union types
175+
.label = not a struct or union
176+
165177
ast_passes_item_underscore = `{$kind}` items in this context need a name
166178
.label = `_` is not a valid name for this `{$kind}` item
167179

compiler/rustc_ast_passes/src/ast_validation.rs

+6-21
Original file line numberDiff line numberDiff line change
@@ -279,8 +279,6 @@ impl<'a> AstValidator<'a> {
279279
}
280280
}
281281

282-
#[allow(rustc::untranslatable_diagnostic)]
283-
#[allow(rustc::diagnostic_outside_of_impl)]
284282
fn check_unnamed_field_ty(&self, ty: &Ty, span: Span) {
285283
if matches!(
286284
&ty.kind,
@@ -293,40 +291,27 @@ impl<'a> AstValidator<'a> {
293291
) {
294292
return;
295293
}
296-
let msg = "unnamed fields can only have struct or union types";
297-
let label = "not a struct or union";
298-
self.err_handler().struct_span_err(span, msg).span_label(ty.span, label).emit();
294+
self.err_handler().emit_err(errors::InvalidUnnamedFieldTy { span, ty_span: ty.span });
299295
}
300296

301-
#[allow(rustc::untranslatable_diagnostic)]
302-
#[allow(rustc::diagnostic_outside_of_impl)]
303297
fn deny_anon_struct_or_union(&self, ty: &Ty) {
304298
let struct_or_union = match &ty.kind {
305299
TyKind::AnonStruct(..) => "struct",
306300
TyKind::AnonUnion(..) => "union",
307301
_ => return,
308302
};
309303
self.err_handler()
310-
.struct_span_err(
311-
ty.span,
312-
format!("anonymous {struct_or_union}s are not allowed outside of unnamed struct or union fields"),
313-
)
314-
.span_label(ty.span, format!("anonymous {struct_or_union} declared here"))
315-
.emit();
304+
.emit_err(errors::AnonStructOrUnionNotAllowed { struct_or_union, span: ty.span });
316305
}
317306

318-
#[allow(rustc::untranslatable_diagnostic)]
319-
#[allow(rustc::diagnostic_outside_of_impl)]
320307
fn deny_unnamed_field(&self, field: &FieldDef) {
321308
if let Some(ident) = field.ident &&
322309
ident.name == kw::Underscore {
323310
self.err_handler()
324-
.struct_span_err(
325-
field.span,
326-
"unnamed fields are not allowed outside of structs or unions",
327-
)
328-
.span_label(ident.span, "unnamed field declared here")
329-
.emit();
311+
.emit_err(errors::InvalidUnnamedField {
312+
span: field.span,
313+
ident_span: ident.span
314+
});
330315
}
331316
}
332317

compiler/rustc_ast_passes/src/errors.rs

+27
Original file line numberDiff line numberDiff line change
@@ -701,3 +701,30 @@ pub struct ConstraintOnNegativeBound {
701701
#[primary_span]
702702
pub span: Span,
703703
}
704+
705+
#[derive(Diagnostic)]
706+
#[diag(ast_passes_invalid_unnamed_field_ty)]
707+
pub struct InvalidUnnamedFieldTy {
708+
#[primary_span]
709+
pub span: Span,
710+
#[label]
711+
pub ty_span: Span,
712+
}
713+
714+
#[derive(Diagnostic)]
715+
#[diag(ast_passes_invalid_unnamed_field)]
716+
pub struct InvalidUnnamedField {
717+
#[primary_span]
718+
pub span: Span,
719+
#[label]
720+
pub ident_span: Span,
721+
}
722+
723+
#[derive(Diagnostic)]
724+
#[diag(ast_passes_anon_struct_or_union_not_allowed)]
725+
pub struct AnonStructOrUnionNotAllowed {
726+
#[primary_span]
727+
#[label]
728+
pub span: Span,
729+
pub struct_or_union: &'static str,
730+
}

0 commit comments

Comments
 (0)