@@ -504,6 +504,14 @@ impl<'tcx> EntryKind<'tcx> {
504504 EntryKind :: Closure ( _) => return None ,
505505 } )
506506 }
507+ fn is_const_fn ( & self , meta : & CrateMetadata ) -> bool {
508+ let constness = match * self {
509+ EntryKind :: Method ( data) => data. decode ( meta) . fn_data . constness ,
510+ EntryKind :: Fn ( data) => data. decode ( meta) . constness ,
511+ _ => hir:: Constness :: NotConst ,
512+ } ;
513+ constness == hir:: Constness :: Const
514+ }
507515}
508516
509517impl < ' a , ' tcx > CrateMetadata {
@@ -839,6 +847,29 @@ impl<'a, 'tcx> CrateMetadata {
839847 self . maybe_entry ( id) . and_then ( |item| item. decode ( self ) . mir ) . is_some ( )
840848 }
841849
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+
842873 pub fn maybe_get_item_mir ( & self ,
843874 tcx : TyCtxt < ' a , ' tcx , ' tcx > ,
844875 id : DefIndex )
@@ -1051,12 +1082,7 @@ impl<'a, 'tcx> CrateMetadata {
10511082 }
10521083
10531084 pub fn is_const_fn ( & self , id : DefIndex ) -> bool {
1054- let constness = match self . entry ( id) . kind {
1055- EntryKind :: Method ( data) => data. decode ( self ) . fn_data . constness ,
1056- EntryKind :: Fn ( data) => data. decode ( self ) . constness ,
1057- _ => hir:: Constness :: NotConst ,
1058- } ;
1059- constness == hir:: Constness :: Const
1085+ self . entry ( id) . kind . is_const_fn ( self )
10601086 }
10611087
10621088 pub fn is_foreign_item ( & self , id : DefIndex ) -> bool {
0 commit comments