Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Add precondition checks to ptr::offset, ptr::add, ptr::sub #130251
New issue
Have a question about this project? No Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “No 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? No Sign in to your account
Add precondition checks to ptr::offset, ptr::add, ptr::sub #130251
Changes from all commits
6d246e4
9d5c961
128ccc3
8d562f6
File filter
Filter by extension
Conversations
Jump to
There are no files selected for viewing
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we do this avoiding
.addr()
? That's aptr2int
in LLVM, which exposes, and thus can't be optimized out.Perhaps have
this: *const u8
and phrase it asthis.wrapping_add(byte_offset) >= this
instead?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's a transmute, not a ptr2int... but LLVM might turn one into the other.
LLVM does optimize out ptr2int (which is not correct, but that doesn't stop it).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, all the more reason to stay in pointer-land if we can then, to avoid those complications :D
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think it is worth thinking about this. This is a UB check and won't even be monomorphized in a build that cares about this level of optimization. And other UB checks are already riddled with calls to
.addr()
; if you want to try to remove them all you're welcome to try.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since
sub
callsunchecked_neg
andoffset
, does this need its own check for anything more thancount * size <= isize::MAX
?Or if it's going to have one for a better error message, it might be good to change the implementation to use the intrinsics directly (
offset(self, unchecked_sub(0, count as isize))
) to avoid emitting three different ubchecks when callingsub
.