-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Use pointers in cell::{Ref,RefMut}
to avoid noalias
#97027
Merged
Merged
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d369045
Use a pointer in cell::Ref so it's not noalias
cuviper 2b8041f
Use a pointer in cell::RefMut so it's not noalias
cuviper 15d8c00
Test RefCell aliasing
cuviper 1e53fab
Remove outdated references to nll-rfc#40
cuviper 1f33c92
Add a comment for covariant `Ref`
cuviper 1c3921f
Read the Ref/RefMut pointer in natvis
cuviper 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 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 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,14 @@ | ||
// compile-flags: -O -C no-prepopulate-passes -Z mutable-noalias=yes | ||
|
||
#![crate_type = "lib"] | ||
|
||
use std::cell::{Ref, RefCell, RefMut}; | ||
|
||
// Make sure that none of the arguments get a `noalias` attribute, because | ||
// the `RefCell` might alias writes after either `Ref`/`RefMut` is dropped. | ||
|
||
// CHECK-LABEL: @maybe_aliased( | ||
// CHECK-NOT: noalias | ||
// CHECK-SAME: %_refcell | ||
#[no_mangle] | ||
pub unsafe fn maybe_aliased(_: Ref<'_, i32>, _: RefMut<'_, i32>, _refcell: &RefCell<i32>) {} |
This file contains 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,36 @@ | ||
// run-pass | ||
// compile-flags: -O | ||
|
||
// Make sure that `Ref` and `RefMut` do not make false promises about aliasing, | ||
// because once they drop, their reference/pointer can alias other writes. | ||
|
||
// Adapted from comex's proof of concept: | ||
// https://github.com/rust-lang/rust/issues/63787#issuecomment-523588164 | ||
|
||
use std::cell::RefCell; | ||
use std::ops::Deref; | ||
|
||
pub fn break_if_r_is_noalias(rc: &RefCell<i32>, r: impl Deref<Target = i32>) -> i32 { | ||
let ptr1 = &*r as *const i32; | ||
let a = *r; | ||
drop(r); | ||
*rc.borrow_mut() = 2; | ||
let r2 = rc.borrow(); | ||
let ptr2 = &*r2 as *const i32; | ||
if ptr2 != ptr1 { | ||
panic!(); | ||
} | ||
// If LLVM knows the pointers are the same, and if `r` was `noalias`, | ||
// then it may replace this with `a + a`, ignoring the earlier write. | ||
a + *r2 | ||
} | ||
|
||
fn main() { | ||
let mut rc = RefCell::new(1); | ||
let res = break_if_r_is_noalias(&rc, rc.borrow()); | ||
assert_eq!(res, 3); | ||
|
||
*rc.get_mut() = 1; | ||
let res = break_if_r_is_noalias(&rc, rc.borrow_mut()); | ||
assert_eq!(res, 3); | ||
} |
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.
I assume this no longer applies now?
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.
Maybe? I'm not really sure what needed fixing -- was it that
orig
had to be destructured, then rebuilt in theErr
case?