1 //! See docs in build/expr/mod.rs
3 use crate::build::expr::category::Category;
4 use crate::build::ForGuard::{OutsideGuard, RefWithinGuard};
5 use crate::build::{BlockAnd, BlockAndExtension, Builder, Capture, CaptureMap};
6 use rustc_hir::def_id::LocalDefId;
7 use rustc_middle::hir::place::Projection as HirProjection;
8 use rustc_middle::hir::place::ProjectionKind as HirProjectionKind;
9 use rustc_middle::middle::region;
10 use rustc_middle::mir::AssertKind::BoundsCheck;
11 use rustc_middle::mir::*;
12 use rustc_middle::thir::*;
13 use rustc_middle::ty::AdtDef;
14 use rustc_middle::ty::{self, CanonicalUserTypeAnnotation, Ty, TyCtxt, Variance};
16 use rustc_target::abi::VariantIdx;
18 use rustc_index::vec::Idx;
20 use std::assert_matches::assert_matches;
23 /// The "outermost" place that holds this value.
24 #[derive(Copy, Clone, Debug, PartialEq)]
25 pub(crate) enum PlaceBase {
26 /// Denotes the start of a `Place`.
29 /// When building place for an expression within a closure, the place might start off a
30 /// captured path. When `capture_disjoint_fields` is enabled, we might not know the capture
31 /// index (within the desugared closure) of the captured path until most of the projections
32 /// are applied. We use `PlaceBase::Upvar` to keep track of the root variable off of which the
33 /// captured path starts, the closure the capture belongs to and the trait the closure
36 /// Once we have figured out the capture index, we can convert the place builder to start from
37 /// `PlaceBase::Local`.
39 /// Consider the following example
41 /// let t = (((10, 10), 10), 10);
44 /// println!("{}", t.0.0.0);
47 /// Here the THIR expression for `t.0.0.0` will be something like
49 /// ```ignore (illustrative)
56 /// When `capture_disjoint_fields` is enabled, `t.0.0.0` is captured and we won't be able to
57 /// figure out that it is captured until all the `Field` projections are applied.
59 /// HirId of the upvar
60 var_hir_id: LocalVarId,
61 /// DefId of the closure
62 closure_def_id: LocalDefId,
66 /// `PlaceBuilder` is used to create places during MIR construction. It allows you to "build up" a
67 /// place by pushing more and more projections onto the end, and then convert the final set into a
68 /// place using the `to_place` method.
70 /// This is used internally when building a place for an expression like `a.b.c`. The fields `b`
71 /// and `c` can be progressively pushed onto the place builder that is created when converting `a`.
72 #[derive(Clone, Debug, PartialEq)]
73 pub(in crate::build) struct PlaceBuilder<'tcx> {
75 projection: Vec<PlaceElem<'tcx>>,
78 /// Given a list of MIR projections, convert them to list of HIR ProjectionKind.
79 /// The projections are truncated to represent a path that might be captured by a
80 /// closure/generator. This implies the vector returned from this function doesn't contain
81 /// ProjectionElems `Downcast`, `ConstantIndex`, `Index`, or `Subslice` because those will never be
82 /// part of a path that is captured by a closure. We stop applying projections once we see the first
83 /// projection that isn't captured by a closure.
84 fn convert_to_hir_projections_and_truncate_for_capture<'tcx>(
85 mir_projections: &[PlaceElem<'tcx>],
86 ) -> Vec<HirProjectionKind> {
87 let mut hir_projections = Vec::new();
88 let mut variant = None;
90 for mir_projection in mir_projections {
91 let hir_projection = match mir_projection {
92 ProjectionElem::Deref => HirProjectionKind::Deref,
93 ProjectionElem::Field(field, _) => {
94 let variant = variant.unwrap_or(VariantIdx::new(0));
95 HirProjectionKind::Field(field.index() as u32, variant)
97 ProjectionElem::Downcast(.., idx) => {
98 // We don't expect to see multi-variant enums here, as earlier
99 // phases will have truncated them already. However, there can
100 // still be downcasts, thanks to single-variant enums.
101 // We keep track of VariantIdx so we can use this information
102 // if the next ProjectionElem is a Field.
103 variant = Some(*idx);
106 // These do not affect anything, they just make sure we know the right type.
107 ProjectionElem::OpaqueCast(_) => continue,
108 ProjectionElem::Index(..)
109 | ProjectionElem::ConstantIndex { .. }
110 | ProjectionElem::Subslice { .. } => {
111 // We don't capture array-access projections.
112 // We can stop here as arrays are captured completely.
117 hir_projections.push(hir_projection);
123 /// Return true if the `proj_possible_ancestor` represents an ancestor path
124 /// to `proj_capture` or `proj_possible_ancestor` is same as `proj_capture`,
125 /// assuming they both start off of the same root variable.
127 /// **Note:** It's the caller's responsibility to ensure that both lists of projections
128 /// start off of the same root variable.
130 /// Eg: 1. `foo.x` which is represented using `projections=[Field(x)]` is an ancestor of
131 /// `foo.x.y` which is represented using `projections=[Field(x), Field(y)]`.
132 /// Note both `foo.x` and `foo.x.y` start off of the same root variable `foo`.
133 /// 2. Since we only look at the projections here function will return `bar.x` as an a valid
134 /// ancestor of `foo.x.y`. It's the caller's responsibility to ensure that both projections
135 /// list are being applied to the same root variable.
136 fn is_ancestor_or_same_capture(
137 proj_possible_ancestor: &[HirProjectionKind],
138 proj_capture: &[HirProjectionKind],
140 // We want to make sure `is_ancestor_or_same_capture("x.0.0", "x.0")` to return false.
141 // Therefore we can't just check if all projections are same in the zipped iterator below.
142 if proj_possible_ancestor.len() > proj_capture.len() {
146 iter::zip(proj_possible_ancestor, proj_capture).all(|(a, b)| a == b)
149 /// Given a closure, returns the index of a capture within the desugared closure struct and the
150 /// `ty::CapturedPlace` which is the ancestor of the Place represented using the `var_hir_id`
151 /// and `projection`.
153 /// Note there will be at most one ancestor for any given Place.
155 /// Returns None, when the ancestor is not found.
156 fn find_capture_matching_projections<'a, 'tcx>(
157 upvars: &'a CaptureMap<'tcx>,
158 var_hir_id: LocalVarId,
159 projections: &[PlaceElem<'tcx>],
160 ) -> Option<(usize, &'a Capture<'tcx>)> {
161 let hir_projections = convert_to_hir_projections_and_truncate_for_capture(projections);
163 upvars.get_by_key_enumerated(var_hir_id.0).find(|(_, capture)| {
164 let possible_ancestor_proj_kinds: Vec<_> =
165 capture.captured_place.place.projections.iter().map(|proj| proj.kind).collect();
166 is_ancestor_or_same_capture(&possible_ancestor_proj_kinds, &hir_projections)
170 /// Takes an upvar place and tries to resolve it into a `PlaceBuilder`
171 /// with `PlaceBase::Local`
172 #[instrument(level = "trace", skip(cx), ret)]
173 fn to_upvars_resolved_place_builder<'tcx>(
174 cx: &Builder<'_, 'tcx>,
175 var_hir_id: LocalVarId,
176 closure_def_id: LocalDefId,
177 projection: &[PlaceElem<'tcx>],
178 ) -> Option<PlaceBuilder<'tcx>> {
179 let Some((capture_index, capture)) =
180 find_capture_matching_projections(
185 let closure_span = cx.tcx.def_span(closure_def_id);
186 if !enable_precise_capture(cx.tcx, closure_span) {
188 "No associated capture found for {:?}[{:#?}] even though \
189 capture_disjoint_fields isn't enabled",
195 "No associated capture found for {:?}[{:#?}]",
196 var_hir_id, projection,
202 // Access the capture by accessing the field within the Closure struct.
203 let capture_info = &cx.upvars[capture_index];
205 let mut upvar_resolved_place_builder = PlaceBuilder::from(capture_info.use_place);
207 // We used some of the projections to build the capture itself,
208 // now we apply the remaining to the upvar resolved place.
209 trace!(?capture.captured_place, ?projection);
210 let remaining_projections = strip_prefix(
211 capture.captured_place.place.base_ty,
213 &capture.captured_place.place.projections,
215 upvar_resolved_place_builder.projection.extend(remaining_projections);
217 Some(upvar_resolved_place_builder)
220 /// Returns projections remaining after stripping an initial prefix of HIR
223 /// Supports only HIR projection kinds that represent a path that might be
224 /// captured by a closure or a generator, i.e., an `Index` or a `Subslice`
225 /// projection kinds are unsupported.
226 fn strip_prefix<'a, 'tcx>(
227 mut base_ty: Ty<'tcx>,
228 projections: &'a [PlaceElem<'tcx>],
229 prefix_projections: &[HirProjection<'tcx>],
230 ) -> impl Iterator<Item = PlaceElem<'tcx>> + 'a {
231 let mut iter = projections
234 // Filter out opaque casts, they are unnecessary in the prefix.
235 .filter(|elem| !matches!(elem, ProjectionElem::OpaqueCast(..)));
236 for projection in prefix_projections {
237 match projection.kind {
238 HirProjectionKind::Deref => {
239 assert_matches!(iter.next(), Some(ProjectionElem::Deref));
241 HirProjectionKind::Field(..) => {
242 if base_ty.is_enum() {
243 assert_matches!(iter.next(), Some(ProjectionElem::Downcast(..)));
245 assert_matches!(iter.next(), Some(ProjectionElem::Field(..)));
247 HirProjectionKind::Index | HirProjectionKind::Subslice => {
248 bug!("unexpected projection kind: {:?}", projection);
251 base_ty = projection.ty;
256 impl<'tcx> PlaceBuilder<'tcx> {
257 pub(in crate::build) fn to_place(&self, cx: &Builder<'_, 'tcx>) -> Place<'tcx> {
258 self.try_to_place(cx).unwrap()
261 /// Creates a `Place` or returns `None` if an upvar cannot be resolved
262 pub(in crate::build) fn try_to_place(&self, cx: &Builder<'_, 'tcx>) -> Option<Place<'tcx>> {
263 let resolved = self.resolve_upvar(cx);
264 let builder = resolved.as_ref().unwrap_or(self);
265 let PlaceBase::Local(local) = builder.base else { return None };
266 let projection = cx.tcx.intern_place_elems(&builder.projection);
267 Some(Place { local, projection })
270 /// Attempts to resolve the `PlaceBuilder`.
271 /// Returns `None` if this is not an upvar.
273 /// Upvars resolve may fail for a `PlaceBuilder` when attempting to
274 /// resolve a disjoint field whose root variable is not captured
275 /// (destructured assignments) or when attempting to resolve a root
276 /// variable (discriminant matching with only wildcard arm) that is
277 /// not captured. This can happen because the final mir that will be
278 /// generated doesn't require a read for this place. Failures will only
279 /// happen inside closures.
280 pub(in crate::build) fn resolve_upvar(
282 cx: &Builder<'_, 'tcx>,
283 ) -> Option<PlaceBuilder<'tcx>> {
284 let PlaceBase::Upvar { var_hir_id, closure_def_id } = self.base else {
287 to_upvars_resolved_place_builder(cx, var_hir_id, closure_def_id, &self.projection)
290 pub(crate) fn base(&self) -> PlaceBase {
294 pub(crate) fn projection(&self) -> &[PlaceElem<'tcx>] {
298 pub(crate) fn field(self, f: Field, ty: Ty<'tcx>) -> Self {
299 self.project(PlaceElem::Field(f, ty))
302 pub(crate) fn deref(self) -> Self {
303 self.project(PlaceElem::Deref)
306 pub(crate) fn downcast(self, adt_def: AdtDef<'tcx>, variant_index: VariantIdx) -> Self {
307 self.project(PlaceElem::Downcast(Some(adt_def.variant(variant_index).name), variant_index))
310 fn index(self, index: Local) -> Self {
311 self.project(PlaceElem::Index(index))
314 pub(crate) fn project(mut self, elem: PlaceElem<'tcx>) -> Self {
315 self.projection.push(elem);
319 /// Same as `.clone().project(..)` but more efficient
320 pub(crate) fn clone_project(&self, elem: PlaceElem<'tcx>) -> Self {
323 projection: Vec::from_iter(self.projection.iter().copied().chain([elem])),
328 impl<'tcx> From<Local> for PlaceBuilder<'tcx> {
329 fn from(local: Local) -> Self {
330 Self { base: PlaceBase::Local(local), projection: Vec::new() }
334 impl<'tcx> From<PlaceBase> for PlaceBuilder<'tcx> {
335 fn from(base: PlaceBase) -> Self {
336 Self { base, projection: Vec::new() }
340 impl<'tcx> From<Place<'tcx>> for PlaceBuilder<'tcx> {
341 fn from(p: Place<'tcx>) -> Self {
342 Self { base: PlaceBase::Local(p.local), projection: p.projection.to_vec() }
346 impl<'a, 'tcx> Builder<'a, 'tcx> {
347 /// Compile `expr`, yielding a place that we can move from etc.
349 /// WARNING: Any user code might:
350 /// * Invalidate any slice bounds checks performed.
351 /// * Change the address that this `Place` refers to.
352 /// * Modify the memory that this place refers to.
353 /// * Invalidate the memory that this place refers to, this will be caught
354 /// by borrow checking.
356 /// Extra care is needed if any user code is allowed to run between calling
357 /// this method and using it, as is the case for `match` and index
359 pub(crate) fn as_place(
361 mut block: BasicBlock,
363 ) -> BlockAnd<Place<'tcx>> {
364 let place_builder = unpack!(block = self.as_place_builder(block, expr));
365 block.and(place_builder.to_place(self))
368 /// This is used when constructing a compound `Place`, so that we can avoid creating
369 /// intermediate `Place` values until we know the full set of projections.
370 pub(crate) fn as_place_builder(
374 ) -> BlockAnd<PlaceBuilder<'tcx>> {
375 self.expr_as_place(block, expr, Mutability::Mut, None)
378 /// Compile `expr`, yielding a place that we can move from etc.
379 /// Mutability note: The caller of this method promises only to read from the resulting
380 /// place. The place itself may or may not be mutable:
381 /// * If this expr is a place expr like a.b, then we will return that place.
382 /// * Otherwise, a temporary is created: in that event, it will be an immutable temporary.
383 pub(crate) fn as_read_only_place(
385 mut block: BasicBlock,
387 ) -> BlockAnd<Place<'tcx>> {
388 let place_builder = unpack!(block = self.as_read_only_place_builder(block, expr));
389 block.and(place_builder.to_place(self))
392 /// This is used when constructing a compound `Place`, so that we can avoid creating
393 /// intermediate `Place` values until we know the full set of projections.
394 /// Mutability note: The caller of this method promises only to read from the resulting
395 /// place. The place itself may or may not be mutable:
396 /// * If this expr is a place expr like a.b, then we will return that place.
397 /// * Otherwise, a temporary is created: in that event, it will be an immutable temporary.
398 fn as_read_only_place_builder(
402 ) -> BlockAnd<PlaceBuilder<'tcx>> {
403 self.expr_as_place(block, expr, Mutability::Not, None)
408 mut block: BasicBlock,
410 mutability: Mutability,
411 fake_borrow_temps: Option<&mut Vec<Local>>,
412 ) -> BlockAnd<PlaceBuilder<'tcx>> {
413 debug!("expr_as_place(block={:?}, expr={:?}, mutability={:?})", block, expr, mutability);
416 let expr_span = expr.span;
417 let source_info = this.source_info(expr_span);
419 ExprKind::Scope { region_scope, lint_level, value } => {
420 this.in_scope((region_scope, source_info), lint_level, |this| {
421 this.expr_as_place(block, &this.thir[value], mutability, fake_borrow_temps)
424 ExprKind::Field { lhs, variant_index, name } => {
425 let lhs = &this.thir[lhs];
426 let mut place_builder =
427 unpack!(block = this.expr_as_place(block, lhs, mutability, fake_borrow_temps,));
428 if let ty::Adt(adt_def, _) = lhs.ty.kind() {
429 if adt_def.is_enum() {
430 place_builder = place_builder.downcast(*adt_def, variant_index);
433 block.and(place_builder.field(name, expr.ty))
435 ExprKind::Deref { arg } => {
436 let place_builder = unpack!(
438 this.expr_as_place(block, &this.thir[arg], mutability, fake_borrow_temps,)
440 block.and(place_builder.deref())
442 ExprKind::Index { lhs, index } => this.lower_index_expression(
452 ExprKind::UpvarRef { closure_def_id, var_hir_id } => {
453 this.lower_captured_upvar(block, closure_def_id.expect_local(), var_hir_id)
456 ExprKind::VarRef { id } => {
457 let place_builder = if this.is_bound_var_in_guard(id) {
458 let index = this.var_local_id(id, RefWithinGuard);
459 PlaceBuilder::from(index).deref()
461 let index = this.var_local_id(id, OutsideGuard);
462 PlaceBuilder::from(index)
464 block.and(place_builder)
467 ExprKind::PlaceTypeAscription { source, ref user_ty } => {
468 let place_builder = unpack!(
469 block = this.expr_as_place(
476 if let Some(user_ty) = user_ty {
477 let annotation_index =
478 this.canonical_user_type_annotations.push(CanonicalUserTypeAnnotation {
479 span: source_info.span,
480 user_ty: user_ty.clone(),
481 inferred_ty: expr.ty,
484 let place = place_builder.to_place(this);
489 kind: StatementKind::AscribeUserType(
492 UserTypeProjection { base: annotation_index, projs: vec![] },
499 block.and(place_builder)
501 ExprKind::ValueTypeAscription { source, ref user_ty } => {
502 let source = &this.thir[source];
504 unpack!(block = this.as_temp(block, source.temp_lifetime, source, mutability));
505 if let Some(user_ty) = user_ty {
506 let annotation_index =
507 this.canonical_user_type_annotations.push(CanonicalUserTypeAnnotation {
508 span: source_info.span,
509 user_ty: user_ty.clone(),
510 inferred_ty: expr.ty,
516 kind: StatementKind::AscribeUserType(
519 UserTypeProjection { base: annotation_index, projs: vec![] },
526 block.and(PlaceBuilder::from(temp))
529 ExprKind::Array { .. }
530 | ExprKind::Tuple { .. }
531 | ExprKind::Adt { .. }
532 | ExprKind::Closure { .. }
533 | ExprKind::Unary { .. }
534 | ExprKind::Binary { .. }
535 | ExprKind::LogicalOp { .. }
536 | ExprKind::Box { .. }
537 | ExprKind::Cast { .. }
538 | ExprKind::Use { .. }
539 | ExprKind::NeverToAny { .. }
540 | ExprKind::Pointer { .. }
541 | ExprKind::Repeat { .. }
542 | ExprKind::Borrow { .. }
543 | ExprKind::AddressOf { .. }
544 | ExprKind::Match { .. }
545 | ExprKind::If { .. }
546 | ExprKind::Loop { .. }
547 | ExprKind::Block { .. }
548 | ExprKind::Let { .. }
549 | ExprKind::Assign { .. }
550 | ExprKind::AssignOp { .. }
551 | ExprKind::Break { .. }
552 | ExprKind::Continue { .. }
553 | ExprKind::Return { .. }
554 | ExprKind::Literal { .. }
555 | ExprKind::NamedConst { .. }
556 | ExprKind::NonHirLiteral { .. }
557 | ExprKind::ZstLiteral { .. }
558 | ExprKind::ConstParam { .. }
559 | ExprKind::ConstBlock { .. }
560 | ExprKind::StaticRef { .. }
561 | ExprKind::InlineAsm { .. }
562 | ExprKind::Yield { .. }
563 | ExprKind::ThreadLocalRef(_)
564 | ExprKind::Call { .. } => {
565 // these are not places, so we need to make a temporary.
566 debug_assert!(!matches!(Category::of(&expr.kind), Some(Category::Place)));
568 unpack!(block = this.as_temp(block, expr.temp_lifetime, expr, mutability));
569 block.and(PlaceBuilder::from(temp))
574 /// Lower a captured upvar. Note we might not know the actual capture index,
575 /// so we create a place starting from `PlaceBase::Upvar`, which will be resolved
576 /// once all projections that allow us to identify a capture have been applied.
577 fn lower_captured_upvar(
580 closure_def_id: LocalDefId,
581 var_hir_id: LocalVarId,
582 ) -> BlockAnd<PlaceBuilder<'tcx>> {
583 block.and(PlaceBuilder::from(PlaceBase::Upvar { var_hir_id, closure_def_id }))
586 /// Lower an index expression
588 /// This has two complications;
590 /// * We need to do a bounds check.
591 /// * We need to ensure that the bounds check can't be invalidated using an
592 /// expression like `x[1][{x = y; 2}]`. We use fake borrows here to ensure
593 /// that this is the case.
594 fn lower_index_expression(
596 mut block: BasicBlock,
599 mutability: Mutability,
600 fake_borrow_temps: Option<&mut Vec<Local>>,
601 temp_lifetime: Option<region::Scope>,
603 source_info: SourceInfo,
604 ) -> BlockAnd<PlaceBuilder<'tcx>> {
605 let base_fake_borrow_temps = &mut Vec::new();
606 let is_outermost_index = fake_borrow_temps.is_none();
607 let fake_borrow_temps = fake_borrow_temps.unwrap_or(base_fake_borrow_temps);
610 unpack!(block = self.expr_as_place(block, base, mutability, Some(fake_borrow_temps),));
612 // Making this a *fresh* temporary means we do not have to worry about
613 // the index changing later: Nothing will ever change this temporary.
614 // The "retagging" transformation (for Stacked Borrows) relies on this.
615 let idx = unpack!(block = self.as_temp(block, temp_lifetime, index, Mutability::Not,));
617 block = self.bounds_check(block, &base_place, idx, expr_span, source_info);
619 if is_outermost_index {
620 self.read_fake_borrows(block, fake_borrow_temps, source_info)
622 self.add_fake_borrows_of_base(
623 base_place.to_place(self),
631 block.and(base_place.index(idx))
637 slice: &PlaceBuilder<'tcx>,
640 source_info: SourceInfo,
642 let usize_ty = self.tcx.types.usize;
643 let bool_ty = self.tcx.types.bool;
645 let len = self.temp(usize_ty, expr_span);
646 let lt = self.temp(bool_ty, expr_span);
649 self.cfg.push_assign(block, source_info, len, Rvalue::Len(slice.to_place(self)));
651 self.cfg.push_assign(
657 Box::new((Operand::Copy(Place::from(index)), Operand::Copy(len))),
660 let msg = BoundsCheck { len: Operand::Move(len), index: Operand::Copy(Place::from(index)) };
661 // assert!(lt, "...")
662 self.assert(block, Operand::Move(lt), true, msg, expr_span)
665 fn add_fake_borrows_of_base(
667 base_place: Place<'tcx>,
669 fake_borrow_temps: &mut Vec<Local>,
671 source_info: SourceInfo,
675 let place_ty = base_place.ty(&self.local_decls, tcx);
676 if let ty::Slice(_) = place_ty.ty.kind() {
677 // We need to create fake borrows to ensure that the bounds
678 // check that we just did stays valid. Since we can't assign to
679 // unsized values, we only need to ensure that none of the
680 // pointers in the base place are modified.
681 for (idx, elem) in base_place.projection.iter().enumerate().rev() {
683 ProjectionElem::Deref => {
684 let fake_borrow_deref_ty = Place::ty_from(
686 &base_place.projection[..idx],
692 tcx.mk_imm_ref(tcx.lifetimes.re_erased, fake_borrow_deref_ty);
693 let fake_borrow_temp =
694 self.local_decls.push(LocalDecl::new(fake_borrow_ty, expr_span));
695 let projection = tcx.intern_place_elems(&base_place.projection[..idx]);
696 self.cfg.push_assign(
699 fake_borrow_temp.into(),
701 tcx.lifetimes.re_erased,
703 Place { local: base_place.local, projection },
706 fake_borrow_temps.push(fake_borrow_temp);
708 ProjectionElem::Index(_) => {
709 let index_ty = Place::ty_from(
711 &base_place.projection[..idx],
715 match index_ty.ty.kind() {
716 // The previous index expression has already
717 // done any index expressions needed here.
718 ty::Slice(_) => break,
720 _ => bug!("unexpected index base"),
723 ProjectionElem::Field(..)
724 | ProjectionElem::Downcast(..)
725 | ProjectionElem::OpaqueCast(..)
726 | ProjectionElem::ConstantIndex { .. }
727 | ProjectionElem::Subslice { .. } => (),
733 fn read_fake_borrows(
736 fake_borrow_temps: &mut Vec<Local>,
737 source_info: SourceInfo,
739 // All indexes have been evaluated now, read all of the
740 // fake borrows so that they are live across those index
742 for temp in fake_borrow_temps {
743 self.cfg.push_fake_read(bb, source_info, FakeReadCause::ForIndex, Place::from(*temp));
748 /// Precise capture is enabled if the feature gate `capture_disjoint_fields` is enabled or if
749 /// user is using Rust Edition 2021 or higher.
750 fn enable_precise_capture(tcx: TyCtxt<'_>, closure_span: Span) -> bool {
751 tcx.features().capture_disjoint_fields || closure_span.rust_2021()