@@ -4,7 +4,7 @@ use crate::traits;
4
4
use crate :: ty:: print:: { FmtPrinter , Printer } ;
5
5
use crate :: ty:: { self , SubstsRef , Ty , TyCtxt , TypeFoldable } ;
6
6
use rustc_hir:: def:: Namespace ;
7
- use rustc_hir:: def_id:: DefId ;
7
+ use rustc_hir:: def_id:: { CrateNum , DefId } ;
8
8
use rustc_macros:: HashStable ;
9
9
use rustc_target:: spec:: abi:: Abi ;
10
10
@@ -91,6 +91,38 @@ impl<'tcx> Instance<'tcx> {
91
91
let ty = tcx. type_of ( self . def . def_id ( ) ) ;
92
92
tcx. subst_and_normalize_erasing_regions ( self . substs , param_env, & ty)
93
93
}
94
+
95
+ pub fn upstream_monomorphization ( & self , tcx : TyCtxt < ' tcx > ) -> Option < CrateNum > {
96
+ // If we are not in share generics mode, we don't link to upstream
97
+ // monomorphizations but always instantiate our own internal versions
98
+ // instead.
99
+ if !tcx. sess . opts . share_generics ( ) {
100
+ return None ;
101
+ }
102
+
103
+ // If this instance has non-erasable parameters, it cannot be a shared
104
+ // monomorphization. Non-generic instances are already handled above
105
+ // by `is_reachable_non_generic()`.
106
+ if self . substs . non_erasable_generics ( ) . next ( ) . is_none ( ) {
107
+ return None ;
108
+ }
109
+
110
+ if self . def_id ( ) . is_local ( ) {
111
+ return None ;
112
+ }
113
+
114
+ match self . def {
115
+ InstanceDef :: Item ( def_id) => {
116
+ tcx
117
+ . upstream_monomorphizations_for ( def_id)
118
+ . and_then ( |monos| monos. get ( & self . substs ) . cloned ( ) )
119
+ }
120
+ InstanceDef :: DropGlue ( _, Some ( _) ) => {
121
+ tcx. upstream_drop_glue_for ( self . substs )
122
+ }
123
+ _ => None ,
124
+ }
125
+ }
94
126
}
95
127
96
128
impl < ' tcx > InstanceDef < ' tcx > {
0 commit comments