Skip to content

Commit f014e91

Browse files
committed
Shrink a loop to its looping part and move out the part that runs after the loop
1 parent 626fd4b commit f014e91

File tree

1 file changed

+50
-50
lines changed

1 file changed

+50
-50
lines changed

compiler/rustc_mir_transform/src/coroutine/by_move_body.rs

+50-50
Original file line numberDiff line numberDiff line change
@@ -144,67 +144,67 @@ impl<'tcx> MirPass<'tcx> for ByMoveBody {
144144
.skip(num_args)
145145
.enumerate()
146146
{
147+
let (mut parent_field_idx, mut parent_capture);
147148
loop {
148-
let &(parent_field_idx, parent_capture) =
149-
parent_captures.peek().expect("we ran out of parent captures!");
149+
(parent_field_idx, parent_capture) =
150+
*parent_captures.peek().expect("we ran out of parent captures!");
150151
// A parent matches a child they share the same prefix of projections.
151152
// The child may have more, if it is capturing sub-fields out of
152153
// something that is captured by-move in the parent closure.
153-
if !child_prefix_matches_parent_projections(parent_capture, child_capture) {
154-
// Make sure the field was used at least once.
155-
assert!(
156-
field_used_at_least_once,
157-
"we captured {parent_capture:#?} but it was not used in the child coroutine?"
158-
);
159-
field_used_at_least_once = false;
160-
// Skip this field.
161-
let _ = parent_captures.next().unwrap();
162-
continue;
154+
if child_prefix_matches_parent_projections(parent_capture, child_capture) {
155+
break;
163156
}
157+
// Make sure the field was used at least once.
158+
assert!(
159+
field_used_at_least_once,
160+
"we captured {parent_capture:#?} but it was not used in the child coroutine?"
161+
);
162+
field_used_at_least_once = false;
163+
// Skip this field.
164+
let _ = parent_captures.next().unwrap();
165+
}
164166

165-
// Store this set of additional projections (fields and derefs).
166-
// We need to re-apply them later.
167-
let child_precise_captures =
168-
&child_capture.place.projections[parent_capture.place.projections.len()..];
167+
// Store this set of additional projections (fields and derefs).
168+
// We need to re-apply them later.
169+
let child_precise_captures =
170+
&child_capture.place.projections[parent_capture.place.projections.len()..];
169171

170-
// If the parent captures by-move, and the child captures by-ref, then we
171-
// need to peel an additional `deref` off of the body of the child.
172-
let needs_deref = child_capture.is_by_ref() && !parent_capture.is_by_ref();
173-
if needs_deref {
174-
assert_ne!(
175-
coroutine_kind,
176-
ty::ClosureKind::FnOnce,
177-
"`FnOnce` coroutine-closures return coroutines that capture from \
172+
// If the parent captures by-move, and the child captures by-ref, then we
173+
// need to peel an additional `deref` off of the body of the child.
174+
let needs_deref = child_capture.is_by_ref() && !parent_capture.is_by_ref();
175+
if needs_deref {
176+
assert_ne!(
177+
coroutine_kind,
178+
ty::ClosureKind::FnOnce,
179+
"`FnOnce` coroutine-closures return coroutines that capture from \
178180
their body; it will always result in a borrowck error!"
179-
);
180-
}
181+
);
182+
}
181183

182-
// Finally, store the type of the parent's captured place. We need
183-
// this when building the field projection in the MIR body later on.
184-
let mut parent_capture_ty = parent_capture.place.ty();
185-
parent_capture_ty = match parent_capture.info.capture_kind {
186-
ty::UpvarCapture::ByValue => parent_capture_ty,
187-
ty::UpvarCapture::ByRef(kind) => Ty::new_ref(
188-
tcx,
189-
tcx.lifetimes.re_erased,
190-
parent_capture_ty,
191-
kind.to_mutbl_lossy(),
192-
),
193-
};
184+
// Finally, store the type of the parent's captured place. We need
185+
// this when building the field projection in the MIR body later on.
186+
let mut parent_capture_ty = parent_capture.place.ty();
187+
parent_capture_ty = match parent_capture.info.capture_kind {
188+
ty::UpvarCapture::ByValue => parent_capture_ty,
189+
ty::UpvarCapture::ByRef(kind) => Ty::new_ref(
190+
tcx,
191+
tcx.lifetimes.re_erased,
192+
parent_capture_ty,
193+
kind.to_mutbl_lossy(),
194+
),
195+
};
194196

195-
field_remapping.insert(
196-
FieldIdx::from_usize(child_field_idx + num_args),
197-
(
198-
FieldIdx::from_usize(parent_field_idx + num_args),
199-
parent_capture_ty,
200-
needs_deref,
201-
child_precise_captures,
202-
),
203-
);
197+
field_remapping.insert(
198+
FieldIdx::from_usize(child_field_idx + num_args),
199+
(
200+
FieldIdx::from_usize(parent_field_idx + num_args),
201+
parent_capture_ty,
202+
needs_deref,
203+
child_precise_captures,
204+
),
205+
);
204206

205-
field_used_at_least_once = true;
206-
break;
207-
}
207+
field_used_at_least_once = true;
208208
}
209209

210210
// Pop the last parent capture

0 commit comments

Comments
 (0)