@@ -2363,28 +2363,42 @@ impl<'tcx> Ty<'tcx> {
2363
2363
}
2364
2364
2365
2365
/// 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
2367
2368
/// parameter. This is kind of a phantom type, except that the
2368
2369
/// most convenient thing for us to are the integral types. This
2369
2370
/// 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`] .
2371
2372
///
2372
2373
/// Note that during type checking, we use an inference variable
2373
2374
/// to represent the closure kind, because it has not yet been
2374
2375
/// 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.
2376
2378
///
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
+ /// ```
2380
2390
///
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.
2382
2394
///
2383
2395
/// ```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()
2386
2400
/// {
2387
- /// // your code
2401
+ /// println!("{:?}", args.as_coroutine_closure().kind());
2388
2402
/// }
2389
2403
/// ```
2390
2404
pub fn to_opt_closure_kind ( self ) -> Option < ty:: ClosureKind > {
@@ -2406,7 +2420,8 @@ impl<'tcx> Ty<'tcx> {
2406
2420
}
2407
2421
}
2408
2422
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`].
2410
2425
pub fn from_closure_kind ( tcx : TyCtxt < ' tcx > , kind : ty:: ClosureKind ) -> Ty < ' tcx > {
2411
2426
match kind {
2412
2427
ty:: ClosureKind :: Fn => tcx. types . i8 ,
0 commit comments