Skip to content

Commit af0d566

Browse files
Deduplicate checking drop terminator
1 parent 2088260 commit af0d566

12 files changed

+69
-107
lines changed

compiler/rustc_const_eval/src/check_consts/check.rs

+38-37
Original file line numberDiff line numberDiff line change
@@ -324,14 +324,6 @@ impl<'mir, 'tcx> Checker<'mir, 'tcx> {
324324
}
325325
}
326326

327-
/// Emits an error at the given `span` if an expression cannot be evaluated in the current
328-
/// context. This is meant for use in a post-const-checker pass such as the const precise
329-
/// live drops lint.
330-
pub fn check_op_spanned_post<O: NonConstOp<'tcx>>(mut self, op: O, span: Span) {
331-
self.check_op_spanned(op, span);
332-
assert!(self.secondary_errors.is_empty());
333-
}
334-
335327
fn check_static(&mut self, def_id: DefId, span: Span) {
336328
if self.tcx.is_thread_local_static(def_id) {
337329
self.tcx.dcx().span_bug(span, "tls access is checked in `Rvalue::ThreadLocalRef`");
@@ -429,6 +421,43 @@ impl<'mir, 'tcx> Checker<'mir, 'tcx> {
429421

430422
true
431423
}
424+
425+
pub fn check_drop_terminator(
426+
&mut self,
427+
dropped_place: Place<'tcx>,
428+
location: Location,
429+
terminator_span: Span,
430+
) {
431+
let ty_of_dropped_place = dropped_place.ty(self.body, self.tcx).ty;
432+
433+
let needs_drop = if let Some(local) = dropped_place.as_local() {
434+
self.qualifs.needs_drop(self.ccx, local, location)
435+
} else {
436+
qualifs::NeedsDrop::in_any_value_of_ty(self.ccx, ty_of_dropped_place)
437+
};
438+
// If this type doesn't need a drop at all, then there's nothing to enforce.
439+
if !needs_drop {
440+
return;
441+
}
442+
443+
let mut err_span = self.span;
444+
let needs_non_const_drop = if let Some(local) = dropped_place.as_local() {
445+
// Use the span where the local was declared as the span of the drop error.
446+
err_span = self.body.local_decls[local].source_info.span;
447+
self.qualifs.needs_non_const_drop(self.ccx, local, location)
448+
} else {
449+
qualifs::NeedsNonConstDrop::in_any_value_of_ty(self.ccx, ty_of_dropped_place)
450+
};
451+
452+
self.check_op_spanned(
453+
ops::LiveDrop {
454+
dropped_at: terminator_span,
455+
dropped_ty: ty_of_dropped_place,
456+
needs_non_const_drop,
457+
},
458+
err_span,
459+
);
460+
}
432461
}
433462

434463
impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
@@ -874,35 +903,7 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
874903
return;
875904
}
876905

877-
let mut err_span = self.span;
878-
let ty_of_dropped_place = dropped_place.ty(self.body, self.tcx).ty;
879-
880-
let needs_drop = if let Some(local) = dropped_place.as_local() {
881-
self.qualifs.needs_drop(self.ccx, local, location)
882-
} else {
883-
qualifs::NeedsDrop::in_any_value_of_ty(self.ccx, ty_of_dropped_place)
884-
};
885-
// If this type doesn't need a drop at all, then there's nothing to enforce.
886-
if !needs_drop {
887-
return;
888-
}
889-
890-
let needs_non_const_drop = if let Some(local) = dropped_place.as_local() {
891-
// Use the span where the local was declared as the span of the drop error.
892-
err_span = self.body.local_decls[local].source_info.span;
893-
self.qualifs.needs_non_const_drop(self.ccx, local, location)
894-
} else {
895-
qualifs::NeedsNonConstDrop::in_any_value_of_ty(self.ccx, ty_of_dropped_place)
896-
};
897-
898-
self.check_op_spanned(
899-
ops::LiveDrop {
900-
dropped_at: Some(terminator.source_info.span),
901-
dropped_ty: ty_of_dropped_place,
902-
needs_non_const_drop,
903-
},
904-
err_span,
905-
);
906+
self.check_drop_terminator(*dropped_place, location, terminator.source_info.span);
906907
}
907908

908909
TerminatorKind::InlineAsm { .. } => self.check_op(ops::InlineAsm),

compiler/rustc_const_eval/src/check_consts/ops.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,7 @@ impl<'tcx> NonConstOp<'tcx> for InlineAsm {
459459

460460
#[derive(Debug)]
461461
pub(crate) struct LiveDrop<'tcx> {
462-
pub dropped_at: Option<Span>,
462+
pub dropped_at: Span,
463463
pub dropped_ty: Ty<'tcx>,
464464
pub needs_non_const_drop: bool,
465465
}

compiler/rustc_const_eval/src/check_consts/post_drop_elaboration.rs

+9-48
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,7 @@ use rustc_span::symbol::sym;
55
use tracing::trace;
66

77
use super::ConstCx;
8-
use super::check::Qualifs;
9-
use super::ops::{self};
10-
use super::qualifs::{NeedsNonConstDrop, Qualif};
118
use crate::check_consts::check::Checker;
12-
use crate::check_consts::qualifs::NeedsDrop;
139
use crate::check_consts::rustc_allow_const_fn_unstable;
1410

1511
/// Returns `true` if we should use the more precise live drop checker that runs after drop
@@ -46,23 +42,16 @@ pub fn check_live_drops<'tcx>(tcx: TyCtxt<'tcx>, body: &mir::Body<'tcx>) {
4642
return;
4743
}
4844

49-
let mut visitor = CheckLiveDrops { ccx: &ccx, qualifs: Qualifs::default() };
45+
// I know it's not great to be creating a new const checker, but I'd
46+
// rather use it so we can deduplicate the error emitting logic that
47+
// it contains.
48+
let mut visitor = CheckLiveDrops { checker: Checker::new(&ccx) };
5049

5150
visitor.visit_body(body);
5251
}
5352

5453
struct CheckLiveDrops<'mir, 'tcx> {
55-
ccx: &'mir ConstCx<'mir, 'tcx>,
56-
qualifs: Qualifs<'mir, 'tcx>,
57-
}
58-
59-
// So we can access `body` and `tcx`.
60-
impl<'mir, 'tcx> std::ops::Deref for CheckLiveDrops<'mir, 'tcx> {
61-
type Target = ConstCx<'mir, 'tcx>;
62-
63-
fn deref(&self) -> &Self::Target {
64-
self.ccx
65-
}
54+
checker: Checker<'mir, 'tcx>,
6655
}
6756

6857
impl<'tcx> Visitor<'tcx> for CheckLiveDrops<'_, 'tcx> {
@@ -82,38 +71,10 @@ impl<'tcx> Visitor<'tcx> for CheckLiveDrops<'_, 'tcx> {
8271

8372
match &terminator.kind {
8473
mir::TerminatorKind::Drop { place: dropped_place, .. } => {
85-
let ty_of_dropped_place = dropped_place.ty(self.body, self.tcx).ty;
86-
87-
let needs_drop = if let Some(local) = dropped_place.as_local() {
88-
self.qualifs.needs_drop(self.ccx, local, location)
89-
} else {
90-
NeedsDrop::in_any_value_of_ty(self.ccx, ty_of_dropped_place)
91-
};
92-
// If this type doesn't need a drop at all, then there's nothing to enforce.
93-
if !needs_drop {
94-
return;
95-
}
96-
97-
let mut err_span = terminator.source_info.span;
98-
99-
let needs_non_const_drop = if let Some(local) = dropped_place.as_local() {
100-
// Use the span where the local was declared as the span of the drop error.
101-
err_span = self.body.local_decls[local].source_info.span;
102-
self.qualifs.needs_non_const_drop(self.ccx, local, location)
103-
} else {
104-
NeedsNonConstDrop::in_any_value_of_ty(self.ccx, ty_of_dropped_place)
105-
};
106-
107-
// I know it's not great to be creating a new const checker, but I'd
108-
// rather use it so we can deduplicate the error emitting logic that
109-
// it contains.
110-
Checker::new(self.ccx).check_op_spanned_post(
111-
ops::LiveDrop {
112-
dropped_at: Some(terminator.source_info.span),
113-
dropped_ty: ty_of_dropped_place,
114-
needs_non_const_drop,
115-
},
116-
err_span,
74+
self.checker.check_drop_terminator(
75+
*dropped_place,
76+
location,
77+
terminator.source_info.span,
11778
);
11879
}
11980

compiler/rustc_const_eval/src/check_consts/qualifs.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -306,11 +306,10 @@ where
306306
return false;
307307
}
308308

309-
// This is currently unconditionally true for all qualifs, since we do
310-
// not recurse into the pointer of a deref projection, but that may change
311-
// in the future. If that changes, each qualif should be required to
312-
// specify whether it operates structurally for deref projections, just like
313-
// we do for `Qualif::is_structural_in_adt`.
309+
// `Deref` currently unconditionally "qualifies" if `in_any_value_of_ty` returns true,
310+
// i.e., we treat all qualifs as non-structural for deref projections. Generally,
311+
// we can say very little about `*ptr` even if we know that `ptr` satisfies all
312+
// sorts of properties.
314313
if matches!(elem, ProjectionElem::Deref) {
315314
// We have to assume that this qualifies.
316315
return true;

compiler/rustc_const_eval/src/errors.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ pub struct LiveDrop<'tcx> {
411411
pub kind: ConstContext,
412412
pub dropped_ty: Ty<'tcx>,
413413
#[label(const_eval_dropped_at_label)]
414-
pub dropped_at: Option<Span>,
414+
pub dropped_at: Span,
415415
}
416416

417417
#[derive(Diagnostic)]

library/core/src/marker.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -953,7 +953,7 @@ marker_impls! {
953953
///
954954
/// This should be used for `~const` bounds,
955955
/// as non-const bounds will always hold for every type.
956-
#[unstable(feature = "const_destruct", issue = "10")]
956+
#[unstable(feature = "const_destruct", issue = "133214")]
957957
#[lang = "destruct"]
958958
#[rustc_on_unimplemented(message = "can't drop `{Self}`", append_const_msg)]
959959
#[rustc_deny_explicit_impl(implement_via_object = false)]

tests/ui/consts/const-block-const-bound.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0658]: use of unstable library feature `const_destruct`
44
LL | use std::marker::Destruct;
55
| ^^^^^^^^^^^^^^^^^^^^^
66
|
7-
= note: see issue #10 <https://github.com/rust-lang/rust/issues/10> for more information
7+
= note: see issue #133214 <https://github.com/rust-lang/rust/issues/133214> for more information
88
= help: add `#![feature(const_destruct)]` to the crate attributes to enable
99
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
1010

@@ -14,7 +14,7 @@ error[E0658]: use of unstable library feature `const_destruct`
1414
LL | const fn f<T: ~const Destruct>(x: T) {}
1515
| ^^^^^^^^
1616
|
17-
= note: see issue #10 <https://github.com/rust-lang/rust/issues/10> for more information
17+
= note: see issue #133214 <https://github.com/rust-lang/rust/issues/133214> for more information
1818
= help: add `#![feature(const_destruct)]` to the crate attributes to enable
1919
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
2020

tests/ui/consts/fn_trait_refs.stderr

+5-5
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0658]: use of unstable library feature `const_destruct`
44
LL | use std::marker::Destruct;
55
| ^^^^^^^^^^^^^^^^^^^^^
66
|
7-
= note: see issue #10 <https://github.com/rust-lang/rust/issues/10> for more information
7+
= note: see issue #133214 <https://github.com/rust-lang/rust/issues/133214> for more information
88
= help: add `#![feature(const_destruct)]` to the crate attributes to enable
99
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
1010

@@ -14,7 +14,7 @@ error[E0658]: use of unstable library feature `const_destruct`
1414
LL | T: ~const Fn<()> + ~const Destruct,
1515
| ^^^^^^^^
1616
|
17-
= note: see issue #10 <https://github.com/rust-lang/rust/issues/10> for more information
17+
= note: see issue #133214 <https://github.com/rust-lang/rust/issues/133214> for more information
1818
= help: add `#![feature(const_destruct)]` to the crate attributes to enable
1919
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
2020

@@ -24,7 +24,7 @@ error[E0658]: use of unstable library feature `const_destruct`
2424
LL | T: ~const FnMut<()> + ~const Destruct,
2525
| ^^^^^^^^
2626
|
27-
= note: see issue #10 <https://github.com/rust-lang/rust/issues/10> for more information
27+
= note: see issue #133214 <https://github.com/rust-lang/rust/issues/133214> for more information
2828
= help: add `#![feature(const_destruct)]` to the crate attributes to enable
2929
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
3030

@@ -34,7 +34,7 @@ error[E0658]: use of unstable library feature `const_destruct`
3434
LL | T: ~const Fn<()> + ~const Destruct,
3535
| ^^^^^^^^
3636
|
37-
= note: see issue #10 <https://github.com/rust-lang/rust/issues/10> for more information
37+
= note: see issue #133214 <https://github.com/rust-lang/rust/issues/133214> for more information
3838
= help: add `#![feature(const_destruct)]` to the crate attributes to enable
3939
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
4040

@@ -44,7 +44,7 @@ error[E0658]: use of unstable library feature `const_destruct`
4444
LL | T: ~const FnMut<()> + ~const Destruct,
4545
| ^^^^^^^^
4646
|
47-
= note: see issue #10 <https://github.com/rust-lang/rust/issues/10> for more information
47+
= note: see issue #133214 <https://github.com/rust-lang/rust/issues/133214> for more information
4848
= help: add `#![feature(const_destruct)]` to the crate attributes to enable
4949
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
5050

tests/ui/traits/const-traits/const-drop-bound.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0658]: use of unstable library feature `const_destruct`
44
LL | use std::marker::Destruct;
55
| ^^^^^^^^^^^^^^^^^^^^^
66
|
7-
= note: see issue #10 <https://github.com/rust-lang/rust/issues/10> for more information
7+
= note: see issue #133214 <https://github.com/rust-lang/rust/issues/133214> for more information
88
= help: add `#![feature(const_destruct)]` to the crate attributes to enable
99
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
1010

@@ -14,7 +14,7 @@ error[E0658]: use of unstable library feature `const_destruct`
1414
LL | const fn foo<T, E>(res: Result<T, E>) -> Option<T> where E: ~const Destruct {
1515
| ^^^^^^^^
1616
|
17-
= note: see issue #10 <https://github.com/rust-lang/rust/issues/10> for more information
17+
= note: see issue #133214 <https://github.com/rust-lang/rust/issues/133214> for more information
1818
= help: add `#![feature(const_destruct)]` to the crate attributes to enable
1919
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
2020

@@ -24,7 +24,7 @@ error[E0658]: use of unstable library feature `const_destruct`
2424
LL | T: ~const Destruct,
2525
| ^^^^^^^^
2626
|
27-
= note: see issue #10 <https://github.com/rust-lang/rust/issues/10> for more information
27+
= note: see issue #133214 <https://github.com/rust-lang/rust/issues/133214> for more information
2828
= help: add `#![feature(const_destruct)]` to the crate attributes to enable
2929
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
3030

@@ -34,7 +34,7 @@ error[E0658]: use of unstable library feature `const_destruct`
3434
LL | E: ~const Destruct,
3535
| ^^^^^^^^
3636
|
37-
= note: see issue #10 <https://github.com/rust-lang/rust/issues/10> for more information
37+
= note: see issue #133214 <https://github.com/rust-lang/rust/issues/133214> for more information
3838
= help: add `#![feature(const_destruct)]` to the crate attributes to enable
3939
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
4040

tests/ui/traits/const-traits/const-drop-fail-2.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0658]: use of unstable library feature `const_destruct`
44
LL | use std::marker::{Destruct, PhantomData};
55
| ^^^^^^^^
66
|
7-
= note: see issue #10 <https://github.com/rust-lang/rust/issues/10> for more information
7+
= note: see issue #133214 <https://github.com/rust-lang/rust/issues/133214> for more information
88
= help: add `#![feature(const_destruct)]` to the crate attributes to enable
99
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
1010

@@ -14,7 +14,7 @@ error[E0658]: use of unstable library feature `const_destruct`
1414
LL | const fn check<T: ~const Destruct>(_: T) {}
1515
| ^^^^^^^^
1616
|
17-
= note: see issue #10 <https://github.com/rust-lang/rust/issues/10> for more information
17+
= note: see issue #133214 <https://github.com/rust-lang/rust/issues/133214> for more information
1818
= help: add `#![feature(const_destruct)]` to the crate attributes to enable
1919
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
2020

tests/ui/traits/const-traits/effects/minicore-drop-without-feature-gate.no.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0493]: destructor of `ConstDrop` cannot be evaluated at compile-time
2-
--> $DIR/minicore-drop-without-feature-gate.rs:23:13
2+
--> $DIR/minicore-drop-without-feature-gate.rs:24:13
33
|
44
LL | let _ = ConstDrop;
55
| ^^^^^^^^^- value is dropped here

tests/ui/traits/const-traits/effects/minicore-drop-without-feature-gate.rs

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
//@ compile-flags: --crate-type=lib -Znext-solver
33
//@ revisions: yes no
44
//@[yes] check-pass
5+
// gate-test-const_destruct
56

67
#![feature(no_core, const_trait_impl)]
78
#![cfg_attr(yes, feature(const_destruct))]

0 commit comments

Comments
 (0)