]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_passes/src/layout_test.rs
Rollup merge of #86434 - CDirkx:ipv6-benchmarking, r=joshtriplett
[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, LayoutError, LayoutOfHelpers, TyAndLayout};
7 use rustc_middle::ty::{ParamEnv, Ty, TyCtxt};
8 use rustc_span::symbol::sym;
9 use rustc_span::Span;
10 use rustc_target::abi::{HasDataLayout, TargetDataLayout};
11
12 pub fn test_layout(tcx: TyCtxt<'_>) {
13     if tcx.features().rustc_attrs {
14         // if the `rustc_attrs` feature is not enabled, don't bother testing layout
15         tcx.hir().visit_all_item_likes(&mut LayoutTest { tcx });
16     }
17 }
18
19 struct LayoutTest<'tcx> {
20     tcx: TyCtxt<'tcx>,
21 }
22
23 impl ItemLikeVisitor<'tcx> for LayoutTest<'tcx> {
24     fn visit_item(&mut self, item: &'tcx hir::Item<'tcx>) {
25         match item.kind {
26             ItemKind::TyAlias(..)
27             | ItemKind::Enum(..)
28             | ItemKind::Struct(..)
29             | ItemKind::Union(..) => {
30                 for attr in self.tcx.get_attrs(item.def_id.to_def_id()).iter() {
31                     if attr.has_name(sym::rustc_layout) {
32                         self.dump_layout_of(item.def_id, item, attr);
33                     }
34                 }
35             }
36             _ => {}
37         }
38     }
39
40     fn visit_trait_item(&mut self, _: &'tcx hir::TraitItem<'tcx>) {}
41     fn visit_impl_item(&mut self, _: &'tcx hir::ImplItem<'tcx>) {}
42     fn visit_foreign_item(&mut self, _: &'tcx hir::ForeignItem<'tcx>) {}
43 }
44
45 impl LayoutTest<'tcx> {
46     fn dump_layout_of(&self, item_def_id: LocalDefId, item: &hir::Item<'tcx>, attr: &Attribute) {
47         let tcx = self.tcx;
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)) {
51             Ok(ty_layout) => {
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() {
57                         sym::abi => {
58                             self.tcx.sess.span_err(item.span, &format!("abi: {:?}", ty_layout.abi));
59                         }
60
61                         sym::align => {
62                             self.tcx
63                                 .sess
64                                 .span_err(item.span, &format!("align: {:?}", ty_layout.align));
65                         }
66
67                         sym::size => {
68                             self.tcx
69                                 .sess
70                                 .span_err(item.span, &format!("size: {:?}", ty_layout.size));
71                         }
72
73                         sym::homogeneous_aggregate => {
74                             self.tcx.sess.span_err(
75                                 item.span,
76                                 &format!(
77                                     "homogeneous_aggregate: {:?}",
78                                     ty_layout
79                                         .homogeneous_aggregate(&UnwrapLayoutCx { tcx, param_env }),
80                                 ),
81                             );
82                         }
83
84                         sym::debug => {
85                             let normalized_ty = self.tcx.normalize_erasing_regions(
86                                 param_env.with_reveal_all_normalized(self.tcx),
87                                 ty,
88                             );
89                             self.tcx.sess.span_err(
90                                 item.span,
91                                 &format!("layout_of({:?}) = {:#?}", normalized_ty, *ty_layout),
92                             );
93                         }
94
95                         name => {
96                             self.tcx.sess.span_err(
97                                 meta_item.span(),
98                                 &format!("unrecognized field name `{}`", name),
99                             );
100                         }
101                     }
102                 }
103             }
104
105             Err(layout_error) => {
106                 self.tcx.sess.span_err(item.span, &format!("layout error: {:?}", layout_error));
107             }
108         }
109     }
110 }
111
112 struct UnwrapLayoutCx<'tcx> {
113     tcx: TyCtxt<'tcx>,
114     param_env: ParamEnv<'tcx>,
115 }
116
117 impl LayoutOfHelpers<'tcx> for UnwrapLayoutCx<'tcx> {
118     type LayoutOfResult = TyAndLayout<'tcx>;
119
120     fn handle_layout_err(&self, err: LayoutError<'tcx>, span: Span, ty: Ty<'tcx>) -> ! {
121         span_bug!(
122             span,
123             "`#[rustc_layout(..)]` test resulted in `layout_of({}) = Err({})`",
124             ty,
125             err
126         );
127     }
128 }
129
130 impl HasTyCtxt<'tcx> for UnwrapLayoutCx<'tcx> {
131     fn tcx(&self) -> TyCtxt<'tcx> {
132         self.tcx
133     }
134 }
135
136 impl HasParamEnv<'tcx> for UnwrapLayoutCx<'tcx> {
137     fn param_env(&self) -> ParamEnv<'tcx> {
138         self.param_env
139     }
140 }
141
142 impl HasDataLayout for UnwrapLayoutCx<'tcx> {
143     fn data_layout(&self) -> &TargetDataLayout {
144         self.tcx.data_layout()
145     }
146 }