Skip to content

Commit a07eed9

Browse files
authored
Rollup merge of rust-lang#93751 - eholk:issue-93648-drop-tracking-projection, r=tmiasko
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. This PR also includes the `-Zdrop-tracking` flag from rust-lang#93313. If that PR lands first, I'll rebase to drop the commit from this one. Fixes rust-lang#93648
2 parents 68fa9b1 + 97b24f3 commit a07eed9

File tree

6 files changed

+33
-19
lines changed

6 files changed

+33
-19
lines changed

compiler/rustc_interface/src/tests.rs

+1
Original file line numberDiff line numberDiff line change
@@ -730,6 +730,7 @@ fn test_debugging_options_tracking_hash() {
730730
tracked!(debug_info_for_profiling, true);
731731
tracked!(debug_macros, true);
732732
tracked!(dep_info_omit_d_target, true);
733+
tracked!(drop_tracking, true);
733734
tracked!(dual_proc_macros, true);
734735
tracked!(fewer_names, Some(true));
735736
tracked!(force_unstable_if_unmarked, true);

compiler/rustc_session/src/options.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1173,6 +1173,8 @@ options! {
11731173
dont_buffer_diagnostics: bool = (false, parse_bool, [UNTRACKED],
11741174
"emit diagnostics rather than buffering (breaks NLL error downgrading, sorting) \
11751175
(default: no)"),
1176+
drop_tracking: bool = (false, parse_bool, [TRACKED],
1177+
"enables drop tracking in generators (default: no)"),
11761178
dual_proc_macros: bool = (false, parse_bool, [TRACKED],
11771179
"load proc macros for both target and host, but only link to the target (default: no)"),
11781180
dump_dep_graph: bool = (false, parse_bool, [UNTRACKED],

compiler/rustc_typeck/src/check/generator_interior.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,6 @@ use tracing::debug;
2222

2323
mod drop_ranges;
2424

25-
// FIXME(eholk): This flag is here to give a quick way to disable drop tracking in case we find
26-
// unexpected breakages while it's still new. It should be removed before too long. For example,
27-
// see #93161.
28-
const ENABLE_DROP_TRACKING: bool = false;
29-
3025
struct InteriorVisitor<'a, 'tcx> {
3126
fcx: &'a FnCtxt<'a, 'tcx>,
3227
types: FxIndexSet<ty::GeneratorInteriorTypeCause<'tcx>>,
@@ -82,7 +77,7 @@ impl<'a, 'tcx> InteriorVisitor<'a, 'tcx> {
8277
yield_data.expr_and_pat_count, self.expr_count, source_span
8378
);
8479

85-
if ENABLE_DROP_TRACKING
80+
if self.fcx.sess().opts.debugging_opts.drop_tracking
8681
&& self
8782
.drop_ranges
8883
.is_dropped_at(hir_id, yield_data.expr_and_pat_count)

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

+14-10
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ pub fn compute_drop_ranges<'a, 'tcx>(
3737
def_id: DefId,
3838
body: &'tcx Body<'tcx>,
3939
) -> DropRanges {
40-
if super::ENABLE_DROP_TRACKING {
40+
if fcx.sess().opts.debugging_opts.drop_tracking {
4141
let consumed_borrowed_places = find_consumed_and_borrowed(fcx, def_id, body);
4242

4343
let num_exprs = fcx.tcx.region_scope_tree(def_id).body_expr_count(body.id()).unwrap_or(0);
@@ -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)