1 use rustc_ast::ast::Attribute;
3 use rustc_hir::def_id::LocalDefId;
4 use rustc_hir::itemlikevisit::ItemLikeVisitor;
5 use rustc_hir::ItemKind;
6 use rustc_middle::ty::layout::{HasParamEnv, HasTyCtxt, TyAndLayout};
7 use rustc_middle::ty::{ParamEnv, Ty, TyCtxt};
8 use rustc_span::symbol::sym;
9 use rustc_target::abi::{HasDataLayout, LayoutOf, TargetDataLayout};
11 pub fn test_layout(tcx: TyCtxt<'_>) {
12 if tcx.features().rustc_attrs {
13 // if the `rustc_attrs` feature is not enabled, don't bother testing layout
14 tcx.hir().krate().visit_all_item_likes(&mut LayoutTest { tcx });
18 struct LayoutTest<'tcx> {
22 impl ItemLikeVisitor<'tcx> for LayoutTest<'tcx> {
23 fn visit_item(&mut self, item: &'tcx hir::Item<'tcx>) {
24 let item_def_id = self.tcx.hir().local_def_id(item.hir_id);
29 | ItemKind::Struct(..)
30 | ItemKind::Union(..) => {
31 for attr in self.tcx.get_attrs(item_def_id.to_def_id()).iter() {
32 if self.tcx.sess.check_name(attr, sym::rustc_layout) {
33 self.dump_layout_of(item_def_id, item, attr);
41 fn visit_trait_item(&mut self, _: &'tcx hir::TraitItem<'tcx>) {}
42 fn visit_impl_item(&mut self, _: &'tcx hir::ImplItem<'tcx>) {}
45 impl LayoutTest<'tcx> {
46 fn dump_layout_of(&self, item_def_id: LocalDefId, item: &hir::Item<'tcx>, attr: &Attribute) {
48 let param_env = self.tcx.param_env(item_def_id);
49 let ty = self.tcx.type_of(item_def_id);
50 match self.tcx.layout_of(param_env.and(ty)) {
52 // Check out the `#[rustc_layout(..)]` attribute to tell what to dump.
53 // The `..` are the names of fields to dump.
54 let meta_items = attr.meta_item_list().unwrap_or_default();
55 for meta_item in meta_items {
56 match meta_item.name_or_empty() {
58 self.tcx.sess.span_err(item.span, &format!("abi: {:?}", ty_layout.abi));
64 .span_err(item.span, &format!("align: {:?}", ty_layout.align));
70 .span_err(item.span, &format!("size: {:?}", ty_layout.size));
73 sym::homogeneous_aggregate => {
74 self.tcx.sess.span_err(
77 "homogeneous_aggregate: {:?}",
79 .homogeneous_aggregate(&UnwrapLayoutCx { tcx, param_env }),
85 let normalized_ty = self.tcx.normalize_erasing_regions(
86 param_env.with_reveal_all_normalized(self.tcx),
89 self.tcx.sess.span_err(
91 &format!("layout_of({:?}) = {:#?}", normalized_ty, *ty_layout),
96 self.tcx.sess.span_err(
98 &format!("unrecognized field name `{}`", name),
105 Err(layout_error) => {
106 self.tcx.sess.span_err(item.span, &format!("layout error: {:?}", layout_error));
112 struct UnwrapLayoutCx<'tcx> {
114 param_env: ParamEnv<'tcx>,
117 impl LayoutOf for UnwrapLayoutCx<'tcx> {
119 type TyAndLayout = TyAndLayout<'tcx>;
121 fn layout_of(&self, ty: Ty<'tcx>) -> Self::TyAndLayout {
122 self.tcx.layout_of(self.param_env.and(ty)).unwrap()
126 impl HasTyCtxt<'tcx> for UnwrapLayoutCx<'tcx> {
127 fn tcx(&self) -> TyCtxt<'tcx> {
132 impl HasParamEnv<'tcx> for UnwrapLayoutCx<'tcx> {
133 fn param_env(&self) -> ParamEnv<'tcx> {
138 impl HasDataLayout for UnwrapLayoutCx<'tcx> {
139 fn data_layout(&self) -> &TargetDataLayout {
140 self.tcx.data_layout()