diff options
| author | bors <bors@rust-lang.org> | 2014-10-10 00:07:08 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2014-10-10 00:07:08 +0000 |
| commit | f9fc49c06e5c71a8d63d6120e1a92b6445fb501d (patch) | |
| tree | 8a7c52db87b4ff8d5a7fa97dc44fb9ac10b92626 /src/libsyntax/parse | |
| parent | 8b12fb326b50c40c7b5acadb4403c7021fedb272 (diff) | |
| parent | 0b517117b3ac9c4981bbe00a529e48e4019554d1 (diff) | |
| download | rust-f9fc49c06e5c71a8d63d6120e1a92b6445fb501d.tar.gz rust-f9fc49c06e5c71a8d63d6120e1a92b6445fb501d.zip | |
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]: https://github.com/rust-lang/rfcs/pull/246
Diffstat (limited to 'src/libsyntax/parse')
| -rw-r--r-- | src/libsyntax/parse/parser.rs | 22 | ||||
| -rw-r--r-- | src/libsyntax/parse/token.rs | 16 |
2 files changed, 21 insertions, 17 deletions
diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 9bfb593786e..e7f40cf0722 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -32,7 +32,7 @@ use ast::{FnUnboxedClosureKind, FnMutUnboxedClosureKind}; use ast::{FnOnceUnboxedClosureKind}; use ast::{ForeignItem, ForeignItemStatic, ForeignItemFn, ForeignMod}; use ast::{Ident, NormalFn, Inherited, ImplItem, Item, Item_, ItemStatic}; -use ast::{ItemEnum, ItemFn, ItemForeignMod, ItemImpl}; +use ast::{ItemEnum, ItemFn, ItemForeignMod, ItemImpl, ItemConst}; use ast::{ItemMac, ItemMod, ItemStruct, ItemTrait, ItemTy}; use ast::{LifetimeDef, Lit, Lit_}; use ast::{LitBool, LitChar, LitByte, LitBinary}; @@ -91,10 +91,10 @@ use std::iter; bitflags! { flags Restrictions: u8 { - static UNRESTRICTED = 0b0000, - static RESTRICTION_STMT_EXPR = 0b0001, - static RESTRICTION_NO_BAR_OP = 0b0010, - static RESTRICTION_NO_STRUCT_LITERAL = 0b0100 + const UNRESTRICTED = 0b0000, + const RESTRICTION_STMT_EXPR = 0b0001, + const RESTRICTION_NO_BAR_OP = 0b0010, + const RESTRICTION_NO_STRUCT_LITERAL = 0b0100 } } @@ -4739,14 +4739,18 @@ impl<'a> Parser<'a> { } } - fn parse_item_const(&mut self, m: Mutability) -> ItemInfo { + fn parse_item_const(&mut self, m: Option<Mutability>) -> ItemInfo { let id = self.parse_ident(); self.expect(&token::COLON); let ty = self.parse_ty(true); self.expect(&token::EQ); let e = self.parse_expr(); self.commit_expr_expecting(&*e, token::SEMI); - (id, ItemStatic(ty, m, e), None) + let item = match m { + Some(m) => ItemStatic(ty, m, e), + None => ItemConst(ty, e), + }; + (id, item, None) } /// Parse a `mod <foo> { ... }` or `mod <foo>;` item @@ -5296,7 +5300,7 @@ impl<'a> Parser<'a> { // STATIC ITEM self.bump(); let m = if self.eat_keyword(keywords::Mut) {MutMutable} else {MutImmutable}; - let (ident, item_, extra_attrs) = self.parse_item_const(m); + let (ident, item_, extra_attrs) = self.parse_item_const(Some(m)); let last_span = self.last_span; let item = self.mk_item(lo, last_span.hi, @@ -5314,7 +5318,7 @@ impl<'a> Parser<'a> { self.span_err(last_span, "const globals cannot be mutable, \ did you mean to declare a static?"); } - let (ident, item_, extra_attrs) = self.parse_item_const(MutImmutable); + let (ident, item_, extra_attrs) = self.parse_item_const(None); let last_span = self.last_span; let item = self.mk_item(lo, last_span.hi, diff --git a/src/libsyntax/parse/token.rs b/src/libsyntax/parse/token.rs index 7cc78891d91..fa6b0c5ad4a 100644 --- a/src/libsyntax/parse/token.rs +++ b/src/libsyntax/parse/token.rs @@ -385,13 +385,13 @@ macro_rules! declare_special_idents_and_keywords {( use ast::{Ident, Name}; $( #[allow(non_uppercase_statics)] - pub static $si_static: Ident = Ident { name: Name($si_name), ctxt: 0 }; + pub const $si_static: Ident = Ident { name: Name($si_name), ctxt: 0 }; )* } pub mod special_names { use ast::Name; - $( #[allow(non_uppercase_statics)] pub static $si_static: Name = Name($si_name); )* + $( #[allow(non_uppercase_statics)] pub const $si_static: Name = Name($si_name); )* } /** @@ -432,13 +432,13 @@ macro_rules! declare_special_idents_and_keywords {( }} // If the special idents get renumbered, remember to modify these two as appropriate -pub static SELF_KEYWORD_NAME: Name = Name(SELF_KEYWORD_NAME_NUM); -static STATIC_KEYWORD_NAME: Name = Name(STATIC_KEYWORD_NAME_NUM); -static SUPER_KEYWORD_NAME: Name = Name(SUPER_KEYWORD_NAME_NUM); +pub const SELF_KEYWORD_NAME: Name = Name(SELF_KEYWORD_NAME_NUM); +const STATIC_KEYWORD_NAME: Name = Name(STATIC_KEYWORD_NAME_NUM); +const SUPER_KEYWORD_NAME: Name = Name(SUPER_KEYWORD_NAME_NUM); -pub static SELF_KEYWORD_NAME_NUM: u32 = 1; -static STATIC_KEYWORD_NAME_NUM: u32 = 2; -static SUPER_KEYWORD_NAME_NUM: u32 = 3; +pub const SELF_KEYWORD_NAME_NUM: u32 = 1; +const STATIC_KEYWORD_NAME_NUM: u32 = 2; +const SUPER_KEYWORD_NAME_NUM: u32 = 3; // NB: leaving holes in the ident table is bad! a different ident will get // interned with the id from the hole, but it will be between the min and max |
