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

Stabilize some MaybeUninit behavior as const #90896

Merged
merged 2 commits into from
Nov 28, 2021
Merged
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
2 changes: 1 addition & 1 deletion library/core/src/intrinsics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -853,7 +853,7 @@ extern "rust-intrinsic" {
/// This will statically either panic, or do nothing.
///
/// This intrinsic does not have a stable counterpart.
#[rustc_const_unstable(feature = "const_assert_type", issue = "none")]
#[rustc_const_stable(feature = "const_assert_type", since = "1.59.0")]
pub fn assert_inhabited<T>();

/// A guard for unsafe functions that cannot ever be executed if `T` does not permit
Expand Down
3 changes: 1 addition & 2 deletions library/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@
#![feature(const_align_of_val)]
#![feature(const_alloc_layout)]
#![feature(const_arguments_as_str)]
#![feature(const_assert_type)]
#![feature(const_bigint_helper_methods)]
#![feature(const_caller_location)]
#![feature(const_cell_into_inner)]
Expand All @@ -117,7 +116,7 @@
#![feature(const_intrinsic_copy)]
#![feature(const_intrinsic_forget)]
#![feature(const_likely)]
#![feature(const_maybe_uninit_as_ptr)]
#![feature(const_maybe_uninit_as_mut_ptr)]
#![feature(const_maybe_uninit_assume_init)]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this be removed now?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, as the method takes a mutable parameter. const_mut_refs hasn't yet been stabilized.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is for assume_init, which is being stabilized... is it not? I am confused.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whoops, when I responded I thought misread where the code was. I'll check the necessity of this.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removing this results in the following errors:

error: `maybe_uninit::MaybeUninit::<T>::as_mut_ptr` is not yet stable as a const fn
   --> library/core/src/mem/maybe_uninit.rs:915:19
    |
915 |             &mut *self.as_mut_ptr()
    |                   ^^^^^^^^^^^^^^^^^
    |
    = help: add `#![feature(const_maybe_uninit_as_mut_ptr)]` to the crate attributes to enable

error: `maybe_uninit::MaybeUninit::<T>::as_mut_ptr` is not yet stable as a const fn
   --> library/core/src/ptr/mod.rs:374:32
    |
374 |         copy_nonoverlapping(x, tmp.as_mut_ptr(), 1);
    |                                ^^^^^^^^^^^^^^^^
    |
    = help: add `#![feature(const_maybe_uninit_as_mut_ptr)]` to the crate attributes to enable

error: `maybe_uninit::MaybeUninit::<T>::as_mut_ptr` is not yet stable as a const fn
   --> library/core/src/ptr/mod.rs:485:17
    |
485 |         let t = t.as_mut_ptr() as *mut u8;
    |                 ^^^^^^^^^^^^^^
    |
    = help: add `#![feature(const_maybe_uninit_as_mut_ptr)]` to the crate attributes to enable

error: `maybe_uninit::MaybeUninit::<T>::as_mut_ptr` is not yet stable as a const fn
   --> library/core/src/ptr/mod.rs:511:17
    |
511 |         let t = t.as_mut_ptr() as *mut u8;
    |                 ^^^^^^^^^^^^^^
    |
    = help: add `#![feature(const_maybe_uninit_as_mut_ptr)]` to the crate attributes to enable

error: `maybe_uninit::MaybeUninit::<T>::as_mut_ptr` is not yet stable as a const fn
   --> library/core/src/ptr/mod.rs:701:34
    |
701 |         copy_nonoverlapping(src, tmp.as_mut_ptr(), 1);
    |                                  ^^^^^^^^^^^^^^^^
    |
    = help: add `#![feature(const_maybe_uninit_as_mut_ptr)]` to the crate attributes to enable

error: `maybe_uninit::MaybeUninit::<T>::as_mut_ptr` is not yet stable as a const fn
   --> library/core/src/ptr/mod.rs:793:47
    |
793 |         copy_nonoverlapping(src as *const u8, tmp.as_mut_ptr() as *mut u8, mem::size_of::<T>());
    |                                               ^^^^^^^^^^^^^^^^
    |
    = help: add `#![feature(const_maybe_uninit_as_mut_ptr)]` to the crate attributes to enable

error: could not compile `core` due to 6 previous errors

As such I'll be leaving the attribute in.

Copy link
Member

@RalfJung RalfJung Nov 28, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was asking about the const_maybe_uninit_assume_init, not the const_maybe_uninit_as_mut_ptr. (Note the line this comment is attached to.)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that probably should have been explicitly stated 🙂 I'll check when I'm on my laptop and send a follow-up if necessary.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removing that line results in an error on line 496 of the same file (inside MaybeUninit::write). That's for the assume_init_mut method.

So...I think this means that const stability isn't currently subject to the same checks, in that a feature can be both simultaneously stable and unstable? If that's the case we should fix that.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I see, makes sense. I opened an issue for this: #91357

#![feature(const_num_from_num)]
#![feature(const_ops)]
Expand Down
11 changes: 6 additions & 5 deletions library/core/src/mem/maybe_uninit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,7 @@ impl<T> MaybeUninit<T> {
/// (Notice that the rules around references to uninitialized data are not finalized yet, but
/// until they are, it is advisable to avoid them.)
#[stable(feature = "maybe_uninit", since = "1.36.0")]
#[rustc_const_unstable(feature = "const_maybe_uninit_as_ptr", issue = "75251")]
#[rustc_const_stable(feature = "const_maybe_uninit_as_ptr", since = "1.59.0")]
#[inline(always)]
pub const fn as_ptr(&self) -> *const T {
// `MaybeUninit` and `ManuallyDrop` are both `repr(transparent)` so we can cast the pointer.
Expand Down Expand Up @@ -567,7 +567,7 @@ impl<T> MaybeUninit<T> {
/// (Notice that the rules around references to uninitialized data are not finalized yet, but
/// until they are, it is advisable to avoid them.)
#[stable(feature = "maybe_uninit", since = "1.36.0")]
#[rustc_const_unstable(feature = "const_maybe_uninit_as_ptr", issue = "75251")]
#[rustc_const_unstable(feature = "const_maybe_uninit_as_mut_ptr", issue = "75251")]
#[inline(always)]
pub const fn as_mut_ptr(&mut self) -> *mut T {
// `MaybeUninit` and `ManuallyDrop` are both `repr(transparent)` so we can cast the pointer.
Expand Down Expand Up @@ -620,7 +620,7 @@ impl<T> MaybeUninit<T> {
/// // `x` had not been initialized yet, so this last line caused undefined behavior. ⚠️
/// ```
#[stable(feature = "maybe_uninit", since = "1.36.0")]
#[rustc_const_unstable(feature = "const_maybe_uninit_assume_init", issue = "none")]
#[rustc_const_stable(feature = "const_maybe_uninit_assume_init", since = "1.59.0")]
#[inline(always)]
#[rustc_diagnostic_item = "assume_init"]
#[track_caller]
Expand Down Expand Up @@ -788,7 +788,8 @@ impl<T> MaybeUninit<T> {
/// }
/// ```
#[stable(feature = "maybe_uninit_ref", since = "1.55.0")]
#[rustc_const_unstable(feature = "const_maybe_uninit_assume_init", issue = "none")]
#[rustc_const_stable(feature = "const_maybe_uninit_assume_init", since = "1.59.0")]
#[cfg_attr(bootstrap, rustc_allow_const_fn_unstable(const_raw_ptr_deref))]
#[inline(always)]
pub const unsafe fn assume_init_ref(&self) -> &T {
// SAFETY: the caller must guarantee that `self` is initialized.
Expand Down Expand Up @@ -968,7 +969,7 @@ impl<T> MaybeUninit<T> {
///
/// [`assume_init_ref`]: MaybeUninit::assume_init_ref
#[unstable(feature = "maybe_uninit_slice", issue = "63569")]
#[rustc_const_unstable(feature = "const_maybe_uninit_assume_init", issue = "none")]
#[rustc_const_unstable(feature = "maybe_uninit_slice", issue = "63569")]
#[inline(always)]
pub const unsafe fn slice_assume_init_ref(slice: &[Self]) -> &[T] {
// SAFETY: casting slice to a `*const [T]` is safe since the caller guarantees that
Expand Down
1 change: 1 addition & 0 deletions library/core/tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#![feature(const_assume)]
#![feature(const_cell_into_inner)]
#![feature(const_convert)]
#![feature(const_maybe_uninit_as_mut_ptr)]
#![feature(const_maybe_uninit_assume_init)]
#![feature(const_ptr_read)]
#![feature(const_ptr_write)]
Expand Down
32 changes: 32 additions & 0 deletions library/core/tests/mem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,3 +269,35 @@ fn uninit_const_assume_init_read() {
const FOO: u32 = unsafe { MaybeUninit::new(42).assume_init_read() };
assert_eq!(FOO, 42);
}

#[test]
fn const_maybe_uninit() {
use std::ptr;

#[derive(Debug, PartialEq)]
struct Foo {
x: u8,
y: u8,
}

const FIELD_BY_FIELD: Foo = unsafe {
let mut val = MaybeUninit::uninit();
init_y(&mut val); // order shouldn't matter
init_x(&mut val);
val.assume_init()
};

const fn init_x(foo: &mut MaybeUninit<Foo>) {
unsafe {
*ptr::addr_of_mut!((*foo.as_mut_ptr()).x) = 1;
}
}

const fn init_y(foo: &mut MaybeUninit<Foo>) {
unsafe {
*ptr::addr_of_mut!((*foo.as_mut_ptr()).y) = 2;
}
}

assert_eq!(FIELD_BY_FIELD, Foo { x: 1, y: 2 });
}