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 ptr_from_ref #106116

Closed
3 tasks done
RalfJung opened this issue Dec 24, 2022 · 13 comments
Closed
3 tasks done

Tracking Issue for ptr_from_ref #106116

RalfJung opened this issue Dec 24, 2022 · 13 comments
Labels
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.

Comments

@RalfJung
Copy link
Member

RalfJung commented Dec 24, 2022

Feature gate: #![feature(ptr_from_ref)]

This is a tracking issue for ptr::from_{ref,mut}: methods to turn references into raw pointers without using as casts. We have methods to avoid almost all as casts around raw pointer handling, except for the initial cast from reference to raw pointer. These new methods close that gap. They also ensure that the pointee type T remains the same, without one having to repeat that type for the cast.

Public API

pub fn from_ref<T: ?Sized>(r: &T) -> *const T;
pub fn from_mut<T: ?Sized>(r: &mut T) -> *mut T;

Steps / History

Unresolved Questions

  • None yet.

Footnotes

  1. https://std-dev-guide.rust-lang.org/feature-lifecycle/stabilization.html

@RalfJung RalfJung added T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. C-tracking-issue Category: A tracking issue for an RFC or an unstable feature. labels Dec 24, 2022
@RalfJung RalfJung changed the title Tracking Issue for XXX Tracking Issue for ptr_from_ref Dec 24, 2022
bors added a commit to rust-lang-ci/rust that referenced this issue Dec 24, 2022
add ptr::from_{ref,mut}

We have methods to avoid almost all `as` casts around raw pointer handling, except for the initial cast from reference to raw pointer. These new methods close that gap.

(I also moved `null_mut` next to `null` to keep the file consistently organized.)

r? libs-api

Tracking issue: rust-lang#106116
@SoniEx2
Copy link
Contributor

SoniEx2 commented Dec 24, 2022

for context of where these may be relevant, see also the discussion in rust-lang/book#3305

RalfJung pushed a commit to RalfJung/miri that referenced this issue Dec 28, 2022
add ptr::from_{ref,mut}

We have methods to avoid almost all `as` casts around raw pointer handling, except for the initial cast from reference to raw pointer. These new methods close that gap.

(I also moved `null_mut` next to `null` to keep the file consistently organized.)

r? libs-api

Tracking issue: rust-lang/rust#106116
@SUPERCILEX
Copy link
Contributor

Filed an issue for a clippy lint: rust-lang/rust-clippy#10130

@scottmcm
Copy link
Member

Pondering based on https://users.rust-lang.org/t/best-way-to-cast-option-t-to-const-t/88402?u=scottmcm: Should this perhaps take Option<&T>?

There's NonNull::from that takes &T...

@SUPERCILEX
Copy link
Contributor

Is there a way to make it accept an Into<Option<&T>>? Otherwise I feel like that will make these methods unpleasant to use in most cases.

@QuineDot
Copy link

You would need a way to create wide null pointers to convert from a generic Option<&T>. core::ptr::null doesn't do that yet, and due to RFC 3324 a valid vtable will be required for dyn pointers.

Probably that's desired functionality for null() anyway, but I'm not sure how it could be done in the wider context of custom DSTs. Perhaps

pub const fn null<T>() -> *const T
where
    T: Pointee + ?Sized,
    <T as Pointee>::Metadata: MetadataForNull,

for some sealed trait the compiler automatically implements for all DynMetadata<dyn Trait> (and an implementation for (), and maybe usize).

Or a new public trait Null that the compiler automatically implements for all dyn Trait (with corresponding blanket implementations for T: Thin).

Alternatively, the safety invariant for *const dyn Trait could be relaxed when the data pointer is Null I guess. Whatever upcasting does under the hood would have to check for Null.

@RalfJung
Copy link
Member Author

Pondering based on https://users.rust-lang.org/t/best-way-to-cast-option-t-to-const-t/88402?u=scottmcm: Should this perhaps take Option<&T>?

I usually think that having as_ref return an Option is a mistake, so this seems to be like just making the symmetric mistake here. It would be consistent, but I'd rather us find a way to have a version of as_ref that returns &T.

I think methods that treat null specially should be working on Option<NonNull<T>>, not *mut T.

@Raekye
Copy link
Contributor

Raekye commented Mar 9, 2023

It seems to me that these functions could trivially be made const. Is there a reason they're not/what would it take to have them changed, since they're not stable yet?

@scottmcm
Copy link
Member

scottmcm commented Mar 9, 2023

@Raekye Seems entirely reasonable; send a PR!

matthiaskrgr pushed a commit to matthiaskrgr/rust that referenced this issue Mar 10, 2023
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Mar 10, 2023
bors added a commit to rust-lang-ci/rust that referenced this issue Mar 10, 2023
…iaskrgr

Rollup of 9 pull requests

Successful merges:

 - rust-lang#108879 (Unconstrained terms should account for infer vars being equated)
 - rust-lang#108936 (Rustdoc: don't hide anonymous reexport)
 - rust-lang#108940 (Add myself to compiler reviewers list)
 - rust-lang#108945 (Make some report and emit errors take DefIds instead of BodyIds)
 - rust-lang#108946 (Document the resulting values produced when using `From<bool>` on floats)
 - rust-lang#108956 (Make ptr::from_ref and ptr::from_mut in rust-lang#106116 const.)
 - rust-lang#108960 (Remove `body_def_id` from `Inherited`)
 - rust-lang#108963 (only call git on git checkouts during bootstrap)
 - rust-lang#108964 (Fix the docs for pointer method with_metadata_of)

Failed merges:

 - rust-lang#108950 (Directly construct Inherited in typeck.)

r? `@ghost`
`@rustbot` modify labels: rollup
thomcc pushed a commit to tcdi/postgrestd that referenced this issue May 31, 2023
add ptr::from_{ref,mut}

We have methods to avoid almost all `as` casts around raw pointer handling, except for the initial cast from reference to raw pointer. These new methods close that gap.

(I also moved `null_mut` next to `null` to keep the file consistently organized.)

r? libs-api

Tracking issue: rust-lang/rust#106116
thomcc pushed a commit to tcdi/postgrestd that referenced this issue Jun 1, 2023
Make ptr::from_ref and ptr::from_mut in #106116 const.

As per rust-lang/rust#106116 (comment)
@scottmcm
Copy link
Member

Pondering: &mut T*const T will compile with both from_ref or from_mut, given that there are coercions available on both sides.

Is there anything that we should do with these to help people from accidentally using from_ref when they should have used from_mut to keep the permission to cast back to *mut and mutate later?

That seems like the kind of mistake that you could make but not notice for a long time if you're just running normal binaries rather than MIRI.

@RalfJung
Copy link
Member Author

We could have a lint for that situation (any time a mutable ref is passed to from_ref, basically).

@WaffleLapkin
Copy link
Member

What's the status here? Is this just waiting for someone to start stabilization process? Do we need more lints? Is something else missing?

@RalfJung
Copy link
Member Author

RalfJung commented Nov 7, 2023

Is this just waiting for someone to start stabilization process?

I think that.
Though having a lint, at least in clippy, is nice -- I don't think it is blocking.

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Dec 15, 2023
…dtolnay

Stabilize `ptr::{from_ref, from_mut}`

I propose to stabilize the following APIs:

```rust
// mod core::ptr

pub const fn from_ref<T: ?Sized>(r: &T) -> *const T;
pub const fn from_mut<T: ?Sized>(r: &mut T) -> *mut T;
```

Tracking issue: rust-lang#106116

---

`@RalfJung` what do you think we should do with `from_mut`? Stabilize it as const, given that you can't call it anyway (no way to get `&mut` in `const fn`)? Defer stabilizing it as const leaving the same issue/feature? Change issue/feature? Change issue/feature to the "`&mut` in const fn" one?
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Dec 15, 2023
…dtolnay

Stabilize `ptr::{from_ref, from_mut}`

I propose to stabilize the following APIs:

```rust
// mod core::ptr

pub const fn from_ref<T: ?Sized>(r: &T) -> *const T;
pub const fn from_mut<T: ?Sized>(r: &mut T) -> *mut T;
```

Tracking issue: rust-lang#106116

---

``@RalfJung`` what do you think we should do with `from_mut`? Stabilize it as const, given that you can't call it anyway (no way to get `&mut` in `const fn`)? Defer stabilizing it as const leaving the same issue/feature? Change issue/feature? Change issue/feature to the "`&mut` in const fn" one?
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Dec 15, 2023
Rollup merge of rust-lang#117824 - WaffleLapkin:ptr_from_ref_stab, r=dtolnay

Stabilize `ptr::{from_ref, from_mut}`

I propose to stabilize the following APIs:

```rust
// mod core::ptr

pub const fn from_ref<T: ?Sized>(r: &T) -> *const T;
pub const fn from_mut<T: ?Sized>(r: &mut T) -> *mut T;
```

Tracking issue: rust-lang#106116

---

``@RalfJung`` what do you think we should do with `from_mut`? Stabilize it as const, given that you can't call it anyway (no way to get `&mut` in `const fn`)? Defer stabilizing it as const leaving the same issue/feature? Change issue/feature? Change issue/feature to the "`&mut` in const fn" one?
@jhpratt
Copy link
Member

jhpratt commented Feb 7, 2024

As #117824 has been merged, this should be closed.

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. 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

8 participants