Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a system for explicitly marking const fns as promotable #51500

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions src/doc/unstable-book/src/language-features/promotable-const-fn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# promotable_const_fn

The tracking issue for this feature is: None.

The `#[promotable_const_fn]` attribute can be used if the `promotable_const_fn` feature gate is
active.
While there are no checks on where it can be used, it only has an effect if applied to a `const fn`.
Since this attribute is never meant to be stabilized, this seems like an ok footgun.

The attribute exists, so it is possible to stabilize the constness of a function without stabilizing
the promotablity of the function at the same time. We eventually want to have a better strategy,
but we need to hash out the details. Until then, any const fn that is doing anything nontrivial
should not be marked with `#[promotable_const_fn]`. Even better: do not mark any functions with the
attribute. The existing "promotable on stable" functions were marked, but we could simply not
stabilize the promotability of any further functions until we have figured out a clean solution
for all the issues that come with promotion.

The reason this attribute even exists are functions like

```rust,ignore
#![feature(const_fn)]

fn main() {
let x: &'static bool = &foo(&1, &2);
}

union Foo<'a> {
a: &'a u8,
b: usize,
}

const fn foo(a: &u8, b: &u8) -> bool {
unsafe { Foo { a: a }.b == Foo { a: b }.b }
}
```

Where this would be perfectly fine at runtime, but changing the function to a const fn would cause
it to be evaluated at compile-time and thus produce a lint about not being able to compute the value
and then emitting a llvm `undef` because there is no value to place into the promoted.

This attribute patches this hole by not promoting functions without the attribute and at the same
time preventing functions with the attribute from using unions or calling other functions that do
not have said attribute.
1 change: 1 addition & 0 deletions src/liballoc/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@
#![feature(optin_builtin_traits)]
#![feature(pattern)]
#![feature(pin)]
#![feature(promotable_const_fn)]
#![feature(ptr_internals)]
#![feature(ptr_offset_from)]
#![feature(repr_transparent)]
Expand Down
2 changes: 2 additions & 0 deletions src/liballoc/raw_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ pub struct RawVec<T, A: Alloc = Global> {
impl<T, A: Alloc> RawVec<T, A> {
/// Like `new` but parameterized over the choice of allocator for
/// the returned RawVec.
#[promotable_const_fn]
pub const fn new_in(a: A) -> Self {
// !0 is usize::MAX. This branch should be stripped at compile time.
// FIXME(mark-i-m): use this line when `if`s are allowed in `const`
Expand Down Expand Up @@ -123,6 +124,7 @@ impl<T> RawVec<T, Global> {
/// RawVec with capacity 0. If T has 0 size, then it makes a
/// RawVec with capacity `usize::MAX`. Useful for implementing
/// delayed allocation.
#[promotable_const_fn]
pub const fn new() -> Self {
Self::new_in(Global)
}
Expand Down
1 change: 1 addition & 0 deletions src/liballoc/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,7 @@ impl String {
#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_const_unstable(feature = "const_string_new")]
#[promotable_const_fn]
pub const fn new() -> String {
String { vec: Vec::new() }
}
Expand Down
1 change: 1 addition & 0 deletions src/liballoc/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,7 @@ impl<T> Vec<T> {
#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_const_unstable(feature = "const_vec_new")]
#[promotable_const_fn]
pub const fn new() -> Vec<T> {
Vec {
buf: RawVec::new(),
Expand Down
1 change: 1 addition & 0 deletions src/libcore/any.rs
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,7 @@ impl TypeId {
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_const_unstable(feature="const_type_id")]
#[promotable_const_fn]
pub const fn of<T: ?Sized + 'static>() -> TypeId {
TypeId {
t: unsafe { intrinsics::type_id::<T>() },
Expand Down
3 changes: 3 additions & 0 deletions src/libcore/cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,7 @@ impl<T> Cell<T> {
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
#[promotable_const_fn]
pub const fn new(value: T) -> Cell<T> {
Cell {
value: UnsafeCell::new(value),
Expand Down Expand Up @@ -588,6 +589,7 @@ impl<T> RefCell<T> {
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
#[promotable_const_fn]
pub const fn new(value: T) -> RefCell<T> {
RefCell {
value: UnsafeCell::new(value),
Expand Down Expand Up @@ -1304,6 +1306,7 @@ impl<T> UnsafeCell<T> {
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
#[promotable_const_fn]
pub const fn new(value: T) -> UnsafeCell<T> {
UnsafeCell { value: value }
}
Expand Down
1 change: 1 addition & 0 deletions src/libcore/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@
#![feature(on_unimplemented)]
#![feature(optin_builtin_traits)]
#![feature(prelude_import)]
#![feature(promotable_const_fn)]
#![feature(repr_simd, platform_intrinsics)]
#![feature(repr_transparent)]
#![feature(rustc_attrs)]
Expand Down
3 changes: 3 additions & 0 deletions src/libcore/mem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,7 @@ pub fn forget<T>(t: T) {
/// [alignment]: ./fn.align_of.html
#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
#[promotable_const_fn]
pub const fn size_of<T>() -> usize {
unsafe { intrinsics::size_of::<T>() }
}
Expand Down Expand Up @@ -405,6 +406,7 @@ pub fn min_align_of_val<T: ?Sized>(val: &T) -> usize {
/// ```
#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
#[promotable_const_fn]
pub const fn align_of<T>() -> usize {
unsafe { intrinsics::min_align_of::<T>() }
}
Expand Down Expand Up @@ -966,6 +968,7 @@ impl<T> ManuallyDrop<T> {
#[stable(feature = "manually_drop", since = "1.20.0")]
#[rustc_const_unstable(feature = "const_manually_drop_new")]
#[inline]
#[promotable_const_fn]
pub const fn new(value: T) -> ManuallyDrop<T> {
ManuallyDrop { value: value }
}
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 @@ -200,6 +200,7 @@ $EndFeature, "
```"),
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
#[promotable_const_fn]
pub const fn min_value() -> Self {
!0 ^ ((!0 as $UnsignedT) >> 1) as Self
}
Expand All @@ -218,6 +219,7 @@ $EndFeature, "
```"),
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
#[promotable_const_fn]
pub const fn max_value() -> Self {
!Self::min_value()
}
Expand Down Expand Up @@ -2103,6 +2105,7 @@ Basic usage:
```"),
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
#[promotable_const_fn]
pub const fn min_value() -> Self { 0 }
}

Expand All @@ -2119,6 +2122,7 @@ stringify!($MaxV), ");", $EndFeature, "
```"),
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
#[promotable_const_fn]
pub const fn max_value() -> Self { !0 }
}

Expand Down
2 changes: 2 additions & 0 deletions src/libcore/num/wrapping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,7 @@ assert_eq!(<Wrapping<", stringify!($t), ">>::min_value(), ",
```"),
#[unstable(feature = "wrapping_int_impl", issue = "32463")]
#[inline]
#[promotable_const_fn]
pub const fn min_value() -> Self {
Wrapping(<$t>::min_value())
}
Expand All @@ -365,6 +366,7 @@ assert_eq!(<Wrapping<", stringify!($t), ">>::max_value(), ",
```"),
#[unstable(feature = "wrapping_int_impl", issue = "32463")]
#[inline]
#[promotable_const_fn]
pub const fn max_value() -> Self {
Wrapping(<$t>::max_value())
}
Expand Down
1 change: 1 addition & 0 deletions src/libcore/ops/range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,7 @@ impl<Idx> RangeInclusive<Idx> {
/// ```
#[stable(feature = "inclusive_range_methods", since = "1.27.0")]
#[inline]
#[promotable_const_fn]
pub const fn new(start: Idx, end: Idx) -> Self {
Self { start, end }
}
Expand Down
4 changes: 4 additions & 0 deletions src/libcore/ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ pub unsafe fn drop_in_place<T: ?Sized>(to_drop: *mut T) {
/// ```
#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
#[promotable_const_fn]
pub const fn null<T>() -> *const T { 0 as *const T }

/// Creates a null mutable raw pointer.
Expand All @@ -88,6 +89,7 @@ pub const fn null<T>() -> *const T { 0 as *const T }
/// ```
#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
#[promotable_const_fn]
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 @@ -2730,6 +2732,7 @@ impl<T: Sized> Unique<T> {
/// This is useful for initializing types which lazily allocate, like
/// `Vec::new` does.
// FIXME: rename to dangling() to match NonNull?
#[promotable_const_fn]
pub const fn empty() -> Self {
unsafe {
Unique::new_unchecked(mem::align_of::<T>() as *mut T)
Expand All @@ -2744,6 +2747,7 @@ impl<T: ?Sized> Unique<T> {
/// # Safety
///
/// `ptr` must be non-null.
#[promotable_const_fn]
pub const unsafe fn new_unchecked(ptr: *mut T) -> Self {
Unique { pointer: NonZero(ptr as _), _marker: PhantomData }
}
Expand Down
3 changes: 3 additions & 0 deletions src/libcore/sync/atomic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ impl AtomicBool {
/// ```
#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
#[promotable_const_fn]
pub const fn new(v: bool) -> AtomicBool {
AtomicBool { v: UnsafeCell::new(v as u8) }
}
Expand Down Expand Up @@ -659,6 +660,7 @@ impl<T> AtomicPtr<T> {
/// ```
#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
#[promotable_const_fn]
pub const fn new(p: *mut T) -> AtomicPtr<T> {
AtomicPtr { p: UnsafeCell::new(p) }
}
Expand Down Expand Up @@ -1011,6 +1013,7 @@ let atomic_forty_two = ", stringify!($atomic_type), "::new(42);
```"),
#[inline]
#[$stable]
#[promotable_const_fn]
pub const fn new(v: $int_type) -> Self {
$atomic_type {v: UnsafeCell::new(v)}
}
Expand Down
4 changes: 4 additions & 0 deletions src/libcore/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ impl Duration {
/// ```
#[stable(feature = "duration", since = "1.3.0")]
#[inline]
#[promotable_const_fn]
pub const fn from_secs(secs: u64) -> Duration {
Duration { secs: secs, nanos: 0 }
}
Expand All @@ -126,6 +127,7 @@ impl Duration {
/// ```
#[stable(feature = "duration", since = "1.3.0")]
#[inline]
#[promotable_const_fn]
pub const fn from_millis(millis: u64) -> Duration {
Duration {
secs: millis / MILLIS_PER_SEC,
Expand All @@ -147,6 +149,7 @@ impl Duration {
/// ```
#[stable(feature = "duration_from_micros", since = "1.27.0")]
#[inline]
#[promotable_const_fn]
pub const fn from_micros(micros: u64) -> Duration {
Duration {
secs: micros / MICROS_PER_SEC,
Expand All @@ -168,6 +171,7 @@ impl Duration {
/// ```
#[stable(feature = "duration_extras", since = "1.27.0")]
#[inline]
#[promotable_const_fn]
pub const fn from_nanos(nanos: u64) -> Duration {
Duration {
secs: nanos / (NANOS_PER_SEC as u64),
Expand Down
Loading