Use const fn to abstract away the contents of UnsafeCell & friends.

This commit is contained in:
Eduard Burtescu 2015-05-27 11:18:36 +03:00
parent 6e8e4f847c
commit 377b0900ae
76 changed files with 417 additions and 525 deletions

View file

@ -11,12 +11,12 @@
use std::sync::atomic;
pub const C1: usize = 1;
pub const C2: atomic::AtomicUsize = atomic::ATOMIC_USIZE_INIT;
pub const C2: atomic::AtomicUsize = atomic::AtomicUsize::new(0);
pub const C3: fn() = foo;
pub const C4: usize = C1 * C1 + C1 / C1;
pub const C5: &'static usize = &C4;
pub static S1: usize = 3;
pub static S2: atomic::AtomicUsize = atomic::ATOMIC_USIZE_INIT;
pub static S2: atomic::AtomicUsize = atomic::AtomicUsize::new(0);
fn foo() {}

View file

@ -18,9 +18,9 @@ use id::Id;
mod s {
#![allow(unstable)]
use std::sync::atomic::{AtomicUsize, ATOMIC_USIZE_INIT, Ordering};
use std::sync::atomic::{AtomicUsize, Ordering};
static S_COUNT: AtomicUsize = ATOMIC_USIZE_INIT;
static S_COUNT: AtomicUsize = AtomicUsize::new(0);
pub fn next_count() -> usize {
S_COUNT.fetch_add(1, Ordering::SeqCst) + 1

View file

@ -26,9 +26,9 @@ use id::Id;
mod s {
#![allow(unstable)]
use std::sync::atomic::{AtomicUsize, ATOMIC_USIZE_INIT, Ordering};
use std::sync::atomic::{AtomicUsize, Ordering};
static S_COUNT: AtomicUsize = ATOMIC_USIZE_INIT;
static S_COUNT: AtomicUsize = AtomicUsize::new(0);
pub fn next_count() -> usize {
S_COUNT.fetch_add(1, Ordering::SeqCst) + 1

View file

@ -17,9 +17,9 @@ use std::cell::Cell;
use id::Id;
mod s {
use std::sync::atomic::{AtomicUsize, ATOMIC_USIZE_INIT, Ordering};
use std::sync::atomic::{AtomicUsize, Ordering};
static S_COUNT: AtomicUsize = ATOMIC_USIZE_INIT;
static S_COUNT: AtomicUsize = AtomicUsize::new(0);
pub fn next_count() -> usize {
S_COUNT.fetch_add(1, Ordering::SeqCst) + 1

View file

@ -17,9 +17,9 @@ use id::Id;
mod s {
#![allow(unstable)]
use std::sync::atomic::{AtomicUsize, ATOMIC_USIZE_INIT, Ordering};
use std::sync::atomic::{AtomicUsize, Ordering};
static S_COUNT: AtomicUsize = ATOMIC_USIZE_INIT;
static S_COUNT: AtomicUsize = AtomicUsize::new(0);
pub fn next_count() -> usize {
S_COUNT.fetch_add(1, Ordering::SeqCst) + 1

View file

@ -16,7 +16,7 @@ use self::foo::S;
mod foo {
use std::cell::{UnsafeCell};
static mut count : UnsafeCell<u64> = UnsafeCell { value: 1 };
static mut count : UnsafeCell<u64> = UnsafeCell::new(1);
pub struct S { pub a: u8, pub b: String, secret_uid: u64 }

View file

@ -10,12 +10,12 @@
use std::cell::UnsafeCell;
const A: UnsafeCell<usize> = UnsafeCell { value: 1 };
const A: UnsafeCell<usize> = UnsafeCell::new(1);
const B: &'static UnsafeCell<usize> = &A;
//~^ ERROR: cannot borrow a constant which contains interior mutability
struct C { a: UnsafeCell<usize> }
const D: C = C { a: UnsafeCell { value: 1 } };
const D: C = C { a: UnsafeCell::new(1) };
const E: &'static UnsafeCell<usize> = &D.a;
//~^ ERROR: cannot borrow a constant which contains interior mutability
const F: &'static C = &D;

View file

@ -17,6 +17,5 @@ static boxed: Box<RefCell<isize>> = box RefCell::new(0);
//~^ ERROR allocations are not allowed in statics
//~| ERROR the trait `core::marker::Sync` is not implemented for the type
//~| ERROR the trait `core::marker::Sync` is not implemented for the type
//~| ERROR E0015
fn main() { }

View file

@ -15,11 +15,11 @@ use std::sync::atomic::*;
use std::ptr;
fn main() {
let x = ATOMIC_BOOL_INIT;
let x = AtomicBool::new(false);
let x = *&x; //~ ERROR: cannot move out of borrowed content
let x = ATOMIC_ISIZE_INIT;
let x = AtomicIsize::new(0);
let x = *&x; //~ ERROR: cannot move out of borrowed content
let x = ATOMIC_USIZE_INIT;
let x = AtomicUsize::new(0);
let x = *&x; //~ ERROR: cannot move out of borrowed content
let x: AtomicPtr<usize> = AtomicPtr::new(ptr::null_mut());
let x = *&x; //~ ERROR: cannot move out of borrowed content

View file

@ -28,9 +28,9 @@ use id::Id;
mod s {
#![allow(unstable)]
use std::sync::atomic::{AtomicUsize, ATOMIC_USIZE_INIT, Ordering};
use std::sync::atomic::{AtomicUsize, Ordering};
static S_COUNT: AtomicUsize = ATOMIC_USIZE_INIT;
static S_COUNT: AtomicUsize = AtomicUsize::new(0);
/// generates globally unique count (global across the current
/// process, that is)

View file

@ -19,7 +19,7 @@
// This test makes sure that the compiler doesn't crash when trying to assign
// debug locations to const-expressions.
use std::sync::MUTEX_INIT;
use std::sync::StaticMutex;
use std::cell::UnsafeCell;
const CONSTANT: u64 = 3 + 4;
@ -49,7 +49,7 @@ const VEC: [u32; 8] = [0; 8];
const NESTED: (Struct, TupleStruct) = (STRUCT, TUPLE_STRUCT);
const UNSAFE_CELL: UnsafeCell<bool> = UnsafeCell { value: false };
const UNSAFE_CELL: UnsafeCell<bool> = UnsafeCell::new(false);
fn main() {
let mut _constant = CONSTANT;
@ -61,6 +61,6 @@ fn main() {
let mut _string = STRING;
let mut _vec = VEC;
let mut _nested = NESTED;
let mut _extern = MUTEX_INIT;
let mut _extern = StaticMutex::new();
let mut _unsafe_cell = UNSAFE_CELL;
}

View file

@ -22,7 +22,7 @@ enum E {
C = 2
}
static FLAG: atomic::AtomicUsize = atomic::ATOMIC_USIZE_INIT;
static FLAG: atomic::AtomicUsize = atomic::AtomicUsize::new(0);
impl Drop for E {
fn drop(&mut self) {

View file

@ -13,10 +13,10 @@
// `T`. Issue #20300.
use std::marker::{PhantomData};
use std::sync::atomic::{AtomicUsize, ATOMIC_USIZE_INIT};
use std::sync::atomic::{AtomicUsize};
use std::sync::atomic::Ordering::SeqCst;
static COUNTER: AtomicUsize = ATOMIC_USIZE_INIT;
static COUNTER: AtomicUsize = AtomicUsize::new(0);
// Preamble.
trait Trait { type Item; }

View file

@ -12,9 +12,9 @@
// destructor.
use std::thread;
use std::sync::atomic::{AtomicUsize, ATOMIC_USIZE_INIT, Ordering};
use std::sync::atomic::{AtomicUsize, Ordering};
static LOG: AtomicUsize = ATOMIC_USIZE_INIT;
static LOG: AtomicUsize = AtomicUsize::new(0);
struct D(u8);

View file

@ -12,9 +12,9 @@
// destructor.
use std::thread;
use std::sync::atomic::{AtomicUsize, ATOMIC_USIZE_INIT, Ordering};
use std::sync::atomic::{AtomicUsize, Ordering};
static LOG: AtomicUsize = ATOMIC_USIZE_INIT;
static LOG: AtomicUsize = AtomicUsize::new(0);
struct D(u8);

View file

@ -38,8 +38,8 @@ unsafe impl<T: Send> Sync for UnsafeEnum<T> {}
static STATIC1: UnsafeEnum<isize> = UnsafeEnum::VariantSafe;
static STATIC2: MyUnsafePack<isize> = MyUnsafePack(UnsafeCell { value: 1 });
const CONST: MyUnsafePack<isize> = MyUnsafePack(UnsafeCell { value: 1 });
static STATIC2: MyUnsafePack<isize> = MyUnsafePack(UnsafeCell::new(1));
const CONST: MyUnsafePack<isize> = MyUnsafePack(UnsafeCell::new(1));
static STATIC3: MyUnsafe<isize> = MyUnsafe{value: CONST};
static STATIC4: &'static MyUnsafePack<isize> = &STATIC2;
@ -50,7 +50,7 @@ struct Wrap<T> {
unsafe impl<T: Send> Sync for Wrap<T> {}
static UNSAFE: MyUnsafePack<isize> = MyUnsafePack(UnsafeCell{value: 2});
static UNSAFE: MyUnsafePack<isize> = MyUnsafePack(UnsafeCell::new(2));
static WRAPPED_UNSAFE: Wrap<&'static MyUnsafePack<isize>> = Wrap { value: &UNSAFE };
fn main() {

View file

@ -15,10 +15,10 @@
extern crate issue_17718 as other;
use std::sync::atomic::{AtomicUsize, ATOMIC_USIZE_INIT, Ordering};
use std::sync::atomic::{AtomicUsize, Ordering};
const C1: usize = 1;
const C2: AtomicUsize = ATOMIC_USIZE_INIT;
const C2: AtomicUsize = AtomicUsize::new(0);
const C3: fn() = foo;
const C4: usize = C1 * C1 + C1 / C1;
const C5: &'static usize = &C4;
@ -28,7 +28,7 @@ const C6: usize = {
};
static S1: usize = 3;
static S2: AtomicUsize = ATOMIC_USIZE_INIT;
static S2: AtomicUsize = AtomicUsize::new(0);
mod test {
static A: usize = 4;

View file

@ -13,7 +13,7 @@
// construction.
use std::sync::atomic::{Ordering, AtomicUsize, ATOMIC_USIZE_INIT};
use std::sync::atomic::{Ordering, AtomicUsize};
#[derive(Debug)]
struct Noisy(u8);
@ -69,7 +69,7 @@ pub fn main() {
assert_eq!(0x03_04, event_log());
}
static LOG: AtomicUsize = ATOMIC_USIZE_INIT;
static LOG: AtomicUsize = AtomicUsize::new(0);
fn reset_log() {
LOG.store(0, Ordering::SeqCst);

View file

@ -14,9 +14,9 @@
use std::thread;
use std::sync::atomic::{AtomicUsize, ATOMIC_USIZE_INIT, Ordering};
use std::sync::atomic::{AtomicUsize, Ordering};
static LOG: AtomicUsize = ATOMIC_USIZE_INIT;
static LOG: AtomicUsize = AtomicUsize::new(0);
struct D(u8);

View file

@ -12,7 +12,7 @@
// even when no Drop-implementations are involved.
use std::sync::atomic::{Ordering, AtomicUsize, ATOMIC_USIZE_INIT};
use std::sync::atomic::{Ordering, AtomicUsize};
struct W { wrapped: u32 }
struct S { f0: W, _f1: i32 }
@ -34,7 +34,7 @@ pub fn main() {
"expect: 0x{:x} actual: 0x{:x}", expect, actual);
}
static LOG: AtomicUsize = ATOMIC_USIZE_INIT;
static LOG: AtomicUsize = AtomicUsize::new(0);
fn event_log() -> usize {
LOG.load(Ordering::SeqCst)

View file

@ -12,7 +12,7 @@
// even when no Drop-implementations are involved.
use std::sync::atomic::{Ordering, AtomicUsize, ATOMIC_USIZE_INIT};
use std::sync::atomic::{Ordering, AtomicUsize};
struct W { wrapped: u32 }
struct S { f0: W, _f1: i32 }
@ -31,7 +31,7 @@ pub fn main() {
"expect: 0x{:x} actual: 0x{:x}", expect, actual);
}
static LOG: AtomicUsize = ATOMIC_USIZE_INIT;
static LOG: AtomicUsize = AtomicUsize::new(0);
fn event_log() -> usize {
LOG.load(Ordering::SeqCst)

View file

@ -11,7 +11,7 @@
#![feature(rand, core)]
use std::sync::atomic::{AtomicUsize, ATOMIC_USIZE_INIT, Ordering};
use std::sync::atomic::{AtomicUsize, Ordering};
use std::__rand::{thread_rng, Rng};
use std::thread;
@ -20,20 +20,20 @@ const MAX_LEN: usize = 32;
static drop_counts: [AtomicUsize; MAX_LEN] =
// FIXME #5244: AtomicUsize is not Copy.
[
ATOMIC_USIZE_INIT, ATOMIC_USIZE_INIT, ATOMIC_USIZE_INIT,
ATOMIC_USIZE_INIT, ATOMIC_USIZE_INIT, ATOMIC_USIZE_INIT,
ATOMIC_USIZE_INIT, ATOMIC_USIZE_INIT, ATOMIC_USIZE_INIT,
ATOMIC_USIZE_INIT, ATOMIC_USIZE_INIT, ATOMIC_USIZE_INIT,
ATOMIC_USIZE_INIT, ATOMIC_USIZE_INIT, ATOMIC_USIZE_INIT,
ATOMIC_USIZE_INIT, ATOMIC_USIZE_INIT, ATOMIC_USIZE_INIT,
ATOMIC_USIZE_INIT, ATOMIC_USIZE_INIT, ATOMIC_USIZE_INIT,
ATOMIC_USIZE_INIT, ATOMIC_USIZE_INIT, ATOMIC_USIZE_INIT,
ATOMIC_USIZE_INIT, ATOMIC_USIZE_INIT, ATOMIC_USIZE_INIT,
ATOMIC_USIZE_INIT, ATOMIC_USIZE_INIT, ATOMIC_USIZE_INIT,
ATOMIC_USIZE_INIT, ATOMIC_USIZE_INIT,
AtomicUsize::new(0), AtomicUsize::new(0), AtomicUsize::new(0),
AtomicUsize::new(0), AtomicUsize::new(0), AtomicUsize::new(0),
AtomicUsize::new(0), AtomicUsize::new(0), AtomicUsize::new(0),
AtomicUsize::new(0), AtomicUsize::new(0), AtomicUsize::new(0),
AtomicUsize::new(0), AtomicUsize::new(0), AtomicUsize::new(0),
AtomicUsize::new(0), AtomicUsize::new(0), AtomicUsize::new(0),
AtomicUsize::new(0), AtomicUsize::new(0), AtomicUsize::new(0),
AtomicUsize::new(0), AtomicUsize::new(0), AtomicUsize::new(0),
AtomicUsize::new(0), AtomicUsize::new(0), AtomicUsize::new(0),
AtomicUsize::new(0), AtomicUsize::new(0), AtomicUsize::new(0),
AtomicUsize::new(0), AtomicUsize::new(0),
];
static creation_count: AtomicUsize = ATOMIC_USIZE_INIT;
static creation_count: AtomicUsize = AtomicUsize::new(0);
#[derive(Clone, PartialEq, PartialOrd, Eq, Ord)]
struct DropCounter { x: u32, creation_id: usize }