]> git.lizzy.rs Git - rust.git/blob - src/librustc_passes/layout_test.rs
Rollup merge of #71657 - Daniel-Worrall:24949, r=estebank
[rust.git] / src / librustc_passes / layout_test.rs
1 use rustc_ast::ast::Attribute;
2 use rustc_hir as hir;
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};
10
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 });
15     }
16 }
17
18 struct LayoutTest<'tcx> {
19     tcx: TyCtxt<'tcx>,
20 }
21
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);
25
26         match item.kind {
27             ItemKind::TyAlias(..)
28             | ItemKind::Enum(..)
29             | ItemKind::Struct(..)
30             | ItemKind::Union(..)
31             | ItemKind::OpaqueTy(..) => {
32                 for attr in self.tcx.get_attrs(item_def_id.to_def_id()).iter() {
33                     if attr.check_name(sym::rustc_layout) {
34                         self.dump_layout_of(item_def_id, item, attr);
35                     }
36                 }
37             }
38             _ => {}
39         }
40     }
41
42     fn visit_trait_item(&mut self, _: &'tcx hir::TraitItem<'tcx>) {}
43     fn visit_impl_item(&mut self, _: &'tcx hir::ImplItem<'tcx>) {}
44 }
45
46 impl LayoutTest<'tcx> {
47     fn dump_layout_of(&self, item_def_id: LocalDefId, item: &hir::Item<'tcx>, attr: &Attribute) {
48         let tcx = self.tcx;
49         let param_env = self.tcx.param_env(item_def_id);
50         let ty = self.tcx.type_of(item_def_id);
51         match self.tcx.layout_of(param_env.and(ty)) {
52             Ok(ty_layout) => {
53                 // Check out the `#[rustc_layout(..)]` attribute to tell what to dump.
54                 // The `..` are the names of fields to dump.
55                 let meta_items = attr.meta_item_list().unwrap_or_default();
56                 for meta_item in meta_items {
57                     match meta_item.name_or_empty() {
58                         sym::abi => {
59                             self.tcx.sess.span_err(item.span, &format!("abi: {:?}", ty_layout.abi));
60                         }
61
62                         sym::align => {
63                             self.tcx
64                                 .sess
65                                 .span_err(item.span, &format!("align: {:?}", ty_layout.align));
66                         }
67
68                         sym::size => {
69                             self.tcx
70                                 .sess
71                                 .span_err(item.span, &format!("size: {:?}", ty_layout.size));
72                         }
73
74                         sym::homogeneous_aggregate => {
75                             self.tcx.sess.span_err(
76                                 item.span,
77                                 &format!(
78                                     "homogeneous_aggregate: {:?}",
79                                     ty_layout
80                                         .homogeneous_aggregate(&UnwrapLayoutCx { tcx, param_env }),
81                                 ),
82                             );
83                         }
84
85                         sym::debug => {
86                             self.tcx.sess.span_err(
87                                 item.span,
88                                 &format!("layout_of({:?}) = {:#?}", ty, *ty_layout),
89                             );
90                         }
91
92                         name => {
93                             self.tcx.sess.span_err(
94                                 meta_item.span(),
95                                 &format!("unrecognized field name `{}`", name),
96                             );
97                         }
98                     }
99                 }
100             }
101
102             Err(layout_error) => {
103                 self.tcx.sess.span_err(item.span, &format!("layout error: {:?}", layout_error));
104             }
105         }
106     }
107 }
108
109 struct UnwrapLayoutCx<'tcx> {
110     tcx: TyCtxt<'tcx>,
111     param_env: ParamEnv<'tcx>,
112 }
113
114 impl LayoutOf for UnwrapLayoutCx<'tcx> {
115     type Ty = Ty<'tcx>;
116     type TyAndLayout = TyAndLayout<'tcx>;
117
118     fn layout_of(&self, ty: Ty<'tcx>) -> Self::TyAndLayout {
119         self.tcx.layout_of(self.param_env.and(ty)).unwrap()
120     }
121 }
122
123 impl HasTyCtxt<'tcx> for UnwrapLayoutCx<'tcx> {
124     fn tcx(&self) -> TyCtxt<'tcx> {
125         self.tcx
126     }
127 }
128
129 impl HasParamEnv<'tcx> for UnwrapLayoutCx<'tcx> {
130     fn param_env(&self) -> ParamEnv<'tcx> {
131         self.param_env
132     }
133 }
134
135 impl HasDataLayout for UnwrapLayoutCx<'tcx> {
136     fn data_layout(&self) -> &TargetDataLayout {
137         self.tcx.data_layout()
138     }
139 }