-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Add [T]::as_ptr_range() and [T]::as_mut_ptr_range(). #65806
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
Merged
bors
merged 4 commits into
rust-lang:master
from
fusion-engineering-forks:slice-ptr-range
Oct 25, 2019
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
4936f96
Add [T]::as_ptr_range() and [T]::as_mut_ptr_range().
m-ou-se f1b69b0
Add slice_ptr_range tracking issue number.
m-ou-se de9b660
Explain why pointer::add in slice::as_ptr_range is safe.
m-ou-se 381c442
Fix slice::as_ptr_range doctest.
m-ou-se File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
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.
Could you please provide a brief safety argument reasoning around the requirements
.add
states. (Notably,vec.as_ptr().add(vec.len())
is mentioned.) (Same below.)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.
Hm, now I'm starting to wonder if it actually is safe. A Vec is never larger than
isize::MAX
bytes, but that guarantee is not there for[T]
, is 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.
I changed it to
wrapping_add
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.
Mmh, I'm wondering where this limitation with isize::MAX is coming from. The documentation doesn't seem to mention why that is, only that this is the case. Is this some LLVM specific problem or something? I don't think this is something Rust is inheriting from the C standard.
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.
cc @rust-lang/lang @rust-lang/wg-unsafe-code-guidelines
(Assuming these statements are normative.)
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 think it refers to any sort of allocation, including stack allocations (as your array example indicates).
That's a mistake on my part, it is defined in
core
as well.So working on the assumption that this is unspecified / may become implementation-defined it seems to me that we have two choices:
Use
.wrapping_add(...)
-- potentially a foot-gun if you use.contains
tho not going to happen for real programs because of LLVM limitations and because of the sheer size of the allocation.Use
assert!(...)
as above. I'm not sure if LLVM would see the assertion and reason that it must always hold and so optimize it away.We can probably switch between the two behaviors even after stabilizing because they are so pathological. However, it seems to me we should add a note to the documentation as well as the tracking issue and then the libs team can pick between one of them.
Haha; Seems we had the opposite take-aways.
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.
Ah, the Index implementation of slices assumes the same thing: https://doc.rust-lang.org/nightly/src/core/slice/mod.rs.html#2678-2724
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 want to wade into the issue of what's normative, but
ptr.offset
and related methods with restrictions toisize::MAX
bytes are used pervasively from practically all safe slice APIs (at least, anything that ever accesses or takes the address of a slice element). Therefore:isize::MAX
bytesisize::MAX
restriction fromoffset
and friends) if we ever wanted to support larger slicesSo I think the current implementation (
unsafe { add }
with comment) is perfectly fine.´I would advise against
wrapping_add
because it can cause performance degradation in some situations and there's no reason to risk that.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.
Yeah that seems like a good conclusion. Let's keep the PR as-is.
@m-ou-se Could you add a note to the second bullet re. this not being normative (at least not yet) as well as link to rust-lang/unsafe-code-guidelines#102 (comment). With that I think we should be good to merge the PR.
(Also let's keep this thread visible since it was an interesting conversation.)
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.
Done.