Skip to content

Commit 7e22af3

Browse files
committed
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
1 parent b2d4eb1 commit 7e22af3

File tree

3 files changed

+53
-3
lines changed

3 files changed

+53
-3
lines changed

src/libsyntax/parse/parser.rs

+21-3
Original file line numberDiff line numberDiff line change
@@ -4746,8 +4746,7 @@ impl<'a> Parser<'a> {
47464746
}
47474747
}
47484748

4749-
fn parse_item_const(&mut self) -> ItemInfo {
4750-
let m = if self.eat_keyword(keywords::Mut) {MutMutable} else {MutImmutable};
4749+
fn parse_item_const(&mut self, m: Mutability) -> ItemInfo {
47514750
let id = self.parse_ident();
47524751
self.expect(&token::COLON);
47534752
let ty = self.parse_ty(true);
@@ -5303,7 +5302,26 @@ impl<'a> Parser<'a> {
53035302
if self.is_keyword(keywords::Static) {
53045303
// STATIC ITEM
53055304
self.bump();
5306-
let (ident, item_, extra_attrs) = self.parse_item_const();
5305+
let m = if self.eat_keyword(keywords::Mut) {MutMutable} else {MutImmutable};
5306+
let (ident, item_, extra_attrs) = self.parse_item_const(m);
5307+
let last_span = self.last_span;
5308+
let item = self.mk_item(lo,
5309+
last_span.hi,
5310+
ident,
5311+
item_,
5312+
visibility,
5313+
maybe_append(attrs, extra_attrs));
5314+
return IoviItem(item);
5315+
}
5316+
if self.is_keyword(keywords::Const) {
5317+
// CONST ITEM
5318+
self.bump();
5319+
if self.eat_keyword(keywords::Mut) {
5320+
let last_span = self.last_span;
5321+
self.span_err(last_span, "const globals cannot be mutable, \
5322+
did you mean to declare a static?");
5323+
}
5324+
let (ident, item_, extra_attrs) = self.parse_item_const(MutImmutable);
53075325
let last_span = self.last_span;
53085326
let item = self.mk_item(lo,
53095327
last_span.hi,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
const
12+
mut //~ ERROR: const globals cannot be mutable, did you mean to declare a static?
13+
FOO: uint = 3;
14+
15+
fn main() {
16+
}
17+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
const FOO: uint = 3;
12+
13+
fn main() {
14+
assert_eq!(FOO, 3);
15+
}

0 commit comments

Comments
 (0)