diff options
| author | bors <bors@rust-lang.org> | 2013-03-19 21:57:49 -0700 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2013-03-19 21:57:49 -0700 |
| commit | f3c879fdd8aad67cf4f6edd3aacb0189c284c920 (patch) | |
| tree | 872abff783f535fce31af87b8bceeba0f10bf8dd /src/libsyntax | |
| parent | 4cb9ca92962dbbe8ffd813c016e9d6f809dd285b (diff) | |
| parent | f8dab3a6c0adff63854d5e238961a771419d23b7 (diff) | |
| download | rust-f3c879fdd8aad67cf4f6edd3aacb0189c284c920.tar.gz rust-f3c879fdd8aad67cf4f6edd3aacb0189c284c920.zip | |
auto merge of #5442 : pcwalton/rust/extern-block-restriction, r=pcwalton
r? @graydon
Diffstat (limited to 'src/libsyntax')
| -rw-r--r-- | src/libsyntax/ast_map.rs | 16 | ||||
| -rw-r--r-- | src/libsyntax/parse/parser.rs | 114 | ||||
| -rw-r--r-- | src/libsyntax/print/pprust.rs | 6 |
3 files changed, 92 insertions, 44 deletions
diff --git a/src/libsyntax/ast_map.rs b/src/libsyntax/ast_map.rs index 5266d1b049a..9371055556e 100644 --- a/src/libsyntax/ast_map.rs +++ b/src/libsyntax/ast_map.rs @@ -87,7 +87,7 @@ pub fn path_elt_to_str(pe: path_elt, itr: @ident_interner) -> ~str { pub enum ast_node { node_item(@item, @path), - node_foreign_item(@foreign_item, foreign_abi, @path), + node_foreign_item(@foreign_item, foreign_abi, visibility, @path), node_trait_method(@trait_method, def_id /* trait did */, @path /* path to the trait */), node_method(@method, def_id /* impl did */, @path /* path to the impl */), @@ -170,7 +170,9 @@ pub fn map_decoded_item(diag: @span_handler, match ii { ii_item(*) | ii_dtor(*) => { /* fallthrough */ } ii_foreign(i) => { - cx.map.insert(i.id, node_foreign_item(i, foreign_abi_rust_intrinsic, + cx.map.insert(i.id, node_foreign_item(i, + foreign_abi_rust_intrinsic, + i.vis, // Wrong but OK @path)); } ii_method(impl_did, m) => { @@ -277,10 +279,18 @@ pub fn map_item(i: @item, &&cx: @mut Ctx, v: visit::vt<@mut Ctx>) { Right(abi) => abi }; for nm.items.each |nitem| { + // Compute the visibility for this native item. + let visibility = match nitem.vis { + public => public, + private => private, + inherited => i.vis + }; + cx.map.insert(nitem.id, node_foreign_item( *nitem, abi, + visibility, // FIXME (#2543) if nm.sort == ast::named { extend(cx, i.ident) @@ -380,7 +390,7 @@ pub fn node_id_to_str(map: map, id: node_id, itr: @ident_interner) -> ~str { }; fmt!("%s %s (id=%?)", item_str, path_str, id) } - Some(node_foreign_item(item, abi, path)) => { + Some(node_foreign_item(item, abi, _, path)) => { fmt!("foreign item %s with abi %? (id=%?)", path_ident_to_str(*path, item.ident, itr), abi, id) } diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 6f4d9332df8..62e1d20e9b2 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 } @@ -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 @@ -3538,7 +3544,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); @@ -3567,7 +3578,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) @@ -3585,7 +3596,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; @@ -3925,17 +3936,24 @@ 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") { + } + 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)) { @@ -3944,7 +3962,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"); @@ -3952,13 +3971,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(); @@ -3967,7 +3988,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) = @@ -3977,47 +3999,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); @@ -4027,7 +4064,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 @@ -4070,16 +4108,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> { @@ -4246,17 +4284,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/libsyntax/print/pprust.rs b/src/libsyntax/print/pprust.rs index 9b8448bc2d9..e2d48979fab 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, ~"]"); } @@ -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); |
