-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
const_mut_refs: allow mutable refs to statics
- 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.