From 049e1f9a1f60cfbc4136bd8496737e707ca05a42 Mon Sep 17 00:00:00 2001 From: Patrick Walton Date: Tue, 19 Mar 2013 14:46:27 -0700 Subject: [PATCH] libsyntax: Accept `static` instead of `const` for globals --- src/libsyntax/parse/parser.rs | 18 ++++++++++++++---- src/libsyntax/print/pprust.rs | 4 ++-- src/test/run-pass/new-style-constants.rs | 8 ++++++++ 3 files changed, 24 insertions(+), 6 deletions(-) create mode 100644 src/test/run-pass/new-style-constants.rs diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index c7e93635d4c..b224ccba114 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -3477,7 +3477,12 @@ pub impl Parser { fn parse_item_foreign_const(&self, vis: ast::visibility, +attrs: ~[attribute]) -> @foreign_item { let lo = self.span.lo; - self.expect_keyword(&~"const"); + + // XXX: Obsolete; remove after snap. + if !self.eat_keyword(&~"const") { + self.expect_keyword(&~"static"); + } + let ident = self.parse_ident(); self.expect(&token::COLON); let ty = self.parse_ty(false); @@ -3506,7 +3511,7 @@ pub impl Parser { fn parse_foreign_item(&self, +attrs: ~[attribute]) -> @foreign_item { let vis = self.parse_visibility(); - if self.is_keyword(&~"const") { + if self.is_keyword(&~"const") || self.is_keyword(&~"static") { self.parse_item_foreign_const(vis, attrs) } else { self.parse_item_foreign_fn(attrs) @@ -3864,13 +3869,18 @@ pub impl Parser { visibility = inherited; } - if items_allowed && self.eat_keyword(&~"const") { + if items_allowed && + (self.is_keyword(&~"const") || + (self.is_keyword(&~"static") && + !self.token_is_keyword(&~"fn", &self.look_ahead(1)))) { // CONST ITEM + self.bump(); let (ident, item_, extra_attrs) = self.parse_item_const(); return iovi_item(self.mk_item(lo, self.last_span.hi, ident, item_, visibility, maybe_append(attrs, extra_attrs))); - } else if foreign_items_allowed && self.is_keyword(&~"const") { + } else if foreign_items_allowed && + (self.is_keyword(&~"const") || self.is_keyword(&~"static")) { // FOREIGN CONST ITEM let item = self.parse_item_foreign_const(visibility, attrs); return iovi_foreign_item(item); diff --git a/src/libsyntax/print/pprust.rs b/src/libsyntax/print/pprust.rs index 93583a1487a..36c9e36ed5d 100644 --- a/src/libsyntax/print/pprust.rs +++ b/src/libsyntax/print/pprust.rs @@ -452,7 +452,7 @@ pub fn print_foreign_item(s: @ps, item: @ast::foreign_item) { end(s); // end the outer fn box } ast::foreign_item_const(t) => { - head(s, ~"const"); + head(s, ~"static"); print_ident(s, item.ident); word_space(s, ~":"); print_type(s, t); @@ -471,7 +471,7 @@ pub fn print_item(s: @ps, &&item: @ast::item) { (s.ann.pre)(ann_node); match /*bad*/ copy item.node { ast::item_const(ty, expr) => { - head(s, visibility_qualified(item.vis, ~"const")); + head(s, visibility_qualified(item.vis, ~"static")); print_ident(s, item.ident); word_space(s, ~":"); print_type(s, ty); diff --git a/src/test/run-pass/new-style-constants.rs b/src/test/run-pass/new-style-constants.rs new file mode 100644 index 00000000000..f5c691d512e --- /dev/null +++ b/src/test/run-pass/new-style-constants.rs @@ -0,0 +1,8 @@ +use core::io::println; + +static FOO: int = 3; + +fn main() { + println(fmt!("%d", FOO)); +} +