1 use rustc_middle
::mir
::visit
::Visitor
;
2 use rustc_middle
::mir
::{self, BasicBlock, Location}
;
3 use rustc_middle
::ty
::TyCtxt
;
6 use super::ops
::{self, NonConstOp}
;
7 use super::qualifs
::{NeedsDrop, Qualif}
;
8 use super::validation
::Qualifs
;
11 /// Returns `true` if we should use the more precise live drop checker that runs after drop
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() {
19 ccx
.tcx
.features().const_precise_live_drops
22 /// Look for live drops in a const context.
24 /// This is separate from the rest of the const checking logic because it must run after drop
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() {
33 let ccx
= ConstCx { body, tcx, const_kind, param_env: tcx.param_env(def_id) }
;
34 if !checking_enabled(&ccx
) {
38 let mut visitor
= CheckLiveDrops { ccx: &ccx, qualifs: Qualifs::default() }
;
40 visitor
.visit_body(body
);
43 struct CheckLiveDrops
<'mir
, 'tcx
> {
44 ccx
: &'mir ConstCx
<'mir
, 'tcx
>,
45 qualifs
: Qualifs
<'mir
, 'tcx
>,
48 // So we can access `body` and `tcx`.
49 impl std
::ops
::Deref
for CheckLiveDrops
<'mir
, 'tcx
> {
50 type Target
= ConstCx
<'mir
, 'tcx
>;
52 fn deref(&self) -> &Self::Target
{
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();
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
);
67 // Ignore drop terminators in cleanup blocks.
72 self.super_basic_block_data(bb
, block
);
75 fn visit_terminator(&mut self, terminator
: &mir
::Terminator
<'tcx
>, location
: Location
) {
76 trace
!("visit_terminator: terminator={:?} location={:?}", terminator
, location
);
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
) {
83 "Drop elaboration left behind a Drop for a type that does not need dropping"
87 if dropped_place
.is_indirect() {
88 self.check_live_drop(terminator
.source_info
.span
);
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
);
103 mir
::TerminatorKind
::DropAndReplace { .. }
=> span_bug
!(
104 terminator
.source_info
.span
,
105 "`DropAndReplace` should be removed by drop elaboration",
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 { .. }
=> {}