-
Notifications
You must be signed in to change notification settings - Fork 13.4k
const_mut_refs: allow mutable pointers to statics #120932
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
Merged
Merged
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
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,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 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
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
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
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); | ||
} | ||
} |
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. FWIW this file does not trigger error code 0017. Nor does the other file trigger error code 0388. Cc #120935 |
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 |
---|---|---|
@@ -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 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
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.