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

clarify wrapping ptr arithmetic docs #80383

Merged
merged 2 commits into from
Dec 29, 2020

Conversation

RalfJung
Copy link
Member

Fixes #80306

@steffahn please let me know if this helps avoid the misunderstanding. :)

@rust-highfive
Copy link
Collaborator

r? @dtolnay

(rust-highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Dec 26, 2020
Copy link
Member

@steffahn steffahn left a comment

Choose a reason for hiding this comment

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

I’ve commented on two details that could be improved. In general, I like this change and it does IMO remove the possibility of misunderstanding.

/// pointer is dereferenced when it is out-of-bounds of the object it is attached to. [`offset`]
/// can be optimized better and is thus preferable in performance-sensitive code.
///
/// `x.wrapping_offset(o).wrapping_offset(-o)` is always the same as `x` (if `-o` does not
Copy link
Member

Choose a reason for hiding this comment

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

Perhaps using x.wrapping_offset(o).wrapping_offset(o.wrapping_neg()) and avoiding the remark about overflow is more clear.

Copy link
Member Author

Choose a reason for hiding this comment

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

Oh right... somehow I thought isize::MIN + isize::MIN would not be zero, but indeed it is zero.

/// is *not* the same as `y`, and dereferencing it is undefined behavior
/// unless `x` and `y` point into the same allocated object.
/// In other words, `let z = x.wrapping_add((y as usize).wrapping_sub(x as usize) /
/// size_of::<T>())` does *not* make `z` the same as `y`: `z` is still attached to the object `x` is
Copy link
Member

Choose a reason for hiding this comment

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

I’m not 100% happy with the fact that the first "example code" in the safety remark is one that doesn’t even mention wrapping_offset. The same applies to the documentation of wrapping_sub.

Copy link
Member Author

Choose a reason for hiding this comment

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

I guess we could forcefully cast... but that just obscures the point even more as the use of usize::wrapping_sub already does. I'd prefer to just write x.wrapping_add(y - x), since then it is much more clear what is happening.

I did some more editing, what do you think?

Copy link
Member

Choose a reason for hiding this comment

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

Yes, clarity is the most important thing here (while staying "correct" is only important to avoid confusion). Stating some preconditions on the size of T and overflow, like you did now, is a nice approach.

Copy link
Member

@steffahn steffahn left a comment

Choose a reason for hiding this comment

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

I like those new changes, feel free to resolve my review comments.

@dtolnay
Copy link
Member

dtolnay commented Dec 26, 2020

@bors r+ rollup

@bors
Copy link
Contributor

bors commented Dec 26, 2020

📌 Commit 8543388 has been approved by dtolnay

@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 Dec 26, 2020
bors added a commit to rust-lang-ci/rust that referenced this pull request Dec 29, 2020
Rollup of 11 pull requests

Successful merges:

 - rust-lang#80383 (clarify wrapping ptr arithmetic docs)
 - rust-lang#80390 (BTreeMap: rename the area access methods)
 - rust-lang#80393 (Add links to the source for the rustc and rustdoc books.)
 - rust-lang#80398 (Use raw version of align_of in rc data_offset)
 - rust-lang#80402 (Document `InferTy` & co.)
 - rust-lang#80403 (fix: small typo error in chalk/mod.rs)
 - rust-lang#80410 (rustdoc book: fix example)
 - rust-lang#80419 (Add regression test for rust-lang#80375)
 - rust-lang#80430 (Add "length" as doc alias to len methods)
 - rust-lang#80431 (Add "chr" as doc alias to char::from_u32)
 - rust-lang#80448 (Fix stabilization version of deque_range feature.)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 9fdd95f into rust-lang:master Dec 29, 2020
@rustbot rustbot added this to the 1.51.0 milestone Dec 29, 2020
@RalfJung RalfJung deleted the wrapping-ptr-arithmetic branch December 29, 2020 21:38
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.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Documentation of wrapping_add/-_sub/-_offset need clarification...
6 participants