]> git.proxmox.com Git - rustc.git/blob - compiler/rustc_mir/src/transform/check_consts/post_drop_elaboration.rs
New upstream version 1.53.0+dfsg1
[rustc.git] / compiler / rustc_mir / 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::Span;
5
6 use super::ops::{self, NonConstOp};
7 use super::qualifs::{NeedsDrop, Qualif};
8 use super::validation::Qualifs;
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: 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 let ccx = ConstCx { body, tcx, const_kind, param_env: tcx.param_env(def_id) };
34 if !checking_enabled(&ccx) {
35 return;
36 }
37
38 let mut visitor = CheckLiveDrops { ccx: &ccx, qualifs: Qualifs::default() };
39
40 visitor.visit_body(body);
41 }
42
43 struct CheckLiveDrops<'mir, 'tcx> {
44 ccx: &'mir ConstCx<'mir, 'tcx>,
45 qualifs: Qualifs<'mir, 'tcx>,
46 }
47
48 // So we can access `body` and `tcx`.
49 impl std::ops::Deref for CheckLiveDrops<'mir, 'tcx> {
50 type Target = ConstCx<'mir, 'tcx>;
51
52 fn deref(&self) -> &Self::Target {
53 &self.ccx
54 }
55 }
56
57 impl CheckLiveDrops<'mir, 'tcx> {
58 fn check_live_drop(&self, span: Span) {
59 ops::LiveDrop { dropped_at: None }.build_error(self.ccx, span).emit();
60 }
61 }
62
63 impl Visitor<'tcx> for CheckLiveDrops<'mir, 'tcx> {
64 fn visit_basic_block_data(&mut self, bb: BasicBlock, block: &mir::BasicBlockData<'tcx>) {
65 trace!("visit_basic_block_data: bb={:?} is_cleanup={:?}", bb, block.is_cleanup);
66
67 // Ignore drop terminators in cleanup blocks.
68 if block.is_cleanup {
69 return;
70 }
71
72 self.super_basic_block_data(bb, block);
73 }
74
75 fn visit_terminator(&mut self, terminator: &mir::Terminator<'tcx>, location: Location) {
76 trace!("visit_terminator: terminator={:?} location={:?}", terminator, location);
77
78 match &terminator.kind {
79 mir::TerminatorKind::Drop { place: dropped_place, .. } => {
80 let dropped_ty = dropped_place.ty(self.body, self.tcx).ty;
81 if !NeedsDrop::in_any_value_of_ty(self.ccx, dropped_ty) {
82 bug!(
83 "Drop elaboration left behind a Drop for a type that does not need dropping"
84 );
85 }
86
87 if dropped_place.is_indirect() {
88 self.check_live_drop(terminator.source_info.span);
89 return;
90 }
91
92 // Drop elaboration is not precise enough to accept code like
93 // `src/test/ui/consts/control-flow/drop-pass.rs`; e.g., when an `Option<Vec<T>>` is
94 // initialized with `None` and never changed, it still emits drop glue.
95 // Hence we additionally check the qualifs here to allow more code to pass.
96 if self.qualifs.needs_drop(self.ccx, dropped_place.local, location) {
97 // Use the span where the dropped local was declared for the error.
98 let span = self.body.local_decls[dropped_place.local].source_info.span;
99 self.check_live_drop(span);
100 }
101 }
102
103 mir::TerminatorKind::DropAndReplace { .. } => span_bug!(
104 terminator.source_info.span,
105 "`DropAndReplace` should be removed by drop elaboration",
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 }