From 049e1f9a1f60cfbc4136bd8496737e707ca05a42 Mon Sep 17 00:00:00 2001 From: Patrick Walton Date: Tue, 19 Mar 2013 14:46:27 -0700 Subject: libsyntax: Accept `static` instead of `const` for globals --- src/libsyntax/parse/parser.rs | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) (limited to 'src/libsyntax/parse') 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); -- cgit 1.4.1-3-g733a5 From b0bea108983446aaa33ecabdd44954e03d5c65e0 Mon Sep 17 00:00:00 2001 From: Patrick Walton Date: Tue, 19 Mar 2013 15:40:04 -0700 Subject: libsyntax: Accept the new `[T, ..N]` style for vec. --- src/libsyntax/parse/parser.rs | 12 +++++++++--- src/libsyntax/print/pprust.rs | 2 +- src/test/run-pass/new-style-fixed-length-vec.rs | 10 ++++++++++ 3 files changed, 20 insertions(+), 4 deletions(-) create mode 100644 src/test/run-pass/new-style-fixed-length-vec.rs (limited to 'src/libsyntax/parse') diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index b224ccba114..40ef0bcc99e 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -642,9 +642,9 @@ pub impl Parser { self.obsolete(*self.last_span, ObsoleteMutVector); } - // Parse the `* e` in `[ int * e ]` + // Parse the `, ..e` in `[ int, ..e ]` // where `e` is a const expression - let t = match self.maybe_parse_fixed_vstore_with_star() { + let t = match self.maybe_parse_fixed_vstore() { None => ty_vec(mt), Some(suffix) => ty_fixed_length_vec(mt, suffix) }; @@ -815,8 +815,14 @@ pub impl Parser { }) } - fn maybe_parse_fixed_vstore_with_star(&self) -> Option<@ast::expr> { + fn maybe_parse_fixed_vstore(&self) -> Option<@ast::expr> { if self.eat(&token::BINOP(token::STAR)) { + // XXX: Obsolete; remove after snapshot. + Some(self.parse_expr()) + } else if *self.token == token::COMMA && + self.look_ahead(1) == token::DOTDOT { + self.bump(); + self.bump(); Some(self.parse_expr()) } else { None diff --git a/src/libsyntax/print/pprust.rs b/src/libsyntax/print/pprust.rs index 36c9e36ed5d..0937091cd13 100644 --- a/src/libsyntax/print/pprust.rs +++ b/src/libsyntax/print/pprust.rs @@ -424,7 +424,7 @@ pub fn print_type_ex(s: @ps, &&ty: @ast::Ty, print_colons: bool) { ast::m_imm => () } print_type(s, mt.ty); - word(s.s, ~" * "); + word(s.s, ~", .."); print_expr(s, v); word(s.s, ~"]"); } diff --git a/src/test/run-pass/new-style-fixed-length-vec.rs b/src/test/run-pass/new-style-fixed-length-vec.rs new file mode 100644 index 00000000000..44261458a9a --- /dev/null +++ b/src/test/run-pass/new-style-fixed-length-vec.rs @@ -0,0 +1,10 @@ +use core::io::println; + +static FOO: [int, ..3] = [1, 2, 3]; + +fn main() { + println(fmt!("%d %d %d", FOO[0], FOO[1], FOO[2])); +} + + + -- cgit 1.4.1-3-g733a5 From e6f53c091e8efeb2258f1f215226a8228acbd868 Mon Sep 17 00:00:00 2001 From: Patrick Walton Date: Tue, 19 Mar 2013 18:00:18 -0700 Subject: libsyntax: Forbid `use` (and most other things) within `extern { ... }` blocks --- src/libcore/libc.rs | 9 ++-- src/libsyntax/parse/parser.rs | 86 ++++++++++++++++++++------------ src/test/run-pass/conditional-compile.rs | 7 --- src/test/run-pass/import-from-foreign.rs | 25 ---------- 4 files changed, 58 insertions(+), 69 deletions(-) delete mode 100644 src/test/run-pass/import-from-foreign.rs (limited to 'src/libsyntax/parse') diff --git a/src/libcore/libc.rs b/src/libcore/libc.rs index e3646ef60f5..6a3ed22cea9 100644 --- a/src/libcore/libc.rs +++ b/src/libcore/libc.rs @@ -1221,9 +1221,8 @@ pub mod funcs { #[nolink] #[abi = "cdecl"] pub mod fcntl { + use libc::types::os::arch::c95::{c_int, c_char}; pub extern { - use libc::types::os::arch::c95::{c_int, c_char}; - #[link_name = "_open"] unsafe fn open(path: *c_char, oflag: c_int, mode: c_int) -> c_int; @@ -1562,11 +1561,11 @@ pub mod funcs { #[cfg(target_os = "macos")] #[cfg(target_os = "freebsd")] pub mod bsd44 { + use libc::types::common::c95::{c_void}; + use libc::types::os::arch::c95::{c_char, c_int, c_uint, size_t}; + #[abi = "cdecl"] pub extern { - use libc::types::common::c95::{c_void}; - use libc::types::os::arch::c95::{c_char, c_int, c_uint, size_t}; - unsafe fn sysctl(name: *c_int, namelen: c_uint, oldp: *mut c_void, oldlenp: *mut size_t, newp: *c_void, newlen: size_t) -> c_int; diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 40ef0bcc99e..8e4115855db 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -120,7 +120,7 @@ pub enum item_or_view_item { enum view_item_parse_mode { VIEW_ITEMS_AND_ITEMS_ALLOWED, - VIEW_ITEMS_AND_FOREIGN_ITEMS_ALLOWED, + FOREIGN_ITEMS_ALLOWED, IMPORTS_AND_ITEMS_ALLOWED } @@ -3535,7 +3535,7 @@ pub impl Parser { items: _, foreign_items: foreign_items } = self.parse_items_and_view_items(first_item_attrs, - VIEW_ITEMS_AND_FOREIGN_ITEMS_ALLOWED, + FOREIGN_ITEMS_ALLOWED, true); let mut items: ~[@foreign_item] = foreign_items; @@ -3885,12 +3885,14 @@ pub impl Parser { return iovi_item(self.mk_item(lo, self.last_span.hi, ident, item_, visibility, maybe_append(attrs, extra_attrs))); - } else if foreign_items_allowed && + } + 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); - } else if items_allowed && + } + if items_allowed && // FUNCTION ITEM (not sure about lookahead condition...) self.is_keyword(&~"fn") && !self.fn_expr_lookahead(self.look_ahead(1u)) { @@ -3899,7 +3901,8 @@ pub impl Parser { return iovi_item(self.mk_item(lo, self.last_span.hi, ident, item_, visibility, maybe_append(attrs, extra_attrs))); - } else if items_allowed && self.eat_keyword(&~"pure") { + } + if items_allowed && self.eat_keyword(&~"pure") { // PURE FUNCTION ITEM // NB: We parse this as impure for bootstrapping purposes. self.expect_keyword(&~"fn"); @@ -3907,13 +3910,15 @@ pub impl Parser { return iovi_item(self.mk_item(lo, self.last_span.hi, ident, item_, visibility, maybe_append(attrs, extra_attrs))); - } else if foreign_items_allowed && + } + if foreign_items_allowed && (self.is_keyword(&~"fn") || self.is_keyword(&~"pure") || self.is_keyword(&~"unsafe")) { // FOREIGN FUNCTION ITEM (no items allowed) let item = self.parse_item_foreign_fn(attrs); return iovi_foreign_item(item); - } else if items_allowed && self.is_keyword(&~"unsafe") + } + if items_allowed && self.is_keyword(&~"unsafe") && self.look_ahead(1u) != token::LBRACE { // UNSAFE FUNCTION ITEM (where items are allowed) self.bump(); @@ -3922,7 +3927,8 @@ pub impl Parser { return iovi_item(self.mk_item(lo, self.last_span.hi, ident, item_, visibility, maybe_append(attrs, extra_attrs))); - } else if self.eat_keyword(&~"extern") { + } + if self.eat_keyword(&~"extern") { if items_allowed && self.eat_keyword(&~"fn") { // EXTERN FUNCTION ITEM let (ident, item_, extra_attrs) = @@ -3932,47 +3938,62 @@ pub impl Parser { maybe_append(attrs, extra_attrs))); } - // EXTERN MODULE ITEM - return self.parse_item_foreign_mod(lo, visibility, attrs, - items_allowed); - } else if items_allowed && self.eat_keyword(&~"mod") { + if !foreign_items_allowed { + // EXTERN MODULE ITEM + return self.parse_item_foreign_mod(lo, visibility, attrs, + items_allowed); + } + } + if items_allowed && !foreign_items_allowed && + self.eat_keyword(&~"mod") { // MODULE ITEM let (ident, item_, extra_attrs) = self.parse_item_mod(attrs); return iovi_item(self.mk_item(lo, self.last_span.hi, ident, item_, visibility, maybe_append(attrs, extra_attrs))); - } else if items_allowed && self.eat_keyword(&~"type") { + } + if items_allowed && !foreign_items_allowed && + self.eat_keyword(&~"type") { // TYPE ITEM let (ident, item_, extra_attrs) = self.parse_item_type(); return iovi_item(self.mk_item(lo, self.last_span.hi, ident, item_, visibility, maybe_append(attrs, extra_attrs))); - } else if items_allowed && self.eat_keyword(&~"enum") { + } + if items_allowed && !foreign_items_allowed && + self.eat_keyword(&~"enum") { // ENUM ITEM let (ident, item_, extra_attrs) = self.parse_item_enum(); return iovi_item(self.mk_item(lo, self.last_span.hi, ident, item_, visibility, maybe_append(attrs, extra_attrs))); - } else if items_allowed && self.eat_keyword(&~"trait") { + } + if items_allowed && !foreign_items_allowed && + self.eat_keyword(&~"trait") { // TRAIT ITEM let (ident, item_, extra_attrs) = self.parse_item_trait(); return iovi_item(self.mk_item(lo, self.last_span.hi, ident, item_, visibility, maybe_append(attrs, extra_attrs))); - } else if items_allowed && self.eat_keyword(&~"impl") { + } + if items_allowed && !foreign_items_allowed && + self.eat_keyword(&~"impl") { // IMPL ITEM let (ident, item_, extra_attrs) = self.parse_item_impl(visibility); return iovi_item(self.mk_item(lo, self.last_span.hi, ident, item_, visibility, maybe_append(attrs, extra_attrs))); - } else if items_allowed && self.eat_keyword(&~"struct") { + } + if items_allowed && !foreign_items_allowed && + self.eat_keyword(&~"struct") { // STRUCT ITEM let (ident, item_, extra_attrs) = self.parse_item_struct(); return iovi_item(self.mk_item(lo, self.last_span.hi, ident, item_, visibility, maybe_append(attrs, extra_attrs))); - } else if self.eat_keyword(&~"use") { + } + if !foreign_items_allowed && self.eat_keyword(&~"use") { // USE ITEM let view_item = self.parse_use(); self.expect(&token::SEMI); @@ -3982,7 +4003,8 @@ pub impl Parser { vis: visibility, span: mk_sp(lo, self.last_span.hi) }); - } else if macros_allowed && !self.is_any_keyword(© *self.token) + } + if macros_allowed && !self.is_any_keyword(© *self.token) && self.look_ahead(1) == token::NOT && (is_plain_ident(&self.look_ahead(2)) || self.look_ahead(2) == token::LPAREN @@ -4025,16 +4047,16 @@ pub impl Parser { let item_ = item_mac(m); return iovi_item(self.mk_item(lo, self.last_span.hi, id, item_, visibility, attrs)); - } else { - // FAILURE TO PARSE ITEM - if visibility != inherited { - let mut s = ~"unmatched visibility `"; - s += if visibility == public { ~"pub" } else { ~"priv" }; - s += ~"`"; - self.span_fatal(*self.last_span, s); - } - return iovi_none; - }; + } + + // FAILURE TO PARSE ITEM + if visibility != inherited { + let mut s = ~"unmatched visibility `"; + s += if visibility == public { ~"pub" } else { ~"priv" }; + s += ~"`"; + self.span_fatal(*self.last_span, s); + } + return iovi_none; } fn parse_item(&self, +attrs: ~[attribute]) -> Option<@ast::item> { @@ -4201,17 +4223,17 @@ pub impl Parser { let items_allowed = match mode { VIEW_ITEMS_AND_ITEMS_ALLOWED | IMPORTS_AND_ITEMS_ALLOWED => true, - VIEW_ITEMS_AND_FOREIGN_ITEMS_ALLOWED => false + FOREIGN_ITEMS_ALLOWED => false }; let restricted_to_imports = match mode { IMPORTS_AND_ITEMS_ALLOWED => true, VIEW_ITEMS_AND_ITEMS_ALLOWED | - VIEW_ITEMS_AND_FOREIGN_ITEMS_ALLOWED => false + FOREIGN_ITEMS_ALLOWED => false }; let foreign_items_allowed = match mode { - VIEW_ITEMS_AND_FOREIGN_ITEMS_ALLOWED => true, + FOREIGN_ITEMS_ALLOWED => true, VIEW_ITEMS_AND_ITEMS_ALLOWED | IMPORTS_AND_ITEMS_ALLOWED => false }; diff --git a/src/test/run-pass/conditional-compile.rs b/src/test/run-pass/conditional-compile.rs index 3a626ca6ac1..223825f60a7 100644 --- a/src/test/run-pass/conditional-compile.rs +++ b/src/test/run-pass/conditional-compile.rs @@ -121,13 +121,6 @@ mod test_foreign_items { mod test_use_statements { #[cfg(bogus)] use flippity_foo; - - pub mod rustrt { - pub extern { - #[cfg(bogus)] - use flippity_foo; - } - } } mod test_methods { diff --git a/src/test/run-pass/import-from-foreign.rs b/src/test/run-pass/import-from-foreign.rs deleted file mode 100644 index feebcdff921..00000000000 --- a/src/test/run-pass/import-from-foreign.rs +++ /dev/null @@ -1,25 +0,0 @@ -// xfail-fast - -// Copyright 2012 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. - -mod spam { - pub fn ham() { } - pub fn eggs() { } -} - -mod rustrt { - #[abi = "cdecl"] - pub extern { - pub use spam::{ham, eggs}; - } -} - -pub fn main() { rustrt::ham(); rustrt::eggs(); } -- cgit 1.4.1-3-g733a5