Skip to main content

rustc_const_eval/check_consts/
post_drop_elaboration.rs

1use rustc_hir::find_attr;
2use rustc_middle::mir::visit::Visitor;
3use rustc_middle::mir::{self, BasicBlock, Location};
4use rustc_middle::ty::TyCtxt;
5use rustc_span::sym;
6use tracing::trace;
7
8use super::ConstCx;
9use crate::check_consts::check::Checker;
10use crate::check_consts::rustc_allow_const_fn_unstable;
11
12/// Returns `true` if we should use the more precise live drop checker that runs after drop
13/// elaboration.
14pub fn checking_enabled(ccx: &ConstCx<'_, '_>) -> bool {
15    // Const-stable functions must always use the stable live drop checker...
16    if ccx.enforce_recursive_const_stability() {
17        // ...except if they have the feature flag set via `rustc_allow_const_fn_unstable`.
18        return rustc_allow_const_fn_unstable(
19            ccx.tcx,
20            ccx.body.source.def_id().expect_local(),
21            sym::const_precise_live_drops,
22        );
23    }
24
25    ccx.tcx.features().const_precise_live_drops()
26}
27
28/// Look for live drops in a const context.
29///
30/// This is separate from the rest of the const checking logic because it must run after drop
31/// elaboration.
32pub fn check_live_drops<'tcx>(tcx: TyCtxt<'tcx>, body: &mir::Body<'tcx>) {
33    let ccx = ConstCx::new(tcx, body);
34    if ccx.const_kind.is_none() {
35        return;
36    }
37
38    if {

        #[allow(deprecated)]
        {
            {
                'done:
                    {
                    for i in tcx.get_all_attrs(body.source.def_id()) {
                        #[allow(unused_imports)]
                        use rustc_hir::attrs::AttributeKind::*;
                        let i: &rustc_hir::Attribute = i;
                        match i {
                            rustc_hir::Attribute::Parsed(RustcDoNotConstCheck) => {
                                break 'done Some(());
                            }
                            rustc_hir::Attribute::Unparsed(..) =>
                                {}
                                #[deny(unreachable_patterns)]
                                _ => {}
                        }
                    }
                    None
                }
            }
        }
    }.is_some()find_attr!(tcx, body.source.def_id(), RustcDoNotConstCheck) {
39        return;
40    }
41
42    if !checking_enabled(&ccx) {
43        return;
44    }
45
46    // I know it's not great to be creating a new const checker, but I'd
47    // rather use it so we can deduplicate the error emitting logic that
48    // it contains.
49    let mut visitor = CheckLiveDrops { checker: Checker::new(&ccx) };
50
51    visitor.visit_body(body);
52}
53
54struct CheckLiveDrops<'mir, 'tcx> {
55    checker: Checker<'mir, 'tcx>,
56}
57
58impl<'tcx> Visitor<'tcx> for CheckLiveDrops<'_, 'tcx> {
59    fn visit_basic_block_data(&mut self, bb: BasicBlock, block: &mir::BasicBlockData<'tcx>) {
60        {
    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:60",
                        "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(60u32),
                        ::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);
61
62        // Ignore drop terminators in cleanup blocks.
63        if block.is_cleanup {
64            return;
65        }
66
67        self.super_basic_block_data(bb, block);
68    }
69
70    fn visit_terminator(&mut self, terminator: &mir::Terminator<'tcx>, location: Location) {
71        {
    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:71",
                        "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(71u32),
                        ::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);
72
73        match &terminator.kind {
74            mir::TerminatorKind::Drop { place: dropped_place, .. } => {
75                self.checker.check_drop_terminator(
76                    *dropped_place,
77                    location,
78                    terminator.source_info.span,
79                );
80            }
81
82            mir::TerminatorKind::UnwindTerminate(_)
83            | mir::TerminatorKind::Call { .. }
84            | mir::TerminatorKind::TailCall { .. }
85            | mir::TerminatorKind::Assert { .. }
86            | mir::TerminatorKind::FalseEdge { .. }
87            | mir::TerminatorKind::FalseUnwind { .. }
88            | mir::TerminatorKind::CoroutineDrop
89            | mir::TerminatorKind::Goto { .. }
90            | mir::TerminatorKind::InlineAsm { .. }
91            | mir::TerminatorKind::UnwindResume
92            | mir::TerminatorKind::Return
93            | mir::TerminatorKind::SwitchInt { .. }
94            | mir::TerminatorKind::Unreachable
95            | mir::TerminatorKind::Yield { .. } => {}
96        }
97    }
98}