]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_mir/src/transform/check_consts/post_drop_elaboration.rs
Add 'compiler/rustc_codegen_cranelift/' from commit '793d26047f994e23415f8f6bb5686ff2...
[rust.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                     return;
83                 }
84
85                 if dropped_place.is_indirect() {
86                     self.check_live_drop(terminator.source_info.span);
87                     return;
88                 }
89
90                 if self.qualifs.needs_drop(self.ccx, dropped_place.local, location) {
91                     // Use the span where the dropped local was declared for the error.
92                     let span = self.body.local_decls[dropped_place.local].source_info.span;
93                     self.check_live_drop(span);
94                 }
95             }
96
97             mir::TerminatorKind::DropAndReplace { .. } => span_bug!(
98                 terminator.source_info.span,
99                 "`DropAndReplace` should be removed by drop elaboration",
100             ),
101
102             mir::TerminatorKind::Abort
103             | mir::TerminatorKind::Call { .. }
104             | mir::TerminatorKind::Assert { .. }
105             | mir::TerminatorKind::FalseEdge { .. }
106             | mir::TerminatorKind::FalseUnwind { .. }
107             | mir::TerminatorKind::GeneratorDrop
108             | mir::TerminatorKind::Goto { .. }
109             | mir::TerminatorKind::InlineAsm { .. }
110             | mir::TerminatorKind::Resume
111             | mir::TerminatorKind::Return
112             | mir::TerminatorKind::SwitchInt { .. }
113             | mir::TerminatorKind::Unreachable
114             | mir::TerminatorKind::Yield { .. } => {}
115         }
116     }
117 }