Skip to content

Commit

Permalink
libsyntax: Accept static instead of const for globals
Browse files Browse the repository at this point in the history
  • Loading branch information
pcwalton committed Mar 19, 2013
1 parent 2e7ec80 commit 049e1f9
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
18 changes: 14 additions & 4 deletions src/libsyntax/parse/parser.rs
Expand Up @@ -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);
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions src/libsyntax/print/pprust.rs
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
8 changes: 8 additions & 0 deletions 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));
}

1 comment on commit 049e1f9

@graydon
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

r+

Please sign in to comment.