Skip to content

Commit 93ad760

Browse files
committed
Introduce #[rustc_force_min_const_fn].
The attribute will force a `const fn` to adhere to the `qualify_min_const_fn` check and as a result be callable inside a `#[stable] const fn` one on stable Rust. The attribute has two purposes: 1. Make `#[unstable] const fn` details in the standard library usable in `#[stable] const fn`s. 2. Prepare a `#[stable] #[rustc_unstable_const(..)] const fn` for eventually removing the `#[rustc_unstable_const(..)]`.
1 parent 85ed538 commit 93ad760

File tree

3 files changed

+15
-7
lines changed

3 files changed

+15
-7
lines changed

src/librustc/ty/constness.rs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,21 +42,27 @@ impl<'tcx> TyCtxt<'tcx> {
4242

4343
/// Returns `true` if this function must conform to `min_const_fn`
4444
pub fn is_min_const_fn(self, def_id: DefId) -> bool {
45-
// Bail out if the signature doesn't contain `const`
45+
// Bail out if the signature doesn't contain `const`.
4646
if !self.is_const_fn_raw(def_id) {
4747
return false;
4848
}
4949

50+
if self.has_attr(def_id, sym::rustc_force_min_const_fn) {
51+
// The `const fn` has `#[rustc_force_min_const_fn]`.
52+
// We have been ordered to interpret this as a `min_const_fn` no matter what.
53+
return true;
54+
}
55+
5056
if self.features().staged_api {
51-
// in order for a libstd function to be considered min_const_fn
52-
// it needs to be stable and have no `rustc_const_unstable` attribute
57+
// For a libstd function to be considered `min_const_fn`,
58+
// it needs to be stable and have no `rustc_const_unstable` attribute.
5359
match self.lookup_stability(def_id) {
54-
// stable functions with unstable const fn aren't `min_const_fn`
60+
// Stable functions with `rustc_const_unstable` aren't `min_const_fn`.
5561
Some(&attr::Stability { const_stability: Some(_), .. }) => false,
56-
// unstable functions don't need to conform
62+
// Unstable functions don't need to conform.
5763
Some(&attr::Stability { ref level, .. }) if level.is_unstable() => false,
58-
// everything else needs to conform, because it would be callable from
59-
// other `min_const_fn` functions
64+
// Everything else needs to conform, because it would be callable from
65+
// other `min_const_fn` functions.
6066
_ => true,
6167
}
6268
} else {

src/libsyntax/feature_gate/builtin_attrs.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,7 @@ pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
432432
rustc_attr!(rustc_promotable, Whitelisted, template!(Word), IMPL_DETAIL),
433433
rustc_attr!(rustc_allow_const_fn_ptr, Whitelisted, template!(Word), IMPL_DETAIL),
434434
rustc_attr!(rustc_args_required_const, Whitelisted, template!(List: "N"), INTERAL_UNSTABLE),
435+
rustc_attr!(rustc_force_min_const_fn, Whitelisted, template!(Word), INTERAL_UNSTABLE),
435436

436437
// ==========================================================================
437438
// Internal attributes, Layout related:

src/libsyntax_pos/symbol.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -577,6 +577,7 @@ symbols! {
577577
rustc_dump_user_substs,
578578
rustc_error,
579579
rustc_expected_cgu_reuse,
580+
rustc_force_min_const_fn,
580581
rustc_if_this_changed,
581582
rustc_inherit_overflow_checks,
582583
rustc_layout,

0 commit comments

Comments
 (0)