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 Box::into_raw_non_null #47336

Closed
SimonSapin opened this issue Jan 10, 2018 · 25 comments
Closed

Tracking issue for Box::into_raw_non_null #47336

SimonSapin opened this issue Jan 10, 2018 · 25 comments
Labels
B-unstable Blocker: Implemented in the nightly compiler and unstable. C-tracking-issue Category: A tracking issue for an RFC or an unstable feature. disposition-close This PR / issue is in PFCP or FCP with a disposition to close it. finished-final-comment-period The final comment period is finished for this PR / Issue. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Comments

@SimonSapin
Copy link
Contributor

This API was introduced in #46952:

impl<T: ?Sized> Box<T> {
    pub fn into_raw_non_null(b: Box<T>) -> NonNull<T> { /* … */ }
}

It is intended as an eventually-stable replacement for Box::into_unique, since there is no plan to stabilize Unique<T> at the moment.

See also discussion at #27730.

@Mark-Simulacrum Mark-Simulacrum 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 Jan 10, 2018
@alexcrichton alexcrichton added the B-unstable Blocker: Implemented in the nightly compiler and unstable. label Jan 11, 2018
SimonSapin added a commit to SimonSapin/rust that referenced this issue Jan 20, 2018
@SimonSapin
Copy link
Contributor Author

It would be nice for this method to be an impl of the From trait instead, but coherence does not allow it since ptr::NonNull and Box are defined in separate crates. Having an Into impl without the corresponding From impl was frowned upon in the PR discussion.

This method can be replaced with unsafe { NonNull::new_unchecked(Box::into_raw(b)) }. This unsafe block is sound because Box is never null. Having a dedicated API removes the need for that unsafe block.

So while we can live without it, I’m still in favor of stabilizing this. It’s small and won’t be a maintenance burden.

@nvzqz
Copy link
Contributor

nvzqz commented Apr 9, 2018

I'm in favor of Box::into_non_null since we have Box::into_unique. Feels more consistent that way.

@SimonSapin
Copy link
Contributor Author

@nvzqz Box::into_unique is unstable though, and I don’t know if it (or Unique) well even be stabilized.

@nvzqz
Copy link
Contributor

nvzqz commented Apr 10, 2018

I'm acutely aware it is. If we choose to stabilize both, then their names should be considered against each other regarding convention.

@mtak-
Copy link
Contributor

mtak- commented Apr 6, 2019

Any movement on this? A cursory search of github for "new_unchecked into_raw" shows examples of the unsafe { NonNull::new_unchecked(Box::into_raw(b)) } pattern.

@luojia65
Copy link
Contributor

luojia65 commented Dec 5, 2019

Any further movements? This could be okay in my projects.

@zakarumych
Copy link
Contributor

This method would be more ergonomic but isn't

unsafe { NonNull::new_unchecked(Box::into_raw(b)) }

without unsafe block is possible today as

NonNull::from(Box::leak(b))

@SimonSapin
Copy link
Contributor Author

Oooh that’s is a very interesting one, thanks @omni-viral. Box::leak even already documents Box::from_raw as a valid way to drop (or presumably recover) a box that has previously been leaked.

I think I’d be happy with:

  • Deprecate into_raw_non_null, with a warning message that says to use NonNull::from(Box::leak(b)) instead
  • Change all internal uses to do so
  • After some time, remove this deprecated unstable method.

How does this sound?

@rfcbot fcp close

@rfcbot
Copy link

rfcbot commented Feb 15, 2020

Team member @SimonSapin has proposed to close 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-close This PR / issue is in PFCP or FCP with a disposition to close it. labels Feb 15, 2020
@dtolnay
Copy link
Member

dtolnay commented Feb 15, 2020

The coherence comment in #47336 (comment) is outdated as of rust 1.41. How about adding the From impl now?

@SimonSapin
Copy link
Contributor Author

Good point, but I’m reconsidering my previous statement that this would be ideal. Unlike e.g. From<&mut T> for NonNull<T>, this conversion gives away ownership of a Box, which would otherwise automatically deallocate when dropped, to a pointer that doesn’t do any ownership tracking and is Copy. So I’m a bit hesitant about enabling it in generic contexts.

Maybe it’s fine, though?

@dtolnay
Copy link
Member

dtolnay commented Feb 17, 2020

Fair enough. I do like NonNull::from(Box::leak(b)) over NonNull::from(b) but I wish there were a way it could be more discoverable.

@Lokathor
Copy link
Contributor

Having it just be longer is definitely worse.

@Mark-Simulacrum
Copy link
Member

I would personally disagree that the length is an issue here, and AFAICT, we can always add the shorter one later, right?

We could also plausibly call this NonNull::leak(Box<T>), right? I'm not sure about the generic being useful here, do we have examples of code with a desire for an Into/From bound with NonNull?

@SimonSapin
Copy link
Contributor Author

Not really, I was initially tempted to use From mostly to avoid having to come up with an awkward name like into_raw_non_null

@SimonSapin
Copy link
Contributor Author

SimonSapin commented Apr 15, 2020

I’ve removed KodrAus’ checkbox, per rust-lang/team@45013d1.

Edit: that didn’t have the expected effect. rfcbot added it back. I’ve checked it instead.

@rfcbot rfcbot added the final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. label Apr 15, 2020
@rfcbot
Copy link

rfcbot commented Apr 15, 2020

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

@SimonSapin
Copy link
Contributor Author

PR #57934 has added Rc::into_raw_non_null and Arc::into_raw_non_null, reusing this tracking issue. But nobody involved commented here or updated the issue description, so the proposed plan at #47336 (comment) does not account for them.

Unlike Box, Rc and Arc do not have a leak method that could be combined with NonNull::from.

I’m inclined to deprecate anyway, even though the replacement unsafe { NonNull::new_unchecked(Rc::into_raw(x) as *mut _) } requires unsafe code. These methods seem very niche, and the implementation PR does not discuss any motivation for adding them.

@dwijnand, given the rest of this thread, what do you think?

@dwijnand
Copy link
Member

the implementation PR does not discuss any motivation for adding them

The previous formulation did (#56998):

/cc @withoutboats who mentioned to me this might be worth adding to the standard library, in withoutboats/smart#4

@dwijnand, given the rest of this thread, what do you think?

I trust your call if you think it's too niche to be worth keeping.

bors-servo added a commit to servo/servo that referenced this issue Apr 15, 2020
Remove use of soon-to-be-deprecated unstable feature

rust-lang/rust#47336 (comment)
bors-servo added a commit to servo/servo that referenced this issue Apr 15, 2020
Remove use of soon-to-be-deprecated unstable feature

rust-lang/rust#47336 (comment)
bors-servo added a commit to servo/servo that referenced this issue Apr 15, 2020
Remove use of soon-to-be-deprecated unstable feature

rust-lang/rust#47336 (comment)
bors-servo added a commit to servo/servo that referenced this issue Apr 15, 2020
Remove use of soon-to-be-deprecated unstable feature

rust-lang/rust#47336 (comment)
bors-servo added a commit to servo/servo that referenced this issue Apr 16, 2020
Remove use of soon-to-be-deprecated unstable feature

rust-lang/rust#47336 (comment)
bors-servo added a commit to servo/servo that referenced this issue Apr 16, 2020
Remove use of soon-to-be-deprecated unstable feature

rust-lang/rust#47336 (comment)
bors-servo added a commit to servo/servo that referenced this issue Apr 16, 2020
Remove use of soon-to-be-deprecated unstable feature

rust-lang/rust#47336 (comment)
@SimonSapin
Copy link
Contributor Author

@rust-lang/libs Any thoughts on Rc::into_raw_non_null and Arc::into_raw_non_null?

@maxeonyx
Copy link

Have just come across this function, while reading the source for Rc. My 2 cents: At some point(s) after making a NonNull<T>, I am going to have to use an unsafe block to dereference it. I am OK with using one more unsafe block (unsafe { NonNull::new_unchecked(Box::into_raw(b)) }) to create it, especially since I can see that it is sound (as stated above). I also think it's niche.

@Kixunil
Copy link
Contributor

Kixunil commented Apr 23, 2020

Would it make sense to add leak to Rc and Arc? This is useful in some exotic cases. E.g. sending such pointer over a pipe. (I've seen such thing in some C++ codebase.)

@rfcbot rfcbot added finished-final-comment-period The final comment period is finished for this PR / Issue. and removed final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. labels Apr 25, 2020
@rfcbot
Copy link

rfcbot commented Apr 25, 2020

The final comment period, with a disposition to close, 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.

Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this issue Apr 25, 2020
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this issue Apr 25, 2020
…nieu

Deprecate `{Box,Rc,Arc}::into_raw_non_null`

Per ongoing FCP at rust-lang#47336 (comment)
See also rust-lang#47336 (comment)
@KodrAus
Copy link
Contributor

KodrAus commented Jul 29, 2020

Since we've shipped the deprecation of these methods I'll go ahead and close this tracking issue now, but please re-open if we'd like to keep it active until the unstable methods have been removed entirely!

@SimonSapin
Copy link
Contributor Author

I think we should keep tracking issues open as long as there are stable items pointing to them in the tree. But this case can be resolved quickly, I filed a removal PR: #74902

Manishearth added a commit to Manishearth/rust that referenced this issue Jul 29, 2020
Remove deprecated unstable `{Box,Rc,Arc}::into_raw_non_null` functions

FCP: rust-lang#47336 (comment)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
B-unstable Blocker: Implemented in the nightly compiler and unstable. C-tracking-issue Category: A tracking issue for an RFC or an unstable feature. disposition-close This PR / issue is in PFCP or FCP with a disposition to close it. finished-final-comment-period The final comment period is finished for this PR / Issue. 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