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 ptr::{from_ref, from_mut} #117824

Merged
merged 1 commit into from
Dec 15, 2023

Conversation

WaffleLapkin
Copy link
Member

I propose to stabilize the following APIs:

// 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: #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?

@rustbot
Copy link
Collaborator

rustbot commented Nov 12, 2023

r? @Mark-Simulacrum

(rustbot has picked a reviewer for you, use r? to override)

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Nov 12, 2023
@WaffleLapkin WaffleLapkin added T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. and removed T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Nov 12, 2023
@rust-log-analyzer

This comment has been minimized.

@RalfJung
Copy link
Member

RalfJung commented Nov 12, 2023

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?

Given that one can already transmute &mut T to *mut T in const fn, I see no issue with stabilizing this.
Cc @rust-lang/wg-const-eval

@rust-log-analyzer

This comment has been minimized.

@Mark-Simulacrum
Copy link
Member

r? dtolnay

@rustbot rustbot assigned dtolnay and unassigned Mark-Simulacrum Nov 18, 2023
@dtolnay dtolnay added the needs-fcp This change is insta-stable, so needs a completed FCP to proceed. label Nov 18, 2023
@dtolnay
Copy link
Member

dtolnay commented Nov 18, 2023

@rust-lang/libs-api:
@rfcbot fcp merge

Notes from the tracking issue:

#106116 (comment) is worth considering: currently you can take a &mut T and use ptr::from_mut to cast to *mut T then coerce to *const T, or you can take &mut T and coerce to &T followed by ptr::from_ref to cast to *const T. These are easy to confuse and appear to accomplish the same thing, but they do not. One of them produces a pointer that is okay to write to (after casting back to *mut) while the other produces a pointer that is UB to write to.

use core::ptr;

fn main() {
    let m: &mut i32 = &mut 0;
    
    let p1: *const i32 = ptr::from_ref(m);
    let p2: *const i32 = ptr::from_mut(m);
}

#106116 (comment) regarding fn from_ptr(Option<&T>) -> *const T — I think it's fine for callers to express this as opt_reference.map_or_else(ptr::null, ptr::from_ref).

@rfcbot
Copy link

rfcbot commented Nov 18, 2023

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 Nov 18, 2023
@rfcbot rfcbot added final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. and removed proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. labels Dec 5, 2023
@rfcbot
Copy link

rfcbot commented Dec 5, 2023

🔔 This is now entering its final comment period, as per the review above. 🔔

@rfcbot rfcbot added finished-final-comment-period The final comment period is finished for this PR / Issue. to-announce Announce this issue on triage meeting and removed final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. labels Dec 15, 2023
@rfcbot
Copy link

rfcbot commented Dec 15, 2023

The final comment period, with a disposition to merge, as per the review above, is now complete.

As the automated representative of the governance process, I would like to thank the author for their work and everyone else who contributed.

This will be merged soon.

Copy link
Member

@dtolnay dtolnay left a comment

Choose a reason for hiding this comment

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

Thanks!

@dtolnay
Copy link
Member

dtolnay commented Dec 15, 2023

@dtolnay
Copy link
Member

dtolnay commented Dec 15, 2023

@bors r+

@bors
Copy link
Contributor

bors commented Dec 15, 2023

📌 Commit b863e9b has been approved by dtolnay

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Dec 15, 2023
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request 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?
bors added a commit to rust-lang-ci/rust that referenced this pull request Dec 15, 2023
…iaskrgr

Rollup of 7 pull requests

Successful merges:

 - rust-lang#117824 (Stabilize `ptr::{from_ref, from_mut}`)
 - rust-lang#118234 (Stabilize `type_name_of_val`)
 - rust-lang#118944 (Move type relations into submodule `relate` in rustc_infer, and notify when it has changed)
 - rust-lang#118977 (Simplify `src-script.js` code)
 - rust-lang#118985 (Remove `@JohnTitor` from diagnostics pings)
 - rust-lang#118986 (Simplify JS code a little bit)
 - rust-lang#118988 (rustdoc: add regression test for JS data file loading)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 8ad8f29 into rust-lang:master Dec 15, 2023
11 checks passed
@rustbot rustbot added this to the 1.76.0 milestone Dec 15, 2023
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request 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?
@WaffleLapkin WaffleLapkin deleted the ptr_from_ref_stab branch December 17, 2023 02:28
@apiraino apiraino removed the to-announce Announce this issue on triage meeting label Dec 28, 2023
@jhpratt jhpratt mentioned this pull request Feb 7, 2024
3 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. finished-final-comment-period The final comment period is finished for this PR / Issue. needs-fcp This change is insta-stable, so needs a completed FCP to proceed. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants