]> git.lizzy.rs Git - rust.git/blob - src/librustc_passes/layout_test.rs
Auto merge of #60277 - estebank:relate-ice, r=varkor
[rust.git] / src / librustc_passes / layout_test.rs
1 use rustc::hir;
2 use rustc::hir::def_id::DefId;
3 use rustc::hir::itemlikevisit::ItemLikeVisitor;
4 use rustc::hir::ItemKind;
5 use rustc::ty::layout::HasDataLayout;
6 use rustc::ty::layout::HasTyCtxt;
7 use rustc::ty::layout::LayoutOf;
8 use rustc::ty::layout::TargetDataLayout;
9 use rustc::ty::layout::TyLayout;
10 use rustc::ty::layout::HasParamEnv;
11 use rustc::ty::ParamEnv;
12 use rustc::ty::Ty;
13 use rustc::ty::TyCtxt;
14 use syntax::ast::Attribute;
15
16 pub fn test_layout<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) {
17     if tcx.features().rustc_attrs {
18         // if the `rustc_attrs` feature is not enabled, don't bother testing layout
19         tcx.hir()
20             .krate()
21             .visit_all_item_likes(&mut VarianceTest { tcx });
22     }
23 }
24
25 struct VarianceTest<'a, 'tcx: 'a> {
26     tcx: TyCtxt<'a, 'tcx, 'tcx>,
27 }
28
29 impl<'a, 'tcx> ItemLikeVisitor<'tcx> for VarianceTest<'a, 'tcx> {
30     fn visit_item(&mut self, item: &'tcx hir::Item) {
31         let item_def_id = self.tcx.hir().local_def_id_from_hir_id(item.hir_id);
32
33         if let ItemKind::Ty(..) = item.node {
34             for attr in self.tcx.get_attrs(item_def_id).iter() {
35                 if attr.check_name("rustc_layout") {
36                     self.dump_layout_of(item_def_id, item, attr);
37                 }
38             }
39         }
40     }
41
42     fn visit_trait_item(&mut self, _: &'tcx hir::TraitItem) {}
43     fn visit_impl_item(&mut self, _: &'tcx hir::ImplItem) {}
44 }
45
46 impl<'a, 'tcx> VarianceTest<'a, 'tcx> {
47     fn dump_layout_of(&self, item_def_id: DefId, item: &hir::Item, 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().get() {
58                         "abi" => {
59                             self.tcx
60                                 .sess
61                                 .span_err(item.span, &format!("abi: {:?}", ty_layout.abi));
62                         }
63
64                         "align" => {
65                             self.tcx
66                                 .sess
67                                 .span_err(item.span, &format!("align: {:?}", ty_layout.align));
68                         }
69
70                         "size" => {
71                             self.tcx
72                                 .sess
73                                 .span_err(item.span, &format!("size: {:?}", ty_layout.size));
74                         }
75
76                         "homogeneous_aggregate" => {
77                             self.tcx.sess.span_err(
78                                 item.span,
79                                 &format!(
80                                     "homogeneous_aggregate: {:?}",
81                                     ty_layout
82                                         .homogeneous_aggregate(&UnwrapLayoutCx { tcx, param_env }),
83                                 ),
84                             );
85                         }
86
87                         name => {
88                             self.tcx.sess.span_err(
89                                 meta_item.span(),
90                                 &format!("unrecognized field name `{}`", name),
91                             );
92                         }
93                     }
94                 }
95             }
96
97             Err(layout_error) => {
98                 self.tcx
99                     .sess
100                     .span_err(item.span, &format!("layout error: {:?}", layout_error));
101             }
102         }
103     }
104 }
105
106 struct UnwrapLayoutCx<'me, 'tcx> {
107     tcx: TyCtxt<'me, 'tcx, 'tcx>,
108     param_env: ParamEnv<'tcx>,
109 }
110
111 impl<'me, 'tcx> LayoutOf for UnwrapLayoutCx<'me, 'tcx> {
112     type Ty = Ty<'tcx>;
113     type TyLayout = TyLayout<'tcx>;
114
115     fn layout_of(&self, ty: Ty<'tcx>) -> Self::TyLayout {
116         self.tcx.layout_of(self.param_env.and(ty)).unwrap()
117     }
118 }
119
120 impl<'me, 'tcx> HasTyCtxt<'tcx> for UnwrapLayoutCx<'me, 'tcx> {
121     fn tcx<'a>(&'a self) -> TyCtxt<'a, 'tcx, 'tcx> {
122         self.tcx
123     }
124 }
125
126 impl<'me, 'tcx> HasParamEnv<'tcx> for UnwrapLayoutCx<'me, 'tcx> {
127     fn param_env(&self) -> ParamEnv<'tcx> {
128         self.param_env
129     }
130 }
131
132 impl<'me, 'tcx> HasDataLayout for UnwrapLayoutCx<'me, 'tcx> {
133     fn data_layout(&self) -> &TargetDataLayout {
134         self.tcx.data_layout()
135     }
136 }