Skip to content

Commit d74d153

Browse files
committed
move the check for instantiation from metadata encoding to the actual decision site
before it was assumed that anything that had a MIR was fair game for local instatiation
1 parent 910c369 commit d74d153

File tree

4 files changed

+37
-4
lines changed

4 files changed

+37
-4
lines changed

src/librustc/middle/cstore.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,11 @@ pub trait CrateStore<'tcx> {
355355
fn get_item_mir<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def: DefId) -> Mir<'tcx>;
356356
fn is_item_mir_available(&self, def: DefId) -> bool;
357357

358+
/// Take a look if we need to inline or monomorphize this. If so, we
359+
/// will emit code for this item in the local crate, and thus
360+
/// create a translation item for it.
361+
fn can_have_local_instance<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def: DefId) -> bool;
362+
358363
// This is basically a 1-based range of ints, which is a little
359364
// silly - I may fix that.
360365
fn crates(&self) -> Vec<CrateNum>;
@@ -528,6 +533,9 @@ impl<'tcx> CrateStore<'tcx> for DummyCrateStore {
528533
fn is_item_mir_available(&self, def: DefId) -> bool {
529534
bug!("is_item_mir_available")
530535
}
536+
fn can_have_local_instance<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def: DefId) -> bool {
537+
bug!("can_have_local_instance")
538+
}
531539

532540
// This is basically a 1-based range of ints, which is a little
533541
// silly - I may fix that.

src/librustc_metadata/cstore_impl.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,11 @@ impl<'tcx> CrateStore<'tcx> for cstore::CStore {
527527
self.get_crate_data(def.krate).is_item_mir_available(def.index)
528528
}
529529

530+
fn can_have_local_instance<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def: DefId) -> bool {
531+
self.dep_graph.read(DepNode::MetaData(def));
532+
def.is_local() || self.get_crate_data(def.krate).can_have_local_instance(tcx, def.index)
533+
}
534+
530535
fn crates(&self) -> Vec<CrateNum>
531536
{
532537
let mut result = vec![];

src/librustc_metadata/decoder.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -847,6 +847,29 @@ impl<'a, 'tcx> CrateMetadata {
847847
self.maybe_entry(id).and_then(|item| item.decode(self).mir).is_some()
848848
}
849849

850+
pub fn can_have_local_instance(&self,
851+
tcx: TyCtxt<'a, 'tcx, 'tcx>,
852+
id: DefIndex) -> bool {
853+
self.maybe_entry(id).map_or(false, |item| {
854+
let item = item.decode(self);
855+
// if we don't have a MIR, then this item was never meant to be locally instantiated
856+
// or we have a bug in the metadata serialization
857+
item.mir.is_some() && (
858+
// items with generics always can have local instances if monomorphized
859+
item.generics.map_or(false, |generics| {
860+
let generics = generics.decode((self, tcx));
861+
generics.parent_types != 0 || !generics.types.is_empty()
862+
}) ||
863+
match item.kind {
864+
EntryKind::Closure(_) => true,
865+
_ => false,
866+
} ||
867+
item.kind.is_const_fn(self) ||
868+
attr::requests_inline(&self.get_attributes(&item))
869+
)
870+
})
871+
}
872+
850873
pub fn maybe_get_item_mir(&self,
851874
tcx: TyCtxt<'a, 'tcx, 'tcx>,
852875
id: DefIndex)

src/librustc_trans/collector.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -706,10 +706,7 @@ impl<'a, 'tcx> MirVisitor<'tcx> for MirNeighborCollector<'a, 'tcx> {
706706
fn can_have_local_instance<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
707707
def_id: DefId)
708708
-> bool {
709-
// Take a look if we have the definition available. If so, we
710-
// will emit code for this item in the local crate, and thus
711-
// create a translation item for it.
712-
def_id.is_local() || tcx.sess.cstore.is_item_mir_available(def_id)
709+
tcx.sess.cstore.can_have_local_instance(tcx, def_id)
713710
}
714711

715712
fn find_drop_glue_neighbors<'a, 'tcx>(scx: &SharedCrateContext<'a, 'tcx>,

0 commit comments

Comments
 (0)