forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rollup merge of rust-lang#120932 - RalfJung:mut-ptr-to-static, r=oli-obk
const_mut_refs: allow mutable pointers to statics Fixes rust-lang#118447 Writing this PR became a bit messy, see [Zulip](https://rust-lang.zulipchat.com/#narrow/stream/146212-t-compiler.2Fconst-eval/topic/Statics.20pointing.20to.20interior.20mutable.20statics) for some of my journey.^^ Turns out there was a long-standing bug in our qualif logic that led to us incorrectly classifying certain places as "no interior mut" when they actually had interior mut. Due to that the `const_refs_to_cell` feature gate was required far less often than it otherwise would, e.g. in [this code](https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=9e0c042c451b3d11d64dd6263679a164). Fixing this however would be a massive breaking change all over libcore and likely the wider ecosystem. So I also changed the const-checking logic to just not require the feature gate for the affected cases. While doing so I discovered a bunch of logic that is not explained and that I could not explain. However I think stabilizing some const-eval feature will make most of that logic inconsequential so I just added some FIXMEs and left it at that. r? `@oli-obk`
- Loading branch information
Showing
13 changed files
with
173 additions
and
62 deletions.
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
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,43 @@ | ||
// check-pass | ||
//! This is the reduced version of the "Linux kernel vtable" use-case. | ||
#![feature(const_mut_refs, const_refs_to_static)] | ||
use std::ptr::addr_of_mut; | ||
|
||
#[repr(C)] | ||
struct ThisModule(i32); | ||
|
||
trait Module { | ||
const THIS_MODULE_PTR: *mut ThisModule; | ||
} | ||
|
||
struct MyModule; | ||
|
||
// Generated by a macro. | ||
extern "C" { | ||
static mut THIS_MODULE: ThisModule; | ||
} | ||
|
||
// Generated by a macro. | ||
impl Module for MyModule { | ||
const THIS_MODULE_PTR: *mut ThisModule = unsafe { addr_of_mut!(THIS_MODULE) }; | ||
} | ||
|
||
struct Vtable { | ||
module: *mut ThisModule, | ||
foo_fn: fn(*mut ()) -> i32, | ||
} | ||
|
||
trait Foo { | ||
type Mod: Module; | ||
|
||
fn foo(&mut self) -> i32; | ||
} | ||
|
||
fn generate_vtable<T: Foo>() -> &'static Vtable { | ||
&Vtable { | ||
module: T::Mod::THIS_MODULE_PTR, | ||
foo_fn: |ptr| unsafe { &mut *ptr.cast::<T>() }.foo(), | ||
} | ||
} | ||
|
||
fn main() {} |
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
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
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,40 @@ | ||
// run-pass | ||
#![feature(const_mut_refs)] | ||
#![feature(sync_unsafe_cell)] | ||
|
||
use std::cell::SyncUnsafeCell; | ||
use std::ptr; | ||
|
||
#[repr(C)] | ||
struct SyncPtr { | ||
foo: *mut u32, | ||
} | ||
unsafe impl Sync for SyncPtr {} | ||
|
||
static mut STATIC: u32 = 42; | ||
|
||
static INTERIOR_MUTABLE_STATIC: SyncUnsafeCell<u32> = SyncUnsafeCell::new(42); | ||
|
||
// A static that mutably points to STATIC. | ||
static PTR: SyncPtr = SyncPtr { | ||
foo: unsafe { ptr::addr_of_mut!(STATIC) }, | ||
}; | ||
static INTERIOR_MUTABLE_PTR: SyncPtr = SyncPtr { | ||
foo: ptr::addr_of!(INTERIOR_MUTABLE_STATIC) as *mut u32, | ||
}; | ||
|
||
fn main() { | ||
let ptr = PTR.foo; | ||
unsafe { | ||
assert_eq!(*ptr, 42); | ||
*ptr = 0; | ||
assert_eq!(*PTR.foo, 0); | ||
} | ||
|
||
let ptr = INTERIOR_MUTABLE_PTR.foo; | ||
unsafe { | ||
assert_eq!(*ptr, 42); | ||
*ptr = 0; | ||
assert_eq!(*INTERIOR_MUTABLE_PTR.foo, 0); | ||
} | ||
} |
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 |
---|---|---|
@@ -1,18 +1,18 @@ | ||
#![feature(const_mut_refs)] | ||
|
||
static X: i32 = 1; | ||
const C: i32 = 2; | ||
static mut M: i32 = 3; | ||
|
||
const CR: &'static mut i32 = &mut C; //~ ERROR mutable references are not allowed | ||
//~| WARN taking a mutable | ||
|
||
static STATIC_REF: &'static mut i32 = &mut X; //~ ERROR E0658 | ||
//~| ERROR cannot borrow | ||
//~| ERROR mutable references are not allowed | ||
static STATIC_REF: &'static mut i32 = &mut X; //~ ERROR cannot borrow immutable static item `X` as mutable | ||
|
||
static CONST_REF: &'static mut i32 = &mut C; //~ ERROR mutable references are not allowed | ||
//~| WARN taking a mutable | ||
|
||
static STATIC_MUT_REF: &'static mut i32 = unsafe { &mut M }; //~ ERROR mutable references are not | ||
static STATIC_MUT_REF: &'static mut i32 = unsafe { &mut M }; | ||
//~^ WARN mutable reference of mutable static is discouraged [static_mut_ref] | ||
|
||
fn main() {} |
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
Oops, something went wrong.