Skip to content

Commit

Permalink
const_eval: Allow std to stabley call const fns from deps.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
aDotInTheVoid committed Nov 29, 2023
1 parent 5facb42 commit ec12fea
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -986,7 +986,9 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
// should be fixed later.
let callee_is_unstable_unmarked = tcx.lookup_const_stability(callee).is_none()
&& tcx.lookup_stability(callee).is_some_and(|s| s.is_unstable());
if callee_is_unstable_unmarked {
let can_call_any_function =
super::rustc_allow_const_fn_unstable(tcx, caller, sym::any);
if callee_is_unstable_unmarked && !can_call_any_function {
trace!("callee_is_unstable_unmarked");
// We do not use `const` modifiers for intrinsic "functions", as intrinsics are
// `extern` functions, and these have no way to get marked `const`. So instead we
Expand Down
6 changes: 6 additions & 0 deletions tests/ui/stability-attribute/auxiliary/normal-const-fn.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// compile-flags: -Zforce-unstable-if-unmarked

// This emulates a dep-of-std (eg hashbrown), that has const functions it
// cannot mark as stable, and is build with force-unstable-if-unmarked.

pub const fn do_something_else() {}
22 changes: 22 additions & 0 deletions tests/ui/stability-attribute/const-stable-cross-crate.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// aux-build:normal-const-fn.rs
// check-pass
#![crate_type = "lib"]
#![feature(staged_api)]
#![feature(rustc_attrs)]
#![feature(rustc_private)]
#![allow(internal_features)]
#![feature(rustc_allow_const_fn_unstable)]
#![stable(feature = "stable_feature", since = "1.0.0")]

extern crate normal_const_fn;

// This ensures std can call const functions in it's deps that don't have
// access to rustc_const_stable annotations (and hense don't have a feature)
// gate.

#[rustc_const_stable(feature = "stable_feature", since = "1.0.0")]
#[stable(feature = "stable_feature", since = "1.0.0")]
#[rustc_allow_const_fn_unstable(any)]
pub const fn do_something() {
normal_const_fn::do_something_else()
}

0 comments on commit ec12fea

Please sign in to comment.