1
Fork 0

Auto merge of #88467 - sexxi-goose:issue-88431, r=nikomatsakis

2229: Drop any deref in move closure

Fixes: #88431

r? `@nikomatsakis`
This commit is contained in:
bors 2021-08-31 06:01:52 +00:00
commit d4f263a558
6 changed files with 77 additions and 52 deletions

View file

@ -1901,31 +1901,20 @@ fn restrict_capture_precision<'tcx>(
return (place, curr_mode);
}
/// Take ownership if data being accessed is owned by the variable used to access it
/// (or if closure attempts to move data that it doesnt own).
/// Note: When taking ownership, only capture data found on the stack.
/// Truncate deref of any reference.
fn adjust_for_move_closure<'tcx>(
mut place: Place<'tcx>,
mut kind: ty::UpvarCapture<'tcx>,
) -> (Place<'tcx>, ty::UpvarCapture<'tcx>) {
let contains_deref_of_ref = place.deref_tys().any(|ty| ty.is_ref());
let first_deref = place.projections.iter().position(|proj| proj.kind == ProjectionKind::Deref);
match kind {
ty::UpvarCapture::ByRef(..) if contains_deref_of_ref => (place, kind),
// If there's any Deref and the data needs to be moved into the closure body,
// or it's a Deref of a Box, truncate the path to the first deref
_ => {
if let Some(idx) = first_deref {
truncate_place_to_len_and_update_capture_kind(&mut place, &mut kind, idx);
}
// AMAN: I think we don't need the span inside the ByValue anymore
// we have more detailed span in CaptureInfo
(place, ty::UpvarCapture::ByValue(None))
}
if let Some(idx) = first_deref {
truncate_place_to_len_and_update_capture_kind(&mut place, &mut kind, idx);
}
// AMAN: I think we don't need the span inside the ByValue anymore
// we have more detailed span in CaptureInfo
(place, ty::UpvarCapture::ByValue(None))
}
/// Adjust closure capture just that if taking ownership of data, only move data