1 use rustc_middle::mir::visit::Visitor;
2 use rustc_middle::mir::{self, BasicBlock, Location};
3 use rustc_middle::ty::TyCtxt;
4 use rustc_span::{symbol::sym, Span};
6 use super::check::Qualifs;
7 use super::ops::{self, NonConstOp};
8 use super::qualifs::{NeedsNonConstDrop, Qualif};
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>(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 if tcx.has_attr(def_id.to_def_id(), sym::rustc_do_not_const_check) {
37 let ccx = ConstCx { body, tcx, const_kind, param_env: tcx.param_env(def_id) };
38 if !checking_enabled(&ccx) {
42 let mut visitor = CheckLiveDrops { ccx: &ccx, qualifs: Qualifs::default() };
44 visitor.visit_body(body);
47 struct CheckLiveDrops<'mir, 'tcx> {
48 ccx: &'mir ConstCx<'mir, 'tcx>,
49 qualifs: Qualifs<'mir, 'tcx>,
52 // So we can access `body` and `tcx`.
53 impl<'mir, 'tcx> std::ops::Deref for CheckLiveDrops<'mir, 'tcx> {
54 type Target = ConstCx<'mir, 'tcx>;
56 fn deref(&self) -> &Self::Target {
61 impl CheckLiveDrops<'_, '_> {
62 fn check_live_drop(&self, span: Span) {
63 ops::LiveDrop { dropped_at: None }.build_error(self.ccx, span).emit();
67 impl<'tcx> Visitor<'tcx> for CheckLiveDrops<'_, 'tcx> {
68 fn visit_basic_block_data(&mut self, bb: BasicBlock, block: &mir::BasicBlockData<'tcx>) {
69 trace!("visit_basic_block_data: bb={:?} is_cleanup={:?}", bb, block.is_cleanup);
71 // Ignore drop terminators in cleanup blocks.
76 self.super_basic_block_data(bb, block);
79 fn visit_terminator(&mut self, terminator: &mir::Terminator<'tcx>, location: Location) {
80 trace!("visit_terminator: terminator={:?} location={:?}", terminator, location);
82 match &terminator.kind {
83 mir::TerminatorKind::Drop { place: dropped_place, .. }
84 | mir::TerminatorKind::DropAndReplace { place: dropped_place, .. } => {
85 let dropped_ty = dropped_place.ty(self.body, self.tcx).ty;
86 if !NeedsNonConstDrop::in_any_value_of_ty(self.ccx, dropped_ty) {
87 // Instead of throwing a bug, we just return here. This is because we have to
88 // run custom `const Drop` impls.
92 if dropped_place.is_indirect() {
93 self.check_live_drop(terminator.source_info.span);
97 // Drop elaboration is not precise enough to accept code like
98 // `src/test/ui/consts/control-flow/drop-pass.rs`; e.g., when an `Option<Vec<T>>` is
99 // initialized with `None` and never changed, it still emits drop glue.
100 // Hence we additionally check the qualifs here to allow more code to pass.
101 if self.qualifs.needs_non_const_drop(self.ccx, dropped_place.local, location) {
102 // Use the span where the dropped local was declared for the error.
103 let span = self.body.local_decls[dropped_place.local].source_info.span;
104 self.check_live_drop(span);
108 mir::TerminatorKind::Abort
109 | mir::TerminatorKind::Call { .. }
110 | mir::TerminatorKind::Assert { .. }
111 | mir::TerminatorKind::FalseEdge { .. }
112 | mir::TerminatorKind::FalseUnwind { .. }
113 | mir::TerminatorKind::GeneratorDrop
114 | mir::TerminatorKind::Goto { .. }
115 | mir::TerminatorKind::InlineAsm { .. }
116 | mir::TerminatorKind::Resume
117 | mir::TerminatorKind::Return
118 | mir::TerminatorKind::SwitchInt { .. }
119 | mir::TerminatorKind::Unreachable
120 | mir::TerminatorKind::Yield { .. } => {}