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
11/// Returns `true` if we should use the more precise live drop checker that runs after drop
12/// elaboration.
13pub fn checking_enabled(ccx: &ConstCx<'_, '_>) -> bool {
14    // Const-stable functions must always use the stable live drop checker...
15    if ccx.enforce_recursive_const_stability() {
16        // ...except if they have the feature flag set via `rustc_allow_const_fn_unstable`.
17        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
27/// Look for live drops in a const context.
28///
29/// This is separate from the rest of the const checking logic because it must run after drop
30/// elaboration.
31pub 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    // I know it's not great to be creating a new const checker, but I'd
46    // rather use it so we can deduplicate the error emitting logic that
47    // it contains.
48    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        // Ignore drop terminators in cleanup blocks.
62        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}