Skip to content

Commit 40d5f00

Browse files
committed
migrate layout_test.rs to translateable diagnostics
1 parent 0609c0f commit 40d5f00

File tree

3 files changed

+115
-33
lines changed

3 files changed

+115
-33
lines changed

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,3 +399,24 @@ passes_duplicate_diagnostic_item_in_crate =
399399
passes_diagnostic_item_first_defined =
400400
the diagnostic item is first defined here
401401
.note = the diagnostic item is first defined in crate `{$orig_crate_name}`.
402+
403+
passes_abi =
404+
abi: {$abi}
405+
406+
passes_align =
407+
align: {$align}
408+
409+
passes_size =
410+
size: {$size}
411+
412+
passes_homogeneous_aggregate =
413+
homogeneous_aggregate: {$homogeneous_aggregate}
414+
415+
passes_layout_of =
416+
layout_of({$normalized_ty}) = {$ty_layout}
417+
418+
passes_unrecognized_field =
419+
unrecognized field name `{$name}`
420+
421+
passes_layout =
422+
layout error: {$layout_error}

compiler/rustc_passes/src/errors.rs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -775,3 +775,60 @@ pub struct DuplicateDiagnosticItemInCrate {
775775
pub crate_name: Symbol,
776776
pub name: Symbol,
777777
}
778+
779+
#[derive(Diagnostic)]
780+
#[diag(passes::abi)]
781+
pub struct Abi {
782+
#[primary_span]
783+
pub span: Span,
784+
pub abi: String,
785+
}
786+
787+
#[derive(Diagnostic)]
788+
#[diag(passes::align)]
789+
pub struct Align {
790+
#[primary_span]
791+
pub span: Span,
792+
pub align: String,
793+
}
794+
795+
#[derive(Diagnostic)]
796+
#[diag(passes::size)]
797+
pub struct Size {
798+
#[primary_span]
799+
pub span: Span,
800+
pub size: String,
801+
}
802+
803+
#[derive(Diagnostic)]
804+
#[diag(passes::homogeneous_aggregate)]
805+
pub struct HomogeneousAggregate {
806+
#[primary_span]
807+
pub span: Span,
808+
pub homogeneous_aggregate: String,
809+
}
810+
811+
#[derive(Diagnostic)]
812+
#[diag(passes::layout_of)]
813+
pub struct LayoutOf {
814+
#[primary_span]
815+
pub span: Span,
816+
pub normalized_ty: String,
817+
pub ty_layout: String,
818+
}
819+
820+
#[derive(Diagnostic)]
821+
#[diag(passes::unrecognized_field)]
822+
pub struct UnrecognizedField {
823+
#[primary_span]
824+
pub span: Span,
825+
pub name: Symbol,
826+
}
827+
828+
#[derive(Diagnostic)]
829+
#[diag(passes::layout)]
830+
pub struct Layout {
831+
#[primary_span]
832+
pub span: Span,
833+
pub layout_error: String,
834+
}

compiler/rustc_passes/src/layout_test.rs

Lines changed: 37 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ use rustc_span::symbol::sym;
77
use rustc_span::Span;
88
use rustc_target::abi::{HasDataLayout, TargetDataLayout};
99

10+
use crate::errors::{Abi, Align, HomogeneousAggregate, Layout, LayoutOf, Size, UnrecognizedField};
11+
1012
pub fn test_layout(tcx: TyCtxt<'_>) {
1113
if tcx.features().rustc_attrs {
1214
// if the `rustc_attrs` feature is not enabled, don't bother testing layout
@@ -35,62 +37,64 @@ fn dump_layout_of<'tcx>(tcx: TyCtxt<'tcx>, item_def_id: LocalDefId, attr: &Attri
3537
for meta_item in meta_items {
3638
match meta_item.name_or_empty() {
3739
sym::abi => {
38-
tcx.sess.span_err(
39-
tcx.def_span(item_def_id.to_def_id()),
40-
&format!("abi: {:?}", ty_layout.abi),
41-
);
40+
tcx.sess.emit_err(Abi {
41+
span: tcx.def_span(item_def_id.to_def_id()),
42+
abi: format!("{:?}", ty_layout.abi),
43+
});
4244
}
4345

4446
sym::align => {
45-
tcx.sess.span_err(
46-
tcx.def_span(item_def_id.to_def_id()),
47-
&format!("align: {:?}", ty_layout.align),
48-
);
47+
tcx.sess.emit_err(Align {
48+
span: tcx.def_span(item_def_id.to_def_id()),
49+
align: format!("{:?}", ty_layout.align),
50+
});
4951
}
5052

5153
sym::size => {
52-
tcx.sess.span_err(
53-
tcx.def_span(item_def_id.to_def_id()),
54-
&format!("size: {:?}", ty_layout.size),
55-
);
54+
tcx.sess.emit_err(Size {
55+
span: tcx.def_span(item_def_id.to_def_id()),
56+
size: format!("{:?}", ty_layout.size),
57+
});
5658
}
5759

5860
sym::homogeneous_aggregate => {
59-
tcx.sess.span_err(
60-
tcx.def_span(item_def_id.to_def_id()),
61-
&format!(
62-
"homogeneous_aggregate: {:?}",
63-
ty_layout.homogeneous_aggregate(&UnwrapLayoutCx { tcx, param_env }),
61+
tcx.sess.emit_err(HomogeneousAggregate {
62+
span: tcx.def_span(item_def_id.to_def_id()),
63+
homogeneous_aggregate: format!(
64+
"{:?}",
65+
ty_layout.homogeneous_aggregate(&UnwrapLayoutCx { tcx, param_env })
6466
),
65-
);
67+
});
6668
}
6769

6870
sym::debug => {
69-
let normalized_ty = tcx.normalize_erasing_regions(
70-
param_env.with_reveal_all_normalized(tcx),
71-
ty,
72-
);
73-
tcx.sess.span_err(
74-
tcx.def_span(item_def_id.to_def_id()),
75-
&format!("layout_of({:?}) = {:#?}", normalized_ty, *ty_layout),
71+
let normalized_ty = format!(
72+
"{:?}",
73+
tcx.normalize_erasing_regions(
74+
param_env.with_reveal_all_normalized(tcx),
75+
ty,
76+
)
7677
);
78+
let ty_layout = format!("{:#?}", *ty_layout);
79+
tcx.sess.emit_err(LayoutOf {
80+
span: tcx.def_span(item_def_id.to_def_id()),
81+
normalized_ty,
82+
ty_layout,
83+
});
7784
}
7885

7986
name => {
80-
tcx.sess.span_err(
81-
meta_item.span(),
82-
&format!("unrecognized field name `{}`", name),
83-
);
87+
tcx.sess.emit_err(UnrecognizedField { span: meta_item.span(), name });
8488
}
8589
}
8690
}
8791
}
8892

8993
Err(layout_error) => {
90-
tcx.sess.span_err(
91-
tcx.def_span(item_def_id.to_def_id()),
92-
&format!("layout error: {:?}", layout_error),
93-
);
94+
tcx.sess.emit_err(Layout {
95+
span: tcx.def_span(item_def_id.to_def_id()),
96+
layout_error: format!("{:?}", layout_error),
97+
});
9498
}
9599
}
96100
}

0 commit comments

Comments
 (0)