2019-01-09 15:16:32 -05:00
|
|
|
use rustc::hir;
|
|
|
|
use rustc::hir::def_id::DefId;
|
|
|
|
use rustc::hir::itemlikevisit::ItemLikeVisitor;
|
|
|
|
use rustc::hir::ItemKind;
|
|
|
|
use rustc::ty::layout::HasDataLayout;
|
|
|
|
use rustc::ty::layout::HasTyCtxt;
|
|
|
|
use rustc::ty::layout::LayoutOf;
|
|
|
|
use rustc::ty::layout::TargetDataLayout;
|
|
|
|
use rustc::ty::layout::TyLayout;
|
2019-05-04 15:02:22 +05:30
|
|
|
use rustc::ty::layout::HasParamEnv;
|
2019-01-09 15:16:32 -05:00
|
|
|
use rustc::ty::ParamEnv;
|
|
|
|
use rustc::ty::Ty;
|
|
|
|
use rustc::ty::TyCtxt;
|
|
|
|
use syntax::ast::Attribute;
|
2019-05-08 13:21:18 +10:00
|
|
|
use syntax::symbol::sym;
|
2019-01-09 15:16:32 -05:00
|
|
|
|
2019-06-21 20:27:44 +02:00
|
|
|
pub fn test_layout(tcx: TyCtxt<'_>) {
|
2019-01-09 15:16:32 -05:00
|
|
|
if tcx.features().rustc_attrs {
|
|
|
|
// if the `rustc_attrs` feature is not enabled, don't bother testing layout
|
|
|
|
tcx.hir()
|
|
|
|
.krate()
|
|
|
|
.visit_all_item_likes(&mut VarianceTest { tcx });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-11 22:03:44 +03:00
|
|
|
struct VarianceTest<'tcx> {
|
2019-06-14 00:48:52 +03:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2019-01-09 15:16:32 -05:00
|
|
|
}
|
|
|
|
|
2019-06-11 22:03:44 +03:00
|
|
|
impl ItemLikeVisitor<'tcx> for VarianceTest<'tcx> {
|
2019-11-28 19:28:50 +01:00
|
|
|
fn visit_item(&mut self, item: &'tcx hir::Item<'tcx>) {
|
2019-06-27 11:28:14 +02:00
|
|
|
let item_def_id = self.tcx.hir().local_def_id(item.hir_id);
|
2019-01-09 15:16:32 -05:00
|
|
|
|
2019-09-26 17:51:36 +01:00
|
|
|
if let ItemKind::TyAlias(..) = item.kind {
|
2019-01-09 15:16:32 -05:00
|
|
|
for attr in self.tcx.get_attrs(item_def_id).iter() {
|
2019-05-08 13:21:18 +10:00
|
|
|
if attr.check_name(sym::rustc_layout) {
|
2019-01-09 15:16:32 -05:00
|
|
|
self.dump_layout_of(item_def_id, item, attr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-28 21:47:10 +01:00
|
|
|
fn visit_trait_item(&mut self, _: &'tcx hir::TraitItem<'tcx>) {}
|
2019-11-28 22:16:44 +01:00
|
|
|
fn visit_impl_item(&mut self, _: &'tcx hir::ImplItem<'tcx>) {}
|
2019-01-09 15:16:32 -05:00
|
|
|
}
|
|
|
|
|
2019-06-11 22:03:44 +03:00
|
|
|
impl VarianceTest<'tcx> {
|
2019-11-28 19:28:50 +01:00
|
|
|
fn dump_layout_of(&self, item_def_id: DefId, item: &hir::Item<'tcx>, attr: &Attribute) {
|
2019-01-09 15:16:32 -05:00
|
|
|
let tcx = self.tcx;
|
|
|
|
let param_env = self.tcx.param_env(item_def_id);
|
|
|
|
let ty = self.tcx.type_of(item_def_id);
|
|
|
|
match self.tcx.layout_of(param_env.and(ty)) {
|
|
|
|
Ok(ty_layout) => {
|
|
|
|
// Check out the `#[rustc_layout(..)]` attribute to tell what to dump.
|
|
|
|
// The `..` are the names of fields to dump.
|
|
|
|
let meta_items = attr.meta_item_list().unwrap_or_default();
|
|
|
|
for meta_item in meta_items {
|
2019-05-08 14:33:06 +10:00
|
|
|
match meta_item.name_or_empty() {
|
|
|
|
sym::abi => {
|
2019-01-09 15:16:32 -05:00
|
|
|
self.tcx
|
|
|
|
.sess
|
|
|
|
.span_err(item.span, &format!("abi: {:?}", ty_layout.abi));
|
|
|
|
}
|
|
|
|
|
2019-05-08 14:33:06 +10:00
|
|
|
sym::align => {
|
2019-01-09 15:16:32 -05:00
|
|
|
self.tcx
|
|
|
|
.sess
|
|
|
|
.span_err(item.span, &format!("align: {:?}", ty_layout.align));
|
|
|
|
}
|
|
|
|
|
2019-05-08 14:33:06 +10:00
|
|
|
sym::size => {
|
2019-01-09 15:16:32 -05:00
|
|
|
self.tcx
|
|
|
|
.sess
|
|
|
|
.span_err(item.span, &format!("size: {:?}", ty_layout.size));
|
|
|
|
}
|
|
|
|
|
2019-05-08 14:33:06 +10:00
|
|
|
sym::homogeneous_aggregate => {
|
2019-01-09 15:16:32 -05:00
|
|
|
self.tcx.sess.span_err(
|
|
|
|
item.span,
|
|
|
|
&format!(
|
|
|
|
"homogeneous_aggregate: {:?}",
|
|
|
|
ty_layout
|
|
|
|
.homogeneous_aggregate(&UnwrapLayoutCx { tcx, param_env }),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-03-17 14:17:47 +03:00
|
|
|
name => {
|
2019-01-09 15:16:32 -05:00
|
|
|
self.tcx.sess.span_err(
|
2019-03-03 20:56:24 +03:00
|
|
|
meta_item.span(),
|
2019-01-09 15:16:32 -05:00
|
|
|
&format!("unrecognized field name `{}`", name),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Err(layout_error) => {
|
|
|
|
self.tcx
|
|
|
|
.sess
|
|
|
|
.span_err(item.span, &format!("layout error: {:?}", layout_error));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-11 22:03:44 +03:00
|
|
|
struct UnwrapLayoutCx<'tcx> {
|
2019-06-14 00:48:52 +03:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2019-01-09 15:16:32 -05:00
|
|
|
param_env: ParamEnv<'tcx>,
|
|
|
|
}
|
|
|
|
|
2019-06-11 22:03:44 +03:00
|
|
|
impl LayoutOf for UnwrapLayoutCx<'tcx> {
|
2019-01-09 15:16:32 -05:00
|
|
|
type Ty = Ty<'tcx>;
|
|
|
|
type TyLayout = TyLayout<'tcx>;
|
|
|
|
|
|
|
|
fn layout_of(&self, ty: Ty<'tcx>) -> Self::TyLayout {
|
|
|
|
self.tcx.layout_of(self.param_env.and(ty)).unwrap()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-11 22:03:44 +03:00
|
|
|
impl HasTyCtxt<'tcx> for UnwrapLayoutCx<'tcx> {
|
2019-06-14 00:48:52 +03:00
|
|
|
fn tcx(&self) -> TyCtxt<'tcx> {
|
2019-01-09 15:16:32 -05:00
|
|
|
self.tcx
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-11 22:03:44 +03:00
|
|
|
impl HasParamEnv<'tcx> for UnwrapLayoutCx<'tcx> {
|
2019-05-04 15:02:22 +05:30
|
|
|
fn param_env(&self) -> ParamEnv<'tcx> {
|
|
|
|
self.param_env
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-11 22:03:44 +03:00
|
|
|
impl HasDataLayout for UnwrapLayoutCx<'tcx> {
|
2019-01-09 15:16:32 -05:00
|
|
|
fn data_layout(&self) -> &TargetDataLayout {
|
|
|
|
self.tcx.data_layout()
|
|
|
|
}
|
|
|
|
}
|