Skip to content

Commit

Permalink
sus™
Browse files Browse the repository at this point in the history
  • Loading branch information
lcnr committed Aug 14, 2024
1 parent 7fae41b commit a57f57b
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 4 deletions.
4 changes: 3 additions & 1 deletion compiler/rustc_mir_build/src/build/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,9 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
// We only want to assign an implicit `()` as the return value of the block if the
// block does not diverge. (Otherwise, we may try to assign a unit to a `!`-type.)
this.cfg.push_assign_unit(block, source_info, destination, this.tcx);
} else if let Some(destination_local) = destination.as_local()
}

if let Some(destination_local) = destination.as_local()
&& let Some(scope) = scope
{
this.schedule_drop(span, scope, destination_local, DropKind::Value);
Expand Down
7 changes: 7 additions & 0 deletions compiler/rustc_mir_build/src/build/expr/into.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
// Generate the implicit `else {}` by assigning unit.
let correct_si = this.source_info(expr_span.shrink_to_hi());
this.cfg.push_assign_unit(else_blk, correct_si, destination, this.tcx);
schedule_drop(this);
}

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

if let Some(drop_scope) = scope {
let local = destination.as_local().expect("cannot unschedule drop of non-Local place");
this.may_unschedule_drop(drop_scope, local, expr);
}

if !expr_is_block_or_scope {
let popped = this.block_context.pop();
assert!(popped.is_some());
Expand Down
40 changes: 37 additions & 3 deletions compiler/rustc_mir_build/src/build/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ use rustc_index::{IndexSlice, IndexVec};
use rustc_middle::middle::region;
use rustc_middle::mir::*;
use rustc_middle::thir::{ExprId, LintLevel};
use rustc_middle::ty::TypeVisitableExt;
use rustc_middle::{bug, span_bug};
use rustc_session::lint::Level;
use rustc_span::{Span, DUMMY_SP};
Expand Down Expand Up @@ -1143,8 +1142,6 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
{
return;
}
// Opaque type may not have been scheduled if its underlying
// type does not need drop.
_ => bug!(
"found wrong drop, expected value drop of {:?} in scope {:?}, found {:?}, all scopes {:?}",
local,
Expand All @@ -1159,6 +1156,43 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
bug!("region scope {:?} not in scope to unschedule drop of {:?}", region_scope, local);
}

pub(crate) fn may_unschedule_drop(
&mut self,
region_scope: region::Scope,
local: Local,
x: &dyn std::fmt::Debug,
) {
if !self.local_decls[local].ty.needs_drop(self.tcx, self.param_env) {
return;
}
for scope in self.scopes.scopes.iter().rev() {
if scope.region_scope == region_scope {
let drop = scope.drops.last();

match drop {
Some(&DropData { local: removed_local, kind: DropKind::Value, .. })
if removed_local == local =>
{
return;
}
_ => tracing::warn!(
"expected drop to unschedule {x:?}, expected value drop of {:?} in scope {:?}, found {:?}, all scopes {:?}",
local,
region_scope,
drop,
self.scopes.scopes,
),
}
}
}

tracing::warn!(
"region scope {:?} to unschedule {x:?} not in scope to unschedule drop of {:?}",
region_scope,
local
);
}

/// Indicates that the "local operands" stored in `local` are
/// *moved* at some point during execution (see `local_scope` for
/// more information about what a "local operand" is -- in short,
Expand Down

0 comments on commit a57f57b

Please sign in to comment.