@@ -6,27 +6,65 @@ use rustc_hir as hir;
6
6
use rustc_hir:: def_id:: DefId ;
7
7
use rustc_hir:: lang_items:: LangItem ;
8
8
use rustc_index:: { Idx , IndexVec } ;
9
+ use rustc_middle:: mir:: visit:: { MutVisitor , PlaceContext } ;
9
10
use rustc_middle:: mir:: * ;
10
11
use rustc_middle:: query:: Providers ;
11
12
use rustc_middle:: ty:: {
12
13
self , CoroutineArgs , CoroutineArgsExt , EarlyBinder , GenericArgs , Ty , TyCtxt ,
13
14
} ;
14
15
use rustc_middle:: { bug, span_bug} ;
15
- use rustc_span:: source_map:: Spanned ;
16
+ use rustc_span:: source_map:: { Spanned , dummy_spanned } ;
16
17
use rustc_span:: { DUMMY_SP , Span } ;
17
18
use tracing:: { debug, instrument} ;
18
19
19
20
use crate :: elaborate_drop:: { DropElaborator , DropFlagMode , DropStyle , Unwind , elaborate_drop} ;
20
21
use crate :: patch:: MirPatch ;
21
22
use crate :: {
22
23
abort_unwinding_calls, add_call_guards, add_moves_for_packed_drops, deref_separator, inline,
23
- instsimplify, mentioned_items, pass_manager as pm, remove_noop_landing_pads, simplify,
24
+ instsimplify, mentioned_items, pass_manager as pm, remove_noop_landing_pads,
25
+ run_optimization_passes, simplify,
24
26
} ;
25
27
28
+ mod async_destructor_ctor;
29
+
26
30
pub ( super ) fn provide ( providers : & mut Providers ) {
27
31
providers. mir_shims = make_shim;
28
32
}
29
33
34
+ // Replace Pin<&mut ImplCoroutine> accesses (_1.0) into Pin<&mut ProxyCoroutine> acceses
35
+ struct FixProxyFutureDropVisitor < ' tcx > {
36
+ tcx : TyCtxt < ' tcx > ,
37
+ replace_to : Local ,
38
+ }
39
+
40
+ impl < ' tcx > MutVisitor < ' tcx > for FixProxyFutureDropVisitor < ' tcx > {
41
+ fn tcx ( & self ) -> TyCtxt < ' tcx > {
42
+ self . tcx
43
+ }
44
+
45
+ fn visit_place (
46
+ & mut self ,
47
+ place : & mut Place < ' tcx > ,
48
+ _context : PlaceContext ,
49
+ _location : Location ,
50
+ ) {
51
+ if place. local == Local :: from_u32 ( 1 ) {
52
+ if place. projection . len ( ) == 1 {
53
+ assert ! ( matches!(
54
+ place. projection. first( ) ,
55
+ Some ( ProjectionElem :: Field ( FieldIdx :: ZERO , _) )
56
+ ) ) ;
57
+ * place = Place :: from ( self . replace_to ) ;
58
+ } else if place. projection . len ( ) == 2 {
59
+ assert ! ( matches!( place. projection[ 0 ] , ProjectionElem :: Field ( FieldIdx :: ZERO , _) ) ) ;
60
+ assert ! ( matches!( place. projection[ 1 ] , ProjectionElem :: Deref ) ) ;
61
+ * place =
62
+ Place :: from ( self . replace_to ) . project_deeper ( & [ ProjectionElem :: Deref ] , self . tcx ) ;
63
+ }
64
+ }
65
+ }
66
+ }
67
+
30
68
fn make_shim < ' tcx > ( tcx : TyCtxt < ' tcx > , instance : ty:: InstanceKind < ' tcx > ) -> Body < ' tcx > {
31
69
debug ! ( "make_shim({:?})" , instance) ;
32
70
@@ -127,14 +165,47 @@ fn make_shim<'tcx>(tcx: TyCtxt<'tcx>, instance: ty::InstanceKind<'tcx>) -> Body<
127
165
ty:: InstanceKind :: ThreadLocalShim ( ..) => build_thread_local_shim ( tcx, instance) ,
128
166
ty:: InstanceKind :: CloneShim ( def_id, ty) => build_clone_shim ( tcx, def_id, ty) ,
129
167
ty:: InstanceKind :: FnPtrAddrShim ( def_id, ty) => build_fn_ptr_addr_shim ( tcx, def_id, ty) ,
130
- ty:: InstanceKind :: FutureDropPollShim ( _def_id, _proxy_ty, _impl_ty) => {
131
- todo ! ( )
168
+ ty:: InstanceKind :: FutureDropPollShim ( def_id, proxy_ty, impl_ty) => {
169
+ let mut body =
170
+ async_destructor_ctor:: build_future_drop_poll_shim ( tcx, def_id, proxy_ty, impl_ty) ;
171
+
172
+ pm:: run_passes (
173
+ tcx,
174
+ & mut body,
175
+ & [
176
+ & mentioned_items:: MentionedItems ,
177
+ & abort_unwinding_calls:: AbortUnwindingCalls ,
178
+ & add_call_guards:: CriticalCallEdges ,
179
+ ] ,
180
+ Some ( MirPhase :: Runtime ( RuntimePhase :: Optimized ) ) ,
181
+ pm:: Optimizations :: Allowed ,
182
+ ) ;
183
+ debug ! ( "make_shim({:?}) = {:?}" , instance, body) ;
184
+ return body;
132
185
}
133
- ty:: InstanceKind :: AsyncDropGlue ( _def_id, _ty) => {
134
- todo ! ( )
186
+ ty:: InstanceKind :: AsyncDropGlue ( def_id, ty) => {
187
+ let mut body = async_destructor_ctor:: build_async_drop_shim ( tcx, def_id, ty) ;
188
+
189
+ pm:: run_passes (
190
+ tcx,
191
+ & mut body,
192
+ & [
193
+ & mentioned_items:: MentionedItems ,
194
+ & simplify:: SimplifyCfg :: MakeShim ,
195
+ & crate :: coroutine:: StateTransform ,
196
+ ] ,
197
+ Some ( MirPhase :: Runtime ( RuntimePhase :: PostCleanup ) ) ,
198
+ pm:: Optimizations :: Allowed ,
199
+ ) ;
200
+ run_optimization_passes ( tcx, & mut body) ;
201
+ debug ! ( "make_shim({:?}) = {:?}" , instance, body) ;
202
+ return body;
135
203
}
136
- ty:: InstanceKind :: AsyncDropGlueCtorShim ( _def_id, _ty) => {
137
- bug ! ( "AsyncDropGlueCtorShim in re-working ({:?})" , instance)
204
+
205
+ ty:: InstanceKind :: AsyncDropGlueCtorShim ( def_id, ty) => {
206
+ let body = async_destructor_ctor:: build_async_destructor_ctor_shim ( tcx, def_id, ty) ;
207
+ debug ! ( "make_shim({:?}) = {:?}" , instance, body) ;
208
+ return body;
138
209
}
139
210
ty:: InstanceKind :: Virtual ( ..) => {
140
211
bug ! ( "InstanceKind::Virtual ({:?}) is for direct calls only" , instance)
0 commit comments