rust/src/librustc_passes/layout_test.rs

143 lines
4.9 KiB
Rust
Raw Normal View History

use rustc_ast::ast::Attribute;
use rustc_hir as hir;
use rustc_hir::def_id::LocalDefId;
use rustc_hir::itemlikevisit::ItemLikeVisitor;
use rustc_hir::ItemKind;
use rustc_middle::ty::layout::{HasParamEnv, HasTyCtxt, TyAndLayout};
use rustc_middle::ty::{ParamEnv, Ty, TyCtxt};
2020-01-01 19:30:57 +01:00
use rustc_span::symbol::sym;
use rustc_target::abi::{HasDataLayout, LayoutOf, TargetDataLayout};
2019-06-21 20:27:44 +02:00
pub fn test_layout(tcx: TyCtxt<'_>) {
if tcx.features().rustc_attrs {
// if the `rustc_attrs` feature is not enabled, don't bother testing layout
2020-03-10 21:53:22 +01:00
tcx.hir().krate().visit_all_item_likes(&mut LayoutTest { tcx });
}
}
2020-03-10 21:53:22 +01:00
struct LayoutTest<'tcx> {
2019-06-14 00:48:52 +03:00
tcx: TyCtxt<'tcx>,
}
2020-03-10 21:53:22 +01:00
impl ItemLikeVisitor<'tcx> for LayoutTest<'tcx> {
2019-11-28 19:28:50 +01:00
fn visit_item(&mut self, item: &'tcx hir::Item<'tcx>) {
let item_def_id = self.tcx.hir().local_def_id(item.hir_id);
match item.kind {
2020-03-20 23:22:36 +01:00
ItemKind::TyAlias(..)
| ItemKind::Enum(..)
| ItemKind::Struct(..)
2020-05-10 11:57:58 +01:00
| ItemKind::Union(..) => {
for attr in self.tcx.get_attrs(item_def_id.to_def_id()).iter() {
if self.tcx.sess.check_name(attr, sym::rustc_layout) {
self.dump_layout_of(item_def_id, item, attr);
}
}
}
_ => {}
}
}
2019-11-28 21:47:10 +01:00
fn visit_trait_item(&mut self, _: &'tcx hir::TraitItem<'tcx>) {}
2019-11-28 22:16:44 +01:00
fn visit_impl_item(&mut self, _: &'tcx hir::ImplItem<'tcx>) {}
}
2020-03-10 21:53:22 +01:00
impl LayoutTest<'tcx> {
fn dump_layout_of(&self, item_def_id: LocalDefId, item: &hir::Item<'tcx>, attr: &Attribute) {
let tcx = self.tcx;
let param_env = self.tcx.param_env(item_def_id);
let ty = self.tcx.type_of(item_def_id);
match self.tcx.layout_of(param_env.and(ty)) {
Ok(ty_layout) => {
// Check out the `#[rustc_layout(..)]` attribute to tell what to dump.
// The `..` are the names of fields to dump.
let meta_items = attr.meta_item_list().unwrap_or_default();
for meta_item in meta_items {
match meta_item.name_or_empty() {
sym::abi => {
2019-12-22 17:42:04 -05:00
self.tcx.sess.span_err(item.span, &format!("abi: {:?}", ty_layout.abi));
}
sym::align => {
self.tcx
.sess
.span_err(item.span, &format!("align: {:?}", ty_layout.align));
}
sym::size => {
self.tcx
.sess
.span_err(item.span, &format!("size: {:?}", ty_layout.size));
}
sym::homogeneous_aggregate => {
self.tcx.sess.span_err(
item.span,
&format!(
"homogeneous_aggregate: {:?}",
ty_layout
.homogeneous_aggregate(&UnwrapLayoutCx { tcx, param_env }),
),
);
}
2020-03-10 22:25:53 +01:00
sym::debug => {
2020-07-22 01:13:42 -04:00
let normalized_ty = self.tcx.normalize_erasing_regions(
param_env.with_reveal_all_normalized(self.tcx),
ty,
);
2020-03-10 22:25:53 +01:00
self.tcx.sess.span_err(
item.span,
2020-05-10 11:57:58 +01:00
&format!("layout_of({:?}) = {:#?}", normalized_ty, *ty_layout),
2020-03-10 22:25:53 +01:00
);
}
name => {
self.tcx.sess.span_err(
meta_item.span(),
&format!("unrecognized field name `{}`", name),
);
}
}
}
}
Err(layout_error) => {
2019-12-22 17:42:04 -05:00
self.tcx.sess.span_err(item.span, &format!("layout error: {:?}", layout_error));
}
}
}
}
struct UnwrapLayoutCx<'tcx> {
2019-06-14 00:48:52 +03:00
tcx: TyCtxt<'tcx>,
param_env: ParamEnv<'tcx>,
}
impl LayoutOf for UnwrapLayoutCx<'tcx> {
type Ty = Ty<'tcx>;
2020-03-04 14:50:21 +00:00
type TyAndLayout = TyAndLayout<'tcx>;
2020-03-04 14:50:21 +00:00
fn layout_of(&self, ty: Ty<'tcx>) -> Self::TyAndLayout {
self.tcx.layout_of(self.param_env.and(ty)).unwrap()
}
}
impl HasTyCtxt<'tcx> for UnwrapLayoutCx<'tcx> {
2019-06-14 00:48:52 +03:00
fn tcx(&self) -> TyCtxt<'tcx> {
self.tcx
}
}
impl HasParamEnv<'tcx> for UnwrapLayoutCx<'tcx> {
2019-05-04 15:02:22 +05:30
fn param_env(&self) -> ParamEnv<'tcx> {
self.param_env
}
}
impl HasDataLayout for UnwrapLayoutCx<'tcx> {
fn data_layout(&self) -> &TargetDataLayout {
self.tcx.data_layout()
}
}