rustc_const_eval/check_consts/
post_drop_elaboration.rs
1use rustc_middle::mir::visit::Visitor;
2use rustc_middle::mir::{self, BasicBlock, Location};
3use rustc_middle::ty::TyCtxt;
4use rustc_span::sym;
5use tracing::trace;
6
7use super::ConstCx;
8use crate::check_consts::check::Checker;
9use crate::check_consts::rustc_allow_const_fn_unstable;
10
11pub fn checking_enabled(ccx: &ConstCx<'_, '_>) -> bool {
14 if ccx.enforce_recursive_const_stability() {
16 return rustc_allow_const_fn_unstable(
18 ccx.tcx,
19 ccx.body.source.def_id().expect_local(),
20 sym::const_precise_live_drops,
21 );
22 }
23
24 ccx.tcx.features().const_precise_live_drops()
25}
26
27pub fn check_live_drops<'tcx>(tcx: TyCtxt<'tcx>, body: &mir::Body<'tcx>) {
32 let ccx = ConstCx::new(tcx, body);
33 if ccx.const_kind.is_none() {
34 return;
35 }
36
37 if tcx.has_attr(body.source.def_id(), sym::rustc_do_not_const_check) {
38 return;
39 }
40
41 if !checking_enabled(&ccx) {
42 return;
43 }
44
45 let mut visitor = CheckLiveDrops { checker: Checker::new(&ccx) };
49
50 visitor.visit_body(body);
51}
52
53struct CheckLiveDrops<'mir, 'tcx> {
54 checker: Checker<'mir, 'tcx>,
55}
56
57impl<'tcx> Visitor<'tcx> for CheckLiveDrops<'_, 'tcx> {
58 fn visit_basic_block_data(&mut self, bb: BasicBlock, block: &mir::BasicBlockData<'tcx>) {
59 trace!("visit_basic_block_data: bb={:?} is_cleanup={:?}", bb, block.is_cleanup);
60
61 if block.is_cleanup {
63 return;
64 }
65
66 self.super_basic_block_data(bb, block);
67 }
68
69 fn visit_terminator(&mut self, terminator: &mir::Terminator<'tcx>, location: Location) {
70 trace!("visit_terminator: terminator={:?} location={:?}", terminator, location);
71
72 match &terminator.kind {
73 mir::TerminatorKind::Drop { place: dropped_place, .. } => {
74 self.checker.check_drop_terminator(
75 *dropped_place,
76 location,
77 terminator.source_info.span,
78 );
79 }
80
81 mir::TerminatorKind::UnwindTerminate(_)
82 | mir::TerminatorKind::Call { .. }
83 | mir::TerminatorKind::TailCall { .. }
84 | mir::TerminatorKind::Assert { .. }
85 | mir::TerminatorKind::FalseEdge { .. }
86 | mir::TerminatorKind::FalseUnwind { .. }
87 | mir::TerminatorKind::CoroutineDrop
88 | mir::TerminatorKind::Goto { .. }
89 | mir::TerminatorKind::InlineAsm { .. }
90 | mir::TerminatorKind::UnwindResume
91 | mir::TerminatorKind::Return
92 | mir::TerminatorKind::SwitchInt { .. }
93 | mir::TerminatorKind::Unreachable
94 | mir::TerminatorKind::Yield { .. } => {}
95 }
96 }
97}