From 7e22af3582aa4a8dcb5b2ac00c7914ef78d2486e Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Thu, 2 Oct 2014 15:06:08 -0700 Subject: [PATCH 1/2] 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 --- src/libsyntax/parse/parser.rs | 24 ++++++++++++++++--- .../compile-fail/issue-17718-const-mut.rs | 17 +++++++++++++ src/test/run-pass/issue-17718-parse-const.rs | 15 ++++++++++++ 3 files changed, 53 insertions(+), 3 deletions(-) create mode 100644 src/test/compile-fail/issue-17718-const-mut.rs create mode 100644 src/test/run-pass/issue-17718-parse-const.rs diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index c8f1b7f9a8e6d..8d70dd3e1a866 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -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); @@ -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, diff --git a/src/test/compile-fail/issue-17718-const-mut.rs b/src/test/compile-fail/issue-17718-const-mut.rs new file mode 100644 index 0000000000000..31a5fee2044dc --- /dev/null +++ b/src/test/compile-fail/issue-17718-const-mut.rs @@ -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 or the MIT license +// , 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() { +} + diff --git a/src/test/run-pass/issue-17718-parse-const.rs b/src/test/run-pass/issue-17718-parse-const.rs new file mode 100644 index 0000000000000..3ca6f473a7900 --- /dev/null +++ b/src/test/run-pass/issue-17718-parse-const.rs @@ -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 or the MIT license +// , 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); +} From da7dcee8f16b7ea635b9e0ca83b529e1d92421b0 Mon Sep 17 00:00:00 2001 From: Eduard Burtescu Date: Fri, 3 Oct 2014 14:26:07 +0300 Subject: [PATCH 2/2] tests: remove old compile-fail test asserting the removal of `const`. --- src/test/compile-fail/removed-syntax-const-item.rs | 11 ----------- 1 file changed, 11 deletions(-) delete mode 100644 src/test/compile-fail/removed-syntax-const-item.rs diff --git a/src/test/compile-fail/removed-syntax-const-item.rs b/src/test/compile-fail/removed-syntax-const-item.rs deleted file mode 100644 index 841c1ec59fdaa..0000000000000 --- a/src/test/compile-fail/removed-syntax-const-item.rs +++ /dev/null @@ -1,11 +0,0 @@ -// Copyright 2013 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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -const i: int = 42; //~ ERROR expected item, found `const`