1 use rustc_middle::mir::visit::Visitor;
2 use rustc_middle::mir::{self, BasicBlock, Location};
3 use rustc_middle::ty::TyCtxt;
6 use super::ops::{self, NonConstOp};
7 use super::qualifs::{NeedsDrop, Qualif};
8 use super::validation::Qualifs;
11 /// Returns `true` if we should use the more precise live drop checker that runs after drop
13 pub fn checking_enabled(ccx: &ConstCx<'_, '_>) -> bool {
14 // Const-stable functions must always use the stable live drop checker.
15 if ccx.is_const_stable_const_fn() {
19 ccx.tcx.features().const_precise_live_drops
22 /// Look for live drops in a const context.
24 /// This is separate from the rest of the const checking logic because it must run after drop
26 pub fn check_live_drops(tcx: TyCtxt<'tcx>, body: &mir::Body<'tcx>) {
27 let def_id = body.source.def_id().expect_local();
28 let const_kind = tcx.hir().body_const_context(def_id);
29 if const_kind.is_none() {
33 let ccx = ConstCx { body, tcx, const_kind, param_env: tcx.param_env(def_id) };
34 if !checking_enabled(&ccx) {
38 let mut visitor = CheckLiveDrops { ccx: &ccx, qualifs: Qualifs::default() };
40 visitor.visit_body(body);
43 struct CheckLiveDrops<'mir, 'tcx> {
44 ccx: &'mir ConstCx<'mir, 'tcx>,
45 qualifs: Qualifs<'mir, 'tcx>,
48 // So we can access `body` and `tcx`.
49 impl std::ops::Deref for CheckLiveDrops<'mir, 'tcx> {
50 type Target = ConstCx<'mir, 'tcx>;
52 fn deref(&self) -> &Self::Target {
57 impl CheckLiveDrops<'mir, 'tcx> {
58 fn check_live_drop(&self, span: Span) {
59 ops::LiveDrop { dropped_at: None }.build_error(self.ccx, span).emit();
63 impl Visitor<'tcx> for CheckLiveDrops<'mir, 'tcx> {
64 fn visit_basic_block_data(&mut self, bb: BasicBlock, block: &mir::BasicBlockData<'tcx>) {
65 trace!("visit_basic_block_data: bb={:?} is_cleanup={:?}", bb, block.is_cleanup);
67 // Ignore drop terminators in cleanup blocks.
72 self.super_basic_block_data(bb, block);
75 fn visit_terminator(&mut self, terminator: &mir::Terminator<'tcx>, location: Location) {
76 trace!("visit_terminator: terminator={:?} location={:?}", terminator, location);
78 match &terminator.kind {
79 mir::TerminatorKind::Drop { place: dropped_place, .. } => {
80 let dropped_ty = dropped_place.ty(self.body, self.tcx).ty;
81 if !NeedsDrop::in_any_value_of_ty(self.ccx, dropped_ty) {
85 if dropped_place.is_indirect() {
86 self.check_live_drop(terminator.source_info.span);
90 if self.qualifs.needs_drop(self.ccx, dropped_place.local, location) {
91 // Use the span where the dropped local was declared for the error.
92 let span = self.body.local_decls[dropped_place.local].source_info.span;
93 self.check_live_drop(span);
97 mir::TerminatorKind::DropAndReplace { .. } => span_bug!(
98 terminator.source_info.span,
99 "`DropAndReplace` should be removed by drop elaboration",
102 mir::TerminatorKind::Abort
103 | mir::TerminatorKind::Call { .. }
104 | mir::TerminatorKind::Assert { .. }
105 | mir::TerminatorKind::FalseEdge { .. }
106 | mir::TerminatorKind::FalseUnwind { .. }
107 | mir::TerminatorKind::GeneratorDrop
108 | mir::TerminatorKind::Goto { .. }
109 | mir::TerminatorKind::InlineAsm { .. }
110 | mir::TerminatorKind::Resume
111 | mir::TerminatorKind::Return
112 | mir::TerminatorKind::SwitchInt { .. }
113 | mir::TerminatorKind::Unreachable
114 | mir::TerminatorKind::Yield { .. } => {}