@@ -3,8 +3,8 @@ use rustc_hir::def_id::DefId;
3
3
use rustc_hir:: lang_items:: LangItem ;
4
4
use rustc_middle:: mir:: * ;
5
5
use rustc_middle:: query:: Providers ;
6
+ use rustc_middle:: ty:: GenericArgs ;
6
7
use rustc_middle:: ty:: { self , CoroutineArgs , EarlyBinder , Ty , TyCtxt } ;
7
- use rustc_middle:: ty:: { GenericArgs , CAPTURE_STRUCT_LOCAL } ;
8
8
use rustc_target:: abi:: { FieldIdx , VariantIdx , FIRST_VARIANT } ;
9
9
10
10
use rustc_index:: { Idx , IndexVec } ;
@@ -68,37 +68,12 @@ fn make_shim<'tcx>(tcx: TyCtxt<'tcx>, instance: ty::InstanceDef<'tcx>) -> Body<'
68
68
69
69
ty:: InstanceDef :: ConstructCoroutineInClosureShim {
70
70
coroutine_closure_def_id,
71
- target_kind,
72
- } => match target_kind {
73
- ty:: ClosureKind :: Fn => unreachable ! ( "shouldn't be building shim for Fn" ) ,
74
- ty:: ClosureKind :: FnMut => {
75
- // No need to optimize the body, it has already been optimized
76
- // since we steal it from the `AsyncFn::call` body and just fix
77
- // the return type.
78
- return build_construct_coroutine_by_mut_shim ( tcx, coroutine_closure_def_id) ;
79
- }
80
- ty:: ClosureKind :: FnOnce => {
81
- build_construct_coroutine_by_move_shim ( tcx, coroutine_closure_def_id)
82
- }
83
- } ,
71
+ receiver_by_ref,
72
+ } => build_construct_coroutine_by_move_shim ( tcx, coroutine_closure_def_id, receiver_by_ref) ,
84
73
85
- ty:: InstanceDef :: CoroutineKindShim { coroutine_def_id, target_kind } => match target_kind {
86
- ty:: ClosureKind :: Fn => unreachable ! ( ) ,
87
- ty:: ClosureKind :: FnMut => {
88
- return tcx
89
- . optimized_mir ( coroutine_def_id)
90
- . coroutine_by_mut_body ( )
91
- . unwrap ( )
92
- . clone ( ) ;
93
- }
94
- ty:: ClosureKind :: FnOnce => {
95
- return tcx
96
- . optimized_mir ( coroutine_def_id)
97
- . coroutine_by_move_body ( )
98
- . unwrap ( )
99
- . clone ( ) ;
100
- }
101
- } ,
74
+ ty:: InstanceDef :: CoroutineKindShim { coroutine_def_id } => {
75
+ return tcx. optimized_mir ( coroutine_def_id) . coroutine_by_move_body ( ) . unwrap ( ) . clone ( ) ;
76
+ }
102
77
103
78
ty:: InstanceDef :: DropGlue ( def_id, ty) => {
104
79
// FIXME(#91576): Drop shims for coroutines aren't subject to the MIR passes at the end
@@ -119,21 +94,11 @@ fn make_shim<'tcx>(tcx: TyCtxt<'tcx>, instance: ty::InstanceDef<'tcx>) -> Body<'
119
94
let body = if id_args. as_coroutine ( ) . kind_ty ( ) == args. as_coroutine ( ) . kind_ty ( ) {
120
95
coroutine_body. coroutine_drop ( ) . unwrap ( )
121
96
} else {
122
- match args. as_coroutine ( ) . kind_ty ( ) . to_opt_closure_kind ( ) . unwrap ( ) {
123
- ty:: ClosureKind :: Fn => {
124
- unreachable ! ( )
125
- }
126
- ty:: ClosureKind :: FnMut => coroutine_body
127
- . coroutine_by_mut_body ( )
128
- . unwrap ( )
129
- . coroutine_drop ( )
130
- . unwrap ( ) ,
131
- ty:: ClosureKind :: FnOnce => coroutine_body
132
- . coroutine_by_move_body ( )
133
- . unwrap ( )
134
- . coroutine_drop ( )
135
- . unwrap ( ) ,
136
- }
97
+ assert_eq ! (
98
+ args. as_coroutine( ) . kind_ty( ) . to_opt_closure_kind( ) . unwrap( ) ,
99
+ ty:: ClosureKind :: FnOnce
100
+ ) ;
101
+ coroutine_body. coroutine_by_move_body ( ) . unwrap ( ) . coroutine_drop ( ) . unwrap ( )
137
102
} ;
138
103
139
104
let mut body = EarlyBinder :: bind ( body. clone ( ) ) . instantiate ( tcx, args) ;
@@ -1047,12 +1012,17 @@ fn build_fn_ptr_addr_shim<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId, self_ty: Ty<'t
1047
1012
fn build_construct_coroutine_by_move_shim < ' tcx > (
1048
1013
tcx : TyCtxt < ' tcx > ,
1049
1014
coroutine_closure_def_id : DefId ,
1015
+ receiver_by_ref : bool ,
1050
1016
) -> Body < ' tcx > {
1051
- let self_ty = tcx. type_of ( coroutine_closure_def_id) . instantiate_identity ( ) ;
1017
+ let mut self_ty = tcx. type_of ( coroutine_closure_def_id) . instantiate_identity ( ) ;
1052
1018
let ty:: CoroutineClosure ( _, args) = * self_ty. kind ( ) else {
1053
1019
bug ! ( ) ;
1054
1020
} ;
1055
1021
1022
+ if receiver_by_ref {
1023
+ self_ty = Ty :: new_mut_ptr ( tcx, self_ty) ;
1024
+ }
1025
+
1056
1026
let poly_sig = args. as_coroutine_closure ( ) . coroutine_closure_sig ( ) . map_bound ( |sig| {
1057
1027
tcx. mk_fn_sig (
1058
1028
[ self_ty] . into_iter ( ) . chain ( sig. tupled_inputs_ty . tuple_fields ( ) ) ,
@@ -1108,7 +1078,7 @@ fn build_construct_coroutine_by_move_shim<'tcx>(
1108
1078
1109
1079
let source = MirSource :: from_instance ( ty:: InstanceDef :: ConstructCoroutineInClosureShim {
1110
1080
coroutine_closure_def_id,
1111
- target_kind : ty :: ClosureKind :: FnOnce ,
1081
+ receiver_by_ref ,
1112
1082
} ) ;
1113
1083
1114
1084
let body =
@@ -1117,40 +1087,3 @@ fn build_construct_coroutine_by_move_shim<'tcx>(
1117
1087
1118
1088
body
1119
1089
}
1120
-
1121
- fn build_construct_coroutine_by_mut_shim < ' tcx > (
1122
- tcx : TyCtxt < ' tcx > ,
1123
- coroutine_closure_def_id : DefId ,
1124
- ) -> Body < ' tcx > {
1125
- let mut body = tcx. optimized_mir ( coroutine_closure_def_id) . clone ( ) ;
1126
- let coroutine_closure_ty = tcx. type_of ( coroutine_closure_def_id) . instantiate_identity ( ) ;
1127
- let ty:: CoroutineClosure ( _, args) = * coroutine_closure_ty. kind ( ) else {
1128
- bug ! ( ) ;
1129
- } ;
1130
- let args = args. as_coroutine_closure ( ) ;
1131
-
1132
- body. local_decls [ RETURN_PLACE ] . ty =
1133
- tcx. instantiate_bound_regions_with_erased ( args. coroutine_closure_sig ( ) . map_bound ( |sig| {
1134
- sig. to_coroutine_given_kind_and_upvars (
1135
- tcx,
1136
- args. parent_args ( ) ,
1137
- tcx. coroutine_for_closure ( coroutine_closure_def_id) ,
1138
- ty:: ClosureKind :: FnMut ,
1139
- tcx. lifetimes . re_erased ,
1140
- args. tupled_upvars_ty ( ) ,
1141
- args. coroutine_captures_by_ref_ty ( ) ,
1142
- )
1143
- } ) ) ;
1144
- body. local_decls [ CAPTURE_STRUCT_LOCAL ] . ty =
1145
- Ty :: new_mut_ref ( tcx, tcx. lifetimes . re_erased , coroutine_closure_ty) ;
1146
-
1147
- body. source = MirSource :: from_instance ( ty:: InstanceDef :: ConstructCoroutineInClosureShim {
1148
- coroutine_closure_def_id,
1149
- target_kind : ty:: ClosureKind :: FnMut ,
1150
- } ) ;
1151
-
1152
- body. pass_count = 0 ;
1153
- dump_mir ( tcx, false , "coroutine_closure_by_mut" , & 0 , & body, |_, _| Ok ( ( ) ) ) ;
1154
-
1155
- body
1156
- }
0 commit comments