Skip to main content

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        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_const_eval/src/check_consts/post_drop_elaboration.rs:59",
                        "rustc_const_eval::check_consts::post_drop_elaboration",
                        ::tracing::Level::TRACE,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_const_eval/src/check_consts/post_drop_elaboration.rs"),
                        ::tracing_core::__macro_support::Option::Some(59u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_const_eval::check_consts::post_drop_elaboration"),
                        ::tracing_core::field::FieldSet::new(&["message"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::TRACE <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::TRACE <=
                    ::tracing::level_filters::LevelFilter::current() &&
            {
                let interest = __CALLSITE.interest();
                !interest.is_never() &&
                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                        interest)
            };
    if enabled {
        (|value_set: ::tracing::field::ValueSet|
                    {
                        let meta = __CALLSITE.metadata();
                        ::tracing::Event::dispatch(meta, &value_set);
                        ;
                    })({
                #[allow(unused_imports)]
                use ::tracing::field::{debug, display, Value};
                let mut iter = __CALLSITE.metadata().fields().iter();
                __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&format_args!("visit_basic_block_data: bb={0:?} is_cleanup={1:?}",
                                                    bb, block.is_cleanup) as &dyn Value))])
            });
    } else { ; }
};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        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_const_eval/src/check_consts/post_drop_elaboration.rs:70",
                        "rustc_const_eval::check_consts::post_drop_elaboration",
                        ::tracing::Level::TRACE,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_const_eval/src/check_consts/post_drop_elaboration.rs"),
                        ::tracing_core::__macro_support::Option::Some(70u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_const_eval::check_consts::post_drop_elaboration"),
                        ::tracing_core::field::FieldSet::new(&["message"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::TRACE <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::TRACE <=
                    ::tracing::level_filters::LevelFilter::current() &&
            {
                let interest = __CALLSITE.interest();
                !interest.is_never() &&
                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                        interest)
            };
    if enabled {
        (|value_set: ::tracing::field::ValueSet|
                    {
                        let meta = __CALLSITE.metadata();
                        ::tracing::Event::dispatch(meta, &value_set);
                        ;
                    })({
                #[allow(unused_imports)]
                use ::tracing::field::{debug, display, Value};
                let mut iter = __CALLSITE.metadata().fields().iter();
                __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&format_args!("visit_terminator: terminator={0:?} location={1:?}",
                                                    terminator, location) as &dyn Value))])
            });
    } else { ; }
};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}