syntax: Enable parsing of const
globals
This rewrites them to the current `ItemStatic` production of the compiler, but I want to get this into a snapshot. It will be illegal to use a `static` in a pattern of a `match` statement, so all those current uses will need to be rewritten to `const` once it's implemented. This requires that the stage0 snapshot is able to parse `const`. cc #17718
This commit is contained in:
parent
b2d4eb186e
commit
7e22af3582
3 changed files with 53 additions and 3 deletions
|
@ -4746,8 +4746,7 @@ impl<'a> Parser<'a> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_item_const(&mut self) -> ItemInfo {
|
fn parse_item_const(&mut self, m: Mutability) -> ItemInfo {
|
||||||
let m = if self.eat_keyword(keywords::Mut) {MutMutable} else {MutImmutable};
|
|
||||||
let id = self.parse_ident();
|
let id = self.parse_ident();
|
||||||
self.expect(&token::COLON);
|
self.expect(&token::COLON);
|
||||||
let ty = self.parse_ty(true);
|
let ty = self.parse_ty(true);
|
||||||
|
@ -5303,7 +5302,26 @@ impl<'a> Parser<'a> {
|
||||||
if self.is_keyword(keywords::Static) {
|
if self.is_keyword(keywords::Static) {
|
||||||
// STATIC ITEM
|
// STATIC ITEM
|
||||||
self.bump();
|
self.bump();
|
||||||
let (ident, item_, extra_attrs) = self.parse_item_const();
|
let m = if self.eat_keyword(keywords::Mut) {MutMutable} else {MutImmutable};
|
||||||
|
let (ident, item_, extra_attrs) = self.parse_item_const(m);
|
||||||
|
let last_span = self.last_span;
|
||||||
|
let item = self.mk_item(lo,
|
||||||
|
last_span.hi,
|
||||||
|
ident,
|
||||||
|
item_,
|
||||||
|
visibility,
|
||||||
|
maybe_append(attrs, extra_attrs));
|
||||||
|
return IoviItem(item);
|
||||||
|
}
|
||||||
|
if self.is_keyword(keywords::Const) {
|
||||||
|
// CONST ITEM
|
||||||
|
self.bump();
|
||||||
|
if self.eat_keyword(keywords::Mut) {
|
||||||
|
let last_span = self.last_span;
|
||||||
|
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 last_span = self.last_span;
|
let last_span = self.last_span;
|
||||||
let item = self.mk_item(lo,
|
let item = self.mk_item(lo,
|
||||||
last_span.hi,
|
last_span.hi,
|
||||||
|
|
17
src/test/compile-fail/issue-17718-const-mut.rs
Normal file
17
src/test/compile-fail/issue-17718-const-mut.rs
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
|
||||||
|
// file at the top-level directory of this distribution and at
|
||||||
|
// http://rust-lang.org/COPYRIGHT.
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||||
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||||
|
// option. This file may not be copied, modified, or distributed
|
||||||
|
// except according to those terms.
|
||||||
|
|
||||||
|
const
|
||||||
|
mut //~ ERROR: const globals cannot be mutable, did you mean to declare a static?
|
||||||
|
FOO: uint = 3;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
}
|
||||||
|
|
15
src/test/run-pass/issue-17718-parse-const.rs
Normal file
15
src/test/run-pass/issue-17718-parse-const.rs
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
|
||||||
|
// file at the top-level directory of this distribution and at
|
||||||
|
// http://rust-lang.org/COPYRIGHT.
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||||
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||||
|
// option. This file may not be copied, modified, or distributed
|
||||||
|
// except according to those terms.
|
||||||
|
|
||||||
|
const FOO: uint = 3;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
assert_eq!(FOO, 3);
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue