Skip to content

Commit ec12fea

Browse files
committed
const_eval: Allow std to stabley call const fns from deps.
Because std's dependencies are build with `-Zforce-unstable-if-unmarked` normally, std is unable to call them in const-stable function. However, becuase they had no explicit feature gates, there was no way to use `rustc_allow_const_fn_unstable` to allow this. Therefor we add `#[rustc_allow_const_fn_unstable(any)]` to allow using these functions. The reson to do this is rust-lang#102575, which relies on calling hashbrow function in const-stable std functions.
1 parent 5facb42 commit ec12fea

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -986,7 +986,9 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
986986
// should be fixed later.
987987
let callee_is_unstable_unmarked = tcx.lookup_const_stability(callee).is_none()
988988
&& tcx.lookup_stability(callee).is_some_and(|s| s.is_unstable());
989-
if callee_is_unstable_unmarked {
989+
let can_call_any_function =
990+
super::rustc_allow_const_fn_unstable(tcx, caller, sym::any);
991+
if callee_is_unstable_unmarked && !can_call_any_function {
990992
trace!("callee_is_unstable_unmarked");
991993
// We do not use `const` modifiers for intrinsic "functions", as intrinsics are
992994
// `extern` functions, and these have no way to get marked `const`. So instead we
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// compile-flags: -Zforce-unstable-if-unmarked
2+
3+
// This emulates a dep-of-std (eg hashbrown), that has const functions it
4+
// cannot mark as stable, and is build with force-unstable-if-unmarked.
5+
6+
pub const fn do_something_else() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// aux-build:normal-const-fn.rs
2+
// check-pass
3+
#![crate_type = "lib"]
4+
#![feature(staged_api)]
5+
#![feature(rustc_attrs)]
6+
#![feature(rustc_private)]
7+
#![allow(internal_features)]
8+
#![feature(rustc_allow_const_fn_unstable)]
9+
#![stable(feature = "stable_feature", since = "1.0.0")]
10+
11+
extern crate normal_const_fn;
12+
13+
// This ensures std can call const functions in it's deps that don't have
14+
// access to rustc_const_stable annotations (and hense don't have a feature)
15+
// gate.
16+
17+
#[rustc_const_stable(feature = "stable_feature", since = "1.0.0")]
18+
#[stable(feature = "stable_feature", since = "1.0.0")]
19+
#[rustc_allow_const_fn_unstable(any)]
20+
pub const fn do_something() {
21+
normal_const_fn::do_something_else()
22+
}

0 commit comments

Comments
 (0)