Skip to content

Commit a57f57b

Browse files
committed
sus™
1 parent 7fae41b commit a57f57b

File tree

3 files changed

+47
-4
lines changed

3 files changed

+47
-4
lines changed

compiler/rustc_mir_build/src/build/block.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,9 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
362362
// We only want to assign an implicit `()` as the return value of the block if the
363363
// block does not diverge. (Otherwise, we may try to assign a unit to a `!`-type.)
364364
this.cfg.push_assign_unit(block, source_info, destination, this.tcx);
365-
} else if let Some(destination_local) = destination.as_local()
365+
}
366+
367+
if let Some(destination_local) = destination.as_local()
366368
&& let Some(scope) = scope
367369
{
368370
this.schedule_drop(span, scope, destination_local, DropKind::Value);

compiler/rustc_mir_build/src/build/expr/into.rs

+7
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
136136
// Generate the implicit `else {}` by assigning unit.
137137
let correct_si = this.source_info(expr_span.shrink_to_hi());
138138
this.cfg.push_assign_unit(else_blk, correct_si, destination, this.tcx);
139+
schedule_drop(this);
139140
}
140141

141142
// The `then` and `else` arms have been lowered into their respective
@@ -339,6 +340,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
339340
};
340341
let borrow = Rvalue::Ref(this.tcx.lifetimes.re_erased, borrow_kind, arg_place);
341342
this.cfg.push_assign(block, source_info, destination, borrow);
343+
schedule_drop(this);
342344
block.unit()
343345
}
344346
ExprKind::AddressOf { mutability, arg } => {
@@ -653,6 +655,11 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
653655
}
654656
};
655657

658+
if let Some(drop_scope) = scope {
659+
let local = destination.as_local().expect("cannot unschedule drop of non-Local place");
660+
this.may_unschedule_drop(drop_scope, local, expr);
661+
}
662+
656663
if !expr_is_block_or_scope {
657664
let popped = this.block_context.pop();
658665
assert!(popped.is_some());

compiler/rustc_mir_build/src/build/scope.rs

+37-3
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,6 @@ use rustc_index::{IndexSlice, IndexVec};
9090
use rustc_middle::middle::region;
9191
use rustc_middle::mir::*;
9292
use rustc_middle::thir::{ExprId, LintLevel};
93-
use rustc_middle::ty::TypeVisitableExt;
9493
use rustc_middle::{bug, span_bug};
9594
use rustc_session::lint::Level;
9695
use rustc_span::{Span, DUMMY_SP};
@@ -1143,8 +1142,6 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
11431142
{
11441143
return;
11451144
}
1146-
// Opaque type may not have been scheduled if its underlying
1147-
// type does not need drop.
11481145
_ => bug!(
11491146
"found wrong drop, expected value drop of {:?} in scope {:?}, found {:?}, all scopes {:?}",
11501147
local,
@@ -1159,6 +1156,43 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
11591156
bug!("region scope {:?} not in scope to unschedule drop of {:?}", region_scope, local);
11601157
}
11611158

1159+
pub(crate) fn may_unschedule_drop(
1160+
&mut self,
1161+
region_scope: region::Scope,
1162+
local: Local,
1163+
x: &dyn std::fmt::Debug,
1164+
) {
1165+
if !self.local_decls[local].ty.needs_drop(self.tcx, self.param_env) {
1166+
return;
1167+
}
1168+
for scope in self.scopes.scopes.iter().rev() {
1169+
if scope.region_scope == region_scope {
1170+
let drop = scope.drops.last();
1171+
1172+
match drop {
1173+
Some(&DropData { local: removed_local, kind: DropKind::Value, .. })
1174+
if removed_local == local =>
1175+
{
1176+
return;
1177+
}
1178+
_ => tracing::warn!(
1179+
"expected drop to unschedule {x:?}, expected value drop of {:?} in scope {:?}, found {:?}, all scopes {:?}",
1180+
local,
1181+
region_scope,
1182+
drop,
1183+
self.scopes.scopes,
1184+
),
1185+
}
1186+
}
1187+
}
1188+
1189+
tracing::warn!(
1190+
"region scope {:?} to unschedule {x:?} not in scope to unschedule drop of {:?}",
1191+
region_scope,
1192+
local
1193+
);
1194+
}
1195+
11621196
/// Indicates that the "local operands" stored in `local` are
11631197
/// *moved* at some point during execution (see `local_scope` for
11641198
/// more information about what a "local operand" is -- in short,

0 commit comments

Comments
 (0)