Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit f9fc49c

Browse files
committedOct 10, 2014
auto merge of #17853 : alexcrichton/rust/issue-17718, r=pcwalton
This change is an implementation of [RFC 69][rfc] which adds a third kind of global to the language, `const`. This global is most similar to what the old `static` was, and if you're unsure about what to use then you should use a `const`. The semantics of these three kinds of globals are: * A `const` does not represent a memory location, but only a value. Constants are translated as rvalues, which means that their values are directly inlined at usage location (similar to a #define in C/C++). Constant values are, well, constant, and can not be modified. Any "modification" is actually a modification to a local value on the stack rather than the actual constant itself. Almost all values are allowed inside constants, whether they have interior mutability or not. There are a few minor restrictions listed in the RFC, but they should in general not come up too often. * A `static` now always represents a memory location (unconditionally). Any references to the same `static` are actually a reference to the same memory location. Only values whose types ascribe to `Sync` are allowed in a `static`. This restriction is in place because many threads may access a `static` concurrently. Lifting this restriction (and allowing unsafe access) is a future extension not implemented at this time. * A `static mut` continues to always represent a memory location. All references to a `static mut` continue to be `unsafe`. This is a large breaking change, and many programs will need to be updated accordingly. A summary of the breaking changes is: * Statics may no longer be used in patterns. Statics now always represent a memory location, which can sometimes be modified. To fix code, repurpose the matched-on-`static` to a `const`. static FOO: uint = 4; match n { FOO => { /* ... */ } _ => { /* ... */ } } change this code to: const FOO: uint = 4; match n { FOO => { /* ... */ } _ => { /* ... */ } } * Statics may no longer refer to other statics by value. Due to statics being able to change at runtime, allowing them to reference one another could possibly lead to confusing semantics. If you are in this situation, use a constant initializer instead. Note, however, that statics may reference other statics by address, however. * Statics may no longer be used in constant expressions, such as array lengths. This is due to the same restrictions as listed above. Use a `const` instead. [breaking-change] Closes #17718 [rfc]: rust-lang/rfcs#246
2 parents 8b12fb3 + 0b51711 commit f9fc49c

File tree

193 files changed

+4052
-3272
lines changed

Some content is hidden

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

193 files changed

+4052
-3272
lines changed
 

‎src/doc/reference.md

Lines changed: 65 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1383,44 +1383,87 @@ a = Cat { name: "Spotty".to_string(), weight: 2.7 };
13831383
In this example, `Cat` is a _struct-like enum variant_,
13841384
whereas `Dog` is simply called an enum variant.
13851385

1386-
### Static items
1386+
### Constant items
13871387

13881388
```{.ebnf .gram}
1389-
static_item : "static" ident ':' type '=' expr ';' ;
1389+
const_item : "const" ident ':' type '=' expr ';' ;
13901390
```
13911391

1392-
A *static item* is a named _constant value_ stored in the global data section
1393-
of a crate. Immutable static items are stored in the read-only data section.
1394-
The constant value bound to a static item is, like all constant values,
1395-
evaluated at compile time. Static items have the `static` lifetime, which
1396-
outlives all other lifetimes in a Rust program. Only values stored in the
1397-
global data section (such as string constants and static items) can have the
1398-
`static` lifetime; dynamically constructed values cannot safely be assigned the
1399-
`static` lifetime. Static items are declared with the `static` keyword. A
1400-
static item must have a _constant expression_ giving its definition.
1392+
A *constant item* is a named _constant value_ which is not associated with a
1393+
specific memory location in the program. Constants are essentially inlined
1394+
wherever they are used, meaning that they are copied directly into the relevant
1395+
context when used. References to the same constant are not necessarily
1396+
guaranteed to refer to the same memory address.
1397+
1398+
Constant values must not have destructors, and otherwise permit most forms of
1399+
data. Constants may refer to the address of other constants, in which case the
1400+
address will have the `static` lifetime. The compiler is, however, still at
1401+
liberty to translate the constant many times, so the address referred to may not
1402+
be stable.
14011403

1402-
Static items must be explicitly typed. The type may be `bool`, `char`,
1403-
a number, or a type derived from those primitive types. The derived types are
1404-
references with the `static` lifetime, fixed-size arrays, tuples, and structs.
1404+
Constants must be explicitly typed. The type may be `bool`, `char`, a number, or
1405+
a type derived from those primitive types. The derived types are references with
1406+
the `static` lifetime, fixed-size arrays, tuples, enum variants, and structs.
14051407

14061408
```
1407-
static BIT1: uint = 1 << 0;
1408-
static BIT2: uint = 1 << 1;
1409+
const BIT1: uint = 1 << 0;
1410+
const BIT2: uint = 1 << 1;
14091411
1410-
static BITS: [uint, ..2] = [BIT1, BIT2];
1411-
static STRING: &'static str = "bitstring";
1412+
const BITS: [uint, ..2] = [BIT1, BIT2];
1413+
const STRING: &'static str = "bitstring";
14121414
14131415
struct BitsNStrings<'a> {
14141416
mybits: [uint, ..2],
14151417
mystring: &'a str
14161418
}
14171419
1418-
static BITS_N_STRINGS: BitsNStrings<'static> = BitsNStrings {
1420+
const BITS_N_STRINGS: BitsNStrings<'static> = BitsNStrings {
14191421
mybits: BITS,
14201422
mystring: STRING
14211423
};
14221424
```
14231425

1426+
### Static items
1427+
1428+
```{.ebnf .gram}
1429+
static_item : "static" ident ':' type '=' expr ';' ;
1430+
```
1431+
1432+
A *static item* is similar to a *constant*, except that it represents a precise
1433+
memory location in the program. A static is never "inlined" at the usage site,
1434+
and all references to it refer to the same memory location. Static items have
1435+
the `static` lifetime, which outlives all other lifetimes in a Rust program.
1436+
Static items may be placed in read-only memory if they do not contain any
1437+
interior mutability.
1438+
1439+
Statics may contain interior mutability through the `UnsafeCell` language item.
1440+
All access to a static is safe, but there are a number of restrictions on
1441+
statics:
1442+
1443+
* Statics may not contain any destructors.
1444+
* The types of static values must ascribe to `Sync` to allow threadsafe access.
1445+
* Statics may not refer to other statics by value, only by reference.
1446+
* Constants cannot refer to statics.
1447+
1448+
Constants should in general be preferred over statics, unless large amounts of
1449+
data are being stored, or single-address and mutability properties are required.
1450+
1451+
```
1452+
use std::sync::atomic;
1453+
1454+
// Note that INIT_ATOMIC_UINT is a *const*, but it may be used to initialize a
1455+
// static. This static can be modified, so it is not placed in read-only memory.
1456+
static COUNTER: atomic::AtomicUint = atomic::INIT_ATOMIC_UINT;
1457+
1458+
// This table is a candidate to be placed in read-only memory.
1459+
static TABLE: &'static [uint] = &[1, 2, 3, /* ... */];
1460+
1461+
for slot in TABLE.iter() {
1462+
println!("{}", slot);
1463+
}
1464+
COUNTER.fetch_add(1, atomic::SeqCst);
1465+
```
1466+
14241467
#### Mutable statics
14251468

14261469
If a static item is declared with the `mut` keyword, then it is allowed to
@@ -1455,6 +1498,9 @@ unsafe fn bump_levels_unsafe2() -> uint {
14551498
}
14561499
```
14571500

1501+
Mutable statics have the same restrictions as normal statics, except that the
1502+
type of the value is not required to ascribe to `Sync`.
1503+
14581504
### Traits
14591505

14601506
A _trait_ describes a set of method types.

‎src/etc/unicode.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -333,14 +333,14 @@ def emit_property_module(f, mod, tbl, emit_fn):
333333
def emit_regex_module(f, cats, w_data):
334334
f.write("pub mod regex {\n")
335335
regex_class = "&'static [(char, char)]"
336-
class_table = "&'static [(&'static str, %s)]" % regex_class
336+
class_table = "&'static [(&'static str, &'static %s)]" % regex_class
337337

338338
emit_table(f, "UNICODE_CLASSES", cats, class_table,
339-
pfun=lambda x: "(\"%s\",super::%s::%s_table)" % (x[0], x[1], x[0]))
339+
pfun=lambda x: "(\"%s\",&super::%s::%s_table)" % (x[0], x[1], x[0]))
340340

341-
f.write(" pub static PERLD: %s = super::general_category::Nd_table;\n\n"
341+
f.write(" pub static PERLD: &'static %s = &super::general_category::Nd_table;\n\n"
342342
% regex_class)
343-
f.write(" pub static PERLS: %s = super::property::White_Space_table;\n\n"
343+
f.write(" pub static PERLS: &'static %s = &super::property::White_Space_table;\n\n"
344344
% regex_class)
345345

346346
emit_table(f, "PERLW", w_data, regex_class)

0 commit comments

Comments
 (0)
Please sign in to comment.