Skip to content

syntax: Enable parsing of const globals #17729

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

Merged
merged 2 commits into from
Oct 3, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions src/libsyntax/parse/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4746,8 +4746,7 @@ impl<'a> Parser<'a> {
}
}

fn parse_item_const(&mut self) -> ItemInfo {
let m = if self.eat_keyword(keywords::Mut) {MutMutable} else {MutImmutable};
fn parse_item_const(&mut self, m: Mutability) -> ItemInfo {
let id = self.parse_ident();
self.expect(&token::COLON);
let ty = self.parse_ty(true);
Expand Down Expand Up @@ -5303,7 +5302,26 @@ impl<'a> Parser<'a> {
if self.is_keyword(keywords::Static) {
// STATIC ITEM
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 item = self.mk_item(lo,
last_span.hi,
Expand Down
17 changes: 17 additions & 0 deletions src/test/compile-fail/issue-17718-const-mut.rs
Original file line number Diff line number Diff line change
@@ -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() {
}

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
// 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.
//
Expand All @@ -8,4 +8,8 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

const i: int = 42; //~ ERROR expected item, found `const`
const FOO: uint = 3;

fn main() {
assert_eq!(FOO, 3);
}