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