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

add wrapping_offset_from which allows wrapping but still requires ptrs to be for the same allocation #112837

Closed
wants to merge 1 commit into from

Conversation

RalfJung
Copy link
Member

@RalfJung RalfJung commented Jun 20, 2023

Fixes #92512: currently, we have wrapping_offset as a const fn, but there is no way inside const to perform a subtraction that would tell how much a pointer has been wrapping-offset. This is an unfortunate lack of symmetry, and an issue for some approaches to implementing slice iterators -- those tend to handle zero-sized types by doing wrapping_offset and then they need a wrapping_offset_from to compute the remaining length of the iterator.

So I propose we add a new wrapping_offset_from that is able to always invert wrapping_offset: wrapping_offset_from is valid if and only if origin could have been produced from self via wrapping_offset. Correspondingly, the requirement for both pointers to be derived from the same allocated object remains (it is also required to make the function const), as does the requirement of being an exact multiple (hoping that it aids LLVM in its codegen). byte_wrapping_offset_from can be used in case the exact-multiple requirement might be violated.

@rustbot
Copy link
Collaborator

rustbot commented Jun 20, 2023

r? @Mark-Simulacrum

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

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Jun 20, 2023
@rustbot
Copy link
Collaborator

rustbot commented Jun 20, 2023

Some changes occurred to the CTFE / Miri engine

cc @rust-lang/miri

The Miri subtree was changed

cc @rust-lang/miri

@RalfJung RalfJung force-pushed the wrapping_offset_from branch 2 times, most recently from 461bb15 to 7428cc4 Compare June 20, 2023 11:32
@rust-log-analyzer

This comment has been minimized.

@RalfJung RalfJung force-pushed the wrapping_offset_from branch from 7428cc4 to 663ef24 Compare June 20, 2023 11:58
@rust-log-analyzer

This comment has been minimized.

@RalfJung RalfJung force-pushed the wrapping_offset_from branch from 663ef24 to c49d0aa Compare June 20, 2023 12:29
Comment on lines +903 to +904
#[unstable(feature = "ptr_wrapping_offset_from", issue = "none")]
#[rustc_const_unstable(feature = "ptr_wrapping_offset_from", issue = "none")]
Copy link
Member Author

Choose a reason for hiding this comment

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

This could be tracked with wrapping_offset_from or with the other bytes methods, not sure which is more appropriate.

#[inline]
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
#[cfg(not(bootstrap))]
pub const unsafe fn wrapping_offset_from(self, origin: *const T) -> isize
Copy link
Member Author

Choose a reason for hiding this comment

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

We could also have a wrapping_sub_ptr, if there is interest.

@the8472 the8472 added T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. and removed T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Jun 20, 2023
@Mark-Simulacrum
Copy link
Member

r=me, but I think this will need a libs-api ACP.

@rustbot author

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jun 24, 2023
@RalfJung
Copy link
Member Author

RalfJung commented Jun 25, 2023 via email

@Mark-Simulacrum
Copy link
Member

Hm, I guess - there is new unstable API though outside of just the intrinsic...

@Mark-Simulacrum Mark-Simulacrum added the I-lang-nominated Nominated for discussion during a lang team meeting. label Jun 25, 2023
@RalfJung
Copy link
Member Author

ACP filed at rust-lang/libs-team#244.

Cc @rust-lang/wg-const-eval since this proposes a new const intrinsic that cannot be implemented with what we already have.

@RalfJung RalfJung added T-lang Relevant to the language team, which will review and decide on the PR/issue. A-const-eval Area: Constant evaluation, covers all const contexts (static, const fn, ...) labels Jun 26, 2023
@bors
Copy link
Contributor

bors commented Jul 6, 2023

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

@RalfJung
Copy link
Member Author

The t-libs ACP got rejected for lack of motivation. I don't really have any way to refute that, so I'll close the PR -- if someone has a motivation that requires this function, please let me know. :)

@RalfJung RalfJung closed this Jul 17, 2023
bors added a commit to rust-lang-ci/rust that referenced this pull request Sep 29, 2023
…jubilee

offset_from: docs improvements

This is the part of rust-lang#112837 that doesn't add a new function, just tweaks the existing docs.
RalfJung pushed a commit to RalfJung/miri that referenced this pull request Sep 30, 2023
offset_from: docs improvements

This is the part of rust-lang/rust#112837 that doesn't add a new function, just tweaks the existing docs.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-const-eval Area: Constant evaluation, covers all const contexts (static, const fn, ...) I-lang-nominated Nominated for discussion during a lang team meeting. S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-lang Relevant to the language team, which will review and decide on the PR/issue. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
6 participants