]> git.proxmox.com Git - rustc.git/blame - compiler/rustc_const_eval/src/transform/check_consts/post_drop_elaboration.rs
New upstream version 1.57.0+dfsg1
[rustc.git] / compiler / rustc_const_eval / src / transform / check_consts / post_drop_elaboration.rs
CommitLineData
f035d41b
XL
1use rustc_middle::mir::visit::Visitor;
2use rustc_middle::mir::{self, BasicBlock, Location};
3use rustc_middle::ty::TyCtxt;
c295e0f8 4use rustc_span::{symbol::sym, Span};
f035d41b 5
94222f64 6use super::check::Qualifs;
1b1a35ee 7use super::ops::{self, NonConstOp};
c295e0f8 8use super::qualifs::{NeedsNonConstDrop, Qualif};
f035d41b
XL
9use super::ConstCx;
10
11/// Returns `true` if we should use the more precise live drop checker that runs after drop
12/// elaboration.
1b1a35ee
XL
13pub 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
f035d41b
XL
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.
29967ef6
XL
26pub fn check_live_drops(tcx: TyCtxt<'tcx>, body: &mir::Body<'tcx>) {
27 let def_id = body.source.def_id().expect_local();
f035d41b
XL
28 let const_kind = tcx.hir().body_const_context(def_id);
29 if const_kind.is_none() {
30 return;
31 }
32
c295e0f8
XL
33 if tcx.has_attr(def_id.to_def_id(), sym::rustc_do_not_const_check) {
34 return;
35 }
36
29967ef6 37 let ccx = ConstCx { body, tcx, const_kind, param_env: tcx.param_env(def_id) };
1b1a35ee 38 if !checking_enabled(&ccx) {
f035d41b
XL
39 return;
40 }
41
f035d41b
XL
42 let mut visitor = CheckLiveDrops { ccx: &ccx, qualifs: Qualifs::default() };
43
44 visitor.visit_body(body);
45}
46
47struct CheckLiveDrops<'mir, 'tcx> {
48 ccx: &'mir ConstCx<'mir, 'tcx>,
49 qualifs: Qualifs<'mir, 'tcx>,
50}
51
52// So we can access `body` and `tcx`.
53impl 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
61impl CheckLiveDrops<'mir, 'tcx> {
62 fn check_live_drop(&self, span: Span) {
1b1a35ee 63 ops::LiveDrop { dropped_at: None }.build_error(self.ccx, span).emit();
f035d41b
XL
64 }
65}
66
67impl Visitor<'tcx> for CheckLiveDrops<'mir, '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 let dropped_ty = dropped_place.ty(self.body, self.tcx).ty;
c295e0f8
XL
85 if !NeedsNonConstDrop::in_any_value_of_ty(self.ccx, dropped_ty) {
86 // Instead of throwing a bug, we just return here. This is because we have to
87 // run custom `const Drop` impls.
88 return;
f035d41b
XL
89 }
90
91 if dropped_place.is_indirect() {
92 self.check_live_drop(terminator.source_info.span);
93 return;
94 }
95
cdc7bbd5
XL
96 // Drop elaboration is not precise enough to accept code like
97 // `src/test/ui/consts/control-flow/drop-pass.rs`; e.g., when an `Option<Vec<T>>` is
98 // initialized with `None` and never changed, it still emits drop glue.
99 // Hence we additionally check the qualifs here to allow more code to pass.
f035d41b
XL
100 if self.qualifs.needs_drop(self.ccx, dropped_place.local, location) {
101 // Use the span where the dropped local was declared for the error.
102 let span = self.body.local_decls[dropped_place.local].source_info.span;
103 self.check_live_drop(span);
104 }
105 }
106
107 mir::TerminatorKind::DropAndReplace { .. } => span_bug!(
108 terminator.source_info.span,
109 "`DropAndReplace` should be removed by drop elaboration",
110 ),
111
112 mir::TerminatorKind::Abort
113 | mir::TerminatorKind::Call { .. }
114 | mir::TerminatorKind::Assert { .. }
115 | mir::TerminatorKind::FalseEdge { .. }
116 | mir::TerminatorKind::FalseUnwind { .. }
117 | mir::TerminatorKind::GeneratorDrop
118 | mir::TerminatorKind::Goto { .. }
119 | mir::TerminatorKind::InlineAsm { .. }
120 | mir::TerminatorKind::Resume
121 | mir::TerminatorKind::Return
122 | mir::TerminatorKind::SwitchInt { .. }
123 | mir::TerminatorKind::Unreachable
124 | mir::TerminatorKind::Yield { .. } => {}
125 }
126 }
127}