Skip to content

Commit

Permalink
auto merge of #17853 : alexcrichton/rust/issue-17718, r=pcwalton
Browse files Browse the repository at this point in the history
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
  • Loading branch information
bors committed Oct 10, 2014
2 parents 386c19d + 0c0a925 commit 5c81a03
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
6 changes: 3 additions & 3 deletions chacha.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ use core::prelude::*;

use {Rng, SeedableRng, Rand};

static KEY_WORDS : uint = 8; // 8 words for the 256-bit key
static STATE_WORDS : uint = 16;
static CHACHA_ROUNDS: uint = 20; // Cryptographically secure from 8 upwards as of this writing
const KEY_WORDS : uint = 8; // 8 words for the 256-bit key
const STATE_WORDS : uint = 16;
const CHACHA_ROUNDS: uint = 20; // Cryptographically secure from 8 upwards as of this writing

/// A random number generator that uses the ChaCha20 algorithm [1].
///
Expand Down
14 changes: 7 additions & 7 deletions isaac.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ use core::slice::raw;

use {Rng, SeedableRng, Rand};

static RAND_SIZE_LEN: u32 = 8;
static RAND_SIZE: u32 = 1 << (RAND_SIZE_LEN as uint);
static RAND_SIZE_UINT: uint = 1 << (RAND_SIZE_LEN as uint);
const RAND_SIZE_LEN: u32 = 8;
const RAND_SIZE: u32 = 1 << (RAND_SIZE_LEN as uint);
const RAND_SIZE_UINT: uint = 1 << (RAND_SIZE_LEN as uint);

/// A random number generator that uses the ISAAC algorithm[1].
///
Expand Down Expand Up @@ -251,8 +251,8 @@ impl Rand for IsaacRng {
}
}

static RAND_SIZE_64_LEN: uint = 8;
static RAND_SIZE_64: uint = 1 << RAND_SIZE_64_LEN;
const RAND_SIZE_64_LEN: uint = 8;
const RAND_SIZE_64: uint = 1 << RAND_SIZE_64_LEN;

/// A random number generator that uses ISAAC-64[1], the 64-bit
/// variant of the ISAAC algorithm.
Expand Down Expand Up @@ -356,8 +356,8 @@ impl Isaac64Rng {
// abbreviations
let mut a = self.a;
let mut b = self.b + self.c;
static MIDPOINT: uint = RAND_SIZE_64 / 2;
static MP_VEC: [(uint, uint), .. 2] = [(0,MIDPOINT), (MIDPOINT, 0)];
const MIDPOINT: uint = RAND_SIZE_64 / 2;
const MP_VEC: [(uint, uint), .. 2] = [(0,MIDPOINT), (MIDPOINT, 0)];
macro_rules! ind (
($x:expr) => {
*self.mem.unsafe_get(($x as uint >> 3) & (RAND_SIZE_64 - 1))
Expand Down

0 comments on commit 5c81a03

Please sign in to comment.