Skip to content

Make core::ops::Place an unsafe trait #47299

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

Merged
merged 2 commits into from
Jan 24, 2018
Merged

Conversation

cramertj
Copy link
Member

@cramertj cramertj commented Jan 9, 2018

Consumers of Place would reasonably expect that the pointer function returns a valid pointer to memory that can actually be written to.

@rust-highfive
Copy link
Contributor

r? @KodrAus

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

@kennytm kennytm added T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jan 10, 2018
@KodrAus
Copy link
Contributor

KodrAus commented Jan 14, 2018

I think that's a totally reasonable constraint 👍

If we only require that all returned pointers are non null, what do you think about using the NonNull (formally Shared) type from #46952 and leaving the function signature as safe? That might capture the contract a bit more clearly.

@bluss
Copy link
Member

bluss commented Jan 14, 2018

@KodrAus That does not in guarantee that the pointer can be dereferenced, so it's not enough.

@bluss bluss mentioned this pull request Jan 14, 2018
4 tasks
/// Returns the address where the input value will be written.
/// Note that the data at this address is generally uninitialized,
/// and thus one should use `ptr::write` for initializing it.
///
/// This function must return a valid (non-zero) pointer to
/// a location at which a value of type `Data` can be written.
Copy link
Member

@bluss bluss Jan 14, 2018

Choose a reason for hiding this comment

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

To stave off misunderstanding, maybe this should say "must return a valid pointer". Since non-null is not the end of the validity requirements.

Copy link
Member

Choose a reason for hiding this comment

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

the end of the validity requirements

Are those requirements listed somewhere where we can refer to?

Copy link
Member Author

@cramertj cramertj Jan 16, 2018

Choose a reason for hiding this comment

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

The wording is actually kind of redundant as-is. The real requirement is "This function must return a pointer that a value of type Data can be written through." The specific requirements regarding aliasing and soundness are sort of floaty without unsafe code guidelines, but there's probably some kind of aliasing requirement in there, too 😄

Copy link
Member

Choose a reason for hiding this comment

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

I agree, it's redundant.

@KodrAus
Copy link
Contributor

KodrAus commented Jan 16, 2018

So I think we're all in agreement that there's more to the contract of Place than can be guaranteed by safe code alone, so marking the trait as unsafe seems like a good move to me.

For the specific guarantees it sounds like we might not be able to get too specific about what a value of T can be written through actually means but maybe we can iterate on the text a bit to see if we can make it any clearer, and let the work on unsafe guidelines clarify all such cases in the future.

What do you all think?

@cramertj
Copy link
Member Author

cramertj commented Jan 16, 2018

I've pushed an update to the wording of the docs. LMK if there are more changes you'd like to see.

@sfackler
Copy link
Member

I'm jumping in a bit late here, but it seems like the second paragraph is basically just restating the first paragraph.

@cramertj
Copy link
Member Author

@sfackler IMO the first paragraph sounds like it's directed at users of the trait-- it describes suggested usage patterns, while the second paragraph focuses on the invariants that must be upheld by the implementer in order for the unsafe trait requirements to be met.

@KodrAus
Copy link
Contributor

KodrAus commented Jan 17, 2018

I'm not sure if there's precedent in std, but some crates capture the distinction @cramertj is making between user docs and implementor docs more concretely. bytes for instance has implementor notes on its trait methods.

@KodrAus
Copy link
Contributor

KodrAus commented Jan 17, 2018

I think rephrasing the trait's contract from the implementor's point of view is good, even though it sounds similar. This looks good to me, thanks @cramertj!

@rfcbot fcp merge

@kennytm
Copy link
Member

kennytm commented Jan 17, 2018

@rust-lang/libs Hi could any of you start the FCP process? rfcbot has not caught up with the new members yet (cc rust-lang/rfcbot-rs#176)

@alexcrichton
Copy link
Member

@rfcbot fcp merge

@rfcbot rfcbot added the proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. label Jan 17, 2018
@rfcbot
Copy link
Collaborator

rfcbot commented Jan 17, 2018

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

No concerns currently listed.

Once these reviewers reach consensus, 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.

@kennytm kennytm added S-waiting-on-team Status: Awaiting decision from the relevant subteam (see the T-<team> label). and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jan 17, 2018
@rfcbot
Copy link
Collaborator

rfcbot commented Jan 23, 2018

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

@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 Jan 23, 2018
@alexcrichton
Copy link
Member

@bors: r+

@bors
Copy link
Collaborator

bors commented Jan 23, 2018

📌 Commit f25f468 has been approved by alexcrichton

@bors
Copy link
Collaborator

bors commented Jan 24, 2018

⌛ Testing commit f25f468 with merge 9758ff9...

bors added a commit that referenced this pull request Jan 24, 2018
Make core::ops::Place an unsafe trait

Consumers of `Place` would reasonably expect that the `pointer` function returns a valid pointer to memory that can actually be written to.
@kennytm kennytm added the S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. label Jan 24, 2018
@kennytm kennytm removed the S-waiting-on-team Status: Awaiting decision from the relevant subteam (see the T-<team> label). label Jan 24, 2018
@bors
Copy link
Collaborator

bors commented Jan 24, 2018

☀️ Test successful - status-appveyor, status-travis
Approved by: alexcrichton
Pushing 9758ff9 to master...

@bors bors merged commit f25f468 into rust-lang:master Jan 24, 2018
@cramertj cramertj deleted the unsafe-placer branch January 24, 2018 16:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. 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.

10 participants