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