-
Notifications
You must be signed in to change notification settings - Fork 13.1k
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
Check type of place base (not projection) for Freeze
in IndirectlyMutableLocals
#65030
Changes from all commits
64457a1
da0969f
6487ce0
2f89429
895f9c4
91c77e9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#![feature(core_intrinsics, rustc_attrs)] | ||
|
||
use std::cell::Cell; | ||
use std::intrinsics::rustc_peek; | ||
|
||
#[rustc_mir(rustc_peek_indirectly_mutable, stop_after_dataflow)] | ||
pub fn mut_ref(flag: bool) -> i32 { | ||
let mut i = 0; | ||
let mut cell = Cell::new(0); | ||
|
||
if flag { | ||
let p = &mut i; | ||
unsafe { rustc_peek(i) }; | ||
*p = 1; | ||
|
||
let p = &cell; | ||
unsafe { rustc_peek(cell) }; | ||
p.set(2); | ||
} else { | ||
unsafe { rustc_peek(i) }; //~ ERROR rustc_peek: bit not set | ||
unsafe { rustc_peek(cell) }; //~ ERROR rustc_peek: bit not set | ||
|
||
let p = &mut cell; | ||
unsafe { rustc_peek(cell) }; | ||
*p = Cell::new(3); | ||
} | ||
|
||
unsafe { rustc_peek(i) }; | ||
unsafe { rustc_peek(cell) }; | ||
i + cell.get() | ||
} | ||
|
||
fn main() {} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
error: rustc_peek: bit not set | ||
--> $DIR/indirect-mutation-1.rs:20:18 | ||
| | ||
LL | unsafe { rustc_peek(i) }; | ||
| ^^^^^^^^^^^^^ | ||
|
||
error: rustc_peek: bit not set | ||
--> $DIR/indirect-mutation-1.rs:21:18 | ||
| | ||
LL | unsafe { rustc_peek(cell) }; | ||
| ^^^^^^^^^^^^^^^^ | ||
|
||
error: stop_after_dataflow ended compilation | ||
|
||
error: aborting due to 3 previous errors | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#![feature(core_intrinsics, rustc_attrs)] | ||
|
||
use std::cell::Cell; | ||
use std::intrinsics::rustc_peek; | ||
|
||
struct Arr { | ||
inner: [Cell<i32>; 0], | ||
_peek: (), | ||
} | ||
|
||
// Zero-sized arrays are never mutable. | ||
#[rustc_mir(rustc_peek_indirectly_mutable, stop_after_dataflow)] | ||
pub fn zst(flag: bool) { | ||
{ | ||
let mut arr: [i32; 0] = []; | ||
let s: &mut [i32] = &mut arr; | ||
unsafe { rustc_peek(arr) }; //~ ERROR rustc_peek: bit not set | ||
} | ||
|
||
{ | ||
let arr: [Cell<i32>; 0] = []; | ||
let s: &[Cell<i32>] = &arr; | ||
unsafe { rustc_peek(arr) }; //~ ERROR rustc_peek: bit not set | ||
|
||
let ss = &s; | ||
unsafe { rustc_peek(arr) }; //~ ERROR rustc_peek: bit not set | ||
} | ||
} | ||
|
||
fn main() {} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
error: rustc_peek: bit not set | ||
--> $DIR/indirect-mutation-2.rs:17:18 | ||
| | ||
LL | unsafe { rustc_peek(arr) }; | ||
| ^^^^^^^^^^^^^^^ | ||
|
||
error: rustc_peek: bit not set | ||
--> $DIR/indirect-mutation-2.rs:23:18 | ||
| | ||
LL | unsafe { rustc_peek(arr) }; | ||
| ^^^^^^^^^^^^^^^ | ||
|
||
error: rustc_peek: bit not set | ||
--> $DIR/indirect-mutation-2.rs:26:18 | ||
| | ||
LL | unsafe { rustc_peek(arr) }; | ||
| ^^^^^^^^^^^^^^^ | ||
|
||
error: stop_after_dataflow ended compilation | ||
|
||
error: aborting due to 4 previous errors | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,14 +12,17 @@ struct PartialInteriorMut { | |
} | ||
|
||
#[rustc_mir(rustc_peek_indirectly_mutable,stop_after_dataflow)] | ||
#[rustc_mir(borrowck_graphviz_postflow="indirect.dot")] | ||
const BOO: i32 = { | ||
let x = PartialInteriorMut { | ||
zst: [], | ||
cell: UnsafeCell::new(0), | ||
}; | ||
|
||
let p_zst: *const _ = &x.zst ; // Doesn't cause `x` to get marked as indirectly mutable. | ||
unsafe { rustc_peek(x) }; //~ ERROR rustc_peek: bit not set | ||
|
||
let p_zst: *const _ = &x.zst ; | ||
|
||
unsafe { rustc_peek(x) }; | ||
|
||
let rmut_cell = unsafe { | ||
// Take advantage of the fact that `zst` and `cell` are at the same location in memory. | ||
|
@@ -30,9 +33,9 @@ const BOO: i32 = { | |
&mut *pmut_cell | ||
}; | ||
|
||
*rmut_cell = 42; // Mutates `x` indirectly even though `x` is not marked indirectly mutable!!! | ||
*rmut_cell = 42; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm a bit confused. This line is UB. You obtained a reference to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm confused about rust's memory model then. If you're not allowed to obtain a reference to one part of a struct and convert it to a reference to a disjoint part of that same struct, then there's no need to consider this in the analysis. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The following quote is from the
I was assuming that the stack-allocated instance of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure at all. Maybe @RalfJung can have a look? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Stacked Borrows disallows this, to keep aliasing under control. But we don't have a final memory model yet. See this document for further details on Stacked Borrows. Also, with Stacked Borrows you can do things like this through raw pointers. Like, |
||
let val = *rmut_cell; | ||
unsafe { rustc_peek(x) }; //~ ERROR rustc_peek: bit not set | ||
unsafe { rustc_peek(x) }; | ||
|
||
val | ||
}; | ||
|
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.
cell
is immutable, how does this line not error?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.
stop_after_dataflow
prevents later all subsequent compiler passes from running, so we never actually get to the pass that would fail on this broken code. I'll make sure these tests compile outside ofrustc_peek
mode when possible though.