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

deduplicate and clarify rules for converting pointers to references #128157

Merged
merged 5 commits into from
Aug 27, 2024

Conversation

lolbinarycat
Copy link

part of #124669

@rustbot
Copy link
Collaborator

rustbot commented Jul 24, 2024

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @cuviper (or someone else) some time within the next two weeks.

Please see the contribution instructions for more information. Namely, in order to ensure the minimum review times lag, PR authors and assigned reviewers should ensure that the review label (S-waiting-on-review and S-waiting-on-author) stays updated, invoking these commands when appropriate:

  • @rustbot author: the review is finished, PR author should check the comments and take action accordingly
  • @rustbot review: the author is ready for a review, this PR will be queued again in the reviewer's queue

@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 Jul 24, 2024
//!
//! * The pointer must point to a valid instance of `T`.
//! This means that the created reference can only refer to
//! uninitialized memory through careful use of `MaybeUninit`.
Copy link
Member

Choose a reason for hiding this comment

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

This is not quite true. Uninit memory in padding is also okay, and inside (some) unions it is okay, too.

This should probably just link to the reference for the definition of "valid". (Also, I don't think we use the term instance usually. The reference says "valid value of type T".)

Copy link
Author

Choose a reason for hiding this comment

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

the rust reference does not seem to have a general definition for "valid", it only has sections on bit validity for certain types.

Copy link
Member

Choose a reason for hiding this comment

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

It does, here. With rust-lang/reference#1540 that becomes a separate section that can even be hyperlinked.

Copy link
Author

Choose a reason for hiding this comment

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

that pr got merged 2 days ago, but the new section doesn't even show up in the nightly reference.

any idea what the staging cycle is for the reference? if it's relativly short i'm fine blocking this PR on that getting updated so it can include that link, which is quite useful.

Copy link
Member

Choose a reason for hiding this comment

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

Not sure, @ehuss is doing book updates every now and then. @ehuss how often do you usually do that?

Copy link
Contributor

Choose a reason for hiding this comment

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

Every 2 weeks.

I'm about to open a PR to update it, but it might take a while to get it reviewed.

//! except for data inside an `UnsafeCell`
// ^ previous documentation was somewhat unclear on if modifications through
// an UnsafeCell are safe even if they would seemingly violate the exclusivity
// of a mut ref.
Copy link
Member

Choose a reason for hiding this comment

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

Previous documentation was quite clear IMO, but note that the documentation is different for functions that return a mutable ref vs a shared ref. The UnsafeCell remark only applies to shared references. By deduplicating the docs you made the comment also apply to mutable references, which is wrong.

Copy link
Author

Choose a reason for hiding this comment

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

i originally had a disclaimer that this does not apply to mutable references, but then i found a function which return a mutable reference and had the UnsafeCell remark, so i removed that and added this hidden comment. consulting the UnsafeCell documentation reveals that your assessment is correct, so i'll add it back.

Copy link
Member

Choose a reason for hiding this comment

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

Ah probably someone made a copy-paste error then, this should not ever show up in a &mut-returning function.

library/core/src/ptr/const_ptr.rs Outdated Show resolved Hide resolved
library/core/src/ptr/const_ptr.rs Outdated Show resolved Hide resolved
library/core/src/ptr/mod.rs Outdated Show resolved Hide resolved
library/core/src/ptr/mut_ptr.rs Outdated Show resolved Hide resolved
library/core/src/ptr/mut_ptr.rs Outdated Show resolved Hide resolved
library/core/src/ptr/mut_ptr.rs Outdated Show resolved Hide resolved
library/core/src/ptr/mut_ptr.rs Outdated Show resolved Hide resolved
library/core/src/ptr/non_null.rs Outdated Show resolved Hide resolved
library/core/src/ptr/non_null.rs Outdated Show resolved Hide resolved
@cuviper
Copy link
Member

cuviper commented Aug 26, 2024

I just pushed my final suggestions, otherwise it looks good, thanks!

@bors r+ rollup

@bors
Copy link
Contributor

bors commented Aug 26, 2024

📌 Commit b11e0a8 has been approved by cuviper

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 Aug 26, 2024
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Aug 26, 2024
…=cuviper

deduplicate and clarify rules for converting pointers to references

part of rust-lang#124669
bors added a commit to rust-lang-ci/rust that referenced this pull request Aug 26, 2024
…iaskrgr

Rollup of 9 pull requests

Successful merges:

 - rust-lang#126013 (Add `#[warn(unreachable_pub)]` to a bunch of compiler crates)
 - rust-lang#128157 (deduplicate and clarify rules for converting pointers to references)
 - rust-lang#129032 (Document & implement the transmutation modeled by `BikeshedIntrinsicFrom`)
 - rust-lang#129250 (Do not ICE on non-ADT rcvr type when looking for crate version collision)
 - rust-lang#129340 (Remove Duplicate E0381 Label)
 - rust-lang#129560 ([rustdoc] Generate source link on impl associated types)
 - rust-lang#129622 (Remove a couple of unused feature enables)
 - rust-lang#129625 (Rename `ParenthesizedGenericArgs` to `GenericArgsMode`)
 - rust-lang#129626 (Remove `ParamMode::ExplicitNamed`)

Failed merges:

 - rust-lang#128166 (Improved `checked_isqrt` and `isqrt` methods)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit c6ceb5b into rust-lang:master Aug 27, 2024
6 checks passed
@rustbot rustbot added this to the 1.82.0 milestone Aug 27, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Aug 27, 2024
Rollup merge of rust-lang#128157 - lolbinarycat:unify-ptr-ref-docs, r=cuviper

deduplicate and clarify rules for converting pointers to references

part of rust-lang#124669
//! not get accessed (read or written) through any raw pointer,
//! except for data inside an `UnsafeCell`.
//! Note that aliased writes are always UB for mutable references,
//! even if they only modify `UnsafeCell` data.
Copy link
Member

@RalfJung RalfJung Aug 27, 2024

Choose a reason for hiding this comment

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

This is wrong. It was copied from docs for a function that returns a mutable reference, but this section here claims to be about shared references.

EDIT: Actually this is not even right for mutable references. It's a weird mix of the two that is wrong for both cases.

/// * The pointer must point to an initialized instance of `T`.
/// When calling this method, you have to ensure that *either*
/// the pointer is null *or*
/// the pointer is [convertible to a reference](crate::ptr#pointer-to-reference-conversion).
Copy link
Member

Choose a reason for hiding this comment

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

That section is about shared references though, and this function returns a mutable reference.

@RalfJung
Copy link
Member

I will file a PR fixing the mistakes here, that's probably easier than going back and forth in the review.

@RalfJung
Copy link
Member

PR is up: #129652

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Aug 27, 2024
fix Pointer to reference conversion docs

The aliasing rules documented in rust-lang#128157 are wrong, this fixes them.
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Aug 27, 2024
fix Pointer to reference conversion docs

The aliasing rules documented in rust-lang#128157 are wrong, this fixes them.
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Aug 27, 2024
Rollup merge of rust-lang#129652 - RalfJung:ptr-to-ref, r=traviscross

fix Pointer to reference conversion docs

The aliasing rules documented in rust-lang#128157 are wrong, this fixes them.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants