Skip to content

Commit 51c482f

Browse files
Bless tests, add comments
1 parent 7234f73 commit 51c482f

31 files changed

+231
-116
lines changed

compiler/rustc_borrowck/src/type_check/input_output.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,9 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
4747
user_provided_sig,
4848
);
4949

50-
// FIXME(async_closures): We must apply the same transformation to our
51-
// signature here as we do during closure checking.
50+
// FIXME(async_closures): It's kind of wacky that we must apply this
51+
// transofmration here, since we do the same thing in HIR typeck.
52+
// Maybe we could just fix up the canonicalized signature during HIR typeck?
5253
if let DefiningTy::CoroutineClosure(_, args) =
5354
self.borrowck_context.universal_regions.defining_ty
5455
{

compiler/rustc_borrowck/src/universal_regions.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,10 @@ pub enum DefiningTy<'tcx> {
9898
Coroutine(DefId, GenericArgsRef<'tcx>),
9999

100100
/// The MIR is a special kind of closure that returns coroutines.
101-
/// TODO: describe how to make the sig...
101+
///
102+
/// See the documentation on `CoroutineClosureSignature` for details
103+
/// on how to construct the callable signature of the coroutine from
104+
/// its args.
102105
CoroutineClosure(DefId, GenericArgsRef<'tcx>),
103106

104107
/// The MIR is a fn item with the given `DefId` and args. The signature

compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
333333
continue;
334334
}
335335

336-
// For this check, we do *not* want to treat async coroutine closures (async blocks)
336+
// For this check, we do *not* want to treat async coroutine-closures (async blocks)
337337
// as proper closures. Doing so would regress type inference when feeding
338338
// the return value of an argument-position async block to an argument-position
339339
// closure wrapped in a block.

compiler/rustc_middle/src/mir/mod.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,8 +260,16 @@ pub struct CoroutineInfo<'tcx> {
260260
/// Coroutine drop glue. This field is populated after the state transform pass.
261261
pub coroutine_drop: Option<Body<'tcx>>,
262262

263-
/// The body of the coroutine, modified to take its upvars by move.
264-
/// TODO:
263+
/// The body of the coroutine, modified to take its upvars by move rather than by ref.
264+
///
265+
/// This is used by coroutine-closures, which must return a different flavor of coroutine
266+
/// when called using `AsyncFnOnce::call_once`. It is produced by the `ByMoveBody` which
267+
/// is run right after building the initial MIR, and will only be populated for coroutines
268+
/// which come out of the async closure desugaring.
269+
///
270+
/// This body should be processed in lockstep with the containing body -- any optimization
271+
/// passes, etc, should be applied to this body as well. This is done automatically if
272+
/// using `run_passes`.
265273
pub by_move_body: Option<Body<'tcx>>,
266274

267275
/// The layout of a coroutine. This field is populated after the state transform pass.

compiler/rustc_middle/src/query/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -756,7 +756,7 @@ rustc_queries! {
756756
}
757757

758758
query coroutine_for_closure(def_id: DefId) -> DefId {
759-
desc { |_tcx| "TODO" }
759+
desc { |_tcx| "Given a coroutine-closure def id, return the def id of the coroutine returned by it" }
760760
separate_provide_extern
761761
}
762762

compiler/rustc_middle/src/traits/select.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,9 @@ pub enum SelectionCandidate<'tcx> {
138138
/// generated for an `async ||` expression.
139139
AsyncClosureCandidate,
140140

141-
// TODO:
141+
/// Implementation of the the `AsyncFnKindHelper` helper trait, which
142+
/// is used internally to delay computation for async closures until after
143+
/// upvar analysis is performed in HIR typeck.
142144
AsyncFnKindHelperCandidate,
143145

144146
/// Implementation of a `Coroutine` trait by one of the anonymous types

compiler/rustc_middle/src/ty/instance.rs

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

104-
/// TODO:
104+
/// `<[coroutine] as Future>::poll`, but for coroutines produced when `AsyncFnOnce`
105+
/// is called on a coroutine-closure whose closure kind is not `FnOnce`. This
106+
/// will select the body that is produced by the `ByMoveBody` transform, and thus
107+
/// take and use all of its upvars by-move rather than by-ref.
105108
CoroutineByMoveShim { coroutine_def_id: DefId },
106109

107110
/// Compiler-generated accessor for thread locals which returns a reference to the thread local

compiler/rustc_middle/src/ty/print/pretty.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -877,7 +877,7 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write {
877877
ty::CoroutineClosure(did, args) => {
878878
p!(write("{{"));
879879
if !self.should_print_verbose() {
880-
p!(write("coroutine closure"));
880+
p!(write("coroutine-closure"));
881881
// FIXME(eddyb) should use `def_span`.
882882
if let Some(did) = did.as_local() {
883883
if self.tcx().sess.opts.unstable_opts.span_free_formats {

compiler/rustc_middle/src/ty/sty.rs

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -352,11 +352,31 @@ pub struct CoroutineClosureArgs<'tcx> {
352352
}
353353

354354
pub struct CoroutineClosureArgsParts<'tcx> {
355+
/// This is the args of the typeck root.
355356
pub parent_args: &'tcx [GenericArg<'tcx>],
357+
/// Represents the maximum calling capability of the closure.
356358
pub closure_kind_ty: Ty<'tcx>,
359+
/// Represents all of the relevant parts of the coroutine returned by this
360+
/// coroutine-closure. This signature parts type will have the general
361+
/// shape of `fn(tupled_inputs, resume_ty) -> (return_ty, yield_ty)`, where
362+
/// `resume_ty`, `return_ty`, and `yield_ty` are the respective types for the
363+
/// coroutine returned by the coroutine-closure.
364+
///
365+
/// Use `coroutine_closure_sig` to break up this type rather than using it
366+
/// yourself.
357367
pub signature_parts_ty: Ty<'tcx>,
368+
/// The upvars captured by the closure. Remains an inference variable
369+
/// until the upvar analysis, which happens late in HIR typeck.
358370
pub tupled_upvars_ty: Ty<'tcx>,
371+
/// a function pointer that has the shape `for<'env> fn() -> (&'env T, ...)`.
372+
/// This allows us to represent the binder of the self-captures of the closure.
373+
///
374+
/// For example, if the coroutine returned by the closure borrows `String`
375+
/// from the closure's upvars, this will be `for<'env> fn() -> (&'env String,)`,
376+
/// while the `tupled_upvars_ty`, representing the by-move version of the same
377+
/// captures, will be `(String,)`.
359378
pub coroutine_captures_by_ref_ty: Ty<'tcx>,
379+
/// Witness type returned by the generator produced by this coroutine-closure.
360380
pub coroutine_witness_ty: Ty<'tcx>,
361381
}
362382

@@ -572,15 +592,27 @@ pub struct CoroutineArgs<'tcx> {
572592
pub struct CoroutineArgsParts<'tcx> {
573593
/// This is the args of the typeck root.
574594
pub parent_args: &'tcx [GenericArg<'tcx>],
575-
// TODO: why
595+
596+
/// The coroutines returned by a coroutine-closure's `AsyncFnOnce`/`AsyncFnMut`
597+
/// implementations must be distinguished since the former takes the closure's
598+
/// upvars by move, and the latter takes the closure's upvars by ref.
599+
///
600+
/// This field distinguishes these fields so that codegen can select the right
601+
/// body for the coroutine. This has the same type representation as the closure
602+
/// kind: `i8`/`i16`/`i32`.
603+
///
604+
/// For regular coroutines, this field will always just be `()`.
576605
pub kind_ty: Ty<'tcx>,
606+
577607
pub resume_ty: Ty<'tcx>,
578608
pub yield_ty: Ty<'tcx>,
579609
pub return_ty: Ty<'tcx>,
610+
580611
/// The interior type of the coroutine.
581612
/// Represents all types that are stored in locals
582613
/// in the coroutine's body.
583614
pub witness: Ty<'tcx>,
615+
584616
/// The upvars captured by the closure. Remains an inference variable
585617
/// until the upvar analysis, which happens late in HIR typeck.
586618
pub tupled_upvars_ty: Ty<'tcx>,
@@ -632,7 +664,7 @@ impl<'tcx> CoroutineArgs<'tcx> {
632664
self.split().parent_args
633665
}
634666

635-
// TODO:
667+
// Returns the kind of the coroutine. See docs on the `kind_ty` field.
636668
pub fn kind_ty(self) -> Ty<'tcx> {
637669
self.split().kind_ty
638670
}

compiler/rustc_mir_transform/src/const_prop_lint.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -607,7 +607,8 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
607607
AggregateKind::Array(_)
608608
| AggregateKind::Tuple
609609
| AggregateKind::Closure(_, _)
610-
| AggregateKind::Coroutine(_, _) => VariantIdx::new(0),
610+
| AggregateKind::Coroutine(_, _)
611+
| AggregateKind::CoroutineClosure(_, _) => VariantIdx::new(0),
611612
},
612613
},
613614

0 commit comments

Comments
 (0)