Skip to content

Commit 97b24f3

Browse files
committed
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.
1 parent 2918584 commit 97b24f3

File tree

3 files changed

+28
-12
lines changed

3 files changed

+28
-12
lines changed

compiler/rustc_typeck/src/check/generator_interior/drop_ranges.rs

+13-9
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,18 @@ impl TrackedValue {
116116
TrackedValue::Variable(hir_id) | TrackedValue::Temporary(hir_id) => *hir_id,
117117
}
118118
}
119+
120+
fn from_place_with_projections_allowed(place_with_id: &PlaceWithHirId<'_>) -> Self {
121+
match place_with_id.place.base {
122+
PlaceBase::Rvalue | PlaceBase::StaticItem => {
123+
TrackedValue::Temporary(place_with_id.hir_id)
124+
}
125+
PlaceBase::Local(hir_id)
126+
| PlaceBase::Upvar(ty::UpvarId { var_path: ty::UpvarPath { hir_id }, .. }) => {
127+
TrackedValue::Variable(hir_id)
128+
}
129+
}
130+
}
119131
}
120132

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

145-
match place_with_id.place.base {
146-
PlaceBase::Rvalue | PlaceBase::StaticItem => {
147-
Ok(TrackedValue::Temporary(place_with_id.hir_id))
148-
}
149-
PlaceBase::Local(hir_id)
150-
| PlaceBase::Upvar(ty::UpvarId { var_path: ty::UpvarPath { hir_id }, .. }) => {
151-
Ok(TrackedValue::Variable(hir_id))
152-
}
153-
}
157+
Ok(TrackedValue::from_place_with_projections_allowed(place_with_id))
154158
}
155159
}
156160

compiler/rustc_typeck/src/check/generator_interior/drop_ranges/record_consumed_borrow.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,9 @@ impl<'tcx> expr_use_visitor::Delegate<'tcx> for ExprUseDelegate<'tcx> {
9696
_diag_expr_id: HirId,
9797
_bk: rustc_middle::ty::BorrowKind,
9898
) {
99-
place_with_id
100-
.try_into()
101-
.map_or(false, |tracked_value| self.places.borrowed.insert(tracked_value));
99+
self.places
100+
.borrowed
101+
.insert(TrackedValue::from_place_with_projections_allowed(place_with_id));
102102
}
103103

104104
fn mutate(
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// edition:2021
2+
// build-pass
3+
// compile-flags: -Zdrop-tracking
4+
5+
fn main() {
6+
let _ = async {
7+
let mut s = (String::new(),);
8+
s.0.push_str("abc");
9+
std::mem::drop(s);
10+
async {}.await;
11+
};
12+
}

0 commit comments

Comments
 (0)