]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_const_eval/src/transform/check_consts/post_drop_elaboration.rs
Rollup merge of #84083 - ltratt:threadid_doc_tweak, r=dtolnay
[rust.git] / compiler / rustc_const_eval / src / transform / check_consts / post_drop_elaboration.rs
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};
5
6 use super::check::Qualifs;
7 use super::ops::{self, NonConstOp};
8 use super::qualifs::{NeedsNonConstDrop, Qualif};
9 use super::ConstCx;
10
11 /// Returns `true` if we should use the more precise live drop checker that runs after drop
12 /// elaboration.
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() {
16         return false;
17     }
18
19     ccx.tcx.features().const_precise_live_drops
20 }
21
22 /// Look for live drops in a const context.
23 ///
24 /// This is separate from the rest of the const checking logic because it must run after drop
25 /// elaboration.
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() {
30         return;
31     }
32
33     if tcx.has_attr(def_id.to_def_id(), sym::rustc_do_not_const_check) {
34         return;
35     }
36
37     let ccx = ConstCx { body, tcx, const_kind, param_env: tcx.param_env(def_id) };
38     if !checking_enabled(&ccx) {
39         return;
40     }
41
42     let mut visitor = CheckLiveDrops { ccx: &ccx, qualifs: Qualifs::default() };
43
44     visitor.visit_body(body);
45 }
46
47 struct CheckLiveDrops<'mir, 'tcx> {
48     ccx: &'mir ConstCx<'mir, 'tcx>,
49     qualifs: Qualifs<'mir, 'tcx>,
50 }
51
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>;
55
56     fn deref(&self) -> &Self::Target {
57         &self.ccx
58     }
59 }
60
61 impl CheckLiveDrops<'_, '_> {
62     fn check_live_drop(&self, span: Span) {
63         ops::LiveDrop { dropped_at: None }.build_error(self.ccx, span).emit();
64     }
65 }
66
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);
70
71         // Ignore drop terminators in cleanup blocks.
72         if block.is_cleanup {
73             return;
74         }
75
76         self.super_basic_block_data(bb, block);
77     }
78
79     fn visit_terminator(&mut self, terminator: &mir::Terminator<'tcx>, location: Location) {
80         trace!("visit_terminator: terminator={:?} location={:?}", terminator, location);
81
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.
89                     return;
90                 }
91
92                 if dropped_place.is_indirect() {
93                     self.check_live_drop(terminator.source_info.span);
94                     return;
95                 }
96
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);
105                 }
106             }
107
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 { .. } => {}
121         }
122     }
123 }