Skip to content

Commit 09ed44c

Browse files
authored
Unrolled build for rust-lang#121141
Rollup merge of rust-lang#121141 - compiler-errors:closure-kind-docs, r=nnethercote Fix closure kind docs I didn't review this close enough lol -- the old code snippet didn't use substs correctly, and had a malformed `if let`
2 parents a447249 + 954d565 commit 09ed44c

File tree

1 file changed

+26
-11
lines changed
  • compiler/rustc_middle/src/ty

1 file changed

+26
-11
lines changed

compiler/rustc_middle/src/ty/sty.rs

+26-11
Original file line numberDiff line numberDiff line change
@@ -2363,28 +2363,42 @@ impl<'tcx> Ty<'tcx> {
23632363
}
23642364

23652365
/// When we create a closure, we record its kind (i.e., what trait
2366-
/// it implements) into its `ClosureArgs` using a type
2366+
/// it implements, constrained by how it uses its borrows) into its
2367+
/// [`ty::ClosureArgs`] or [`ty::CoroutineClosureArgs`] using a type
23672368
/// parameter. This is kind of a phantom type, except that the
23682369
/// most convenient thing for us to are the integral types. This
23692370
/// function converts such a special type into the closure
2370-
/// kind. To go the other way, use `closure_kind.to_ty(tcx)`.
2371+
/// kind. To go the other way, use [`Ty::from_closure_kind`].
23712372
///
23722373
/// Note that during type checking, we use an inference variable
23732374
/// to represent the closure kind, because it has not yet been
23742375
/// inferred. Once upvar inference (in `rustc_hir_analysis/src/check/upvar.rs`)
2375-
/// is complete, that type variable will be unified.
2376+
/// is complete, that type variable will be unified with one of
2377+
/// the integral types.
23762378
///
2377-
/// To be noted that you can use [`ClosureArgs::kind()`] or [`CoroutineClosureArgs::kind()`]
2378-
/// to get the same information, which you can get by calling [`GenericArgs::as_closure()`]
2379-
/// or [`GenericArgs::as_coroutine_closure()`], depending on the type of the closure.
2379+
/// ```rust,ignore (snippet of compiler code)
2380+
/// if let TyKind::Closure(def_id, args) = closure_ty.kind()
2381+
/// && let Some(closure_kind) = args.as_closure().kind_ty().to_opt_closure_kind()
2382+
/// {
2383+
/// println!("{closure_kind:?}");
2384+
/// } else if let TyKind::CoroutineClosure(def_id, args) = closure_ty.kind()
2385+
/// && let Some(closure_kind) = args.as_coroutine_closure().kind_ty().to_opt_closure_kind()
2386+
/// {
2387+
/// println!("{closure_kind:?}");
2388+
/// }
2389+
/// ```
23802390
///
2381-
/// Otherwise, this method can be used as follows:
2391+
/// After upvar analysis, you should instead use [`ClosureArgs::kind()`]
2392+
/// or [`CoroutineClosureArgs::kind()`] to assert that the `ClosureKind`
2393+
/// has been constrained instead of manually calling this method.
23822394
///
23832395
/// ```rust,ignore (snippet of compiler code)
2384-
/// let TyKind::Closure(def_id, [closure_fn_kind_ty, ..]) = closure_ty.kind()
2385-
/// && let Some(closure_kind) = closure_fn_kind_ty.expect_ty().to_opt_closure_kind()
2396+
/// if let TyKind::Closure(def_id, args) = closure_ty.kind()
2397+
/// {
2398+
/// println!("{:?}", args.as_closure().kind());
2399+
/// } else if let TyKind::CoroutineClosure(def_id, args) = closure_ty.kind()
23862400
/// {
2387-
/// // your code
2401+
/// println!("{:?}", args.as_coroutine_closure().kind());
23882402
/// }
23892403
/// ```
23902404
pub fn to_opt_closure_kind(self) -> Option<ty::ClosureKind> {
@@ -2406,7 +2420,8 @@ impl<'tcx> Ty<'tcx> {
24062420
}
24072421
}
24082422

2409-
/// Inverse of [`Ty::to_opt_closure_kind`].
2423+
/// Inverse of [`Ty::to_opt_closure_kind`]. See docs on that method
2424+
/// for explanation of the relationship between `Ty` and [`ty::ClosureKind`].
24102425
pub fn from_closure_kind(tcx: TyCtxt<'tcx>, kind: ty::ClosureKind) -> Ty<'tcx> {
24112426
match kind {
24122427
ty::ClosureKind::Fn => tcx.types.i8,

0 commit comments

Comments
 (0)