@@ -13,6 +13,7 @@ mod module {
1313 def_id:: { DefId as RDefId , LocalDefId as RLocalDefId } ,
1414 hir_id:: OwnerId as ROwnerId ,
1515 } ;
16+ use rustc_middle:: ty;
1617
1718 mod store {
1819 //! This module helps at store bodies to avoid stealing.
@@ -97,7 +98,7 @@ mod module {
9798 }
9899 pub use store:: * ;
99100
100- pub fn get_thir < ' tcx , S : UnderOwnerState < ' tcx > > (
101+ pub fn get_thir < ' tcx , S : BaseState < ' tcx > > (
101102 did : RLocalDefId ,
102103 s : & S ,
103104 ) -> (
@@ -122,7 +123,11 @@ mod module {
122123 }
123124
124125 pub trait IsBody : Sized + std:: fmt:: Debug + Clone + ' static {
125- fn body < ' tcx , S : UnderOwnerState < ' tcx > > ( did : RDefId , s : & S ) -> Option < Self > ;
126+ fn body < ' tcx , S : UnderOwnerState < ' tcx > > (
127+ s : & S ,
128+ did : RDefId ,
129+ instantiate : Option < ty:: GenericArgsRef < ' tcx > > ,
130+ ) -> Option < Self > ;
126131
127132 /// Reuse a MIR body we already got. Panic if that's impossible.
128133 fn from_mir < ' tcx , S : UnderOwnerState < ' tcx > > (
@@ -133,7 +138,7 @@ mod module {
133138 }
134139 }
135140
136- pub fn make_fn_def < ' tcx , Body : IsBody , S : UnderOwnerState < ' tcx > > (
141+ pub fn make_fn_def < ' tcx , Body : IsBody , S : BaseState < ' tcx > > (
137142 fn_sig : & rustc_hir:: FnSig ,
138143 body_id : & rustc_hir:: BodyId ,
139144 s : & S ,
@@ -142,11 +147,11 @@ mod module {
142147 let ldid = hir_id. owner . def_id ;
143148
144149 let ( thir, expr_entrypoint) = get_thir ( ldid, s) ;
145- let s = & with_owner_id ( s . base ( ) , thir . clone ( ) , ( ) , ldid . to_def_id ( ) ) ;
150+ let s = & s . with_owner_id ( ldid . to_def_id ( ) ) . with_thir ( thir . clone ( ) ) ;
146151 FnDef {
147152 params : thir. params . raw . sinto ( s) ,
148153 ret : thir. exprs [ expr_entrypoint] . ty . sinto ( s) ,
149- body : Body :: body ( ldid. to_def_id ( ) , s ) . s_unwrap ( s) ,
154+ body : Body :: body ( s , ldid. to_def_id ( ) , None ) . s_unwrap ( s) ,
150155 sig_span : fn_sig. span . sinto ( s) ,
151156 header : fn_sig. header . sinto ( s) ,
152157 }
@@ -161,13 +166,17 @@ mod module {
161166 // be local. It is safe to do so, because if we have access to HIR objects,
162167 // it necessarily means we are exploring a local item (we don't have
163168 // access to the HIR of external objects, only their MIR).
164- Body :: body ( s. base ( ) . tcx . hir_body_owner_def_id ( id) . to_def_id ( ) , s ) . s_unwrap ( s)
169+ Body :: body ( s, s . base ( ) . tcx . hir_body_owner_def_id ( id) . to_def_id ( ) , None ) . s_unwrap ( s)
165170 }
166171
167172 mod implementations {
168173 use super :: * ;
169174 impl IsBody for ( ) {
170- fn body < ' tcx , S : UnderOwnerState < ' tcx > > ( _did : RDefId , _s : & S ) -> Option < Self > {
175+ fn body < ' tcx , S : UnderOwnerState < ' tcx > > (
176+ _s : & S ,
177+ _did : RDefId ,
178+ _instantiate : Option < ty:: GenericArgsRef < ' tcx > > ,
179+ ) -> Option < Self > {
171180 Some ( ( ) )
172181 }
173182 fn from_mir < ' tcx , S : UnderOwnerState < ' tcx > > (
@@ -178,9 +187,15 @@ mod module {
178187 }
179188 }
180189 impl IsBody for ThirBody {
181- fn body < ' tcx , S : UnderOwnerState < ' tcx > > ( did : RDefId , s : & S ) -> Option < Self > {
190+ fn body < ' tcx , S : BaseState < ' tcx > > (
191+ s : & S ,
192+ did : RDefId ,
193+ instantiate : Option < ty:: GenericArgsRef < ' tcx > > ,
194+ ) -> Option < Self > {
182195 let did = did. as_local ( ) ?;
183196 let ( thir, expr) = get_thir ( did, s) ;
197+ assert ! ( instantiate. is_none( ) , "monomorphized thir isn't supported" ) ;
198+ let s = & s. with_owner_id ( did. to_def_id ( ) ) ;
184199 Some ( if * CORE_EXTRACTION_MODE {
185200 let expr = & thir. exprs [ expr] ;
186201 Decorated {
@@ -191,36 +206,41 @@ mod module {
191206 span : expr. span . sinto ( s) ,
192207 }
193208 } else {
194- expr. sinto ( & with_owner_id ( s . base ( ) , thir, ( ) , did . to_def_id ( ) ) )
209+ expr. sinto ( & s . with_thir ( thir) )
195210 } )
196211 }
197212 }
198213
199214 impl < A : IsBody , B : IsBody > IsBody for ( A , B ) {
200- fn body < ' tcx , S : UnderOwnerState < ' tcx > > ( did : RDefId , s : & S ) -> Option < Self > {
201- Some ( ( A :: body ( did, s) ?, B :: body ( did, s) ?) )
215+ fn body < ' tcx , S : UnderOwnerState < ' tcx > > (
216+ s : & S ,
217+ did : RDefId ,
218+ instantiate : Option < ty:: GenericArgsRef < ' tcx > > ,
219+ ) -> Option < Self > {
220+ Some ( ( A :: body ( s, did, instantiate) ?, B :: body ( s, did, instantiate) ?) )
202221 }
203222 }
204223
205224 impl < MirKind : IsMirKind + Clone + ' static > IsBody for MirBody < MirKind > {
206- fn body < ' tcx , S : UnderOwnerState < ' tcx > > ( did : RDefId , s : & S ) -> Option < Self > {
207- MirKind :: get_mir ( s. base ( ) . tcx , did, |body| {
208- let body = Rc :: new ( body. clone ( ) ) ;
209- body. sinto ( & with_owner_id ( s. base ( ) , ( ) , body. clone ( ) , did) )
225+ fn body < ' tcx , S : UnderOwnerState < ' tcx > > (
226+ s : & S ,
227+ did : RDefId ,
228+ instantiate : Option < ty:: GenericArgsRef < ' tcx > > ,
229+ ) -> Option < Self > {
230+ let tcx = s. base ( ) . tcx ;
231+ let typing_env = s. typing_env ( ) ;
232+ MirKind :: get_mir ( tcx, did, |body| {
233+ let body = substitute ( tcx, typing_env, instantiate, body. clone ( ) ) ;
234+ let body = Rc :: new ( body) ;
235+ body. sinto ( & s. with_mir ( body. clone ( ) ) )
210236 } )
211237 }
212238 fn from_mir < ' tcx , S : UnderOwnerState < ' tcx > > (
213239 s : & S ,
214240 body : rustc_middle:: mir:: Body < ' tcx > ,
215241 ) -> Option < Self > {
216242 let body = Rc :: new ( body. clone ( ) ) ;
217- let s = & State {
218- base : s. base ( ) ,
219- owner_id : s. owner_id ( ) ,
220- mir : body. clone ( ) ,
221- binder : ( ) ,
222- thir : ( ) ,
223- } ;
243+ let s = & s. with_mir ( body. clone ( ) ) ;
224244 Some ( body. sinto ( s) )
225245 }
226246 }
0 commit comments