mirror of
https://github.com/rust-lang/rust.git
synced 2026-05-16 04:55:22 +03:00
Elide storage markers when elaborating deref projections
This commit is contained in:
@@ -68,7 +68,7 @@ pub fn terminator_loc(&self, body: &Body<'tcx>, bb: BasicBlock) -> Location {
|
||||
Location { block: bb, statement_index: offset }
|
||||
}
|
||||
|
||||
pub fn new_local_with_info(
|
||||
pub fn new_internal_with_info(
|
||||
&mut self,
|
||||
ty: Ty<'tcx>,
|
||||
span: Span,
|
||||
@@ -76,14 +76,17 @@ pub fn new_local_with_info(
|
||||
) -> Local {
|
||||
let index = self.next_local;
|
||||
self.next_local += 1;
|
||||
let mut new_decl = LocalDecl::new(ty, span);
|
||||
let mut new_decl = LocalDecl::new(ty, span).internal();
|
||||
new_decl.local_info = local_info;
|
||||
self.new_locals.push(new_decl);
|
||||
Local::new(index as usize)
|
||||
}
|
||||
|
||||
pub fn new_temp(&mut self, ty: Ty<'tcx>, span: Span) -> Local {
|
||||
self.new_local_with_info(ty, span, None)
|
||||
let index = self.next_local;
|
||||
self.next_local += 1;
|
||||
self.new_locals.push(LocalDecl::new(ty, span));
|
||||
Local::new(index as usize)
|
||||
}
|
||||
|
||||
pub fn new_internal(&mut self, ty: Ty<'tcx>, span: Span) -> Local {
|
||||
|
||||
@@ -28,8 +28,6 @@ fn visit_place(&mut self, place: &mut Place<'tcx>, cntxt: PlaceContext, loc: Loc
|
||||
let mut last_len = 0;
|
||||
let mut last_deref_idx = 0;
|
||||
|
||||
let mut prev_temp: Option<Local> = None;
|
||||
|
||||
for (idx, elem) in place.projection[0..].iter().enumerate() {
|
||||
if *elem == ProjectionElem::Deref {
|
||||
last_deref_idx = idx;
|
||||
@@ -39,14 +37,12 @@ fn visit_place(&mut self, place: &mut Place<'tcx>, cntxt: PlaceContext, loc: Loc
|
||||
for (idx, (p_ref, p_elem)) in place.iter_projections().enumerate() {
|
||||
if !p_ref.projection.is_empty() && p_elem == ProjectionElem::Deref {
|
||||
let ty = p_ref.ty(&self.local_decls, self.tcx).ty;
|
||||
let temp = self.patcher.new_local_with_info(
|
||||
let temp = self.patcher.new_internal_with_info(
|
||||
ty,
|
||||
self.local_decls[p_ref.local].source_info.span,
|
||||
Some(Box::new(LocalInfo::DerefTemp)),
|
||||
);
|
||||
|
||||
self.patcher.add_statement(loc, StatementKind::StorageLive(temp));
|
||||
|
||||
// We are adding current p_ref's projections to our
|
||||
// temp value, excluding projections we already covered.
|
||||
let deref_place = Place::from(place_local)
|
||||
@@ -66,22 +62,8 @@ fn visit_place(&mut self, place: &mut Place<'tcx>, cntxt: PlaceContext, loc: Loc
|
||||
Place::from(temp).project_deeper(&place.projection[idx..], self.tcx);
|
||||
*place = temp_place;
|
||||
}
|
||||
|
||||
// We are destroying the previous temp since it's no longer used.
|
||||
if let Some(prev_temp) = prev_temp {
|
||||
self.patcher.add_statement(loc, StatementKind::StorageDead(prev_temp));
|
||||
}
|
||||
|
||||
prev_temp = Some(temp);
|
||||
}
|
||||
}
|
||||
|
||||
// Since we won't be able to reach final temp, we destroy it outside the loop.
|
||||
if let Some(prev_temp) = prev_temp {
|
||||
let last_loc =
|
||||
Location { block: loc.block, statement_index: loc.statement_index + 1 };
|
||||
self.patcher.add_statement(last_loc, StatementKind::StorageDead(prev_temp));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user