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

Tracking Issue for const_ptr_as_ref #91822

Open
1 of 3 tasks
lilasta opened this issue Dec 12, 2021 · 6 comments
Open
1 of 3 tasks

Tracking Issue for const_ptr_as_ref #91822

lilasta opened this issue Dec 12, 2021 · 6 comments
Labels
C-tracking-issue Category: A tracking issue for an RFC or an unstable feature. disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Comments

@lilasta
Copy link
Contributor

lilasta commented Dec 12, 2021

Feature gate: #![feature(const_ptr_as_ref)]

Public API

impl<T: ?Sized> NonNull<T> {
    pub const unsafe fn as_mut<'a>(&mut self) -> &'a mut T;
}

Steps / History

Unresolved Questions

  • None yet.
@lilasta lilasta added C-tracking-issue Category: A tracking issue for an RFC or an unstable feature. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. labels Dec 12, 2021
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Dec 21, 2021
Make `PTR::as_ref` and similar methods `const`.

Tracking issue: rust-lang#91822
Feature gate: `#![feature(const_ptr_as_ref)]`

```rust
// core::ptr
impl<T: ?Sized> *const T {
    pub const unsafe fn as_ref<'a>(self) -> Option<&'a T>;
    pub const unsafe fn as_uninit_ref<'a>(self) -> Option<&'a MaybeUninit<T>>
    where
        T: Sized;
    pub const unsafe fn as_uninit_slice<'a>(self) -> Option<&'a [MaybeUninit<T>]>;
}

impl<T: ?Sized> *mut T {
    pub const unsafe fn as_ref<'a>(self) -> Option<&'a T>;
    pub const unsafe fn as_uninit_ref<'a>(self) -> Option<&'a MaybeUninit<T>>
    where
        T: Sized;
    pub const unsafe fn as_mut<'a>(self) -> Option<&'a mut T>;
    pub const unsafe fn as_uninit_mut<'a>(self) -> Option<&'a mut MaybeUninit<T>>
    where
        T: Sized;
    pub const unsafe fn as_uninit_slice<'a>(self) -> Option<&'a [MaybeUninit<T>]>;
    pub const unsafe fn as_uninit_slice_mut<'a>(self) -> Option<&'a mut [MaybeUninit<T>]>;
}

impl<T: Sized> NonNull<T> {
    pub const unsafe fn as_uninit_ref<'a>(&self) -> &'a MaybeUninit<T>;
    pub const unsafe fn as_uninit_mut<'a>(&mut self) -> &'a mut MaybeUninit<T>;
}

impl<T: ?Sized> NonNull<T> {
    pub const unsafe fn as_ref<'a>(&self) -> &'a T;
    pub const unsafe fn as_mut<'a>(&mut self) -> &'a mut T;
    pub const unsafe fn as_uninit_slice<'a>(&self) -> &'a [MaybeUninit<T>];
    pub const unsafe fn as_uninit_slice_mut<'a>(&self) -> &'a mut [MaybeUninit<T>];
}
```
Dylan-DPC added a commit to Dylan-DPC/rust that referenced this issue Jul 30, 2023
`const`-stablilize `NonNull::as_ref`

A bunch of pointer to reference methods have been made unstably const some time ago in rust-lang#91823 under the feature gate `const_ptr_as_ref`.
Out of these, `NonNull::as_ref` can be implemented as a `const fn` in stable rust today, so i hereby propose to const stabilize this function only.

Tracking issue: rust-lang#91822

`@rustbot` label +T-libs-api -T-libs
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Jul 30, 2023
`const`-stablilize `NonNull::as_ref`

A bunch of pointer to reference methods have been made unstably const some time ago in rust-lang#91823 under the feature gate `const_ptr_as_ref`.
Out of these, `NonNull::as_ref` can be implemented as a `const fn` in stable rust today, so i hereby propose to const stabilize this function only.

Tracking issue: rust-lang#91822

``@rustbot`` label +T-libs-api -T-libs
workingjubilee added a commit to workingjubilee/rustc that referenced this issue May 3, 2024
… r=workingjubilee

Implement ptr_as_ref_unchecked

Implementation of rust-lang#122034.

Prefixed the feature name with `ptr_` for clarity.

Linked const-unstability to rust-lang#91822, so the post there should probably be updated to mentions the 3 new methods when/if this PR is merged.
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue May 3, 2024
… r=workingjubilee

Implement ptr_as_ref_unchecked

Implementation of rust-lang#122034.

Prefixed the feature name with `ptr_` for clarity.

Linked const-unstability to rust-lang#91822, so the post there should probably be updated to mentions the 3 new methods when/if this PR is merged.
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue May 3, 2024
… r=workingjubilee

Implement ptr_as_ref_unchecked

Implementation of rust-lang#122034.

Prefixed the feature name with `ptr_` for clarity.

Linked const-unstability to rust-lang#91822, so the post there should probably be updated to mentions the 3 new methods when/if this PR is merged.
rust-timer added a commit to rust-lang-ci/rust that referenced this issue May 3, 2024
Rollup merge of rust-lang#122492 - GrigorenkoPV:ptr_as_ref_unchecked, r=workingjubilee

Implement ptr_as_ref_unchecked

Implementation of rust-lang#122034.

Prefixed the feature name with `ptr_` for clarity.

Linked const-unstability to rust-lang#91822, so the post there should probably be updated to mentions the 3 new methods when/if this PR is merged.
@GrigorenkoPV
Copy link
Contributor

Hello!

Now that #122492 has been merged, 3 new methods are also const-gated behind this flag

  • <*const T>::as_ref_unchecked
  • <*mut T>::as_ref_unchecked
  • <*mut T>::as_mut_unchecked

Should they be added to the OP?

@lilasta
Copy link
Contributor Author

lilasta commented May 4, 2024

I have added them to the list.

@RalfJung
Copy link
Member

RalfJung commented Sep 8, 2024

@rust-lang/libs-api I propose we const-stabilize the subset of the methods above that are stable for non-const calls:

// core::ptr
impl<T: ?Sized> *const T {
    pub const unsafe fn as_ref<'a>(self) -> Option<&'a T>;
}

impl<T: ?Sized> *mut T {
    pub const unsafe fn as_ref<'a>(self) -> Option<&'a T>;
    pub const unsafe fn as_mut<'a>(self) -> Option<&'a mut T>;
}

impl<T: ?Sized> NonNull<T> {
    pub const unsafe fn as_mut<'a>(&mut self) -> &'a mut T;
}

The rest should be moved to the feature gate tracking their general stability, no separate const-feature-gate is needed. (EDIT: that is happening in #130164)

@RalfJung RalfJung added the I-libs-api-nominated Nominated for discussion during a libs-api team meeting. label Sep 8, 2024
@RalfJung
Copy link
Member

RalfJung commented Sep 8, 2024

Ah wait, the Option-returning ones are non-trivial since they require the is_null test... they should be moved under the const_ptr_is_null feature gate (#74939).

So then only one function is left to be stabilized here: NonNull::as_mut.

@dtolnay
Copy link
Member

dtolnay commented Sep 9, 2024

@rfcbot fcp merge

Subsequent to #129195, we are ready to stabilize const on NonNull::as_mut:

pub const unsafe fn as_mut<'a>(&mut self) -> &'a mut T {
// SAFETY: the caller must guarantee that `self` meets all the
// requirements for a mutable reference.
unsafe { &mut *self.as_ptr() }
}

This function is the mut analog of NonNull::as_ref for which we stabilized const in #102198.

pub const unsafe fn as_ref<'a>(&self) -> &'a T {
// SAFETY: the caller must guarantee that `self` meets all the
// requirements for a reference.
// `cast_const` avoids a mutable raw pointer deref.
unsafe { &*self.as_ptr().cast_const() }
}

@rfcbot
Copy link

rfcbot commented Sep 9, 2024

Team member @dtolnay has proposed to merge this. The next step is review by the rest of the tagged team members:

No concerns currently listed.

Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up!

See this document for info about what commands tagged team members can give me.

@rfcbot rfcbot added proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. labels Sep 9, 2024
@dtolnay dtolnay removed the I-libs-api-nominated Nominated for discussion during a libs-api team meeting. label Sep 9, 2024
workingjubilee added a commit to workingjubilee/rustc that referenced this issue Sep 10, 2024
move some const fn out of the const_ptr_as_ref feature

When a `const fn` is still `#[unstable]`, it should generally use the same feature to track its regular stability and const-stability. Then when that feature moves towards stabilization we can decide whether the const-ness can be stabilized as well, or whether it should be moved into a new feature.

Also, functions like `ptr::as_ref` (which returns an `Option<&mut T>`) require `is_null`, which is tricky and blocked on some design concerns (see rust-lang#74939). So move those to the is_null feature gate, as they should be stabilized together with `ptr.is_null()`.

Affects rust-lang#91822, rust-lang#122034, rust-lang#75402, rust-lang#74939
workingjubilee added a commit to workingjubilee/rustc that referenced this issue Sep 10, 2024
move some const fn out of the const_ptr_as_ref feature

When a `const fn` is still `#[unstable]`, it should generally use the same feature to track its regular stability and const-stability. Then when that feature moves towards stabilization we can decide whether the const-ness can be stabilized as well, or whether it should be moved into a new feature.

Also, functions like `ptr::as_ref` (which returns an `Option<&mut T>`) require `is_null`, which is tricky and blocked on some design concerns (see rust-lang#74939). So move those to the is_null feature gate, as they should be stabilized together with `ptr.is_null()`.

Affects rust-lang#91822, rust-lang#122034, rust-lang#75402, rust-lang#74939
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Sep 10, 2024
Rollup merge of rust-lang#130164 - RalfJung:const_ptr_as_ref, r=dtolnay

move some const fn out of the const_ptr_as_ref feature

When a `const fn` is still `#[unstable]`, it should generally use the same feature to track its regular stability and const-stability. Then when that feature moves towards stabilization we can decide whether the const-ness can be stabilized as well, or whether it should be moved into a new feature.

Also, functions like `ptr::as_ref` (which returns an `Option<&mut T>`) require `is_null`, which is tricky and blocked on some design concerns (see rust-lang#74939). So move those to the is_null feature gate, as they should be stabilized together with `ptr.is_null()`.

Affects rust-lang#91822, rust-lang#122034, rust-lang#75402, rust-lang#74939
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-tracking-issue Category: A tracking issue for an RFC or an unstable feature. disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

5 participants