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