forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
rustc: Allow safe access of shareable static muts
Taking a shareable loan of a `static mut` is safe if the type contained in the location is ascribes to `Share`. This commit removes the need to have an `unsafe` block in these situations. Unsafe blocks are still required to write to or take mutable loans out of statics. An example of code that no longer requires unsafe code is: use std::sync::atomics; static mut CNT: atomics::AtomicUint = atomics::INIT_ATOMIC_UINT; let cnt = CNT.fetch_add(1, atomics::SeqCst); As a consequence of this change, this code is not permitted: static mut FOO: uint = 30; println!("{}", FOO); The type `uint` is `Share`, and the static only has a shareable loan taken out on it, so the access does not require an unsafe block. Writes to the static are still unsafe, however, as they can invoke undefined behavior if not properly synchronized. Closes rust-lang#13232
- Loading branch information
1 parent
b495933
commit 4c62486
Showing
5 changed files
with
214 additions
and
4 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,66 @@ | ||
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT | ||
// file at the top-level directory of this distribution and at | ||
// http://rust-lang.org/COPYRIGHT. | ||
// | ||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
use std::kinds::marker; | ||
|
||
struct NonSharable { | ||
field: uint, | ||
noshare: marker::NoShare | ||
} | ||
|
||
struct Sharable { | ||
field: uint | ||
} | ||
|
||
impl Sharable { | ||
fn foo(&self) {} | ||
fn foo_mut(&mut self) {} | ||
} | ||
|
||
static mut NON_SHARABLE: NonSharable = NonSharable { | ||
field: 1, | ||
noshare: marker::NoShare, | ||
}; | ||
|
||
static mut SHARABLE: Sharable = Sharable { field: 0 }; | ||
|
||
pub fn fn_mut(_: &mut Sharable) {} | ||
|
||
pub fn main() { | ||
SHARABLE.foo(); | ||
|
||
SHARABLE.foo_mut(); | ||
//~^ ERROR: mutable use of static requires unsafe function or block | ||
|
||
SHARABLE.field = 2; | ||
//~^ ERROR: mutable use of static requires unsafe function or block | ||
|
||
fn_mut(&mut SHARABLE); | ||
//~^ ERROR mutable use of static requires unsafe function or block | ||
|
||
NON_SHARABLE.field = 2; | ||
//~^ ERROR: use of non-Share static mut requires unsafe function or block | ||
//~^^ ERROR: mutable use of static requires unsafe function or block | ||
|
||
SHARABLE = Sharable {field: 1}; | ||
//~^ ERROR: mutable use of static requires unsafe function or block | ||
|
||
let _: &mut Sharable = &mut SHARABLE; | ||
//~^ ERROR mutable use of static requires unsafe function or block | ||
|
||
let _ = &NON_SHARABLE.field; | ||
//~^ ERROR: use of non-Share static mut requires unsafe function or block | ||
|
||
let mut slc = ['a', 'c']; | ||
slc[NON_SHARABLE.field] = 'b'; | ||
//~^ ERROR: use of non-Share static mut requires unsafe function or block | ||
|
||
slc[SHARABLE.field] = 'b'; | ||
} |
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,39 @@ | ||
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT | ||
// file at the top-level directory of this distribution and at | ||
// http://rust-lang.org/COPYRIGHT. | ||
// | ||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
struct Sharable { | ||
field: uint | ||
} | ||
|
||
impl Sharable { | ||
fn foo(&self) {} | ||
fn foo_mut(&mut self) {} | ||
} | ||
|
||
static mut FOO: Sharable = Sharable { field: 1 }; | ||
|
||
fn borrow_static(_: &Sharable) {} | ||
|
||
pub fn main() { | ||
|
||
FOO.foo(); | ||
|
||
borrow_static(&FOO); | ||
|
||
let _ = &FOO; | ||
|
||
unsafe { let _: &mut Sharable = &mut FOO; } | ||
|
||
let mut slc = ['a', 'c']; | ||
slc[FOO.field] = 'b'; | ||
|
||
let _ = &((((FOO)))); | ||
} | ||
|