Skip to content

Clarify preconditions of raw size/align methods - #158893

Merged
rust-bors[bot] merged 1 commit into
rust-lang:mainfrom
Jules-Bertholet:for-pointer-docs
Jul 28, 2026
Merged

Clarify preconditions of raw size/align methods#158893
rust-bors[bot] merged 1 commit into
rust-lang:mainfrom
Jules-Bertholet:for-pointer-docs

Conversation

@Jules-Bertholet

Copy link
Copy Markdown
Contributor

The current documentation doesn't guarantee that these methods are safe to call for pointers derived from references to new unsized types that Rust may add in the future.

Take 2 of #103372, wording from @CAD97 in #103372 (comment).

Tracking issue: #69835

The current documentation doesn't guarantee
that these methods are safe to call for pointers
derived from references to new unsized types
that Rust may add in the future.

Co-authored-by: Crystal Durham <cad97@cad97.com>
@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 7, 2026
@rustbot

rustbot commented Jul 7, 2026

Copy link
Copy Markdown
Collaborator

r? @JohnTitor

rustbot has assigned @JohnTitor.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: libs
  • libs expanded to 12 candidates
  • Random selection from Darksonn, JohnTitor, Mark-Simulacrum, clarfonthey, jhpratt

@CAD97

CAD97 commented Jul 7, 2026

Copy link
Copy Markdown
Contributor

The one wrinkle with documenting this guarantee is that in order to rely on it you need to accept not only that it is sound to reborrow the pointer as a reference at this point, but also that it is safe to do so with respect to the rest of the program. I.e. computing the size/align could potentially require doing a read of the value (e.g. for a thin c string).

Such is clearly sufficient to allow calling these functions. I'm just not fully confident that the actual requirement is made clear. I also am struggling to come up with a use case that would want this guarantee that couldn't just call the reference taking functions instead.

@Jules-Bertholet

Jules-Bertholet commented Jul 7, 2026

Copy link
Copy Markdown
Contributor Author

you need to accept not only that it is sound to reborrow the pointer as a reference at this point, but also that it is safe to do so with respect to the rest of the program

Either it's safe to get a reference (and pass it to arbitrary safe code, including size_of_val), or it's not. I used the word "safe" instead of your original suggestion of "valid" for this reason.

@JohnTitor JohnTitor left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I also am struggling to come up with a use case that would want this guarantee that couldn't just call the reference taking functions instead.

I guess the purpose here is rather making the safety contract complete for future unsized types. For that, I think the proposed one sounds clearer.

@CAD97 I'm happy to discuss further if needed, otherwise r= you & me

View changes since this review

@JohnTitor

Copy link
Copy Markdown
Member

Ok, let's go ahead, this can be tweaked later if something else is raised.
Thanks for the PR! @bors r+ rollup

@rust-bors

rust-bors Bot commented Jul 27, 2026

Copy link
Copy Markdown
Contributor

📌 Commit 33189b4 has been approved by JohnTitor

It is now in the queue for this repository.

🌲 The tree is currently closed for pull requests below priority 100. This pull request will be tested once the tree is reopened.

Reason for tree closure: spurious failures

@rust-bors rust-bors Bot 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 Jul 27, 2026
jhpratt added a commit to jhpratt/rust that referenced this pull request Jul 28, 2026
…r=JohnTitor

Clarify preconditions of raw size/align methods

The current documentation doesn't guarantee that these methods are safe to call for pointers derived from references to new unsized types that Rust may add in the future.

Take 2 of rust-lang#103372, wording from @CAD97 in rust-lang#103372 (comment).

Tracking issue: rust-lang#69835
rust-bors Bot pushed a commit that referenced this pull request Jul 28, 2026
Rollup of 9 pull requests

Successful merges:

 - #153563 (Lint against iterator functions that panic when `N` is zero )
 - #159960 (Allow `UnsafeCell` content access without `get` in `invalid_reference_casting` lint)
 - #158893 (Clarify preconditions of raw size/align methods)
 - #159220 (Don't optimize across storage markers in SimplifyComparisonIntegral)
 - #159309 (Move tests batch 18)
 - #159450 (Add codegen test for enum clone)
 - #160017 (Make BorrowSet methods public again)
 - #160022 (Refactor rustc_hir re-exports)
 - #160041 (Correct tracking issue for `casefold` feature)
@rust-bors
rust-bors Bot merged commit 39fab15 into rust-lang:main Jul 28, 2026
13 checks passed
@rustbot rustbot added this to the 1.99.0 milestone Jul 28, 2026
rust-timer added a commit that referenced this pull request Jul 28, 2026
Rollup merge of #158893 - Jules-Bertholet:for-pointer-docs, r=JohnTitor

Clarify preconditions of raw size/align methods

The current documentation doesn't guarantee that these methods are safe to call for pointers derived from references to new unsized types that Rust may add in the future.

Take 2 of #103372, wording from @CAD97 in #103372 (comment).

Tracking issue: #69835
@Jules-Bertholet
Jules-Bertholet deleted the for-pointer-docs branch July 28, 2026 15:45
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.

4 participants