Skip to content

Commit d03a4b0

Browse files
committed
test: Convert statics to constants
Additionally, add lots of tests for new functionality around statics and `static mut`.
1 parent 9c09c94 commit d03a4b0

File tree

74 files changed

+535
-172
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

74 files changed

+535
-172
lines changed

src/test/auxiliary/cci_const.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@
1111
pub extern fn bar() {
1212
}
1313

14-
pub static foopy: &'static str = "hi there";
15-
pub static uint_val: uint = 12;
16-
pub static uint_expr: uint = (1 << uint_val) - 1;
14+
pub const foopy: &'static str = "hi there";
15+
pub const uint_val: uint = 12;
16+
pub const uint_expr: uint = (1 << uint_val) - 1;

src/test/auxiliary/iss.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ pub struct C<'a> {
1717
}
1818

1919
fn no_op() { }
20-
pub static D : C<'static> = C {
20+
pub const D : C<'static> = C {
2121
k: no_op
2222
};
2323

src/test/auxiliary/issue-13620-1.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@ pub struct Foo {
1414

1515
extern fn the_foo() {}
1616

17-
pub static FOO: Foo = Foo {
17+
pub const FOO: Foo = Foo {
1818
foo: the_foo
1919
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
pub use foo::FOO2;
12+
13+
pub const FOO: uint = 3;
14+
const BAR: uint = 3;
15+
16+
mod foo {
17+
pub const FOO2: uint = 3;
18+
}

src/test/auxiliary/issue-17718.rs

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
use std::sync::atomic;
12+
13+
pub const C1: uint = 1;
14+
pub const C2: atomic::AtomicUint = atomic::INIT_ATOMIC_UINT;
15+
pub const C3: fn() = foo;
16+
pub const C4: uint = C1 * C1 + C1 / C1;
17+
pub const C5: &'static uint = &C4;
18+
19+
pub static S1: uint = 3;
20+
pub static S2: atomic::AtomicUint = atomic::INIT_ATOMIC_UINT;
21+
22+
fn foo() {}

src/test/auxiliary/issue13213aux.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ mod private {
2121
pub struct P {
2222
p: i32,
2323
}
24-
pub static THREE: P = P { p: 3 };
24+
pub const THREE: P = P { p: 3 };
2525
}
2626

2727
pub static A: S = S { p: private::THREE };

src/test/bench/shootout-fasta-redux.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -45,24 +45,24 @@ use std::io::{stdout, IoResult};
4545
use std::os;
4646
use std::slice::bytes::copy_memory;
4747

48-
static LINE_LEN: uint = 60;
49-
static LOOKUP_SIZE: uint = 4 * 1024;
50-
static LOOKUP_SCALE: f32 = (LOOKUP_SIZE - 1) as f32;
48+
const LINE_LEN: uint = 60;
49+
const LOOKUP_SIZE: uint = 4 * 1024;
50+
const LOOKUP_SCALE: f32 = (LOOKUP_SIZE - 1) as f32;
5151

5252
// Random number generator constants
53-
static IM: u32 = 139968;
54-
static IA: u32 = 3877;
55-
static IC: u32 = 29573;
53+
const IM: u32 = 139968;
54+
const IA: u32 = 3877;
55+
const IC: u32 = 29573;
5656

57-
static ALU: &'static str = "GGCCGGGCGCGGTGGCTCACGCCTGTAATCCCAGCACTTTG\
57+
const ALU: &'static str = "GGCCGGGCGCGGTGGCTCACGCCTGTAATCCCAGCACTTTG\
5858
GGAGGCCGAGGCGGGCGGATCACCTGAGGTCAGGAGTTCGA\
5959
GACCAGCCTGGCCAACATGGTGAAACCCCGTCTCTACTAAA\
6060
AATACAAAAATTAGCCGGGCGTGGTGGCGCGCGCCTGTAAT\
6161
CCCAGCTACTCGGGAGGCTGAGGCAGGAGAATCGCTTGAAC\
6262
CCGGGAGGCGGAGGTTGCAGTGAGCCGAGATCGCGCCACTG\
6363
CACTCCAGCCTGGGCGACAGAGCGAGACTCCGTCTCAAAAA";
6464

65-
static NULL_AMINO_ACID: AminoAcid = AminoAcid { c: ' ' as u8, p: 0.0 };
65+
const NULL_AMINO_ACID: AminoAcid = AminoAcid { c: ' ' as u8, p: 0.0 };
6666

6767
static IUB: [AminoAcid, ..15] = [
6868
AminoAcid { c: 'a' as u8, p: 0.27 },

src/test/bench/shootout-fasta.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ use std::io::{BufferedWriter, File};
4545
use std::cmp::min;
4646
use std::os;
4747

48-
static LINE_LENGTH: uint = 60;
49-
static IM: u32 = 139968;
48+
const LINE_LENGTH: uint = 60;
49+
const IM: u32 = 139968;
5050

5151
struct MyRandom {
5252
last: u32

src/test/bench/shootout-mandelbrot.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@ use std::os;
4949
use std::simd::f64x2;
5050
use std::sync::{Arc, Future};
5151

52-
static ITER: int = 50;
53-
static LIMIT: f64 = 2.0;
54-
static WORKERS: uint = 16;
52+
const ITER: int = 50;
53+
const LIMIT: f64 = 2.0;
54+
const WORKERS: uint = 16;
5555

5656
#[inline(always)]
5757
fn mandelbrot<W: io::Writer>(w: uint, mut out: W) -> io::IoResult<()> {
@@ -144,7 +144,7 @@ fn mandelbrot<W: io::Writer>(w: uint, mut out: W) -> io::IoResult<()> {
144144
fn write_line(init_i: f64, vec_init_r: &[f64], res: &mut Vec<u8>) {
145145
let v_init_i : f64x2 = f64x2(init_i, init_i);
146146
let v_2 : f64x2 = f64x2(2.0, 2.0);
147-
static LIMIT_SQUARED: f64 = LIMIT * LIMIT;
147+
const LIMIT_SQUARED: f64 = LIMIT * LIMIT;
148148

149149
for chunk_init_r in vec_init_r.chunks(8) {
150150
let mut cur_byte = 0xff;

src/test/bench/shootout-nbody.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@
3838
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
3939
// OF THE POSSIBILITY OF SUCH DAMAGE.
4040

41-
static PI: f64 = 3.141592653589793;
42-
static SOLAR_MASS: f64 = 4.0 * PI * PI;
43-
static YEAR: f64 = 365.24;
44-
static N_BODIES: uint = 5;
41+
const PI: f64 = 3.141592653589793;
42+
const SOLAR_MASS: f64 = 4.0 * PI * PI;
43+
const YEAR: f64 = 365.24;
44+
const N_BODIES: uint = 5;
4545

4646
static BODIES: [Planet, ..N_BODIES] = [
4747
// Sun

src/test/compile-fail/check-static-immutable-mut-slices.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@
1111
// Checks that immutable static items can't have mutable slices
1212

1313
static TEST: &'static mut [int] = &mut [];
14-
//~^ ERROR static items are not allowed to have mutable slices
14+
//~^ ERROR statics are not allowed to have mutable references
1515

1616
pub fn main() { }

src/test/compile-fail/check-static-values-constraints.rs

+27-23
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
// Verifies all possible restrictions for static items values.
11+
// Verifies all possible restrictions for statics values.
1212

1313
use std::kinds::marker;
1414

@@ -21,7 +21,7 @@ impl Drop for WithDtor {
2121
// This enum will be used to test the following rules:
2222
// 1. Variants are safe for static
2323
// 2. Expr calls are allowed as long as they arguments are safe
24-
// 3. Expr calls with unsafe arguments for static items are rejected
24+
// 3. Expr calls with unsafe arguments for statics are rejected
2525
enum SafeEnum {
2626
Variant1,
2727
Variant2(int),
@@ -35,7 +35,7 @@ static STATIC2: SafeEnum = Variant2(0);
3535

3636
// This one should fail
3737
static STATIC3: SafeEnum = Variant3(WithDtor);
38-
//~^ ERROR static items are not allowed to have destructors
38+
//~^ ERROR statics are not allowed to have destructors
3939

4040

4141
// This enum will be used to test that variants
@@ -52,9 +52,9 @@ impl Drop for UnsafeEnum {
5252

5353

5454
static STATIC4: UnsafeEnum = Variant5;
55-
//~^ ERROR static items are not allowed to have destructors
55+
//~^ ERROR statics are not allowed to have destructors
5656
static STATIC5: UnsafeEnum = Variant6(0);
57-
//~^ ERROR static items are not allowed to have destructors
57+
//~^ ERROR statics are not allowed to have destructors
5858

5959

6060
struct SafeStruct {
@@ -68,7 +68,7 @@ static STATIC6: SafeStruct = SafeStruct{field1: Variant1, field2: Variant2(0)};
6868

6969
// field2 has an unsafe value, hence this should fail
7070
static STATIC7: SafeStruct = SafeStruct{field1: Variant1, field2: Variant3(WithDtor)};
71-
//~^ ERROR static items are not allowed to have destructors
71+
//~^ ERROR statics are not allowed to have destructors
7272

7373
// Test variadic constructor for structs. The base struct should be examined
7474
// as well as every field present in the constructor.
@@ -79,7 +79,7 @@ static STATIC8: SafeStruct = SafeStruct{field1: Variant1,
7979
// This example should fail because field1 in the base struct is not safe
8080
static STATIC9: SafeStruct = SafeStruct{field1: Variant1,
8181
..SafeStruct{field1: Variant3(WithDtor), field2: Variant1}};
82-
//~^ ERROR static items are not allowed to have destructors
82+
//~^ ERROR statics are not allowed to have destructors
8383

8484
struct UnsafeStruct;
8585

@@ -89,44 +89,48 @@ impl Drop for UnsafeStruct {
8989

9090
// Types with destructors are not allowed for statics
9191
static STATIC10: UnsafeStruct = UnsafeStruct;
92-
//~^ ERROR static items are not allowed to have destructor
92+
//~^ ERROR statics are not allowed to have destructor
9393

9494
struct MyOwned;
9595

9696
static STATIC11: Box<MyOwned> = box MyOwned;
97-
//~^ ERROR static items are not allowed to have custom pointers
97+
//~^ ERROR statics are not allowed to have custom pointers
9898

9999
// The following examples test that mutable structs are just forbidden
100100
// to have types with destructors
101101
// These should fail
102102
static mut STATIC12: UnsafeStruct = UnsafeStruct;
103-
//~^ ERROR mutable static items are not allowed to have destructors
103+
//~^ ERROR mutable statics are not allowed to have destructors
104+
//~^^ ERROR statics are not allowed to have destructors
104105

105106
static mut STATIC13: SafeStruct = SafeStruct{field1: Variant1, field2: Variant3(WithDtor)};
106-
//~^ ERROR mutable static items are not allowed to have destructors
107+
//~^ ERROR mutable statics are not allowed to have destructors
108+
//~^^ ERROR: statics are not allowed to have destructors
107109

108110
static mut STATIC14: SafeStruct = SafeStruct {
109-
//~^ ERROR mutable static items are not allowed to have destructors
111+
//~^ ERROR mutable statics are not allowed to have destructors
110112
field1: Variant1,
111113
field2: Variant4("str".to_string())
112114
};
113115

114-
static STATIC15: &'static [Box<MyOwned>] = &[box MyOwned, box MyOwned];
115-
//~^ ERROR static items are not allowed to have custom pointers
116-
//~^^ ERROR static items are not allowed to have custom pointers
116+
static STATIC15: &'static [Box<MyOwned>] = &[
117+
box MyOwned, //~ ERROR statics are not allowed to have custom pointers
118+
box MyOwned, //~ ERROR statics are not allowed to have custom pointers
119+
];
117120

118-
static STATIC16: (&'static Box<MyOwned>, &'static Box<MyOwned>) =
119-
(&box MyOwned, &box MyOwned);
120-
//~^ ERROR static items are not allowed to have custom pointers
121-
//~^^ ERROR static items are not allowed to have custom pointers
121+
static STATIC16: (&'static Box<MyOwned>, &'static Box<MyOwned>) = (
122+
&box MyOwned, //~ ERROR statics are not allowed to have custom pointers
123+
&box MyOwned, //~ ERROR statics are not allowed to have custom pointers
124+
);
122125

123126
static mut STATIC17: SafeEnum = Variant1;
124-
//~^ ERROR mutable static items are not allowed to have destructors
127+
//~^ ERROR mutable statics are not allowed to have destructors
125128

126-
static STATIC19: Box<int> = box 3;
127-
//~^ ERROR static items are not allowed to have custom pointers
129+
static STATIC19: Box<int> =
130+
box 3;
131+
//~^ ERROR statics are not allowed to have custom pointers
128132

129133
pub fn main() {
130134
let y = { static x: Box<int> = box 3; x };
131-
//~^ ERROR static items are not allowed to have custom pointers
135+
//~^ ERROR statics are not allowed to have custom pointers
132136
}

src/test/compile-fail/issue-15524.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
static N: int = 1;
11+
const N: int = 1;
1212

1313
enum Foo {
1414
A = 1,

src/test/compile-fail/issue-16149.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ extern {
1515
fn main() {
1616
let boolValue = match 42 {
1717
externalValue => true,
18-
//~^ ERROR extern statics cannot be referenced in patterns
18+
//~^ ERROR static variables cannot be referenced in a pattern
1919
_ => false
2020
};
2121
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
struct S { a: uint }
12+
static A: S = S { a: 3 };
13+
static B: &'static uint = &A.a;
14+
//~^ ERROR: cannot refer to the interior of another static
15+
static C: &'static uint = &(A.a);
16+
//~^ ERROR: cannot refer to the interior of another static
17+
18+
static D: [uint, ..1] = [1];
19+
static E: uint = D[0];
20+
//~^ ERROR: cannot refer to other statics by value
21+
static F: &'static uint = &D[0];
22+
//~^ ERROR: cannot refer to the interior of another static
23+
24+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
const C1: &'static mut [uint] = &mut [];
12+
//~^ ERROR: constants are not allowed to have mutable references
13+
14+
static mut S: uint = 3;
15+
const C2: &'static mut uint = &mut S;
16+
//~^ ERROR: constants cannot refer to other statics
17+
//~^^ ERROR: are not allowed to have mutable references
18+
19+
fn main() {}
20+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
use std::cell::UnsafeCell;
12+
13+
const A: UnsafeCell<uint> = UnsafeCell { value: 1 };
14+
const B: &'static UnsafeCell<uint> = &A;
15+
//~^ ERROR: cannot borrow a constant which contains interior mutability
16+
17+
struct C { a: UnsafeCell<uint> }
18+
const D: C = C { a: UnsafeCell { value: 1 } };
19+
const E: &'static UnsafeCell<uint> = &D.a;
20+
//~^ ERROR: cannot borrow a constant which contains interior mutability
21+
const F: &'static C = &D;
22+
//~^ ERROR: cannot borrow a constant which contains interior mutability
23+
24+
fn main() {}

src/test/compile-fail/issue-17718-const-destructors.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@ impl Drop for A {
1414
}
1515

1616
const FOO: A = A;
17-
//~ ERROR: constants are not allowed to have destructors
17+
//~^ ERROR: constants are not allowed to have destructors
1818

1919
fn main() {}

0 commit comments

Comments
 (0)