Skip to content

Commit a173c5c

Browse files
Add const-stability helpers
1 parent 110e59e commit a173c5c

File tree

4 files changed

+45
-10
lines changed

4 files changed

+45
-10
lines changed

compiler/rustc_mir/src/transform/check_consts/mod.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,12 @@ impl ConstCx<'mir, 'tcx> {
5151
pub fn const_kind(&self) -> hir::ConstContext {
5252
self.const_kind.expect("`const_kind` must not be called on a non-const fn")
5353
}
54+
55+
pub fn is_const_stable_const_fn(&self) -> bool {
56+
self.const_kind == Some(hir::ConstContext::ConstFn)
57+
&& self.tcx.features().staged_api
58+
&& is_const_stable(self.tcx, self.def_id.to_def_id())
59+
}
5460
}
5561

5662
/// Returns `true` if this `DefId` points to one of the official `panic` lang items.
@@ -63,3 +69,35 @@ pub fn allow_internal_unstable(tcx: TyCtxt<'tcx>, def_id: DefId, feature_gate: S
6369
attr::allow_internal_unstable(&tcx.sess, attrs)
6470
.map_or(false, |mut features| features.any(|name| name == feature_gate))
6571
}
72+
73+
// Returns `true` if the given `const fn` is "const-stable".
74+
//
75+
// Const-stability is only relevant for `const fn` within a `staged_api` crate. Only "const-stable"
76+
// functions can be called in a const-context by users of the stable compiler. "const-stable"
77+
// functions are subject to more stringent restrictions than "const-unstable" functions: They
78+
// cannot use unstable features and can only call other "const-stable" functions.
79+
pub fn is_const_stable(tcx: TyCtxt<'tcx>, def_id: DefId) -> bool {
80+
use attr::{ConstStability, Stability, StabilityLevel};
81+
82+
// Const-stability is only relevant for `const fn`.
83+
assert!(tcx.is_const_fn_raw(def_id));
84+
85+
// Functions with `#[rustc_const_unstable]` are const-unstable.
86+
match tcx.lookup_const_stability(def_id) {
87+
Some(ConstStability { level: StabilityLevel::Unstable { .. }, .. }) => return false,
88+
Some(ConstStability { level: StabilityLevel::Stable { .. }, .. }) => return true,
89+
None => {}
90+
}
91+
92+
// Functions with `#[unstable]` are const-unstable.
93+
//
94+
// FIXME(ecstaticmorse): We should keep const-stability attributes wholly separate from normal stability
95+
// attributes. `#[unstable]` should be irrelevant.
96+
if let Some(Stability { level: StabilityLevel::Unstable { .. }, .. }) =
97+
tcx.lookup_stability(def_id)
98+
{
99+
return false;
100+
}
101+
102+
true
103+
}

compiler/rustc_mir/src/transform/check_consts/ops.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@ pub fn non_const<O: NonConstOp>(ccx: &ConstCx<'_, '_>, op: O, span: Span) {
1818
Status::Allowed => return,
1919

2020
Status::Unstable(gate) if ccx.tcx.features().enabled(gate) => {
21-
let unstable_in_stable = ccx.const_kind() == hir::ConstContext::ConstFn
22-
&& ccx.tcx.features().enabled(sym::staged_api)
23-
&& !ccx.tcx.has_attr(ccx.def_id.to_def_id(), sym::rustc_const_unstable)
21+
let unstable_in_stable = ccx.is_const_stable_const_fn()
2422
&& !super::allow_internal_unstable(ccx.tcx, ccx.def_id.to_def_id(), gate);
2523

2624
if unstable_in_stable {

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ use super::ConstCx;
1111

1212
/// Returns `true` if we should use the more precise live drop checker that runs after drop
1313
/// elaboration.
14-
pub fn checking_enabled(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> bool {
14+
pub fn checking_enabled(ccx: &ConstCx<'_, '_>) -> bool {
1515
// Const-stable functions must always use the stable live drop checker.
16-
if tcx.features().staged_api && !tcx.has_attr(def_id.to_def_id(), sym::rustc_const_unstable) {
16+
if ccx.is_const_stable_const_fn() {
1717
return false;
1818
}
1919

20-
tcx.features().const_precise_live_drops
20+
ccx.tcx.features().const_precise_live_drops
2121
}
2222

2323
/// Look for live drops in a const context.
@@ -30,12 +30,11 @@ pub fn check_live_drops(tcx: TyCtxt<'tcx>, def_id: LocalDefId, body: &mir::Body<
3030
return;
3131
}
3232

33-
if !checking_enabled(tcx, def_id) {
33+
let ccx = ConstCx { body, tcx, def_id, const_kind, param_env: tcx.param_env(def_id) };
34+
if !checking_enabled(&ccx) {
3435
return;
3536
}
3637

37-
let ccx = ConstCx { body, tcx, def_id, const_kind, param_env: tcx.param_env(def_id) };
38-
3938
let mut visitor = CheckLiveDrops { ccx: &ccx, qualifs: Qualifs::default() };
4039

4140
visitor.visit_body(body);

compiler/rustc_mir/src/transform/check_consts/validation.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -551,7 +551,7 @@ impl Visitor<'tcx> for Validator<'mir, 'tcx> {
551551
| TerminatorKind::DropAndReplace { place: dropped_place, .. } => {
552552
// If we are checking live drops after drop-elaboration, don't emit duplicate
553553
// errors here.
554-
if super::post_drop_elaboration::checking_enabled(self.tcx, self.def_id) {
554+
if super::post_drop_elaboration::checking_enabled(self.ccx) {
555555
return;
556556
}
557557

0 commit comments

Comments
 (0)