Skip to content

Commit

Permalink
Drop tracking: track borrows of projections
Browse files Browse the repository at this point in the history
Previous efforts to ignore partially consumed values meant we were also
not considering borrows of a projection. This led to cases where we'd
miss borrowed types which MIR expected to be there, leading to ICEs.
  • Loading branch information
eholk committed Feb 8, 2022
1 parent 2918584 commit 97b24f3
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 12 deletions.
22 changes: 13 additions & 9 deletions compiler/rustc_typeck/src/check/generator_interior/drop_ranges.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,18 @@ impl TrackedValue {
TrackedValue::Variable(hir_id) | TrackedValue::Temporary(hir_id) => *hir_id,
}
}

fn from_place_with_projections_allowed(place_with_id: &PlaceWithHirId<'_>) -> Self {
match place_with_id.place.base {
PlaceBase::Rvalue | PlaceBase::StaticItem => {
TrackedValue::Temporary(place_with_id.hir_id)
}
PlaceBase::Local(hir_id)
| PlaceBase::Upvar(ty::UpvarId { var_path: ty::UpvarPath { hir_id }, .. }) => {
TrackedValue::Variable(hir_id)
}
}
}
}

/// Represents a reason why we might not be able to convert a HirId or Place
Expand All @@ -142,15 +154,7 @@ impl TryFrom<&PlaceWithHirId<'_>> for TrackedValue {
return Err(TrackedValueConversionError::PlaceProjectionsNotSupported);
}

match place_with_id.place.base {
PlaceBase::Rvalue | PlaceBase::StaticItem => {
Ok(TrackedValue::Temporary(place_with_id.hir_id))
}
PlaceBase::Local(hir_id)
| PlaceBase::Upvar(ty::UpvarId { var_path: ty::UpvarPath { hir_id }, .. }) => {
Ok(TrackedValue::Variable(hir_id))
}
}
Ok(TrackedValue::from_place_with_projections_allowed(place_with_id))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,9 @@ impl<'tcx> expr_use_visitor::Delegate<'tcx> for ExprUseDelegate<'tcx> {
_diag_expr_id: HirId,
_bk: rustc_middle::ty::BorrowKind,
) {
place_with_id
.try_into()
.map_or(false, |tracked_value| self.places.borrowed.insert(tracked_value));
self.places
.borrowed
.insert(TrackedValue::from_place_with_projections_allowed(place_with_id));
}

fn mutate(
Expand Down
12 changes: 12 additions & 0 deletions src/test/ui/async-await/issue-93648.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// edition:2021
// build-pass
// compile-flags: -Zdrop-tracking

fn main() {
let _ = async {
let mut s = (String::new(),);
s.0.push_str("abc");
std::mem::drop(s);
async {}.await;
};
}

0 comments on commit 97b24f3

Please sign in to comment.