Skip to content

Commit 6884fb4

Browse files
Deduplicate checking drop terminator
1 parent 328532d commit 6884fb4

File tree

9 files changed

+60
-89
lines changed

9 files changed

+60
-89
lines changed

compiler/rustc_const_eval/src/check_consts/check.rs

+38-29
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,43 @@ impl<'mir, 'tcx> Checker<'mir, 'tcx> {
429429

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

434471
impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
@@ -873,35 +910,7 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
873910
return;
874911
}
875912

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

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

compiler/rustc_const_eval/src/check_consts/post_drop_elaboration.rs

+6-45
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,13 @@ 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+
let mut visitor = CheckLiveDrops { checker: Checker::new(&ccx) };
5046

5147
visitor.visit_body(body);
5248
}
5349

5450
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-
}
51+
checker: Checker<'mir, 'tcx>,
6652
}
6753

6854
impl<'tcx> Visitor<'tcx> for CheckLiveDrops<'_, 'tcx> {
@@ -82,38 +68,13 @@ impl<'tcx> Visitor<'tcx> for CheckLiveDrops<'_, 'tcx> {
8268

8369
match &terminator.kind {
8470
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-
10771
// I know it's not great to be creating a new const checker, but I'd
10872
// rather use it so we can deduplicate the error emitting logic that
10973
// 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

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)