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

Document return value of zero-size/zero-heap pointer types. #39757

Closed
wants to merge 1 commit into from

Conversation

frewsxcv
Copy link
Member

Fixes #39625.

@rust-highfive
Copy link
Collaborator

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @aturon (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

Please see the contribution instructions for more information.

@frewsxcv
Copy link
Member Author

Not too satisfied with the term 'meaningless' in my changes, so please let me know if you know a better way of wording this.

@frewsxcv frewsxcv added the A-docs Area: documentation for any part of the project, including the compiler, standard library, and tools label Feb 12, 2017
@@ -282,6 +282,9 @@ impl<T> Arc<T> {
/// To avoid a memory leak the pointer must be converted back to an `Arc` using
/// [`Arc::from_raw`][from_raw].
///
/// If `T` is zero-sized (e.g. `Arc<()>`), the returned pointer address
/// will be meaningless.
Copy link
Member Author

Choose a reason for hiding this comment

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

Nit for myself: I can probably just s/will be meaningless/is meaningless/.

Copy link
Member

Choose a reason for hiding this comment

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

I think for Rc and Arc this is wrong, because from_raw exists and there's the two refcounts, not just the ZST.

Copy link
Contributor

Choose a reason for hiding this comment

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

@eddyb can you elaborate on what you mean here? This method returns a pointer to the contents, right?

But it does seem weird to simultaneously say that the return value is meaningless, and then have from_raw say that the result must have been generated by into_raw.

I guess I'm not sure what meaningless means, really.

Copy link
Member

Choose a reason for hiding this comment

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

The value is one-byte-past-the-refcounts and can be used to get back an Rc, and you can distinguish between them, so definitely not meaningless (whereas an &ZST by itself is useless for anything short of a proof that there is a value of type ZST somewhere).

@alexcrichton
Copy link
Member

I wonder if we could explore why we need to clarify this? All of these returned pointers are valid for 0 bytes, so it seems like that's what we should document rather than explicitly calling out the "empty cases" for all situations here (if at all). It also seems to me that if a pointer is valid for 0 bytes it doesn't really matter what it ends up being in practice because you still can't read it.

@nagisa
Copy link
Member

nagisa commented Feb 12, 2017

It is not meaningless, either. You must use the same pointer returned by these functions to reconstruct the smart pointers in order to have the correct behaviour when these smart pointers are destructed.

@ollie27
Copy link
Member

ollie27 commented Feb 12, 2017

I think the important thing to emphasize is that the pointer is always non-null.

@@ -245,6 +245,9 @@ impl CString {

/// Transfers ownership of the string to a C caller.
///
/// If the `CString` is empty, the returned pointer address will be
/// meaningless.
Copy link
Member

Choose a reason for hiding this comment

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

For CString and CStr the pointer is definitely not meaningless as even if the string is empty it will still point to a single zero byte.

Copy link
Member Author

Choose a reason for hiding this comment

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

Good call. Reverted in the latest force push.

@frewsxcv
Copy link
Member Author

I wonder if we could explore why we need to clarify this?

Did you see the original issue? #39625

It is not meaningless, either. You must use the same pointer returned by these functions to reconstruct the smart pointers in order to have the correct behaviour when these smart pointers are destructed.

I read this a few times and still don't really understand what this means. Which smart pointers are you referring to here?

I don't feel strongly about these changes, so if people here think these changes are unnecessary, please go give feedback in the original issue and then we could possibly close that and this PR.

@aturon
Copy link
Member

aturon commented Feb 13, 2017

cc @rust-lang/compiler -- can someone take on the review here?

@nagisa
Copy link
Member

nagisa commented Feb 13, 2017

For posterity sake.

can someone take on the review here?

I’ll gladly look over, but this is one of those issues that also may span to T-lang and T-libs depending on how documentation gets worded.

@steveklabnik
Copy link
Member

This is tough, but I think @nagisa has good points.

@bors
Copy link
Contributor

bors commented Feb 16, 2017

☔ The latest upstream changes (presumably #39876) made this pull request unmergeable. Please resolve the merge conflicts.

@frewsxcv
Copy link
Member Author

Thanks everyone for the feedback. Considering the feedback I got, it seems like there's not much we can document here. Unless anyone knows actionable items I can take regarding this, I'll plan on closing this.

@frewsxcv frewsxcv closed this Feb 17, 2017
@frewsxcv frewsxcv deleted the zero-pointer branch February 17, 2017 13:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-docs Area: documentation for any part of the project, including the compiler, standard library, and tools
Projects
None yet
Development

Successfully merging this pull request may close these issues.

10 participants