From 97b24f3236a59c920b8978604739fcd274c678de Mon Sep 17 00:00:00 2001 From: Eric Holk Date: Mon, 7 Feb 2022 16:01:27 -0800 Subject: [PATCH] Drop tracking: track borrows of projections 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. --- .../check/generator_interior/drop_ranges.rs | 22 +++++++++++-------- .../drop_ranges/record_consumed_borrow.rs | 6 ++--- src/test/ui/async-await/issue-93648.rs | 12 ++++++++++ 3 files changed, 28 insertions(+), 12 deletions(-) create mode 100644 src/test/ui/async-await/issue-93648.rs diff --git a/compiler/rustc_typeck/src/check/generator_interior/drop_ranges.rs b/compiler/rustc_typeck/src/check/generator_interior/drop_ranges.rs index 1f7db877bea..c4e7d6a199e 100644 --- a/compiler/rustc_typeck/src/check/generator_interior/drop_ranges.rs +++ b/compiler/rustc_typeck/src/check/generator_interior/drop_ranges.rs @@ -116,6 +116,18 @@ fn hir_id(&self) -> HirId { 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 @@ -142,15 +154,7 @@ fn try_from(place_with_id: &PlaceWithHirId<'_>) -> Result { 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)) } } diff --git a/compiler/rustc_typeck/src/check/generator_interior/drop_ranges/record_consumed_borrow.rs b/compiler/rustc_typeck/src/check/generator_interior/drop_ranges/record_consumed_borrow.rs index 059a135a6fb..9a308586aff 100644 --- a/compiler/rustc_typeck/src/check/generator_interior/drop_ranges/record_consumed_borrow.rs +++ b/compiler/rustc_typeck/src/check/generator_interior/drop_ranges/record_consumed_borrow.rs @@ -96,9 +96,9 @@ fn borrow( _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( diff --git a/src/test/ui/async-await/issue-93648.rs b/src/test/ui/async-await/issue-93648.rs new file mode 100644 index 00000000000..4ce3ac1e874 --- /dev/null +++ b/src/test/ui/async-await/issue-93648.rs @@ -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; + }; +}