Skip to content

Commit

Permalink
Introduce #[rustc_force_min_const_fn].
Browse files Browse the repository at this point in the history
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(..)]`.
  • Loading branch information
Centril committed Aug 29, 2019
1 parent 85ed538 commit 93ad760
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 7 deletions.
20 changes: 13 additions & 7 deletions src/librustc/ty/constness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,21 +42,27 @@ impl<'tcx> TyCtxt<'tcx> {

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

if self.has_attr(def_id, sym::rustc_force_min_const_fn) {

This comment has been minimized.

Copy link
@oli-obk

oli-obk Sep 8, 2019

One thing that could be done is what's essentially the opposite scheme. Everything requires min_const_fn by default, unless it has an additional rustc_not_min_const_fn attribute (which makes the function uncallable from any min_const_fn).

Or rename the attribute to rustc_require_min_const_fn, to make it sound less like an override

This comment has been minimized.

Copy link
@Centril

Centril Sep 8, 2019

Author Owner

One thing that could be done is what's essentially the opposite scheme.

Oh... I like that! -- but what about user-land const fn? Do we only enforce this for staged_api crates?

This comment has been minimized.

Copy link
@oli-obk

oli-obk Sep 8, 2019

user land const fn just keeps doing what it does now. As long as there's a const fn feature gate active, everything is not min_const_fn. The reason we have a complex scheme in libstd is stability. Users using nightly with feature gates already opt out of stability.

// The `const fn` has `#[rustc_force_min_const_fn]`.
// We have been ordered to interpret this as a `min_const_fn` no matter what.
return true;
}

if self.features().staged_api {
// in order for a libstd function to be considered min_const_fn
// it needs to be stable and have no `rustc_const_unstable` attribute
// For a libstd function to be considered `min_const_fn`,
// it needs to be stable and have no `rustc_const_unstable` attribute.
match self.lookup_stability(def_id) {
// stable functions with unstable const fn aren't `min_const_fn`
// Stable functions with `rustc_const_unstable` aren't `min_const_fn`.
Some(&attr::Stability { const_stability: Some(_), .. }) => false,
// unstable functions don't need to conform
// Unstable functions don't need to conform.
Some(&attr::Stability { ref level, .. }) if level.is_unstable() => false,
// everything else needs to conform, because it would be callable from
// other `min_const_fn` functions
// Everything else needs to conform, because it would be callable from
// other `min_const_fn` functions.
_ => true,
}
} else {
Expand Down
1 change: 1 addition & 0 deletions src/libsyntax/feature_gate/builtin_attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,7 @@ pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
rustc_attr!(rustc_promotable, Whitelisted, template!(Word), IMPL_DETAIL),
rustc_attr!(rustc_allow_const_fn_ptr, Whitelisted, template!(Word), IMPL_DETAIL),
rustc_attr!(rustc_args_required_const, Whitelisted, template!(List: "N"), INTERAL_UNSTABLE),
rustc_attr!(rustc_force_min_const_fn, Whitelisted, template!(Word), INTERAL_UNSTABLE),

// ==========================================================================
// Internal attributes, Layout related:
Expand Down
1 change: 1 addition & 0 deletions src/libsyntax_pos/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,7 @@ symbols! {
rustc_dump_user_substs,
rustc_error,
rustc_expected_cgu_reuse,
rustc_force_min_const_fn,
rustc_if_this_changed,
rustc_inherit_overflow_checks,
rustc_layout,
Expand Down

0 comments on commit 93ad760

Please sign in to comment.