Skip to content

Commit 8448c3c

Browse files
Rollup merge of rust-lang#118689 - compiler-errors:const-drop, r=fee1-dead
Fix const drop checking Fixes confirmation of `~const Destruct` and const drops. r? fee1-dead
2 parents 75dc956 + efe8ae7 commit 8448c3c

File tree

10 files changed

+57
-67
lines changed

10 files changed

+57
-67
lines changed

compiler/rustc_const_eval/src/transform/check_consts/check.rs

+10-13
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use std::mem;
2222
use std::ops::{ControlFlow, Deref};
2323

2424
use super::ops::{self, NonConstOp, Status};
25-
use super::qualifs::{self, CustomEq, HasMutInterior, NeedsDrop};
25+
use super::qualifs::{self, CustomEq, HasMutInterior, NeedsDrop, NeedsNonConstDrop};
2626
use super::resolver::FlowSensitiveAnalysis;
2727
use super::{ConstCx, Qualif};
2828
use crate::const_eval::is_unstable_const_fn;
@@ -35,7 +35,7 @@ type QualifResults<'mir, 'tcx, Q> =
3535
pub struct Qualifs<'mir, 'tcx> {
3636
has_mut_interior: Option<QualifResults<'mir, 'tcx, HasMutInterior>>,
3737
needs_drop: Option<QualifResults<'mir, 'tcx, NeedsDrop>>,
38-
// needs_non_const_drop: Option<QualifResults<'mir, 'tcx, NeedsNonConstDrop>>,
38+
needs_non_const_drop: Option<QualifResults<'mir, 'tcx, NeedsNonConstDrop>>,
3939
}
4040

4141
impl<'mir, 'tcx> Qualifs<'mir, 'tcx> {
@@ -78,27 +78,25 @@ impl<'mir, 'tcx> Qualifs<'mir, 'tcx> {
7878
local: Local,
7979
location: Location,
8080
) -> bool {
81-
// FIXME(effects) replace with `NeedsNonconstDrop` after const traits work again
82-
/*
8381
let ty = ccx.body.local_decls[local].ty;
84-
if !NeedsDrop::in_any_value_of_ty(ccx, ty) {
82+
// Peeking into opaque types causes cycles if the current function declares said opaque
83+
// type. Thus we avoid short circuiting on the type and instead run the more expensive
84+
// analysis that looks at the actual usage within this function
85+
if !ty.has_opaque_types() && !NeedsNonConstDrop::in_any_value_of_ty(ccx, ty) {
8586
return false;
8687
}
8788

8889
let needs_non_const_drop = self.needs_non_const_drop.get_or_insert_with(|| {
8990
let ConstCx { tcx, body, .. } = *ccx;
9091

91-
FlowSensitiveAnalysis::new(NeedsDrop, ccx)
92-
.into_engine(tcx, &body)
92+
FlowSensitiveAnalysis::new(NeedsNonConstDrop, ccx)
93+
.into_engine(tcx, body)
9394
.iterate_to_fixpoint()
94-
.into_results_cursor(&body)
95+
.into_results_cursor(body)
9596
});
9697

9798
needs_non_const_drop.seek_before_primary_effect(location);
9899
needs_non_const_drop.get().contains(local)
99-
*/
100-
101-
self.needs_drop(ccx, local, location)
102100
}
103101

104102
/// Returns `true` if `local` is `HasMutInterior` at the given `Location`.
@@ -1013,9 +1011,8 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
10131011
let mut err_span = self.span;
10141012
let ty_of_dropped_place = dropped_place.ty(self.body, self.tcx).ty;
10151013

1016-
// FIXME(effects) replace with `NeedsNonConstDrop` once we fix const traits
10171014
let ty_needs_non_const_drop =
1018-
qualifs::NeedsDrop::in_any_value_of_ty(self.ccx, ty_of_dropped_place);
1015+
qualifs::NeedsNonConstDrop::in_any_value_of_ty(self.ccx, ty_of_dropped_place);
10191016

10201017
debug!(?ty_of_dropped_place, ?ty_needs_non_const_drop);
10211018

compiler/rustc_const_eval/src/transform/check_consts/post_drop_elaboration.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use rustc_span::{symbol::sym, Span};
55

66
use super::check::Qualifs;
77
use super::ops::{self, NonConstOp};
8-
use super::qualifs::{NeedsDrop, Qualif};
8+
use super::qualifs::{NeedsNonConstDrop, Qualif};
99
use super::ConstCx;
1010

1111
/// Returns `true` if we should use the more precise live drop checker that runs after drop
@@ -83,8 +83,7 @@ impl<'tcx> Visitor<'tcx> for CheckLiveDrops<'_, 'tcx> {
8383
mir::TerminatorKind::Drop { place: dropped_place, .. } => {
8484
let dropped_ty = dropped_place.ty(self.body, self.tcx).ty;
8585

86-
// FIXME(effects) use `NeedsNonConstDrop`
87-
if !NeedsDrop::in_any_value_of_ty(self.ccx, dropped_ty) {
86+
if !NeedsNonConstDrop::in_any_value_of_ty(self.ccx, dropped_ty) {
8887
// Instead of throwing a bug, we just return here. This is because we have to
8988
// run custom `const Drop` impls.
9089
return;

compiler/rustc_const_eval/src/transform/check_consts/qualifs.rs

+18-4
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ pub fn in_any_value_of_ty<'tcx>(
2323
ConstQualifs {
2424
has_mut_interior: HasMutInterior::in_any_value_of_ty(cx, ty),
2525
needs_drop: NeedsDrop::in_any_value_of_ty(cx, ty),
26-
// FIXME(effects)
27-
needs_non_const_drop: NeedsDrop::in_any_value_of_ty(cx, ty),
26+
needs_non_const_drop: NeedsNonConstDrop::in_any_value_of_ty(cx, ty),
2827
custom_eq: CustomEq::in_any_value_of_ty(cx, ty),
2928
tainted_by_errors,
3029
}
@@ -155,12 +154,27 @@ impl Qualif for NeedsNonConstDrop {
155154
return false;
156155
}
157156

158-
// FIXME(effects) constness
157+
// FIXME(effects): If `destruct` is not a `const_trait`,
158+
// or effects are disabled in this crate, then give up.
159+
let destruct_def_id = cx.tcx.require_lang_item(LangItem::Destruct, Some(cx.body.span));
160+
if cx.tcx.generics_of(destruct_def_id).host_effect_index.is_none()
161+
|| !cx.tcx.features().effects
162+
{
163+
return NeedsDrop::in_any_value_of_ty(cx, ty);
164+
}
165+
159166
let obligation = Obligation::new(
160167
cx.tcx,
161168
ObligationCause::dummy_with_span(cx.body.span),
162169
cx.param_env,
163-
ty::TraitRef::from_lang_item(cx.tcx, LangItem::Destruct, cx.body.span, [ty]),
170+
ty::TraitRef::new(
171+
cx.tcx,
172+
destruct_def_id,
173+
[
174+
ty::GenericArg::from(ty),
175+
ty::GenericArg::from(cx.tcx.expected_const_effect_param_for_body(cx.def_id())),
176+
],
177+
),
164178
);
165179

166180
let infcx = cx.tcx.infer_ctxt().build();

compiler/rustc_middle/src/ty/util.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -781,9 +781,9 @@ impl<'tcx> TyCtxt<'tcx> {
781781
}
782782

783783
pub fn expected_const_effect_param_for_body(self, def_id: LocalDefId) -> ty::Const<'tcx> {
784-
// if the callee does have the param, we need to equate the param to some const
785-
// value no matter whether the effects feature is enabled in the local crate,
786-
// because inference will fail if we don't.
784+
// FIXME(effects): This is suspicious and should probably not be done,
785+
// especially now that we enforce host effects and then properly handle
786+
// effect vars during fallback.
787787
let mut host_always_on =
788788
!self.features().effects || self.sess.opts.unstable_opts.unleash_the_miri_inside_of_you;
789789

compiler/rustc_trait_selection/src/traits/select/candidate_assembly.rs

+12-3
Original file line numberDiff line numberDiff line change
@@ -872,9 +872,18 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
872872
) {
873873
// If the predicate is `~const Destruct` in a non-const environment, we don't actually need
874874
// to check anything. We'll short-circuit checking any obligations in confirmation, too.
875-
// FIXME(effects)
876-
if true {
877-
candidates.vec.push(ConstDestructCandidate(None));
875+
let Some(host_effect_index) =
876+
self.tcx().generics_of(obligation.predicate.def_id()).host_effect_index
877+
else {
878+
candidates.vec.push(BuiltinCandidate { has_nested: false });
879+
return;
880+
};
881+
// If the obligation has `host = true`, then the obligation is non-const and it's always
882+
// trivially implemented.
883+
if obligation.predicate.skip_binder().trait_ref.args.const_at(host_effect_index)
884+
== self.tcx().consts.true_
885+
{
886+
candidates.vec.push(BuiltinCandidate { has_nested: false });
878887
return;
879888
}
880889

compiler/rustc_trait_selection/src/traits/select/confirmation.rs

+9-7
Original file line numberDiff line numberDiff line change
@@ -1172,11 +1172,13 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
11721172
obligation: &PolyTraitObligation<'tcx>,
11731173
impl_def_id: Option<DefId>,
11741174
) -> Result<Vec<PredicateObligation<'tcx>>, SelectionError<'tcx>> {
1175-
// `~const Destruct` in a non-const environment is always trivially true, since our type is `Drop`
1176-
// FIXME(effects)
1177-
if true {
1178-
return Ok(vec![]);
1179-
}
1175+
let Some(host_effect_index) =
1176+
self.tcx().generics_of(obligation.predicate.def_id()).host_effect_index
1177+
else {
1178+
bug!()
1179+
};
1180+
let host_effect_param: ty::GenericArg<'tcx> =
1181+
obligation.predicate.skip_binder().trait_ref.args.const_at(host_effect_index).into();
11801182

11811183
let drop_trait = self.tcx().require_lang_item(LangItem::Drop, None);
11821184

@@ -1284,7 +1286,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
12841286
self.tcx(),
12851287
LangItem::Destruct,
12861288
cause.span,
1287-
[nested_ty],
1289+
[nested_ty.into(), host_effect_param],
12881290
),
12891291
polarity: ty::ImplPolarity::Positive,
12901292
}),
@@ -1310,7 +1312,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
13101312
self.tcx(),
13111313
LangItem::Destruct,
13121314
cause.span,
1313-
[nested_ty],
1315+
[nested_ty.into(), host_effect_param],
13141316
),
13151317
polarity: ty::ImplPolarity::Positive,
13161318
});

tests/ui/consts/precise-drop-with-promoted.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
// Regression test for issue #89938.
2+
// check-pass
23
// compile-flags: --crate-type=lib
3-
// known-bug: #103507
4-
// failure-status: 101
5-
// normalize-stderr-test "note: .*\n\n" -> ""
6-
// normalize-stderr-test "thread 'rustc' panicked.*\n.*\n" -> ""
7-
// normalize-stderr-test "(error: internal compiler error: [^:]+):\d+:\d+: " -> "$1:LL:CC: "
8-
// rustc-env:RUST_BACKTRACE=0
94

105
#![feature(const_precise_live_drops)]
116

tests/ui/consts/precise-drop-with-promoted.stderr

-6
This file was deleted.

tests/ui/rfcs/rfc-2632-const-trait-impl/effects/minicore.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// check-pass
2+
13
#![crate_type = "lib"]
24
#![feature(no_core, lang_items, unboxed_closures, auto_traits, intrinsics, rustc_attrs)]
35
#![feature(fundamental)]
@@ -6,8 +8,6 @@
68
#![no_std]
79
#![no_core]
810

9-
// known-bug: #110395
10-
1111
#[lang = "sized"]
1212
trait Sized {}
1313
#[lang = "copy"]

tests/ui/rfcs/rfc-2632-const-trait-impl/effects/minicore.stderr

-20
This file was deleted.

0 commit comments

Comments
 (0)