Skip to content

Commit 427896d

Browse files
Construct body for by-move coroutine closure output
1 parent fc4fff4 commit 427896d

File tree

24 files changed

+233
-15
lines changed

24 files changed

+233
-15
lines changed

compiler/rustc_borrowck/src/type_check/input_output.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
8585
self.tcx(),
8686
ty::CoroutineArgsParts {
8787
parent_args: args.parent_args(),
88+
kind_ty: Ty::from_closure_kind(self.tcx(), args.kind()),
8889
resume_ty: next_ty_var(),
8990
yield_ty: next_ty_var(),
9091
witness: next_ty_var(),

compiler/rustc_const_eval/src/interpret/terminator.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
546546
| ty::InstanceDef::ReifyShim(..)
547547
| ty::InstanceDef::ClosureOnceShim { .. }
548548
| ty::InstanceDef::ConstructCoroutineInClosureShim { .. }
549+
| ty::InstanceDef::CoroutineByMoveShim { .. }
549550
| ty::InstanceDef::FnPtrShim(..)
550551
| ty::InstanceDef::DropGlue(..)
551552
| ty::InstanceDef::CloneShim(..)

compiler/rustc_hir_typeck/src/callee.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
183183
coroutine_closure_sig.to_coroutine(
184184
self.tcx,
185185
closure_args.parent_args(),
186+
closure_args.kind_ty(),
186187
self.tcx.coroutine_for_closure(def_id),
187188
tupled_upvars_ty,
188189
),

compiler/rustc_hir_typeck/src/closure.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,10 +175,20 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
175175
interior,
176176
));
177177

178+
let kind_ty = match kind {
179+
hir::CoroutineKind::Desugared(_, hir::CoroutineSource::Closure) => self
180+
.next_ty_var(TypeVariableOrigin {
181+
kind: TypeVariableOriginKind::ClosureSynthetic,
182+
span: expr_span,
183+
}),
184+
_ => tcx.types.unit,
185+
};
186+
178187
let coroutine_args = ty::CoroutineArgs::new(
179188
tcx,
180189
ty::CoroutineArgsParts {
181190
parent_args,
191+
kind_ty,
182192
resume_ty,
183193
yield_ty,
184194
return_ty: liberated_sig.output(),
@@ -256,6 +266,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
256266
sig.to_coroutine(
257267
tcx,
258268
parent_args,
269+
closure_kind_ty,
259270
tcx.coroutine_for_closure(expr_def_id),
260271
coroutine_upvars_ty,
261272
)

compiler/rustc_hir_typeck/src/upvar.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,16 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
393393
args.as_coroutine_closure().coroutine_captures_by_ref_ty(),
394394
coroutine_captures_by_ref_ty,
395395
);
396+
397+
let ty::Coroutine(_, args) = *self.typeck_results.borrow().expr_ty(body.value).kind()
398+
else {
399+
bug!();
400+
};
401+
self.demand_eqtype(
402+
span,
403+
args.as_coroutine().kind_ty(),
404+
Ty::from_closure_kind(self.tcx, closure_kind),
405+
);
396406
}
397407

398408
self.log_closure_min_capture_info(closure_def_id, span);

compiler/rustc_middle/src/mir/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,10 @@ pub struct CoroutineInfo<'tcx> {
262262
/// Coroutine drop glue. This field is populated after the state transform pass.
263263
pub coroutine_drop: Option<Body<'tcx>>,
264264

265+
/// The body of the coroutine, modified to take its upvars by move.
266+
/// TODO:
267+
pub by_move_body: Option<Body<'tcx>>,
268+
265269
/// The layout of a coroutine. This field is populated after the state transform pass.
266270
pub coroutine_layout: Option<CoroutineLayout<'tcx>>,
267271

@@ -281,6 +285,7 @@ impl<'tcx> CoroutineInfo<'tcx> {
281285
coroutine_kind,
282286
yield_ty: Some(yield_ty),
283287
resume_ty: Some(resume_ty),
288+
by_move_body: None,
284289
coroutine_drop: None,
285290
coroutine_layout: None,
286291
}

compiler/rustc_middle/src/mir/mono.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,7 @@ impl<'tcx> CodegenUnit<'tcx> {
403403
| InstanceDef::Virtual(..)
404404
| InstanceDef::ClosureOnceShim { .. }
405405
| InstanceDef::ConstructCoroutineInClosureShim { .. }
406+
| InstanceDef::CoroutineByMoveShim { .. }
406407
| InstanceDef::DropGlue(..)
407408
| InstanceDef::CloneShim(..)
408409
| InstanceDef::ThreadLocalShim(..)

compiler/rustc_middle/src/mir/visit.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,7 @@ macro_rules! make_mir_visitor {
346346
ty::InstanceDef::ThreadLocalShim(_def_id) |
347347
ty::InstanceDef::ClosureOnceShim { call_once: _def_id, track_caller: _ } |
348348
ty::InstanceDef::ConstructCoroutineInClosureShim { coroutine_closure_def_id: _def_id, target_kind: _ } |
349+
ty::InstanceDef::CoroutineByMoveShim { coroutine_def_id: _def_id } |
349350
ty::InstanceDef::DropGlue(_def_id, None) => {}
350351

351352
ty::InstanceDef::FnPtrShim(_def_id, ty) |

compiler/rustc_middle/src/ty/instance.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,9 @@ pub enum InstanceDef<'tcx> {
101101
target_kind: ty::ClosureKind,
102102
},
103103

104+
/// TODO:
105+
CoroutineByMoveShim { coroutine_def_id: DefId },
106+
104107
/// Compiler-generated accessor for thread locals which returns a reference to the thread local
105108
/// the `DefId` defines. This is used to export thread locals from dylibs on platforms lacking
106109
/// native support.
@@ -186,6 +189,7 @@ impl<'tcx> InstanceDef<'tcx> {
186189
coroutine_closure_def_id: def_id,
187190
target_kind: _,
188191
}
192+
| ty::InstanceDef::CoroutineByMoveShim { coroutine_def_id: def_id }
189193
| InstanceDef::DropGlue(def_id, _)
190194
| InstanceDef::CloneShim(def_id, _)
191195
| InstanceDef::FnPtrAddrShim(def_id, _) => def_id,
@@ -206,6 +210,7 @@ impl<'tcx> InstanceDef<'tcx> {
206210
| InstanceDef::Intrinsic(..)
207211
| InstanceDef::ClosureOnceShim { .. }
208212
| ty::InstanceDef::ConstructCoroutineInClosureShim { .. }
213+
| ty::InstanceDef::CoroutineByMoveShim { .. }
209214
| InstanceDef::DropGlue(..)
210215
| InstanceDef::CloneShim(..)
211216
| InstanceDef::FnPtrAddrShim(..) => None,
@@ -302,6 +307,7 @@ impl<'tcx> InstanceDef<'tcx> {
302307
| InstanceDef::DropGlue(_, Some(_)) => false,
303308
InstanceDef::ClosureOnceShim { .. }
304309
| InstanceDef::ConstructCoroutineInClosureShim { .. }
310+
| InstanceDef::CoroutineByMoveShim { .. }
305311
| InstanceDef::DropGlue(..)
306312
| InstanceDef::Item(_)
307313
| InstanceDef::Intrinsic(..)
@@ -340,6 +346,7 @@ fn fmt_instance(
340346
InstanceDef::FnPtrShim(_, ty) => write!(f, " - shim({ty})"),
341347
InstanceDef::ClosureOnceShim { .. } => write!(f, " - shim"),
342348
InstanceDef::ConstructCoroutineInClosureShim { .. } => write!(f, " - shim"),
349+
InstanceDef::CoroutineByMoveShim { .. } => write!(f, " - shim"),
343350
InstanceDef::DropGlue(_, None) => write!(f, " - shim(None)"),
344351
InstanceDef::DropGlue(_, Some(ty)) => write!(f, " - shim(Some({ty}))"),
345352
InstanceDef::CloneShim(_, ty) => write!(f, " - shim({ty})"),
@@ -631,7 +638,19 @@ impl<'tcx> Instance<'tcx> {
631638
};
632639

633640
if tcx.lang_items().get(coroutine_callable_item) == Some(trait_item_id) {
634-
Some(Instance { def: ty::InstanceDef::Item(coroutine_def_id), args: args })
641+
let ty::Coroutine(_, id_args) = *tcx.type_of(coroutine_def_id).skip_binder().kind()
642+
else {
643+
bug!()
644+
};
645+
646+
if args.as_coroutine().kind_ty() == id_args.as_coroutine().kind_ty() {
647+
Some(Instance { def: ty::InstanceDef::Item(coroutine_def_id), args })
648+
} else {
649+
Some(Instance {
650+
def: ty::InstanceDef::CoroutineByMoveShim { coroutine_def_id },
651+
args,
652+
})
653+
}
635654
} else {
636655
// All other methods should be defaulted methods of the built-in trait.
637656
// This is important for `Iterator`'s combinators, but also useful for

compiler/rustc_middle/src/ty/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1681,6 +1681,7 @@ impl<'tcx> TyCtxt<'tcx> {
16811681
| ty::InstanceDef::Virtual(..)
16821682
| ty::InstanceDef::ClosureOnceShim { .. }
16831683
| ty::InstanceDef::ConstructCoroutineInClosureShim { .. }
1684+
| ty::InstanceDef::CoroutineByMoveShim { .. }
16841685
| ty::InstanceDef::DropGlue(..)
16851686
| ty::InstanceDef::CloneShim(..)
16861687
| ty::InstanceDef::ThreadLocalShim(..)

0 commit comments

Comments
 (0)