-
Notifications
You must be signed in to change notification settings - Fork 12.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
rustc: Add const
globals to the language
#17853
Commits on Oct 9, 2014
-
rustc: Reformat check_const with modern style
Remove a bunch of two-space tabs
Configuration menu - View commit details
-
Copy full SHA for a89ad58 - Browse repository at this point
Copy the full SHA a89ad58View commit details -
rustc: Add
const
globals to the languageThis 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]: rust-lang/rfcs#246
Configuration menu - View commit details
-
Copy full SHA for 90d03d7 - Browse repository at this point
Copy the full SHA 90d03d7View commit details -
Configuration menu - View commit details
-
Copy full SHA for 4d87af9 - Browse repository at this point
Copy the full SHA 4d87af9View commit details -
Configuration menu - View commit details
-
Copy full SHA for 1a43377 - Browse repository at this point
Copy the full SHA 1a43377View commit details -
The tables in libunicode are far too large to want to be inlined into any other program, so these tables are all going to remain `static`. For them to be legal, they cannot reference one another by value, but instead use references now. This commit also modifies the src/etc/unicode.py script to generate the right tables.
Configuration menu - View commit details
-
Copy full SHA for 34d66de - Browse repository at this point
Copy the full SHA 34d66deView commit details -
Configuration menu - View commit details
-
Copy full SHA for abb3d3e - Browse repository at this point
Copy the full SHA abb3d3eView commit details -
libc: Convert all statics to constant
This crate is largely just one giant header file, so there's no need for any of these values to actually have a memory location, they're all just basically a regular #define.
Configuration menu - View commit details
-
Copy full SHA for 3370053 - Browse repository at this point
Copy the full SHA 3370053View commit details -
rand: Convert statics to constants
This leaves the ziggurat tables as `pub static` as they're likely too large to want to go into the metadata anyway.
Configuration menu - View commit details
-
Copy full SHA for d9874bf - Browse repository at this point
Copy the full SHA d9874bfView commit details -
std: Convert statics to constants
This commit repurposes most statics as constants in the standard library itself, with the exception of TLS keys which precisely have their own memory location as an implementation detail. This commit also rewrites the bitflags syntax to use `const` instead of `static`. All invocations will need to replace the word `static` with `const` when declaring flags. Due to the modification of the `bitflags!` syntax, this is a: [breaking-change]
Configuration menu - View commit details
-
Copy full SHA for ab5935c - Browse repository at this point
Copy the full SHA ab5935cView commit details -
Configuration menu - View commit details
-
Copy full SHA for 6d4cf37 - Browse repository at this point
Copy the full SHA 6d4cf37View commit details -
Configuration menu - View commit details
-
Copy full SHA for abb1b2a - Browse repository at this point
Copy the full SHA abb1b2aView commit details -
regex: Convert statics to constants
This require a bit of finesse to work around the changes with libunicode, but nothing too major!
Configuration menu - View commit details
-
Copy full SHA for a64bb66 - Browse repository at this point
Copy the full SHA a64bb66View commit details -
Configuration menu - View commit details
-
Copy full SHA for 6532a8c - Browse repository at this point
Copy the full SHA 6532a8cView commit details -
Configuration menu - View commit details
-
Copy full SHA for 8ccb616 - Browse repository at this point
Copy the full SHA 8ccb616View commit details -
Configuration menu - View commit details
-
Copy full SHA for edf8841 - Browse repository at this point
Copy the full SHA edf8841View commit details -
Configuration menu - View commit details
-
Copy full SHA for d3eaf32 - Browse repository at this point
Copy the full SHA d3eaf32View commit details -
Configuration menu - View commit details
-
Copy full SHA for b8fb0cf - Browse repository at this point
Copy the full SHA b8fb0cfView commit details -
Configuration menu - View commit details
-
Copy full SHA for 831f909 - Browse repository at this point
Copy the full SHA 831f909View commit details -
Configuration menu - View commit details
-
Copy full SHA for 1bfe450 - Browse repository at this point
Copy the full SHA 1bfe450View commit details -
rustdoc: Implement constant documentation
At the same time, migrate statics to constants.
Configuration menu - View commit details
-
Copy full SHA for 01d58fe - Browse repository at this point
Copy the full SHA 01d58feView commit details -
syntax: Tweak the return value of bytes!()
Instead of returning &'static [u8], an invocation of `bytes!()` now returns `&'static [u8, ..N]` where `N` is the length of the byte vector. This should functionally be the same, but there are some cases where an explicit cast may be needed, so this is a: [breaking-change]
Configuration menu - View commit details
-
Copy full SHA for 9c09c94 - Browse repository at this point
Copy the full SHA 9c09c94View commit details -
test: Convert statics to constants
Additionally, add lots of tests for new functionality around statics and `static mut`.
Configuration menu - View commit details
-
Copy full SHA for d03a4b0 - Browse repository at this point
Copy the full SHA d03a4b0View commit details -
4
Configuration menu - View commit details
-
Copy full SHA for a3e8f41 - Browse repository at this point
Copy the full SHA a3e8f41View commit details -
5
Configuration menu - View commit details
-
Copy full SHA for 0b51711 - Browse repository at this point
Copy the full SHA 0b51711View commit details