From dfbb6e864091740608fb551fef7eabbd7567351c Mon Sep 17 00:00:00 2001 From: Maik Klein Date: Fri, 27 Oct 2017 11:27:05 +0200 Subject: [PATCH] Move instance related methods from TyCtxt to Instance --- src/librustc/ty/instance.rs | 36 +++++++++++++++++++++++++++ src/librustc_mir/monomorphize/item.rs | 2 +- 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/src/librustc/ty/instance.rs b/src/librustc/ty/instance.rs index 1f505a07dab..5259e790e92 100644 --- a/src/librustc/ty/instance.rs +++ b/src/librustc/ty/instance.rs @@ -77,6 +77,42 @@ impl<'tcx> InstanceDef<'tcx> { pub fn attrs<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>) -> ty::Attributes<'tcx> { tcx.get_attrs(self.def_id()) } + + pub fn is_inline<'a>( + &self, + tcx: TyCtxt<'a, 'tcx, 'tcx> + ) -> bool { + use hir::map::DefPathData; + let def_id = match *self { + ty::InstanceDef::Item(def_id) => def_id, + ty::InstanceDef::DropGlue(_, Some(_)) => return false, + _ => return true + }; + match tcx.def_key(def_id).disambiguated_data.data { + DefPathData::StructCtor | + DefPathData::EnumVariant(..) | + DefPathData::ClosureExpr => true, + _ => false + } + } + + pub fn requires_local<'a>( + &self, + tcx: TyCtxt<'a, 'tcx, 'tcx> + ) -> bool { + use syntax::attr::requests_inline; + if self.is_inline(tcx) { + return true + } + if let ty::InstanceDef::DropGlue(..) = *self { + // Drop glue wants to be instantiated at every translation + // unit, but without an #[inline] hint. We should make this + // available to normal end-users. + return true + } + requests_inline(&self.attrs(tcx)[..]) || + tcx.is_const_fn(self.def_id()) + } } impl<'tcx> fmt::Display for Instance<'tcx> { diff --git a/src/librustc_mir/monomorphize/item.rs b/src/librustc_mir/monomorphize/item.rs index a81808d8afc..72efe23f77b 100644 --- a/src/librustc_mir/monomorphize/item.rs +++ b/src/librustc_mir/monomorphize/item.rs @@ -96,7 +96,7 @@ pub trait MonoItemExt<'a, 'tcx>: fmt::Debug { // If this function isn't inlined or otherwise has explicit // linkage, then we'll be creating a globally shared version. if self.explicit_linkage(tcx).is_some() || - !tcx.requires_local_instance(instance) + !instance.def.requires_local(tcx) { return InstantiationMode::GloballyShared { may_conflict: false } }