|
71 | 71 |
|
72 | 72 | use rustc_data_structures::unord::UnordMap; |
73 | 73 | use rustc_hir as hir; |
74 | | -use rustc_middle::hir::place::{PlaceBase, Projection, ProjectionKind}; |
| 74 | +use rustc_middle::hir::place::{Projection, ProjectionKind}; |
75 | 75 | use rustc_middle::mir::visit::MutVisitor; |
76 | 76 | use rustc_middle::mir::{self, dump_mir, MirPass}; |
77 | 77 | use rustc_middle::ty::{self, InstanceDef, Ty, TyCtxt, TypeVisitableExt}; |
@@ -124,36 +124,10 @@ impl<'tcx> MirPass<'tcx> for ByMoveBody { |
124 | 124 | .tuple_fields() |
125 | 125 | .len(); |
126 | 126 |
|
127 | | - let mut field_remapping = UnordMap::default(); |
128 | | - |
129 | | - let mut child_captures = tcx |
130 | | - .closure_captures(coroutine_def_id) |
131 | | - .iter() |
132 | | - .copied() |
133 | | - // By construction we capture all the args first. |
134 | | - .skip(num_args) |
135 | | - .enumerate() |
136 | | - .peekable(); |
137 | | - |
138 | | - // One parent capture may correspond to several child captures if we end up |
139 | | - // refining the set of captures via edition-2021 precise captures. We want to |
140 | | - // match up any number of child captures with one parent capture, so we keep |
141 | | - // peeking off this `Peekable` until the child doesn't match anymore. |
142 | | - for (parent_field_idx, parent_capture) in |
143 | | - tcx.closure_captures(parent_def_id).iter().copied().enumerate() |
144 | | - { |
145 | | - // Make sure we use every field at least once, b/c why are we capturing something |
146 | | - // if it's not used in the inner coroutine. |
147 | | - let mut field_used_at_least_once = false; |
148 | | - |
149 | | - // A parent matches a child if they share the same prefix of projections. |
150 | | - // The child may have more, if it is capturing sub-fields out of |
151 | | - // something that is captured by-move in the parent closure. |
152 | | - while child_captures.peek().map_or(false, |(_, child_capture)| { |
153 | | - child_prefix_matches_parent_projections(parent_capture, child_capture) |
154 | | - }) { |
155 | | - let (child_field_idx, child_capture) = child_captures.next().unwrap(); |
156 | | - |
| 127 | + let field_remapping: UnordMap<_, _> = ty::analyze_coroutine_closure_captures( |
| 128 | + tcx.closure_captures(parent_def_id).iter().copied(), |
| 129 | + tcx.closure_captures(coroutine_def_id).iter().skip(num_args).copied(), |
| 130 | + |(parent_field_idx, parent_capture), (child_field_idx, child_capture)| { |
157 | 131 | // Store this set of additional projections (fields and derefs). |
158 | 132 | // We need to re-apply them later. |
159 | 133 | let child_precise_captures = |
@@ -184,26 +158,18 @@ impl<'tcx> MirPass<'tcx> for ByMoveBody { |
184 | 158 | ), |
185 | 159 | }; |
186 | 160 |
|
187 | | - field_remapping.insert( |
| 161 | + ( |
188 | 162 | FieldIdx::from_usize(child_field_idx + num_args), |
189 | 163 | ( |
190 | 164 | FieldIdx::from_usize(parent_field_idx + num_args), |
191 | 165 | parent_capture_ty, |
192 | 166 | needs_deref, |
193 | 167 | child_precise_captures, |
194 | 168 | ), |
195 | | - ); |
196 | | - |
197 | | - field_used_at_least_once = true; |
198 | | - } |
199 | | - |
200 | | - // Make sure the field was used at least once. |
201 | | - assert!( |
202 | | - field_used_at_least_once, |
203 | | - "we captured {parent_capture:#?} but it was not used in the child coroutine?" |
204 | | - ); |
205 | | - } |
206 | | - assert_eq!(child_captures.next(), None, "leftover child captures?"); |
| 169 | + ) |
| 170 | + }, |
| 171 | + ) |
| 172 | + .collect(); |
207 | 173 |
|
208 | 174 | if coroutine_kind == ty::ClosureKind::FnOnce { |
209 | 175 | assert_eq!(field_remapping.len(), tcx.closure_captures(parent_def_id).len()); |
@@ -233,23 +199,6 @@ impl<'tcx> MirPass<'tcx> for ByMoveBody { |
233 | 199 | } |
234 | 200 | } |
235 | 201 |
|
236 | | -fn child_prefix_matches_parent_projections( |
237 | | - parent_capture: &ty::CapturedPlace<'_>, |
238 | | - child_capture: &ty::CapturedPlace<'_>, |
239 | | -) -> bool { |
240 | | - let PlaceBase::Upvar(parent_base) = parent_capture.place.base else { |
241 | | - bug!("expected capture to be an upvar"); |
242 | | - }; |
243 | | - let PlaceBase::Upvar(child_base) = child_capture.place.base else { |
244 | | - bug!("expected capture to be an upvar"); |
245 | | - }; |
246 | | - |
247 | | - assert!(child_capture.place.projections.len() >= parent_capture.place.projections.len()); |
248 | | - parent_base.var_path.hir_id == child_base.var_path.hir_id |
249 | | - && std::iter::zip(&child_capture.place.projections, &parent_capture.place.projections) |
250 | | - .all(|(child, parent)| child.kind == parent.kind) |
251 | | -} |
252 | | - |
253 | 202 | struct MakeByMoveBody<'tcx> { |
254 | 203 | tcx: TyCtxt<'tcx>, |
255 | 204 | field_remapping: UnordMap<FieldIdx, (FieldIdx, Ty<'tcx>, bool, &'tcx [Projection<'tcx>])>, |
|
0 commit comments