-
-
Notifications
You must be signed in to change notification settings - Fork 15k
make more slice mutable ref getters rustc_no_writable #157816
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
Open
RalfJung
wants to merge
3
commits into
rust-lang:main
Choose a base branch
from
RalfJung:rustc_no_writable
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+77
−0
Open
Changes from all commits
Commits
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
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
48 changes: 48 additions & 0 deletions
48
src/tools/miri/tests/pass/tree_borrows/slice_get_mut_no_implicit_write.rs
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| //@compile-flags: -Zmiri-tree-borrows -Zmiri-tree-borrows-implicit-writes | ||
|
|
||
| // This test reproduces the pattern used by `BorrowedCursor::as_mut`, which appears in `Socket::recv_with_flags` and `std::fs::read`. | ||
| // Many crates depend on similar patterns. Before https://github.com/rust-lang/rust/pull/157202 this failed under Tree | ||
| // Borrows with Implicit Writes. With the attribute `#[rustc_no_writable]` added to | ||
| // `slice::get_unchecked_mut`, both this test and the affected crates work. | ||
| fn borrowed_buf() { | ||
| struct BorrowedBuf<'a> { | ||
| buf: &'a mut [u8], | ||
| } | ||
|
|
||
| impl<'a> BorrowedBuf<'a> { | ||
| fn capacity(&self) -> usize { | ||
| self.buf.len() | ||
| } | ||
|
|
||
| unsafe fn as_mut(&mut self) -> &mut [u8] { | ||
| unsafe { self.buf.get_unchecked_mut(..) } | ||
| } | ||
| } | ||
|
|
||
| let mut arr = [0u8; 4]; | ||
| let mut buf = BorrowedBuf { buf: &mut arr }; | ||
|
|
||
| let ptr = unsafe { buf.as_mut() }.as_mut_ptr(); | ||
| let _ = buf.capacity(); | ||
| unsafe { | ||
| ptr.write(42); | ||
| } | ||
| } | ||
|
|
||
| // A variant of the above that uses `index_mut` notation. | ||
| fn index_mut() { | ||
| fn dostuff(x: &mut [u8]) { | ||
| unsafe { | ||
| let ptr = (&mut x[..]).as_mut_ptr(); | ||
| let _len = x.len(); | ||
| ptr.write(99); | ||
| } | ||
| } | ||
|
|
||
| dostuff(&mut [1, 2, 3, 4]); | ||
| } | ||
|
|
||
| fn main() { | ||
| borrowed_buf(); | ||
| index_mut(); | ||
| } |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.
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'd be nice if we could just add the attribute to the trait rather than each impl... but I am not sure how to implement that and it is not the usual behavior for such attributes.
View changes since the review
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.
How exactly would that work? Just apply to the abstract method and then assume each implementation should have it applied?
That feels kind of unprecedented, since even attributes like
#[inline]don't support 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.
I think it makes sense because of the conceptual alignment between what SliceIndex is for and what the attribute is trying to express.
I think
#[track_caller]can work like this, based on what I see inshould_inherit_track_callerin the compiler.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.
Huh, I had no idea you were able to use
#[track_caller]in that way, and it turns out you're right: https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=c726a3e5c00af55c75daaa67bf6533fcSemantically, it makes sense, just, I had no idea that we had precedent for this.
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.
After catching up on the semantics of this attribute, at least from my understanding, the presence or absence of this attribute is always safe?
We don't really distinguish unsafe attributes (to my knowledge) for internal attributes, at least at the moment, but I guess the main issue with such a thing is if it would have noticeable effects that the method implementations would have to account for, e.g., such that those implementations would be best marked as unsafe or similar.
I guess this technically doesn't matter in this particular review, just trying to grasp a bit better how this would affect methods like this. I'm not sure if this necessarily changes any compiler semantics besides emitting an LLVM flag, e.g., if a future borrow checker would take this attribute into account.
Uh oh!
There was an error while loading. Please reload this page.
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.
The attribute affects whether code has UB, but on its own (i.e. in otherwise safe code) the presence or absence of the attribute can never cause UB. Furthermore, adding the attribute can never introduce UB to a program, but removing the attribute can introduce UB if surrounding unsafe code relied on the presence of the attribute. By default, our codegen backend entirely ignores the attribute; it takes a
-Zflag (or Miri) for the attribute to do anything.This is not meant to be taken into account by a borrow checker, just by LLVM and Miri. For now, it is just an experiment. If the experiment goes well, we could consider a more proper (e.g. type-based) solution.