Skip to content

Commit

Permalink
Auto merge of #43017 - durka:stabilize-const-invocation, r=eddyb
Browse files Browse the repository at this point in the history
Individualize feature gates for const fn invocation

This PR changes the meaning of `#![feature(const_fn)]` so it is only required to declare a const fn but not to call one. Based on discussion at #24111. I was hoping we could have an FCP here in order to move that conversation forward.

This sets the stage for future stabilization of the constness of several functions in the standard library (listed below), so could someone please tag the lang team for review.

- `std::cell`
    - `Cell::new`
    - `RefCell::new`
    - `UnsafeCell::new`
- `std::mem`
    - `size_of`
    - `align_of`
- `std::ptr`
    - `null`
    - `null_mut`
- `std::sync`
    - `atomic`
        - `Atomic{Bool,Ptr,Isize,Usize}::new`
    - `once`
        - `Once::new`
- primitives
    - `{integer}::min_value`
    - `{integer}::max_value`

Some other functions are const but they are also unstable or hidden, e.g. `Unique::new` so they don't have to be considered at this time.

After this stabilization, the following `*_INIT` constants in the standard library can be deprecated. I wasn't sure whether to include those deprecations in the current PR.

- `std::sync`
    - `atomic`
        - `ATOMIC_{BOOL,ISIZE,USIZE}_INIT`
    - `once`
        - `ONCE_INIT`
  • Loading branch information
bors committed Sep 16, 2017
2 parents b965d55 + 332c38c commit ae8efdc
Show file tree
Hide file tree
Showing 60 changed files with 312 additions and 102 deletions.
6 changes: 3 additions & 3 deletions src/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions src/libcore/cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,7 @@ impl<T> Cell<T> {
/// let c = Cell::new(5);
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
#[cfg_attr(not(stage0), rustc_const_unstable(feature = "const_cell_new"))]
#[inline]
pub const fn new(value: T) -> Cell<T> {
Cell {
Expand Down Expand Up @@ -543,6 +544,7 @@ impl<T> RefCell<T> {
/// let c = RefCell::new(5);
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
#[cfg_attr(not(stage0), rustc_const_unstable(feature = "const_refcell_new"))]
#[inline]
pub const fn new(value: T) -> RefCell<T> {
RefCell {
Expand Down Expand Up @@ -1188,6 +1190,7 @@ impl<T> UnsafeCell<T> {
/// let uc = UnsafeCell::new(5);
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
#[cfg_attr(not(stage0), rustc_const_unstable(feature = "const_unsafe_cell_new"))]
#[inline]
pub const fn new(value: T) -> UnsafeCell<T> {
UnsafeCell { value: value }
Expand Down
18 changes: 18 additions & 0 deletions src/libcore/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,30 @@
#![feature(prelude_import)]
#![feature(repr_simd, platform_intrinsics)]
#![feature(rustc_attrs)]
#![cfg_attr(not(stage0), feature(rustc_const_unstable))]
#![feature(specialization)]
#![feature(staged_api)]
#![feature(unboxed_closures)]
#![feature(untagged_unions)]
#![feature(unwind_attributes)]

#![cfg_attr(not(stage0), feature(const_min_value))]
#![cfg_attr(not(stage0), feature(const_max_value))]
#![cfg_attr(not(stage0), feature(const_atomic_bool_new))]
#![cfg_attr(not(stage0), feature(const_atomic_isize_new))]
#![cfg_attr(not(stage0), feature(const_atomic_usize_new))]
#![cfg_attr(not(stage0), feature(const_atomic_i8_new))]
#![cfg_attr(not(stage0), feature(const_atomic_u8_new))]
#![cfg_attr(not(stage0), feature(const_atomic_i16_new))]
#![cfg_attr(not(stage0), feature(const_atomic_u16_new))]
#![cfg_attr(not(stage0), feature(const_atomic_i32_new))]
#![cfg_attr(not(stage0), feature(const_atomic_u32_new))]
#![cfg_attr(not(stage0), feature(const_atomic_i64_new))]
#![cfg_attr(not(stage0), feature(const_atomic_u64_new))]
#![cfg_attr(not(stage0), feature(const_unsafe_cell_new))]
#![cfg_attr(not(stage0), feature(const_cell_new))]
#![cfg_attr(not(stage0), feature(const_nonzero_new))]

#[prelude_import]
#[allow(unused)]
use prelude::v1::*;
Expand Down
2 changes: 2 additions & 0 deletions src/libcore/mem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ pub fn forget<T>(t: T) {
/// ```
#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
#[cfg_attr(not(stage0), rustc_const_unstable(feature = "const_size_of"))]
pub const fn size_of<T>() -> usize {
unsafe { intrinsics::size_of::<T>() }
}
Expand Down Expand Up @@ -280,6 +281,7 @@ pub fn min_align_of_val<T: ?Sized>(val: &T) -> usize {
/// ```
#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
#[cfg_attr(not(stage0), rustc_const_unstable(feature = "const_align_of"))]
pub const fn align_of<T>() -> usize {
unsafe { intrinsics::min_align_of::<T>() }
}
Expand Down
4 changes: 4 additions & 0 deletions src/libcore/nonzero.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ pub struct NonZero<T: Zeroable>(T);
impl<T: Zeroable> NonZero<T> {
/// Creates an instance of NonZero with the provided value.
/// You must indeed ensure that the value is actually "non-zero".
#[unstable(feature = "nonzero",
reason = "needs an RFC to flesh out the design",
issue = "27730")]
#[cfg_attr(not(stage0), rustc_const_unstable(feature = "const_nonzero_new"))]
#[inline]
pub const unsafe fn new_unchecked(inner: T) -> Self {
NonZero(inner)
Expand Down
4 changes: 4 additions & 0 deletions src/libcore/num/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ macro_rules! int_impl {
/// assert_eq!(i8::min_value(), -128);
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
#[cfg_attr(not(stage0), rustc_const_unstable(feature = "const_min_value"))]
#[inline]
pub const fn min_value() -> Self {
!0 ^ ((!0 as $UnsignedT) >> 1) as Self
Expand All @@ -122,6 +123,7 @@ macro_rules! int_impl {
/// assert_eq!(i8::max_value(), 127);
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
#[cfg_attr(not(stage0), rustc_const_unstable(feature = "const_max_value"))]
#[inline]
pub const fn max_value() -> Self {
!Self::min_value()
Expand Down Expand Up @@ -1280,6 +1282,7 @@ macro_rules! uint_impl {
/// assert_eq!(u8::min_value(), 0);
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
#[cfg_attr(not(stage0), rustc_const_unstable(feature = "const_min_value"))]
#[inline]
pub const fn min_value() -> Self { 0 }

Expand All @@ -1291,6 +1294,7 @@ macro_rules! uint_impl {
/// assert_eq!(u8::max_value(), 255);
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
#[cfg_attr(not(stage0), rustc_const_unstable(feature = "const_max_value"))]
#[inline]
pub const fn max_value() -> Self { !0 }

Expand Down
6 changes: 6 additions & 0 deletions src/libcore/ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ pub unsafe fn drop_in_place<T: ?Sized>(to_drop: *mut T) {
/// ```
#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
#[cfg_attr(not(stage0), rustc_const_unstable(feature = "const_ptr_null"))]
pub const fn null<T>() -> *const T { 0 as *const T }

/// Creates a null mutable raw pointer.
Expand All @@ -90,6 +91,7 @@ pub const fn null<T>() -> *const T { 0 as *const T }
/// ```
#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
#[cfg_attr(not(stage0), rustc_const_unstable(feature = "const_ptr_null_mut"))]
pub const fn null_mut<T>() -> *mut T { 0 as *mut T }

/// Swaps the values at two mutable locations of the same type, without
Expand Down Expand Up @@ -1097,6 +1099,8 @@ impl<T: ?Sized> Unique<T> {
/// # Safety
///
/// `ptr` must be non-null.
#[unstable(feature = "unique", issue = "27730")]
#[cfg_attr(not(stage0), rustc_const_unstable(feature = "const_unique_new"))]
pub const unsafe fn new_unchecked(ptr: *mut T) -> Self {
Unique { pointer: NonZero::new_unchecked(ptr), _marker: PhantomData }
}
Expand Down Expand Up @@ -1230,6 +1234,8 @@ impl<T: ?Sized> Shared<T> {
/// # Safety
///
/// `ptr` must be non-null.
#[unstable(feature = "shared", issue = "27730")]
#[cfg_attr(not(stage0), rustc_const_unstable(feature = "const_shared_new"))]
pub const unsafe fn new_unchecked(ptr: *mut T) -> Self {
Shared { pointer: NonZero::new_unchecked(ptr), _marker: PhantomData }
}
Expand Down
15 changes: 14 additions & 1 deletion src/libcore/sync/atomic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ impl AtomicBool {
/// ```
#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
#[cfg_attr(not(stage0), rustc_const_unstable(feature = "const_atomic_bool_new"))]
pub const fn new(v: bool) -> AtomicBool {
AtomicBool { v: UnsafeCell::new(v as u8) }
}
Expand Down Expand Up @@ -649,6 +650,7 @@ impl<T> AtomicPtr<T> {
/// ```
#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
#[cfg_attr(not(stage0), rustc_const_unstable(feature = "const_atomic_ptr_new"))]
pub const fn new(p: *mut T) -> AtomicPtr<T> {
AtomicPtr { p: UnsafeCell::new(p) }
}
Expand Down Expand Up @@ -920,7 +922,7 @@ impl<T> AtomicPtr<T> {

#[cfg(target_has_atomic = "ptr")]
macro_rules! atomic_int {
($stable:meta,
($stable:meta, $const_unstable:meta,
$stable_cxchg:meta,
$stable_debug:meta,
$stable_access:meta,
Expand Down Expand Up @@ -969,6 +971,7 @@ macro_rules! atomic_int {
/// ```
#[inline]
#[$stable]
#[cfg_attr(not(stage0), $const_unstable)]
pub const fn new(v: $int_type) -> Self {
$atomic_type {v: UnsafeCell::new(v)}
}
Expand Down Expand Up @@ -1332,6 +1335,7 @@ macro_rules! atomic_int {
#[cfg(target_has_atomic = "8")]
atomic_int! {
unstable(feature = "integer_atomics", issue = "32976"),
rustc_const_unstable(feature = "const_atomic_i8_new"),
unstable(feature = "integer_atomics", issue = "32976"),
unstable(feature = "integer_atomics", issue = "32976"),
unstable(feature = "integer_atomics", issue = "32976"),
Expand All @@ -1340,6 +1344,7 @@ atomic_int! {
#[cfg(target_has_atomic = "8")]
atomic_int! {
unstable(feature = "integer_atomics", issue = "32976"),
rustc_const_unstable(feature = "const_atomic_u8_new"),
unstable(feature = "integer_atomics", issue = "32976"),
unstable(feature = "integer_atomics", issue = "32976"),
unstable(feature = "integer_atomics", issue = "32976"),
Expand All @@ -1348,6 +1353,7 @@ atomic_int! {
#[cfg(target_has_atomic = "16")]
atomic_int! {
unstable(feature = "integer_atomics", issue = "32976"),
rustc_const_unstable(feature = "const_atomic_i16_new"),
unstable(feature = "integer_atomics", issue = "32976"),
unstable(feature = "integer_atomics", issue = "32976"),
unstable(feature = "integer_atomics", issue = "32976"),
Expand All @@ -1356,6 +1362,7 @@ atomic_int! {
#[cfg(target_has_atomic = "16")]
atomic_int! {
unstable(feature = "integer_atomics", issue = "32976"),
rustc_const_unstable(feature = "const_atomic_u16_new"),
unstable(feature = "integer_atomics", issue = "32976"),
unstable(feature = "integer_atomics", issue = "32976"),
unstable(feature = "integer_atomics", issue = "32976"),
Expand All @@ -1364,6 +1371,7 @@ atomic_int! {
#[cfg(target_has_atomic = "32")]
atomic_int! {
unstable(feature = "integer_atomics", issue = "32976"),
rustc_const_unstable(feature = "const_atomic_i32_new"),
unstable(feature = "integer_atomics", issue = "32976"),
unstable(feature = "integer_atomics", issue = "32976"),
unstable(feature = "integer_atomics", issue = "32976"),
Expand All @@ -1372,6 +1380,7 @@ atomic_int! {
#[cfg(target_has_atomic = "32")]
atomic_int! {
unstable(feature = "integer_atomics", issue = "32976"),
rustc_const_unstable(feature = "const_atomic_u32_new"),
unstable(feature = "integer_atomics", issue = "32976"),
unstable(feature = "integer_atomics", issue = "32976"),
unstable(feature = "integer_atomics", issue = "32976"),
Expand All @@ -1380,6 +1389,7 @@ atomic_int! {
#[cfg(target_has_atomic = "64")]
atomic_int! {
unstable(feature = "integer_atomics", issue = "32976"),
rustc_const_unstable(feature = "const_atomic_i64_new"),
unstable(feature = "integer_atomics", issue = "32976"),
unstable(feature = "integer_atomics", issue = "32976"),
unstable(feature = "integer_atomics", issue = "32976"),
Expand All @@ -1388,6 +1398,7 @@ atomic_int! {
#[cfg(target_has_atomic = "64")]
atomic_int! {
unstable(feature = "integer_atomics", issue = "32976"),
rustc_const_unstable(feature = "const_atomic_u64_new"),
unstable(feature = "integer_atomics", issue = "32976"),
unstable(feature = "integer_atomics", issue = "32976"),
unstable(feature = "integer_atomics", issue = "32976"),
Expand All @@ -1396,6 +1407,7 @@ atomic_int! {
#[cfg(target_has_atomic = "ptr")]
atomic_int!{
stable(feature = "rust1", since = "1.0.0"),
rustc_const_unstable(feature = "const_atomic_isize_new"),
stable(feature = "extended_compare_and_swap", since = "1.10.0"),
stable(feature = "atomic_debug", since = "1.3.0"),
stable(feature = "atomic_access", since = "1.15.0"),
Expand All @@ -1404,6 +1416,7 @@ atomic_int!{
#[cfg(target_has_atomic = "ptr")]
atomic_int!{
stable(feature = "rust1", since = "1.0.0"),
rustc_const_unstable(feature = "const_atomic_usize_new"),
stable(feature = "extended_compare_and_swap", since = "1.10.0"),
stable(feature = "atomic_debug", since = "1.3.0"),
stable(feature = "atomic_access", since = "1.15.0"),
Expand Down
5 changes: 4 additions & 1 deletion src/libcore/tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
#![deny(warnings)]

#![feature(box_syntax)]
#![feature(const_fn)]
#![feature(core_float)]
#![feature(core_private_bignum)]
#![feature(core_private_diy_float)]
Expand Down Expand Up @@ -42,6 +41,10 @@
#![feature(try_from)]
#![feature(unique)]

#![feature(const_atomic_bool_new)]
#![feature(const_atomic_usize_new)]
#![feature(const_atomic_isize_new)]

extern crate core;
extern crate test;
extern crate rand;
Expand Down
8 changes: 7 additions & 1 deletion src/librustc/ich/impls_syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,12 @@ impl_stable_hash_for!(enum ::syntax::abi::Abi {
});

impl_stable_hash_for!(struct ::syntax::attr::Deprecation { since, note });
impl_stable_hash_for!(struct ::syntax::attr::Stability { level, feature, rustc_depr });
impl_stable_hash_for!(struct ::syntax::attr::Stability {
level,
feature,
rustc_depr,
rustc_const_unstable
});

impl<'a, 'gcx, 'tcx> HashStable<StableHashingContext<'a, 'gcx, 'tcx>>
for ::syntax::attr::StabilityLevel {
Expand All @@ -102,6 +107,7 @@ for ::syntax::attr::StabilityLevel {
}

impl_stable_hash_for!(struct ::syntax::attr::RustcDeprecation { since, reason });
impl_stable_hash_for!(struct ::syntax::attr::RustcConstUnstable { feature });


impl_stable_hash_for!(enum ::syntax::attr::IntType {
Expand Down
4 changes: 3 additions & 1 deletion src/librustc/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
#![feature(box_patterns)]
#![feature(box_syntax)]
#![feature(conservative_impl_trait)]
#![feature(const_fn)]
#![feature(core_intrinsics)]
#![feature(i128_type)]
#![cfg_attr(windows, feature(libc))]
Expand All @@ -36,6 +35,9 @@
#![feature(trace_macros)]
#![feature(test)]

#![cfg_attr(stage0, feature(const_fn))]
#![cfg_attr(not(stage0), feature(const_atomic_bool_new))]

#![recursion_limit="256"]

extern crate arena;
Expand Down
1 change: 1 addition & 0 deletions src/librustc/middle/stability.rs
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,7 @@ impl<'a, 'tcx> Index<'tcx> {
},
feature: Symbol::intern("rustc_private"),
rustc_depr: None,
rustc_const_unstable: None,
});
annotator.parent_stab = Some(stability);
}
Expand Down
5 changes: 4 additions & 1 deletion src/librustc_apfloat/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,14 @@
#![deny(warnings)]
#![forbid(unsafe_code)]

#![feature(const_fn)]
#![feature(i128_type)]
#![feature(slice_patterns)]
#![feature(try_from)]

#![cfg_attr(stage0, feature(const_fn))]
#![cfg_attr(not(stage0), feature(const_min_value))]
#![cfg_attr(not(stage0), feature(const_max_value))]

#[macro_use]
extern crate rustc_bitflags;

Expand Down
4 changes: 3 additions & 1 deletion src/librustc_const_eval/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@
#![feature(slice_patterns)]
#![feature(box_patterns)]
#![feature(box_syntax)]
#![feature(const_fn)]
#![feature(i128_type)]

#![cfg_attr(stage0, feature(const_fn))]
#![cfg_attr(not(stage0), feature(const_min_value))]

extern crate arena;
#[macro_use] extern crate syntax;
#[macro_use] extern crate log;
Expand Down
5 changes: 4 additions & 1 deletion src/librustc_const_math/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,13 @@
html_root_url = "https://doc.rust-lang.org/nightly/")]
#![deny(warnings)]

#![feature(const_fn)]
#![feature(i128)]
#![feature(i128_type)]

#![cfg_attr(stage0, feature(const_fn))]
#![cfg_attr(not(stage0), feature(const_min_value))]
#![cfg_attr(not(stage0), feature(const_max_value))]

extern crate rustc_apfloat;

extern crate syntax;
Expand Down
Loading

0 comments on commit ae8efdc

Please sign in to comment.