Skip to content

Commit a656bc5

Browse files
authored
Rollup merge of #90069 - tmiasko:promoted-const-qualif, r=oli-obk
Fix const qualification when executed after promotion The const qualification was so far performed before the promotion and the implementation assumed that it will never encounter a promoted. With `const_precise_live_drops` feature, checking for live drops is delayed until after drop elaboration, which in turn runs after promotion. so the assumption is no longer true. When evaluating `NeedsNonConstDrop` it is now possible to encounter promoteds. Use type base qualification for the promoted. It is a sound approximation in general, and in the specific case of promoteds and `NeedsNonConstDrop` it is precise. Fixes #89938.
2 parents 9ed9025 + 74c6636 commit a656bc5

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

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

+9-2
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ pub trait Qualif {
4646
/// Whether this `Qualif` is cleared when a local is moved from.
4747
const IS_CLEARED_ON_MOVE: bool = false;
4848

49+
/// Whether this `Qualif` might be evaluated after the promotion and can encounter a promoted.
50+
const ALLOW_PROMOTED: bool = false;
51+
4952
/// Extracts the field of `ConstQualifs` that corresponds to this `Qualif`.
5053
fn in_qualifs(qualifs: &ConstQualifs) -> bool;
5154

@@ -129,6 +132,7 @@ pub struct NeedsNonConstDrop;
129132
impl Qualif for NeedsNonConstDrop {
130133
const ANALYSIS_NAME: &'static str = "flow_needs_nonconst_drop";
131134
const IS_CLEARED_ON_MOVE: bool = true;
135+
const ALLOW_PROMOTED: bool = true;
132136

133137
fn in_qualifs(qualifs: &ConstQualifs) -> bool {
134138
qualifs.needs_non_const_drop
@@ -310,9 +314,12 @@ where
310314
// Check the qualifs of the value of `const` items.
311315
if let Some(ct) = constant.literal.const_for_ty() {
312316
if let ty::ConstKind::Unevaluated(ty::Unevaluated { def, substs_: _, promoted }) = ct.val {
313-
assert!(promoted.is_none());
317+
// Use qualifs of the type for the promoted. Promoteds in MIR body should be possible
318+
// only for `NeedsNonConstDrop` with precise drop checking. This is the only const
319+
// check performed after the promotion. Verify that with an assertion.
320+
assert!(promoted.is_none() || Q::ALLOW_PROMOTED);
314321
// Don't peek inside trait associated constants.
315-
if cx.tcx.trait_of_item(def.did).is_none() {
322+
if promoted.is_none() && cx.tcx.trait_of_item(def.did).is_none() {
316323
let qualifs = if let Some((did, param_did)) = def.as_const_arg() {
317324
cx.tcx.at(constant.span).mir_const_qualif_const_arg((did, param_did))
318325
} else {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Regression test for issue #89938.
2+
// check-pass
3+
// compile-flags: --crate-type=lib
4+
#![feature(const_precise_live_drops)]
5+
6+
pub const fn f() {
7+
let _: Option<String> = None;
8+
let _: &'static Option<String> = &None;
9+
}

0 commit comments

Comments
 (0)