migrate layout_test.rs to translateable diagnostics

This commit is contained in:
Nathan Stocks 2022-09-23 12:23:18 -06:00
parent 0609c0f1da
commit 40d5f00e16
3 changed files with 115 additions and 33 deletions

View file

@ -399,3 +399,24 @@ passes_duplicate_diagnostic_item_in_crate =
passes_diagnostic_item_first_defined = passes_diagnostic_item_first_defined =
the diagnostic item is first defined here the diagnostic item is first defined here
.note = the diagnostic item is first defined in crate `{$orig_crate_name}`. .note = the diagnostic item is first defined in crate `{$orig_crate_name}`.
passes_abi =
abi: {$abi}
passes_align =
align: {$align}
passes_size =
size: {$size}
passes_homogeneous_aggregate =
homogeneous_aggregate: {$homogeneous_aggregate}
passes_layout_of =
layout_of({$normalized_ty}) = {$ty_layout}
passes_unrecognized_field =
unrecognized field name `{$name}`
passes_layout =
layout error: {$layout_error}

View file

@ -775,3 +775,60 @@ pub struct DuplicateDiagnosticItemInCrate {
pub crate_name: Symbol, pub crate_name: Symbol,
pub name: Symbol, pub name: Symbol,
} }
#[derive(Diagnostic)]
#[diag(passes::abi)]
pub struct Abi {
#[primary_span]
pub span: Span,
pub abi: String,
}
#[derive(Diagnostic)]
#[diag(passes::align)]
pub struct Align {
#[primary_span]
pub span: Span,
pub align: String,
}
#[derive(Diagnostic)]
#[diag(passes::size)]
pub struct Size {
#[primary_span]
pub span: Span,
pub size: String,
}
#[derive(Diagnostic)]
#[diag(passes::homogeneous_aggregate)]
pub struct HomogeneousAggregate {
#[primary_span]
pub span: Span,
pub homogeneous_aggregate: String,
}
#[derive(Diagnostic)]
#[diag(passes::layout_of)]
pub struct LayoutOf {
#[primary_span]
pub span: Span,
pub normalized_ty: String,
pub ty_layout: String,
}
#[derive(Diagnostic)]
#[diag(passes::unrecognized_field)]
pub struct UnrecognizedField {
#[primary_span]
pub span: Span,
pub name: Symbol,
}
#[derive(Diagnostic)]
#[diag(passes::layout)]
pub struct Layout {
#[primary_span]
pub span: Span,
pub layout_error: String,
}

View file

@ -7,6 +7,8 @@ use rustc_span::symbol::sym;
use rustc_span::Span; use rustc_span::Span;
use rustc_target::abi::{HasDataLayout, TargetDataLayout}; use rustc_target::abi::{HasDataLayout, TargetDataLayout};
use crate::errors::{Abi, Align, HomogeneousAggregate, Layout, LayoutOf, Size, UnrecognizedField};
pub fn test_layout(tcx: TyCtxt<'_>) { pub fn test_layout(tcx: TyCtxt<'_>) {
if tcx.features().rustc_attrs { if tcx.features().rustc_attrs {
// if the `rustc_attrs` feature is not enabled, don't bother testing layout // 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
for meta_item in meta_items { for meta_item in meta_items {
match meta_item.name_or_empty() { match meta_item.name_or_empty() {
sym::abi => { sym::abi => {
tcx.sess.span_err( tcx.sess.emit_err(Abi {
tcx.def_span(item_def_id.to_def_id()), span: tcx.def_span(item_def_id.to_def_id()),
&format!("abi: {:?}", ty_layout.abi), abi: format!("{:?}", ty_layout.abi),
); });
} }
sym::align => { sym::align => {
tcx.sess.span_err( tcx.sess.emit_err(Align {
tcx.def_span(item_def_id.to_def_id()), span: tcx.def_span(item_def_id.to_def_id()),
&format!("align: {:?}", ty_layout.align), align: format!("{:?}", ty_layout.align),
); });
} }
sym::size => { sym::size => {
tcx.sess.span_err( tcx.sess.emit_err(Size {
tcx.def_span(item_def_id.to_def_id()), span: tcx.def_span(item_def_id.to_def_id()),
&format!("size: {:?}", ty_layout.size), size: format!("{:?}", ty_layout.size),
); });
} }
sym::homogeneous_aggregate => { sym::homogeneous_aggregate => {
tcx.sess.span_err( tcx.sess.emit_err(HomogeneousAggregate {
tcx.def_span(item_def_id.to_def_id()), span: tcx.def_span(item_def_id.to_def_id()),
&format!( homogeneous_aggregate: format!(
"homogeneous_aggregate: {:?}", "{:?}",
ty_layout.homogeneous_aggregate(&UnwrapLayoutCx { tcx, param_env }), ty_layout.homogeneous_aggregate(&UnwrapLayoutCx { tcx, param_env })
), ),
); });
} }
sym::debug => { sym::debug => {
let normalized_ty = tcx.normalize_erasing_regions( let normalized_ty = format!(
param_env.with_reveal_all_normalized(tcx), "{:?}",
ty, tcx.normalize_erasing_regions(
); param_env.with_reveal_all_normalized(tcx),
tcx.sess.span_err( ty,
tcx.def_span(item_def_id.to_def_id()), )
&format!("layout_of({:?}) = {:#?}", normalized_ty, *ty_layout),
); );
let ty_layout = format!("{:#?}", *ty_layout);
tcx.sess.emit_err(LayoutOf {
span: tcx.def_span(item_def_id.to_def_id()),
normalized_ty,
ty_layout,
});
} }
name => { name => {
tcx.sess.span_err( tcx.sess.emit_err(UnrecognizedField { span: meta_item.span(), name });
meta_item.span(),
&format!("unrecognized field name `{}`", name),
);
} }
} }
} }
} }
Err(layout_error) => { Err(layout_error) => {
tcx.sess.span_err( tcx.sess.emit_err(Layout {
tcx.def_span(item_def_id.to_def_id()), span: tcx.def_span(item_def_id.to_def_id()),
&format!("layout error: {:?}", layout_error), layout_error: format!("{:?}", layout_error),
); });
} }
} }
} }