rust/src/librustc/middle/trans
Alex Crichton 90d03d7926 rustc: Add const globals to the language
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]

[rfc]: https://github.com/rust-lang/rfcs/pull/246
2014-10-09 09:44:50 -07:00
..
_match.rs rustc: Add const globals to the language 2014-10-09 09:44:50 -07:00
adt.rs Set the non_uppercase_statics lint to warn by default 2014-10-03 20:39:56 +13:00
asm.rs Fix librustc 2014-09-30 12:52:47 -07:00
base.rs rustc: Add const globals to the language 2014-10-09 09:44:50 -07:00
basic_block.rs
build.rs
builder.rs Use slice syntax instead of slice_to, etc. 2014-10-07 15:49:53 +13:00
cabi_arm.rs
cabi_mips.rs
cabi_x86_64.rs
cabi_x86_win64.rs
cabi_x86.rs
cabi.rs
callee.rs rustc: Add const globals to the language 2014-10-09 09:44:50 -07:00
cleanup.rs rustc: remove support for Gc. 2014-10-02 16:59:31 +03:00
closure.rs Correctly trans capture-by-ref unboxed closures 2014-10-02 21:08:45 -07:00
common.rs rustc: remove support for Gc. 2014-10-02 16:59:31 +03:00
consts.rs rustc: Add const globals to the language 2014-10-09 09:44:50 -07:00
context.rs rustc: Add const globals to the language 2014-10-09 09:44:50 -07:00
controlflow.rs debuginfo: Make sure that all calls to drop glue are associated with debug locations. 2014-09-25 14:17:14 +02:00
datum.rs rustc: remove support for Gc. 2014-10-02 16:59:31 +03:00
debuginfo.rs rustc: Add const globals to the language 2014-10-09 09:44:50 -07:00
doc.rs rustc: remove support for Gc. 2014-10-02 16:59:31 +03:00
expr.rs rustc: Add const globals to the language 2014-10-09 09:44:50 -07:00
foreign.rs
glue.rs Correctly generate drop glue for Box<str> 2014-10-03 23:00:20 -07:00
inline.rs rustc: Add const globals to the language 2014-10-09 09:44:50 -07:00
intrinsic.rs Add intrinsics::unreachable 2014-10-04 20:09:09 -07:00
llrepr.rs
machine.rs
macros.rs
meth.rs Integrate builtin bounds fully into the trait checker 2014-09-25 07:06:27 -04:00
mod.rs
monomorphize.rs Remove dead code from librustc 2014-09-24 21:03:55 +02:00
reflect.rs Remove use of final and override (now reserved) 2014-10-07 22:18:12 -04:00
tvec.rs rustc: remove support for Gc. 2014-10-02 16:59:31 +03:00
type_.rs
type_of.rs rustc: remove support for Gc. 2014-10-02 16:59:31 +03:00
value.rs