From 38ac9e3984392ab11e33b77d5e1927c1fa17fb07 Mon Sep 17 00:00:00 2001 From: Eduard Burtescu Date: Tue, 23 Dec 2014 15:07:30 +0200 Subject: syntax: merge ast::ViewItem into ast::Item. --- src/libsyntax/parse/parser.rs | 434 ++++++++++++------------------------------ 1 file changed, 119 insertions(+), 315 deletions(-) (limited to 'src/libsyntax/parse') diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 83a7504bc49..a474e4c2bad 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -9,7 +9,6 @@ // except according to those terms. pub use self::PathParsingMode::*; -use self::ItemOrViewItem::*; use abi; use ast::{AssociatedType, BareFnTy}; @@ -35,6 +34,7 @@ use ast::{ForeignItem, ForeignItemStatic, ForeignItemFn, ForeignMod, FunctionRet use ast::{Ident, Inherited, ImplItem, Item, Item_, ItemStatic}; use ast::{ItemEnum, ItemFn, ItemForeignMod, ItemImpl, ItemConst}; use ast::{ItemMac, ItemMod, ItemStruct, ItemTrait, ItemTy}; +use ast::{ItemExternCrate, ItemUse}; use ast::{LifetimeDef, Lit, Lit_}; use ast::{LitBool, LitChar, LitByte, LitBinary}; use ast::{LitStr, LitInt, Local, LocalLet}; @@ -59,7 +59,6 @@ use ast::{TyParam, TyParamBound, TyParen, TyPath, TyPolyTraitRef, TyPtr, TyQPath use ast::{TyRptr, TyTup, TyU32, TyVec, UnUniq}; use ast::{TypeImplItem, TypeTraitItem, Typedef, UnboxedClosureKind}; use ast::{UnnamedField, UnsafeBlock}; -use ast::{ViewItem, ViewItem_, ViewItemExternCrate, ViewItemUse}; use ast::{ViewPath, ViewPathGlob, ViewPathList, ViewPathSimple}; use ast::{Visibility, WhereClause}; use ast; @@ -122,14 +121,9 @@ pub enum BoundParsingMode { Modified, } -enum ItemOrViewItem { - /// Indicates a failure to parse any kind of item. The attributes are - /// returned. - IoviNone(Vec), - IoviItem(P), - IoviForeignItem(P), - IoviViewItem(ViewItem) -} +/// The `Err` case indicates a failure to parse any kind of item. +/// The attributes are returned. +type MaybeItem = Result, Vec>; /// Possibly accept an `token::Interpolated` expression (a pre-parsed expression @@ -231,19 +225,6 @@ macro_rules! maybe_whole { } } ); - (iovi $p:expr, $constructor:ident) => ( - { - let found = match ($p).token { - token::Interpolated(token::$constructor(_)) => { - Some(($p).bump_and_get()) - } - _ => None - }; - if let Some(token::Interpolated(token::$constructor(x))) = found { - return IoviItem(x.clone()); - } - } - ); (pair_empty $p:expr, $constructor:ident) => ( { let found = match ($p).token { @@ -269,14 +250,6 @@ fn maybe_append(mut lhs: Vec, rhs: Option>) lhs } - -struct ParsedItemsAndViewItems { - attrs_remaining: Vec, - view_items: Vec, - items: Vec> , - foreign_items: Vec> -} - /* ident is handled by common.rs */ pub struct Parser<'a> { @@ -3032,8 +3005,7 @@ impl<'a> Parser<'a> { let body = self.parse_expr(); let fakeblock = P(ast::Block { id: ast::DUMMY_NODE_ID, - view_items: Vec::new(), - stmts: Vec::new(), + stmts: vec![], span: body.span, expr: Some(body), rules: DefaultBlock, @@ -3731,20 +3703,13 @@ impl<'a> Parser<'a> { } else { let found_attrs = !item_attrs.is_empty(); let item_err = Parser::expected_item_err(&item_attrs[]); - match self.parse_item_or_view_item(item_attrs, false) { - IoviItem(i) => { + match self.parse_item_(item_attrs, false) { + Ok(i) => { let hi = i.span.hi; let decl = P(spanned(lo, hi, DeclItem(i))); P(spanned(lo, hi, StmtDecl(decl, ast::DUMMY_NODE_ID))) } - IoviViewItem(vi) => { - self.span_fatal(vi.span, - "view items must be declared at the top of the block"); - } - IoviForeignItem(_) => { - self.fatal("foreign items are not allowed here"); - } - IoviNone(_) => { + Err(_) => { if found_attrs { let last_span = self.last_span; self.span_err(last_span, item_err); @@ -3794,36 +3759,17 @@ impl<'a> Parser<'a> { (inner, self.parse_block_tail_(lo, DefaultBlock, next)) } - /// Precondition: already parsed the '{' or '#{' - /// I guess that also means "already parsed the 'impure'" if - /// necessary, and this should take a qualifier. - /// Some blocks start with "#{"... + /// Precondition: already parsed the '{'. fn parse_block_tail(&mut self, lo: BytePos, s: BlockCheckMode) -> P { self.parse_block_tail_(lo, s, Vec::new()) } /// Parse the rest of a block expression or function body fn parse_block_tail_(&mut self, lo: BytePos, s: BlockCheckMode, - first_item_attrs: Vec ) -> P { - let mut stmts = Vec::new(); + first_item_attrs: Vec) -> P { + let mut stmts = vec![]; let mut expr = None; - - // wouldn't it be more uniform to parse view items only, here? - let ParsedItemsAndViewItems { - attrs_remaining, - view_items, - items, - .. - } = self.parse_items_and_view_items(first_item_attrs, - false, false); - - for item in items.into_iter() { - let span = item.span; - let decl = P(spanned(span.lo, span.hi, DeclItem(item))); - stmts.push(P(spanned(span.lo, span.hi, StmtDecl(decl, ast::DUMMY_NODE_ID)))); - } - - let mut attributes_box = attrs_remaining; + let mut attributes_box = first_item_attrs; while self.token != token::CloseDelim(token::Brace) { // parsing items even when they're not allowed lets us give @@ -3932,7 +3878,6 @@ impl<'a> Parser<'a> { let hi = self.span.hi; self.bump(); P(ast::Block { - view_items: view_items, stmts: stmts, expr: expr, id: ast::DUMMY_NODE_ID, @@ -5031,39 +4976,34 @@ impl<'a> Parser<'a> { first_item_attrs: Vec, inner_lo: BytePos) -> Mod { - // parse all of the items up to closing or an attribute. - // view items are legal here. - let ParsedItemsAndViewItems { - attrs_remaining, - view_items, - items: starting_items, - .. - } = self.parse_items_and_view_items(first_item_attrs, true, true); - let mut items: Vec> = starting_items; - let attrs_remaining_len = attrs_remaining.len(); + // Parse all of the items up to closing or an attribute. + + let mut attrs = first_item_attrs; + attrs.push_all(&self.parse_outer_attributes()[]); + let mut items = vec![]; + + loop { + match self.parse_item_(attrs, true) { + Err(returned_attrs) => { + attrs = returned_attrs; + break + } + Ok(item) => { + attrs = self.parse_outer_attributes(); + items.push(item) + } + } + } // don't think this other loop is even necessary.... - let mut first = true; while self.token != term { - let mut attrs = self.parse_outer_attributes(); - if first { - let mut tmp = attrs_remaining.clone(); - tmp.push_all(&attrs[]); - attrs = tmp; - first = false; - } - debug!("parse_mod_items: parse_item_or_view_item(attrs={:?})", - attrs); - match self.parse_item_or_view_item(attrs, - true /* macros allowed */) { - IoviItem(item) => items.push(item), - IoviViewItem(view_item) => { - self.span_fatal(view_item.span, - "view items must be declared at the top of \ - the module"); - } - _ => { + let mut attrs = mem::replace(&mut attrs, vec![]); + attrs.push_all(&self.parse_outer_attributes()[]); + debug!("parse_mod_items: parse_item_(attrs={:?})", attrs); + match self.parse_item_(attrs, true /* macros allowed */) { + Ok(item) => items.push(item), + Err(_) => { let token_str = self.this_token_to_string(); self.fatal(&format!("expected item, found `{}`", token_str)[]) @@ -5071,16 +5011,15 @@ impl<'a> Parser<'a> { } } - if first && attrs_remaining_len > 0u { + if !attrs.is_empty() { // We parsed attributes for the first item but didn't find it let last_span = self.last_span; self.span_err(last_span, - Parser::expected_item_err(&attrs_remaining[])); + Parser::expected_item_err(&attrs[])); } ast::Mod { inner: mk_sp(inner_lo, self.span.lo), - view_items: view_items, items: items } } @@ -5298,23 +5237,12 @@ impl<'a> Parser<'a> { /// parse_foreign_items. fn parse_foreign_mod_items(&mut self, abi: abi::Abi, - first_item_attrs: Vec ) + first_item_attrs: Vec) -> ForeignMod { - let ParsedItemsAndViewItems { - attrs_remaining, - view_items, - items: _, - foreign_items, - } = self.parse_foreign_items(first_item_attrs, true); - if !attrs_remaining.is_empty() { - let last_span = self.last_span; - self.span_err(last_span, - Parser::expected_item_err(&attrs_remaining[])); - } + let foreign_items = self.parse_foreign_items(first_item_attrs); assert!(self.token == token::CloseDelim(token::Brace)); ast::ForeignMod { abi: abi, - view_items: view_items, items: foreign_items } } @@ -5329,8 +5257,8 @@ impl<'a> Parser<'a> { fn parse_item_extern_crate(&mut self, lo: BytePos, visibility: Visibility, - attrs: Vec ) - -> ItemOrViewItem { + attrs: Vec) + -> P { let span = self.span; let (maybe_path, ident) = match self.token { @@ -5374,12 +5302,13 @@ impl<'a> Parser<'a> { } }; - IoviViewItem(ast::ViewItem { - node: ViewItemExternCrate(ident, maybe_path, ast::DUMMY_NODE_ID), - attrs: attrs, - vis: visibility, - span: mk_sp(lo, self.last_span.hi) - }) + let last_span = self.last_span; + self.mk_item(lo, + last_span.hi, + ident, + ItemExternCrate(maybe_path), + visibility, + attrs) } /// Parse `extern` for foreign ABIs @@ -5396,8 +5325,8 @@ impl<'a> Parser<'a> { lo: BytePos, opt_abi: Option, visibility: Visibility, - attrs: Vec ) - -> ItemOrViewItem { + attrs: Vec) + -> P { self.expect(&token::OpenDelim(token::Brace)); @@ -5408,13 +5337,12 @@ impl<'a> Parser<'a> { self.expect(&token::CloseDelim(token::Brace)); let last_span = self.last_span; - let item = self.mk_item(lo, - last_span.hi, - special_idents::invalid, - ItemForeignMod(m), - visibility, - maybe_append(attrs, Some(inner))); - return IoviItem(item); + self.mk_item(lo, + last_span.hi, + special_idents::invalid, + ItemForeignMod(m), + visibility, + maybe_append(attrs, Some(inner))) } /// Parse type Foo = Bar; @@ -5556,14 +5484,12 @@ impl<'a> Parser<'a> { } } - /// Parse one of the items or view items allowed by the - /// flags; on failure, return IoviNone. + /// Parse one of the items allowed by the flags; on failure, + /// return `Err(remaining_attrs)`. /// NB: this function no longer parses the items inside an /// extern crate. - fn parse_item_or_view_item(&mut self, - attrs: Vec , - macros_allowed: bool) - -> ItemOrViewItem { + fn parse_item_(&mut self, attrs: Vec, + macros_allowed: bool) -> MaybeItem { let nt_item = match self.token { token::Interpolated(token::NtItem(ref item)) => { Some((**item).clone()) @@ -5576,7 +5502,7 @@ impl<'a> Parser<'a> { let mut attrs = attrs; mem::swap(&mut item.attrs, &mut attrs); item.attrs.extend(attrs.into_iter()); - return IoviItem(P(item)); + return Ok(P(item)); } None => {} } @@ -5585,22 +5511,24 @@ impl<'a> Parser<'a> { let visibility = self.parse_visibility(); - // must be a view item: if self.eat_keyword(keywords::Use) { - // USE ITEM (IoviViewItem) - let view_item = self.parse_use(); + // USE ITEM + let item_ = ItemUse(self.parse_view_path()); self.expect(&token::Semi); - return IoviViewItem(ast::ViewItem { - node: view_item, - attrs: attrs, - vis: visibility, - span: mk_sp(lo, self.last_span.hi) - }); + + let last_span = self.last_span; + let item = self.mk_item(lo, + last_span.hi, + token::special_idents::invalid, + item_, + visibility, + attrs); + return Ok(item); } - // either a view item or an item: + if self.eat_keyword(keywords::Extern) { if self.eat_keyword(keywords::Crate) { - return self.parse_item_extern_crate(lo, visibility, attrs); + return Ok(self.parse_item_extern_crate(lo, visibility, attrs)); } let opt_abi = self.parse_opt_abi(); @@ -5617,9 +5545,9 @@ impl<'a> Parser<'a> { item_, visibility, maybe_append(attrs, extra_attrs)); - return IoviItem(item); + return Ok(item); } else if self.check(&token::OpenDelim(token::Brace)) { - return self.parse_item_foreign_mod(lo, opt_abi, visibility, attrs); + return Ok(self.parse_item_foreign_mod(lo, opt_abi, visibility, attrs)); } let span = self.span; @@ -5634,7 +5562,6 @@ impl<'a> Parser<'a> { self.span_err(span, "`virtual` structs have been removed from the language"); } - // the rest are all guaranteed to be items: if self.token.is_keyword(keywords::Static) { // STATIC ITEM self.bump(); @@ -5647,7 +5574,7 @@ impl<'a> Parser<'a> { item_, visibility, maybe_append(attrs, extra_attrs)); - return IoviItem(item); + return Ok(item); } if self.token.is_keyword(keywords::Const) { // CONST ITEM @@ -5665,7 +5592,7 @@ impl<'a> Parser<'a> { item_, visibility, maybe_append(attrs, extra_attrs)); - return IoviItem(item); + return Ok(item); } if self.token.is_keyword(keywords::Unsafe) && self.look_ahead(1u, |t| t.is_keyword(keywords::Trait)) @@ -5682,7 +5609,7 @@ impl<'a> Parser<'a> { item_, visibility, maybe_append(attrs, extra_attrs)); - return IoviItem(item); + return Ok(item); } if self.token.is_keyword(keywords::Unsafe) && self.look_ahead(1u, |t| t.is_keyword(keywords::Impl)) @@ -5698,7 +5625,7 @@ impl<'a> Parser<'a> { item_, visibility, maybe_append(attrs, extra_attrs)); - return IoviItem(item); + return Ok(item); } if self.token.is_keyword(keywords::Fn) { // FUNCTION ITEM @@ -5712,7 +5639,7 @@ impl<'a> Parser<'a> { item_, visibility, maybe_append(attrs, extra_attrs)); - return IoviItem(item); + return Ok(item); } if self.token.is_keyword(keywords::Unsafe) && self.look_ahead(1u, |t| *t != token::OpenDelim(token::Brace)) { @@ -5733,7 +5660,7 @@ impl<'a> Parser<'a> { item_, visibility, maybe_append(attrs, extra_attrs)); - return IoviItem(item); + return Ok(item); } if self.eat_keyword(keywords::Mod) { // MODULE ITEM @@ -5746,7 +5673,7 @@ impl<'a> Parser<'a> { item_, visibility, maybe_append(attrs, extra_attrs)); - return IoviItem(item); + return Ok(item); } if self.eat_keyword(keywords::Type) { // TYPE ITEM @@ -5758,7 +5685,7 @@ impl<'a> Parser<'a> { item_, visibility, maybe_append(attrs, extra_attrs)); - return IoviItem(item); + return Ok(item); } if self.eat_keyword(keywords::Enum) { // ENUM ITEM @@ -5770,7 +5697,7 @@ impl<'a> Parser<'a> { item_, visibility, maybe_append(attrs, extra_attrs)); - return IoviItem(item); + return Ok(item); } if self.eat_keyword(keywords::Trait) { // TRAIT ITEM @@ -5783,7 +5710,7 @@ impl<'a> Parser<'a> { item_, visibility, maybe_append(attrs, extra_attrs)); - return IoviItem(item); + return Ok(item); } if self.eat_keyword(keywords::Impl) { // IMPL ITEM @@ -5795,7 +5722,7 @@ impl<'a> Parser<'a> { item_, visibility, maybe_append(attrs, extra_attrs)); - return IoviItem(item); + return Ok(item); } if self.eat_keyword(keywords::Struct) { // STRUCT ITEM @@ -5807,32 +5734,30 @@ impl<'a> Parser<'a> { item_, visibility, maybe_append(attrs, extra_attrs)); - return IoviItem(item); + return Ok(item); } self.parse_macro_use_or_failure(attrs,macros_allowed,lo,visibility) } - /// Parse a foreign item; on failure, return IoviNone. - fn parse_foreign_item(&mut self, - attrs: Vec , - macros_allowed: bool) - -> ItemOrViewItem { - maybe_whole!(iovi self, NtItem); + /// Parse a foreign item; on failure, return `Err(remaining_attrs)`. + fn parse_foreign_item(&mut self, attrs: Vec) + -> Result, Vec> { let lo = self.span.lo; let visibility = self.parse_visibility(); if self.token.is_keyword(keywords::Static) { // FOREIGN STATIC ITEM - let item = self.parse_item_foreign_static(visibility, attrs); - return IoviForeignItem(item); + return Ok(self.parse_item_foreign_static(visibility, attrs)); } if self.token.is_keyword(keywords::Fn) || self.token.is_keyword(keywords::Unsafe) { // FOREIGN FUNCTION ITEM - let item = self.parse_item_foreign_fn(visibility, attrs); - return IoviForeignItem(item); + return Ok(self.parse_item_foreign_fn(visibility, attrs)); } - self.parse_macro_use_or_failure(attrs,macros_allowed,lo,visibility) + + // FIXME #5668: this will occur for a macro invocation: + let item = try!(self.parse_macro_use_or_failure(attrs, true, lo, visibility)); + self.span_fatal(item.span, "macros cannot expand to foreign items"); } /// This is the fall-through for parsing items. @@ -5842,7 +5767,7 @@ impl<'a> Parser<'a> { macros_allowed: bool, lo: BytePos, visibility: Visibility - ) -> ItemOrViewItem { + ) -> MaybeItem { if macros_allowed && !self.token.is_any_keyword() && self.look_ahead(1, |t| *t == token::Not) && (self.look_ahead(2, |t| t.is_plain_ident()) @@ -5891,7 +5816,7 @@ impl<'a> Parser<'a> { item_, visibility, attrs); - return IoviItem(item); + return Ok(item); } // FAILURE TO PARSE ITEM @@ -5902,7 +5827,7 @@ impl<'a> Parser<'a> { self.span_fatal(last_span, "unmatched visibility `pub`"); } } - return IoviNone(attrs); + Err(attrs) } pub fn parse_item_with_outer_attributes(&mut self) -> Option> { @@ -5911,30 +5836,9 @@ impl<'a> Parser<'a> { } pub fn parse_item(&mut self, attrs: Vec) -> Option> { - match self.parse_item_or_view_item(attrs, true) { - IoviNone(_) => None, - IoviViewItem(_) => - self.fatal("view items are not allowed here"), - IoviForeignItem(_) => - self.fatal("foreign items are not allowed here"), - IoviItem(item) => Some(item) - } + self.parse_item_(attrs, true).ok() } - /// Parse a ViewItem, e.g. `use foo::bar` or `extern crate foo` - pub fn parse_view_item(&mut self, attrs: Vec) -> ViewItem { - match self.parse_item_or_view_item(attrs, false) { - IoviViewItem(vi) => vi, - _ => self.fatal("expected `use` or `extern crate`"), - } - } - - /// Parse, e.g., "use a::b::{z,y}" - fn parse_use(&mut self) -> ViewItem_ { - return ViewItemUse(self.parse_view_path()); - } - - /// Matches view_path : MOD? non_global_path as IDENT /// | MOD? non_global_path MOD_SEP LBRACE RBRACE /// | MOD? non_global_path MOD_SEP LBRACE ident_seq RBRACE @@ -5959,8 +5863,7 @@ impl<'a> Parser<'a> { global: false, segments: Vec::new() }; - return P(spanned(lo, self.span.hi, - ViewPathList(path, idents, ast::DUMMY_NODE_ID))); + return P(spanned(lo, self.span.hi, ViewPathList(path, idents))); } let first_ident = self.parse_ident(); @@ -5994,8 +5897,7 @@ impl<'a> Parser<'a> { } }).collect() }; - return P(spanned(lo, self.span.hi, - ViewPathList(path, idents, ast::DUMMY_NODE_ID))); + return P(spanned(lo, self.span.hi, ViewPathList(path, idents))); } // foo::bar::* @@ -6011,8 +5913,7 @@ impl<'a> Parser<'a> { } }).collect() }; - return P(spanned(lo, self.span.hi, - ViewPathGlob(path, ast::DUMMY_NODE_ID))); + return P(spanned(lo, self.span.hi, ViewPathGlob(path))); } _ => break @@ -6033,136 +5934,39 @@ impl<'a> Parser<'a> { if self.eat_keyword(keywords::As) { rename_to = self.parse_ident() } - P(spanned(lo, - self.last_span.hi, - ViewPathSimple(rename_to, path, ast::DUMMY_NODE_ID))) - } - - /// Parses a sequence of items. Stops when it finds program - /// text that can't be parsed as an item - /// - mod_items uses extern_mod_allowed = true - /// - block_tail_ uses extern_mod_allowed = false - fn parse_items_and_view_items(&mut self, - first_item_attrs: Vec , - mut extern_mod_allowed: bool, - macros_allowed: bool) - -> ParsedItemsAndViewItems { - let mut attrs = first_item_attrs; - attrs.push_all(&self.parse_outer_attributes()[]); - // First, parse view items. - let mut view_items : Vec = Vec::new(); - let mut items = Vec::new(); - - // I think this code would probably read better as a single - // loop with a mutable three-state-variable (for extern crates, - // view items, and regular items) ... except that because - // of macros, I'd like to delay that entire check until later. - loop { - match self.parse_item_or_view_item(attrs, macros_allowed) { - IoviNone(attrs) => { - return ParsedItemsAndViewItems { - attrs_remaining: attrs, - view_items: view_items, - items: items, - foreign_items: Vec::new() - } - } - IoviViewItem(view_item) => { - match view_item.node { - ViewItemUse(..) => { - // `extern crate` must precede `use`. - extern_mod_allowed = false; - } - ViewItemExternCrate(..) if !extern_mod_allowed => { - self.span_err(view_item.span, - "\"extern crate\" declarations are \ - not allowed here"); - } - ViewItemExternCrate(..) => {} - } - view_items.push(view_item); - } - IoviItem(item) => { - items.push(item); - attrs = self.parse_outer_attributes(); - break; - } - IoviForeignItem(_) => { - panic!(); - } - } - attrs = self.parse_outer_attributes(); - } - - // Next, parse items. - loop { - match self.parse_item_or_view_item(attrs, macros_allowed) { - IoviNone(returned_attrs) => { - attrs = returned_attrs; - break - } - IoviViewItem(view_item) => { - attrs = self.parse_outer_attributes(); - self.span_err(view_item.span, - "`use` and `extern crate` declarations must precede items"); - } - IoviItem(item) => { - attrs = self.parse_outer_attributes(); - items.push(item) - } - IoviForeignItem(_) => { - panic!(); - } - } - } - - ParsedItemsAndViewItems { - attrs_remaining: attrs, - view_items: view_items, - items: items, - foreign_items: Vec::new() - } + P(spanned(lo, self.last_span.hi, ViewPathSimple(rename_to, path))) } /// Parses a sequence of foreign items. Stops when it finds program /// text that can't be parsed as an item - fn parse_foreign_items(&mut self, first_item_attrs: Vec , - macros_allowed: bool) - -> ParsedItemsAndViewItems { + fn parse_foreign_items(&mut self, first_item_attrs: Vec) + -> Vec> { let mut attrs = first_item_attrs; attrs.push_all(&self.parse_outer_attributes()[]); let mut foreign_items = Vec::new(); loop { - match self.parse_foreign_item(attrs, macros_allowed) { - IoviNone(returned_attrs) => { + match self.parse_foreign_item(attrs) { + Ok(foreign_item) => { + foreign_items.push(foreign_item); + } + Err(returned_attrs) => { if self.check(&token::CloseDelim(token::Brace)) { attrs = returned_attrs; break } self.unexpected(); - }, - IoviViewItem(view_item) => { - // I think this can't occur: - self.span_err(view_item.span, - "`use` and `extern crate` declarations must precede items"); - } - IoviItem(item) => { - // FIXME #5668: this will occur for a macro invocation: - self.span_fatal(item.span, "macros cannot expand to foreign items"); - } - IoviForeignItem(foreign_item) => { - foreign_items.push(foreign_item); } } attrs = self.parse_outer_attributes(); } - ParsedItemsAndViewItems { - attrs_remaining: attrs, - view_items: Vec::new(), - items: Vec::new(), - foreign_items: foreign_items + if !attrs.is_empty() { + let last_span = self.last_span; + self.span_err(last_span, + Parser::expected_item_err(&attrs[])); } + + foreign_items } /// Parses a source module as a crate. This is the main -- cgit 1.4.1-3-g733a5 From 7cece8725b6a7e12045bdbff257ecec7327654bf Mon Sep 17 00:00:00 2001 From: Eduard Burtescu Date: Tue, 13 Jan 2015 17:30:17 +0200 Subject: syntax: fix fallout of merging ast::ViewItem into ast::Item. --- src/libsyntax/ast_map/mod.rs | 25 +--------- src/libsyntax/ast_util.rs | 82 +++++++------------------------ src/libsyntax/config.rs | 36 ++------------ src/libsyntax/ext/build.rs | 77 ++++++++++++++--------------- src/libsyntax/ext/deriving/generic/mod.rs | 2 +- src/libsyntax/ext/expand.rs | 5 +- src/libsyntax/ext/quote.rs | 28 ++++------- src/libsyntax/feature_gate.rs | 24 +++------ src/libsyntax/fold.rs | 56 ++++++--------------- src/libsyntax/parse/mod.rs | 22 ++++----- src/libsyntax/print/pprust.rs | 68 ++++++++++--------------- src/libsyntax/std_inject.rs | 46 ++++++----------- src/libsyntax/test.rs | 69 +++++++++++++------------- src/libsyntax/util/parser_testing.rs | 7 --- src/libsyntax/visit.rs | 73 ++++++++++----------------- 15 files changed, 202 insertions(+), 418 deletions(-) (limited to 'src/libsyntax/parse') diff --git a/src/libsyntax/ast_map/mod.rs b/src/libsyntax/ast_map/mod.rs index f462a730d3a..2d710e636d8 100644 --- a/src/libsyntax/ast_map/mod.rs +++ b/src/libsyntax/ast_map/mod.rs @@ -107,7 +107,6 @@ pub fn path_to_string>(path: PI) -> String { #[derive(Copy, Show)] pub enum Node<'ast> { NodeItem(&'ast Item), - NodeViewItem(&'ast ViewItem), NodeForeignItem(&'ast ForeignItem), NodeTraitItem(&'ast TraitItem), NodeImplItem(&'ast ImplItem), @@ -134,7 +133,6 @@ enum MapEntry<'ast> { /// All the node types, with a parent ID. EntryItem(NodeId, &'ast Item), - EntryViewItem(NodeId, &'ast ViewItem), EntryForeignItem(NodeId, &'ast ForeignItem), EntryTraitItem(NodeId, &'ast TraitItem), EntryImplItem(NodeId, &'ast ImplItem), @@ -169,7 +167,6 @@ impl<'ast> MapEntry<'ast> { fn from_node(p: NodeId, node: Node<'ast>) -> MapEntry<'ast> { match node { NodeItem(n) => EntryItem(p, n), - NodeViewItem(n) => EntryViewItem(p, n), NodeForeignItem(n) => EntryForeignItem(p, n), NodeTraitItem(n) => EntryTraitItem(p, n), NodeImplItem(n) => EntryImplItem(p, n), @@ -188,7 +185,6 @@ impl<'ast> MapEntry<'ast> { fn parent(self) -> Option { Some(match self { EntryItem(id, _) => id, - EntryViewItem(id, _) => id, EntryForeignItem(id, _) => id, EntryTraitItem(id, _) => id, EntryImplItem(id, _) => id, @@ -208,7 +204,6 @@ impl<'ast> MapEntry<'ast> { fn to_node(self) -> Option> { Some(match self { EntryItem(_, n) => NodeItem(n), - EntryViewItem(_, n) => NodeViewItem(n), EntryForeignItem(_, n) => NodeForeignItem(n), EntryTraitItem(_, n) => NodeTraitItem(n), EntryImplItem(_, n) => NodeImplItem(n), @@ -341,13 +336,6 @@ impl<'ast> Map<'ast> { } } - pub fn expect_view_item(&self, id: NodeId) -> &'ast ViewItem { - match self.find(id) { - Some(NodeViewItem(view_item)) => view_item, - _ => panic!("expected view item, found {}", self.node_to_string(id)) - } - } - pub fn expect_struct(&self, id: NodeId) -> &'ast StructDef { match self.find(id) { Some(NodeItem(i)) => { @@ -533,7 +521,6 @@ impl<'ast> Map<'ast> { pub fn opt_span(&self, id: NodeId) -> Option { let sp = match self.find(id) { Some(NodeItem(item)) => item.span, - Some(NodeViewItem(item)) => item.span, Some(NodeForeignItem(foreign_item)) => foreign_item.span, Some(NodeTraitItem(trait_method)) => { match *trait_method { @@ -826,11 +813,6 @@ impl<'ast> Visitor<'ast> for NodeCollector<'ast> { self.parent = parent; } - fn visit_view_item(&mut self, item: &'ast ViewItem) { - self.insert(item.id(), NodeViewItem(item)); - visit::walk_view_item(self, item); - } - fn visit_pat(&mut self, pat: &'ast Pat) { self.insert(pat.id, match pat.node { // Note: this is at least *potentially* a pattern... @@ -904,7 +886,6 @@ pub fn map_crate<'ast, F: FoldOps>(forest: &'ast mut Forest, fold_ops: F) -> Map let krate = mem::replace(&mut forest.krate, Crate { module: Mod { inner: DUMMY_SP, - view_items: vec![], items: vec![], }, attrs: vec![], @@ -1036,7 +1017,6 @@ impl<'a> NodePrinter for pprust::State<'a> { fn print_node(&mut self, node: &Node) -> IoResult<()> { match *node { NodeItem(a) => self.print_item(&*a), - NodeViewItem(a) => self.print_view_item(&*a), NodeForeignItem(a) => self.print_foreign_item(&*a), NodeTraitItem(a) => self.print_trait_method(&*a), NodeImplItem(a) => self.print_impl_item(&*a), @@ -1065,6 +1045,8 @@ fn node_id_to_string(map: &Map, id: NodeId, include_id: bool) -> String { Some(NodeItem(item)) => { let path_str = map.path_to_str_with_ident(id, item.ident); let item_str = match item.node { + ItemExternCrate(..) => "extern crate", + ItemUse(..) => "use", ItemStatic(..) => "static", ItemConst(..) => "const", ItemFn(..) => "fn", @@ -1079,9 +1061,6 @@ fn node_id_to_string(map: &Map, id: NodeId, include_id: bool) -> String { }; format!("{} {}{}", item_str, path_str, id_str) } - Some(NodeViewItem(item)) => { - format!("view item {}{}", pprust::view_item_to_string(&*item), id_str) - } Some(NodeForeignItem(item)) => { let path_str = map.path_to_str_with_ident(id, item.ident); format!("foreign item {}{}", path_str, id_str) diff --git a/src/libsyntax/ast_util.rs b/src/libsyntax/ast_util.rs index bc7fbd46fd8..192af7bb789 100644 --- a/src/libsyntax/ast_util.rs +++ b/src/libsyntax/ast_util.rs @@ -410,37 +410,6 @@ impl<'a, 'v, O: IdVisitingOperation> Visitor<'v> for IdVisitor<'a, O> { visit::walk_mod(self, module) } - fn visit_view_item(&mut self, view_item: &ViewItem) { - if !self.pass_through_items { - if self.visited_outermost { - return; - } else { - self.visited_outermost = true; - } - } - match view_item.node { - ViewItemExternCrate(_, _, node_id) => { - self.operation.visit_id(node_id) - } - ViewItemUse(ref view_path) => { - match view_path.node { - ViewPathSimple(_, _, node_id) | - ViewPathGlob(_, node_id) => { - self.operation.visit_id(node_id) - } - ViewPathList(_, ref paths, node_id) => { - self.operation.visit_id(node_id); - for path in paths.iter() { - self.operation.visit_id(path.node.id()) - } - } - } - } - } - visit::walk_view_item(self, view_item); - self.visited_outermost = false; - } - fn visit_foreign_item(&mut self, foreign_item: &ForeignItem) { self.operation.visit_id(foreign_item.id); visit::walk_foreign_item(self, foreign_item) @@ -456,10 +425,24 @@ impl<'a, 'v, O: IdVisitingOperation> Visitor<'v> for IdVisitor<'a, O> { } self.operation.visit_id(item.id); - if let ItemEnum(ref enum_definition, _) = item.node { - for variant in enum_definition.variants.iter() { - self.operation.visit_id(variant.node.id) + match item.node { + ItemUse(ref view_path) => { + match view_path.node { + ViewPathSimple(_, _) | + ViewPathGlob(_) => {} + ViewPathList(_, ref paths) => { + for path in paths.iter() { + self.operation.visit_id(path.node.id()) + } + } + } + } + ItemEnum(ref enum_definition, _) => { + for variant in enum_definition.variants.iter() { + self.operation.visit_id(variant.node.id) + } } + _ => {} } visit::walk_item(self, item); @@ -662,37 +645,6 @@ pub fn walk_pat(pat: &Pat, mut it: F) -> bool where F: FnMut(&Pat) -> bool { walk_pat_(pat, &mut it) } -pub trait EachViewItem { - fn each_view_item(&self, f: F) -> bool where F: FnMut(&ast::ViewItem) -> bool; -} - -struct EachViewItemData where F: FnMut(&ast::ViewItem) -> bool { - callback: F, -} - -impl<'v, F> Visitor<'v> for EachViewItemData where F: FnMut(&ast::ViewItem) -> bool { - fn visit_view_item(&mut self, view_item: &ast::ViewItem) { - let _ = (self.callback)(view_item); - } -} - -impl EachViewItem for ast::Crate { - fn each_view_item(&self, f: F) -> bool where F: FnMut(&ast::ViewItem) -> bool { - let mut visit = EachViewItemData { - callback: f, - }; - visit::walk_crate(&mut visit, self); - true - } -} - -pub fn view_path_id(p: &ViewPath) -> NodeId { - match p.node { - ViewPathSimple(_, _, id) | ViewPathGlob(_, id) - | ViewPathList(_, _, id) => id - } -} - /// Returns true if the given struct def is tuple-like; i.e. that its fields /// are unnamed. pub fn struct_def_is_tuple_like(struct_def: &ast::StructDef) -> bool { diff --git a/src/libsyntax/config.rs b/src/libsyntax/config.rs index 3f91831a5df..3eaac0fe333 100644 --- a/src/libsyntax/config.rs +++ b/src/libsyntax/config.rs @@ -63,28 +63,13 @@ pub fn strip_items(krate: ast::Crate, in_cfg: F) -> ast::Crate where ctxt.fold_crate(krate) } -fn filter_view_item(cx: &mut Context, - view_item: ast::ViewItem) - -> Option where - F: FnMut(&[ast::Attribute]) -> bool -{ - if view_item_in_cfg(cx, &view_item) { - Some(view_item) - } else { - None - } -} - fn fold_mod(cx: &mut Context, - ast::Mod {inner, - view_items, items}: ast::Mod) -> ast::Mod where + ast::Mod {inner, items}: ast::Mod) + -> ast::Mod where F: FnMut(&[ast::Attribute]) -> bool { ast::Mod { inner: inner, - view_items: view_items.into_iter().filter_map(|a| { - filter_view_item(cx, a).map(|x| cx.fold_view_item(x)) - }).collect(), items: items.into_iter().flat_map(|a| { cx.fold_item(a).into_iter() }).collect() @@ -104,15 +89,12 @@ fn filter_foreign_item(cx: &mut Context, } fn fold_foreign_mod(cx: &mut Context, - ast::ForeignMod {abi, view_items, items}: ast::ForeignMod) + ast::ForeignMod {abi, items}: ast::ForeignMod) -> ast::ForeignMod where F: FnMut(&[ast::Attribute]) -> bool { ast::ForeignMod { abi: abi, - view_items: view_items.into_iter().filter_map(|a| { - filter_view_item(cx, a).map(|x| cx.fold_view_item(x)) - }).collect(), items: items.into_iter() .filter_map(|a| filter_foreign_item(cx, a)) .collect() @@ -216,18 +198,14 @@ fn retain_stmt(cx: &mut Context, stmt: &ast::Stmt) -> bool where fn fold_block(cx: &mut Context, b: P) -> P where F: FnMut(&[ast::Attribute]) -> bool { - b.map(|ast::Block {id, view_items, stmts, expr, rules, span}| { + b.map(|ast::Block {id, stmts, expr, rules, span}| { let resulting_stmts: Vec> = stmts.into_iter().filter(|a| retain_stmt(cx, &**a)).collect(); let resulting_stmts = resulting_stmts.into_iter() .flat_map(|stmt| cx.fold_stmt(stmt).into_iter()) .collect(); - let filtered_view_items = view_items.into_iter().filter_map(|a| { - filter_view_item(cx, a).map(|x| cx.fold_view_item(x)) - }).collect(); ast::Block { id: id, - view_items: filtered_view_items, stmts: resulting_stmts, expr: expr.map(|x| cx.fold_expr(x)), rules: rules, @@ -267,12 +245,6 @@ fn foreign_item_in_cfg(cx: &mut Context, item: &ast::ForeignItem) -> bool return (cx.in_cfg)(item.attrs.as_slice()); } -fn view_item_in_cfg(cx: &mut Context, item: &ast::ViewItem) -> bool where - F: FnMut(&[ast::Attribute]) -> bool -{ - return (cx.in_cfg)(item.attrs.as_slice()); -} - fn trait_method_in_cfg(cx: &mut Context, meth: &ast::TraitItem) -> bool where F: FnMut(&[ast::Attribute]) -> bool { diff --git a/src/libsyntax/ext/build.rs b/src/libsyntax/ext/build.rs index c34142aec39..2adf0f1a1a2 100644 --- a/src/libsyntax/ext/build.rs +++ b/src/libsyntax/ext/build.rs @@ -97,7 +97,6 @@ pub trait AstBuilder { expr: Option>) -> P; fn block_expr(&self, expr: P) -> P; fn block_all(&self, span: Span, - view_items: Vec, stmts: Vec>, expr: Option>) -> P; @@ -242,7 +241,7 @@ pub trait AstBuilder { fn item_mod(&self, span: Span, inner_span: Span, name: Ident, attrs: Vec, - vi: Vec , items: Vec> ) -> P; + items: Vec>) -> P; fn item_static(&self, span: Span, @@ -280,15 +279,15 @@ pub trait AstBuilder { value: ast::Lit_) -> P; - fn view_use(&self, sp: Span, - vis: ast::Visibility, vp: P) -> ast::ViewItem; - fn view_use_simple(&self, sp: Span, vis: ast::Visibility, path: ast::Path) -> ast::ViewItem; - fn view_use_simple_(&self, sp: Span, vis: ast::Visibility, - ident: ast::Ident, path: ast::Path) -> ast::ViewItem; - fn view_use_list(&self, sp: Span, vis: ast::Visibility, - path: Vec , imports: &[ast::Ident]) -> ast::ViewItem; - fn view_use_glob(&self, sp: Span, - vis: ast::Visibility, path: Vec ) -> ast::ViewItem; + fn item_use(&self, sp: Span, + vis: ast::Visibility, vp: P) -> P; + fn item_use_simple(&self, sp: Span, vis: ast::Visibility, path: ast::Path) -> P; + fn item_use_simple_(&self, sp: Span, vis: ast::Visibility, + ident: ast::Ident, path: ast::Path) -> P; + fn item_use_list(&self, sp: Span, vis: ast::Visibility, + path: Vec, imports: &[ast::Ident]) -> P; + fn item_use_glob(&self, sp: Span, + vis: ast::Visibility, path: Vec) -> P; } impl<'a> AstBuilder for ExtCtxt<'a> { @@ -519,7 +518,7 @@ impl<'a> AstBuilder for ExtCtxt<'a> { fn block(&self, span: Span, stmts: Vec>, expr: Option>) -> P { - self.block_all(span, Vec::new(), stmts, expr) + self.block_all(span, stmts, expr) } fn stmt_item(&self, sp: Span, item: P) -> P { @@ -528,15 +527,13 @@ impl<'a> AstBuilder for ExtCtxt<'a> { } fn block_expr(&self, expr: P) -> P { - self.block_all(expr.span, Vec::new(), Vec::new(), Some(expr)) + self.block_all(expr.span, Vec::new(), Some(expr)) } fn block_all(&self, span: Span, - view_items: Vec, stmts: Vec>, expr: Option>) -> P { P(ast::Block { - view_items: view_items, stmts: stmts, expr: expr, id: ast::DUMMY_NODE_ID, @@ -1031,16 +1028,14 @@ impl<'a> AstBuilder for ExtCtxt<'a> { } fn item_mod(&self, span: Span, inner_span: Span, name: Ident, - attrs: Vec , - vi: Vec , - items: Vec> ) -> P { + attrs: Vec, + items: Vec>) -> P { self.item( span, name, attrs, ast::ItemMod(ast::Mod { inner: inner_span, - view_items: vi, items: items, }) ) @@ -1101,47 +1096,47 @@ impl<'a> AstBuilder for ExtCtxt<'a> { P(respan(sp, ast::MetaNameValue(name, respan(sp, value)))) } - fn view_use(&self, sp: Span, - vis: ast::Visibility, vp: P) -> ast::ViewItem { - ast::ViewItem { - node: ast::ViewItemUse(vp), - attrs: Vec::new(), + fn item_use(&self, sp: Span, + vis: ast::Visibility, vp: P) -> P { + P(ast::Item { + id: ast::DUMMY_NODE_ID, + ident: special_idents::invalid, + attrs: vec![], + node: ast::ItemUse(vp), vis: vis, span: sp - } + }) } - fn view_use_simple(&self, sp: Span, vis: ast::Visibility, path: ast::Path) -> ast::ViewItem { + fn item_use_simple(&self, sp: Span, vis: ast::Visibility, path: ast::Path) -> P { let last = path.segments.last().unwrap().identifier; - self.view_use_simple_(sp, vis, last, path) + self.item_use_simple_(sp, vis, last, path) } - fn view_use_simple_(&self, sp: Span, vis: ast::Visibility, - ident: ast::Ident, path: ast::Path) -> ast::ViewItem { - self.view_use(sp, vis, + fn item_use_simple_(&self, sp: Span, vis: ast::Visibility, + ident: ast::Ident, path: ast::Path) -> P { + self.item_use(sp, vis, P(respan(sp, ast::ViewPathSimple(ident, - path, - ast::DUMMY_NODE_ID)))) + path)))) } - fn view_use_list(&self, sp: Span, vis: ast::Visibility, - path: Vec , imports: &[ast::Ident]) -> ast::ViewItem { + fn item_use_list(&self, sp: Span, vis: ast::Visibility, + path: Vec, imports: &[ast::Ident]) -> P { let imports = imports.iter().map(|id| { respan(sp, ast::PathListIdent { name: *id, id: ast::DUMMY_NODE_ID }) }).collect(); - self.view_use(sp, vis, + self.item_use(sp, vis, P(respan(sp, ast::ViewPathList(self.path(sp, path), - imports, - ast::DUMMY_NODE_ID)))) + imports)))) } - fn view_use_glob(&self, sp: Span, - vis: ast::Visibility, path: Vec ) -> ast::ViewItem { - self.view_use(sp, vis, + fn item_use_glob(&self, sp: Span, + vis: ast::Visibility, path: Vec) -> P { + self.item_use(sp, vis, P(respan(sp, - ast::ViewPathGlob(self.path(sp, path), ast::DUMMY_NODE_ID)))) + ast::ViewPathGlob(self.path(sp, path))))) } } diff --git a/src/libsyntax/ext/deriving/generic/mod.rs b/src/libsyntax/ext/deriving/generic/mod.rs index 293e4befd3b..73d002172a9 100644 --- a/src/libsyntax/ext/deriving/generic/mod.rs +++ b/src/libsyntax/ext/deriving/generic/mod.rs @@ -1073,7 +1073,7 @@ impl<'a> MethodDef<'a> { // // } let arm_expr = cx.expr_block( - cx.block_all(sp, Vec::new(), index_let_stmts, Some(arm_expr))); + cx.block_all(sp, index_let_stmts, Some(arm_expr))); // Builds arm: // _ => { let __self0_vi = ...; diff --git a/src/libsyntax/ext/expand.rs b/src/libsyntax/ext/expand.rs index c95bdeefd45..2aa5f72c0c4 100644 --- a/src/libsyntax/ext/expand.rs +++ b/src/libsyntax/ext/expand.rs @@ -206,7 +206,6 @@ pub fn expand_expr(e: P, fld: &mut MacroExpander) -> P { // wrap the if-let expr in a block let span = els.span; let blk = P(ast::Block { - view_items: vec![], stmts: vec![], expr: Some(P(els)), id: ast::DUMMY_NODE_ID, @@ -799,8 +798,7 @@ pub fn expand_block(blk: P, fld: &mut MacroExpander) -> P { // expand the elements of a block. pub fn expand_block_elts(b: P, fld: &mut MacroExpander) -> P { - b.map(|Block {id, view_items, stmts, expr, rules, span}| { - let new_view_items = view_items.into_iter().map(|x| fld.fold_view_item(x)).collect(); + b.map(|Block {id, stmts, expr, rules, span}| { let new_stmts = stmts.into_iter().flat_map(|x| { // perform all pending renames let renamed_stmt = { @@ -821,7 +819,6 @@ pub fn expand_block_elts(b: P, fld: &mut MacroExpander) -> P { }); Block { id: fld.new_id(id), - view_items: new_view_items, stmts: new_stmts, expr: new_expr, rules: rules, diff --git a/src/libsyntax/ext/quote.rs b/src/libsyntax/ext/quote.rs index c42b188302c..e63a920994b 100644 --- a/src/libsyntax/ext/quote.rs +++ b/src/libsyntax/ext/quote.rs @@ -352,18 +352,11 @@ pub mod rt { impl<'a> ExtParseUtils for ExtCtxt<'a> { fn parse_item(&self, s: String) -> P { - let res = parse::parse_item_from_source_str( + parse::parse_item_from_source_str( "".to_string(), s, self.cfg(), - self.parse_sess()); - match res { - Some(ast) => ast, - None => { - error!("parse error"); - panic!() - } - } + self.parse_sess()).expect("parse error") } fn parse_stmt(&self, s: String) -> P { @@ -767,7 +760,6 @@ fn expand_tts(cx: &ExtCtxt, sp: Span, tts: &[ast::TokenTree]) vector.extend(mk_tts(cx, &tts[]).into_iter()); let block = cx.expr_block( cx.block_all(sp, - Vec::new(), vector, Some(cx.expr_ident(sp, id_ext("tt"))))); @@ -778,18 +770,18 @@ fn expand_wrapper(cx: &ExtCtxt, sp: Span, cx_expr: P, expr: P) -> P { - let uses = [ - &["syntax", "ext", "quote", "rt"], - ].iter().map(|path| { - let path = path.iter().map(|s| s.to_string()).collect(); - cx.view_use_glob(sp, ast::Inherited, ids_ext(path)) - }).collect(); - // Explicitly borrow to avoid moving from the invoker (#16992) let cx_expr_borrow = cx.expr_addr_of(sp, cx.expr_deref(sp, cx_expr)); let stmt_let_ext_cx = cx.stmt_let(sp, false, id_ext("ext_cx"), cx_expr_borrow); - cx.expr_block(cx.block_all(sp, uses, vec!(stmt_let_ext_cx), Some(expr))) + let stmts = [ + &["syntax", "ext", "quote", "rt"], + ].iter().map(|path| { + let path = path.iter().map(|s| s.to_string()).collect(); + cx.stmt_item(sp, cx.item_use_glob(sp, ast::Inherited, ids_ext(path))) + }).chain(Some(stmt_let_ext_cx).into_iter()).collect(); + + cx.expr_block(cx.block_all(sp, stmts, Some(expr))) } fn expand_parse_call(cx: &ExtCtxt, diff --git a/src/libsyntax/feature_gate.rs b/src/libsyntax/feature_gate.rs index 6ab31c63a14..762a1dcbfc3 100644 --- a/src/libsyntax/feature_gate.rs +++ b/src/libsyntax/feature_gate.rs @@ -226,22 +226,6 @@ impl<'a, 'v> Visitor<'v> for PostExpansionVisitor<'a> { } } - fn visit_view_item(&mut self, i: &ast::ViewItem) { - match i.node { - ast::ViewItemUse(..) => {} - ast::ViewItemExternCrate(..) => { - for attr in i.attrs.iter() { - if attr.check_name("plugin") { - self.gate_feature("plugin", attr.span, - "compiler plugins are experimental \ - and possibly buggy"); - } - } - } - } - visit::walk_view_item(self, i) - } - fn visit_item(&mut self, i: &ast::Item) { for attr in i.attrs.iter() { if attr.name() == "thread_local" { @@ -260,6 +244,14 @@ impl<'a, 'v> Visitor<'v> for PostExpansionVisitor<'a> { } } match i.node { + ast::ItemExternCrate(_) => { + if attr::contains_name(&i.attrs[], "plugin") { + self.gate_feature("plugin", i.span, + "compiler plugins are experimental \ + and possibly buggy"); + } + } + ast::ItemForeignMod(ref foreign_module) => { if attr::contains_name(&i.attrs[], "link_args") { self.gate_feature("link_args", i.span, diff --git a/src/libsyntax/fold.rs b/src/libsyntax/fold.rs index f484650ad5b..44ee17a0d96 100644 --- a/src/libsyntax/fold.rs +++ b/src/libsyntax/fold.rs @@ -78,10 +78,6 @@ pub trait Folder : Sized { noop_fold_view_path(view_path, self) } - fn fold_view_item(&mut self, vi: ViewItem) -> ViewItem { - noop_fold_view_item(vi, self) - } - fn fold_foreign_item(&mut self, ni: P) -> P { noop_fold_foreign_item(ni, self) } @@ -349,16 +345,13 @@ pub fn noop_fold_meta_items(meta_items: Vec>, fld: &mut T pub fn noop_fold_view_path(view_path: P, fld: &mut T) -> P { view_path.map(|Spanned {node, span}| Spanned { node: match node { - ViewPathSimple(ident, path, node_id) => { - let id = fld.new_id(node_id); - ViewPathSimple(ident, fld.fold_path(path), id) + ViewPathSimple(ident, path) => { + ViewPathSimple(ident, fld.fold_path(path)) } - ViewPathGlob(path, node_id) => { - let id = fld.new_id(node_id); - ViewPathGlob(fld.fold_path(path), id) + ViewPathGlob(path) => { + ViewPathGlob(fld.fold_path(path)) } - ViewPathList(path, path_list_idents, node_id) => { - let id = fld.new_id(node_id); + ViewPathList(path, path_list_idents) => { ViewPathList(fld.fold_path(path), path_list_idents.move_map(|path_list_ident| { Spanned { @@ -373,8 +366,7 @@ pub fn noop_fold_view_path(view_path: P, fld: &mut T) -> P< }, span: fld.new_span(path_list_ident.span) } - }), - id) + })) } }, span: fld.new_span(span) @@ -470,11 +462,10 @@ pub fn noop_fold_qpath(qpath: P, fld: &mut T) -> P { }) } -pub fn noop_fold_foreign_mod(ForeignMod {abi, view_items, items}: ForeignMod, +pub fn noop_fold_foreign_mod(ForeignMod {abi, items}: ForeignMod, fld: &mut T) -> ForeignMod { ForeignMod { abi: abi, - view_items: view_items.move_map(|x| fld.fold_view_item(x)), items: items.move_map(|x| fld.fold_foreign_item(x)), } } @@ -953,28 +944,9 @@ fn noop_fold_variant_arg(VariantArg {id, ty}: VariantArg, folder: &mu } } -pub fn noop_fold_view_item(ViewItem {node, attrs, vis, span}: ViewItem, - folder: &mut T) -> ViewItem { - ViewItem { - node: match node { - ViewItemExternCrate(ident, string, node_id) => { - ViewItemExternCrate(ident, string, - folder.new_id(node_id)) - } - ViewItemUse(view_path) => { - ViewItemUse(folder.fold_view_path(view_path)) - } - }, - attrs: attrs.move_map(|a| folder.fold_attribute(a)), - vis: vis, - span: folder.new_span(span) - } -} - pub fn noop_fold_block(b: P, folder: &mut T) -> P { - b.map(|Block {id, view_items, stmts, expr, rules, span}| Block { + b.map(|Block {id, stmts, expr, rules, span}| Block { id: folder.new_id(id), - view_items: view_items.move_map(|x| folder.fold_view_item(x)), stmts: stmts.into_iter().flat_map(|s| folder.fold_stmt(s).into_iter()).collect(), expr: expr.map(|x| folder.fold_expr(x)), rules: rules, @@ -984,6 +956,10 @@ pub fn noop_fold_block(b: P, folder: &mut T) -> P { pub fn noop_fold_item_underscore(i: Item_, folder: &mut T) -> Item_ { match i { + ItemExternCrate(string) => ItemExternCrate(string), + ItemUse(view_path) => { + ItemUse(folder.fold_view_path(view_path)) + } ItemStatic(t, m, e) => { ItemStatic(folder.fold_ty(t), m, folder.fold_expr(e)) } @@ -1103,10 +1079,9 @@ pub fn noop_fold_type_method(m: TypeMethod, fld: &mut T) -> TypeMetho } } -pub fn noop_fold_mod(Mod {inner, view_items, items}: Mod, folder: &mut T) -> Mod { +pub fn noop_fold_mod(Mod {inner, items}: Mod, folder: &mut T) -> Mod { Mod { inner: folder.new_span(inner), - view_items: view_items.move_map(|x| folder.fold_view_item(x)), items: items.into_iter().flat_map(|x| folder.fold_item(x).into_iter()).collect(), } } @@ -1137,9 +1112,8 @@ pub fn noop_fold_crate(Crate {module, attrs, config, mut exported_mac } None => (ast::Mod { inner: span, - view_items: Vec::new(), - items: Vec::new(), - }, Vec::new(), span) + items: vec![], + }, vec![], span) }; for def in exported_macros.iter_mut() { diff --git a/src/libsyntax/parse/mod.rs b/src/libsyntax/parse/mod.rs index dd376fe9e10..49dca01ef50 100644 --- a/src/libsyntax/parse/mod.rs +++ b/src/libsyntax/parse/mod.rs @@ -757,11 +757,10 @@ mod test { use attr::{first_attr_value_str_by_name, AttrMetaMethods}; use parse::parser::Parser; use parse::token::{str_to_ident}; - use print::pprust::view_item_to_string; + use print::pprust::item_to_string; use ptr::P; use util::parser_testing::{string_to_tts, string_to_parser}; - use util::parser_testing::{string_to_expr, string_to_item}; - use util::parser_testing::{string_to_stmt, string_to_view_item}; + use util::parser_testing::{string_to_expr, string_to_item, string_to_stmt}; // produce a codemap::span fn sp(a: u32, b: u32) -> Span { @@ -1079,7 +1078,6 @@ mod test { } }, P(ast::Block { - view_items: Vec::new(), stmts: vec!(P(Spanned{ node: ast::StmtSemi(P(ast::Expr{ id: ast::DUMMY_NODE_ID, @@ -1111,25 +1109,25 @@ mod test { #[test] fn parse_use() { let use_s = "use foo::bar::baz;"; - let vitem = string_to_view_item(use_s.to_string()); - let vitem_s = view_item_to_string(&vitem); + let vitem = string_to_item(use_s.to_string()); + let vitem_s = item_to_string(&vitem); assert_eq!(&vitem_s[], use_s); let use_s = "use foo::bar as baz;"; - let vitem = string_to_view_item(use_s.to_string()); - let vitem_s = view_item_to_string(&vitem); + let vitem = string_to_item(use_s.to_string()); + let vitem_s = item_to_string(&vitem); assert_eq!(&vitem_s[], use_s); } #[test] fn parse_extern_crate() { let ex_s = "extern crate foo;"; - let vitem = string_to_view_item(ex_s.to_string()); - let vitem_s = view_item_to_string(&vitem); + let vitem = string_to_item(ex_s.to_string()); + let vitem_s = item_to_string(&vitem); assert_eq!(&vitem_s[], ex_s); let ex_s = "extern crate \"foo\" as bar;"; - let vitem = string_to_view_item(ex_s.to_string()); - let vitem_s = view_item_to_string(&vitem); + let vitem = string_to_item(ex_s.to_string()); + let vitem_s = item_to_string(&vitem); assert_eq!(&vitem_s[], ex_s); } diff --git a/src/libsyntax/print/pprust.rs b/src/libsyntax/print/pprust.rs index b59e770c6ba..19499db2862 100644 --- a/src/libsyntax/print/pprust.rs +++ b/src/libsyntax/print/pprust.rs @@ -337,10 +337,6 @@ pub fn item_to_string(i: &ast::Item) -> String { $to_string(|s| s.print_item(i)) } -pub fn view_item_to_string(i: &ast::ViewItem) -> String { - $to_string(|s| s.print_view_item(i)) -} - pub fn generics_to_string(generics: &ast::Generics) -> String { $to_string(|s| s.print_generics(generics)) } @@ -638,9 +634,6 @@ impl<'a> State<'a> { pub fn print_mod(&mut self, _mod: &ast::Mod, attrs: &[ast::Attribute]) -> IoResult<()> { try!(self.print_inner_attributes(attrs)); - for vitem in _mod.view_items.iter() { - try!(self.print_view_item(vitem)); - } for item in _mod.items.iter() { try!(self.print_item(&**item)); } @@ -650,9 +643,6 @@ impl<'a> State<'a> { pub fn print_foreign_mod(&mut self, nmod: &ast::ForeignMod, attrs: &[ast::Attribute]) -> IoResult<()> { try!(self.print_inner_attributes(attrs)); - for vitem in nmod.view_items.iter() { - try!(self.print_view_item(vitem)); - } for item in nmod.items.iter() { try!(self.print_foreign_item(&**item)); } @@ -809,6 +799,28 @@ impl<'a> State<'a> { try!(self.print_outer_attributes(&item.attrs[])); try!(self.ann.pre(self, NodeItem(item))); match item.node { + ast::ItemExternCrate(ref optional_path) => { + try!(self.head(&visibility_qualified(item.vis, + "extern crate")[])); + for &(ref p, style) in optional_path.iter() { + try!(self.print_string(p.get(), style)); + try!(space(&mut self.s)); + try!(word(&mut self.s, "as")); + try!(space(&mut self.s)); + } + try!(self.print_ident(item.ident)); + try!(word(&mut self.s, ";")); + try!(self.end()); // end inner head-block + try!(self.end()); // end outer head-block + } + ast::ItemUse(ref vp) => { + try!(self.head(&visibility_qualified(item.vis, + "use")[])); + try!(self.print_view_path(&**vp)); + try!(word(&mut self.s, ";")); + try!(self.end()); // end inner head-block + try!(self.end()); // end outer head-block + } ast::ItemStatic(ref ty, m, ref expr) => { try!(self.head(&visibility_qualified(item.vis, "static")[])); @@ -1380,9 +1392,6 @@ impl<'a> State<'a> { try!(self.print_inner_attributes(attrs)); - for vi in blk.view_items.iter() { - try!(self.print_view_item(vi)); - } for st in blk.stmts.iter() { try!(self.print_stmt(&**st)); } @@ -2577,7 +2586,7 @@ impl<'a> State<'a> { pub fn print_view_path(&mut self, vp: &ast::ViewPath) -> IoResult<()> { match vp.node { - ast::ViewPathSimple(ident, ref path, _) => { + ast::ViewPathSimple(ident, ref path) => { try!(self.print_path(path, false)); // FIXME(#6993) can't compare identifiers directly here @@ -2591,12 +2600,12 @@ impl<'a> State<'a> { Ok(()) } - ast::ViewPathGlob(ref path, _) => { + ast::ViewPathGlob(ref path) => { try!(self.print_path(path, false)); word(&mut self.s, "::*") } - ast::ViewPathList(ref path, ref idents, _) => { + ast::ViewPathList(ref path, ref idents) => { if path.segments.is_empty() { try!(word(&mut self.s, "{")); } else { @@ -2618,33 +2627,6 @@ impl<'a> State<'a> { } } - pub fn print_view_item(&mut self, item: &ast::ViewItem) -> IoResult<()> { - try!(self.hardbreak_if_not_bol()); - try!(self.maybe_print_comment(item.span.lo)); - try!(self.print_outer_attributes(&item.attrs[])); - try!(self.print_visibility(item.vis)); - match item.node { - ast::ViewItemExternCrate(id, ref optional_path, _) => { - try!(self.head("extern crate")); - for &(ref p, style) in optional_path.iter() { - try!(self.print_string(p.get(), style)); - try!(space(&mut self.s)); - try!(word(&mut self.s, "as")); - try!(space(&mut self.s)); - } - try!(self.print_ident(id)); - } - - ast::ViewItemUse(ref vp) => { - try!(self.head("use")); - try!(self.print_view_path(&**vp)); - } - } - try!(word(&mut self.s, ";")); - try!(self.end()); // end inner head-block - self.end() // end outer head-block - } - pub fn print_mutability(&mut self, mutbl: ast::Mutability) -> IoResult<()> { match mutbl { diff --git a/src/libsyntax/std_inject.rs b/src/libsyntax/std_inject.rs index 28b9eaa54aa..d75fbcf199d 100644 --- a/src/libsyntax/std_inject.rs +++ b/src/libsyntax/std_inject.rs @@ -20,8 +20,6 @@ use parse::token; use ptr::P; use util::small_vector::SmallVector; -use std::mem; - pub fn maybe_inject_crates_ref(krate: ast::Crate, alt_std_name: Option) -> ast::Crate { if use_std(&krate) { @@ -60,20 +58,16 @@ impl<'a> fold::Folder for StandardLibraryInjector<'a> { None => token::intern_and_get_ident("std"), }; - let mut vis = vec!(ast::ViewItem { - node: ast::ViewItemExternCrate(token::str_to_ident("std"), - Some((actual_crate_name, ast::CookedStr)), - ast::DUMMY_NODE_ID), + krate.module.items.insert(0, P(ast::Item { + id: ast::DUMMY_NODE_ID, + ident: token::str_to_ident("std"), attrs: vec!( attr::mk_attr_outer(attr::mk_attr_id(), attr::mk_word_item( InternedString::new("macro_use")))), + node: ast::ItemExternCrate(Some((actual_crate_name, ast::CookedStr))), vis: ast::Inherited, span: DUMMY_SP - }); - - // `extern crate` must be precede `use` items - mem::swap(&mut vis, &mut krate.module.view_items); - krate.module.view_items.extend(vis.into_iter()); + })); // don't add #![no_std] here, that will block the prelude injection later. // Add it during the prelude injection instead. @@ -123,7 +117,7 @@ impl<'a> fold::Folder for PreludeInjector<'a> { } } - fn fold_mod(&mut self, ast::Mod {inner, view_items, items}: ast::Mod) -> ast::Mod { + fn fold_mod(&mut self, mut mod_: ast::Mod) -> ast::Mod { let prelude_path = ast::Path { span: DUMMY_SP, global: false, @@ -143,18 +137,11 @@ impl<'a> fold::Folder for PreludeInjector<'a> { ], }; - let (crates, uses): (Vec<_>, _) = view_items.iter().cloned().partition(|x| { - match x.node { - ast::ViewItemExternCrate(..) => true, - _ => false, - } - }); - - // add prelude after any `extern crate` but before any `use` - let mut view_items = crates; - let vp = P(codemap::dummy_spanned(ast::ViewPathGlob(prelude_path, ast::DUMMY_NODE_ID))); - view_items.push(ast::ViewItem { - node: ast::ViewItemUse(vp), + let vp = P(codemap::dummy_spanned(ast::ViewPathGlob(prelude_path))); + mod_.items.insert(0, P(ast::Item { + id: ast::DUMMY_NODE_ID, + ident: special_idents::invalid, + node: ast::ItemUse(vp), attrs: vec![ast::Attribute { span: DUMMY_SP, node: ast::Attribute_ { @@ -170,14 +157,9 @@ impl<'a> fold::Folder for PreludeInjector<'a> { }], vis: ast::Inherited, span: DUMMY_SP, - }); - view_items.extend(uses.into_iter()); - - fold::noop_fold_mod(ast::Mod { - inner: inner, - view_items: view_items, - items: items - }, self) + })); + + fold::noop_fold_mod(mod_, self) } } diff --git a/src/libsyntax/test.rs b/src/libsyntax/test.rs index 5f869d5093f..38aa7b1c769 100644 --- a/src/libsyntax/test.rs +++ b/src/libsyntax/test.rs @@ -105,11 +105,11 @@ impl<'a> fold::Folder for TestHarnessGenerator<'a> { // Add a special __test module to the crate that will contain code // generated for the test harness let (mod_, reexport) = mk_test_module(&mut self.cx); - folded.module.items.push(mod_); match reexport { - Some(re) => folded.module.view_items.push(re), + Some(re) => folded.module.items.push(re), None => {} } + folded.module.items.push(mod_); folded } @@ -205,22 +205,19 @@ impl<'a> fold::Folder for TestHarnessGenerator<'a> { fn mk_reexport_mod(cx: &mut TestCtxt, tests: Vec, tested_submods: Vec<(ast::Ident, ast::Ident)>) -> (P, ast::Ident) { - let mut view_items = Vec::new(); let super_ = token::str_to_ident("super"); - view_items.extend(tests.into_iter().map(|r| { - cx.ext_cx.view_use_simple(DUMMY_SP, ast::Public, + let items = tests.into_iter().map(|r| { + cx.ext_cx.item_use_simple(DUMMY_SP, ast::Public, cx.ext_cx.path(DUMMY_SP, vec![super_, r])) - })); - view_items.extend(tested_submods.into_iter().map(|(r, sym)| { + }).chain(tested_submods.into_iter().map(|(r, sym)| { let path = cx.ext_cx.path(DUMMY_SP, vec![super_, r, sym]); - cx.ext_cx.view_use_simple_(DUMMY_SP, ast::Public, r, path) + cx.ext_cx.item_use_simple_(DUMMY_SP, ast::Public, r, path) })); let reexport_mod = ast::Mod { inner: DUMMY_SP, - view_items: view_items, - items: Vec::new(), + items: items.collect(), }; let sym = token::gensym_ident("__test_reexports"); @@ -388,29 +385,29 @@ mod __test { */ -fn mk_std(cx: &TestCtxt) -> ast::ViewItem { +fn mk_std(cx: &TestCtxt) -> P { let id_test = token::str_to_ident("test"); - let (vi, vis) = if cx.is_test_crate { - (ast::ViewItemUse( + let (vi, vis, ident) = if cx.is_test_crate { + (ast::ItemUse( P(nospan(ast::ViewPathSimple(id_test, - path_node(vec!(id_test)), - ast::DUMMY_NODE_ID)))), - ast::Public) + path_node(vec!(id_test)))))), + ast::Public, token::special_idents::invalid) } else { - (ast::ViewItemExternCrate(id_test, None, ast::DUMMY_NODE_ID), - ast::Inherited) + (ast::ItemExternCrate(None), ast::Inherited, id_test) }; - ast::ViewItem { + P(ast::Item { + id: ast::DUMMY_NODE_ID, + ident: ident, node: vi, - attrs: Vec::new(), + attrs: vec![], vis: vis, span: DUMMY_SP - } + }) } -fn mk_test_module(cx: &mut TestCtxt) -> (P, Option) { +fn mk_test_module(cx: &mut TestCtxt) -> (P, Option>) { // Link to test crate - let view_items = vec!(mk_std(cx)); + let import = mk_std(cx); // A constant vector of test descriptors. let tests = mk_tests(cx); @@ -427,8 +424,7 @@ fn mk_test_module(cx: &mut TestCtxt) -> (P, Option) { let testmod = ast::Mod { inner: DUMMY_SP, - view_items: view_items, - items: vec!(mainfn, tests), + items: vec![import, mainfn, tests], }; let item_ = ast::ItemMod(testmod); @@ -439,34 +435,35 @@ fn mk_test_module(cx: &mut TestCtxt) -> (P, Option) { vec![unstable]))); attr::mk_attr_inner(attr::mk_attr_id(), allow) }; - let item = ast::Item { - ident: mod_ident, + let item = P(ast::Item { id: ast::DUMMY_NODE_ID, + ident: mod_ident, + attrs: vec![allow_unstable], node: item_, vis: ast::Public, span: DUMMY_SP, - attrs: vec![allow_unstable], - }; + }); let reexport = cx.reexport_test_harness_main.as_ref().map(|s| { // building `use = __test::main` let reexport_ident = token::str_to_ident(s.get()); let use_path = nospan(ast::ViewPathSimple(reexport_ident, - path_node(vec![mod_ident, token::str_to_ident("main")]), - ast::DUMMY_NODE_ID)); + path_node(vec![mod_ident, token::str_to_ident("main")]))); - ast::ViewItem { - node: ast::ViewItemUse(P(use_path)), + P(ast::Item { + id: ast::DUMMY_NODE_ID, + ident: token::special_idents::invalid, attrs: vec![], + node: ast::ItemUse(P(use_path)), vis: ast::Inherited, span: DUMMY_SP - } + }) }); - debug!("Synthetic test module:\n{}\n", pprust::item_to_string(&item)); + debug!("Synthetic test module:\n{}\n", pprust::item_to_string(&*item)); - (P(item), reexport) + (item, reexport) } fn nospan(t: T) -> codemap::Spanned { diff --git a/src/libsyntax/util/parser_testing.rs b/src/libsyntax/util/parser_testing.rs index 83bbff8473d..4a288814b6b 100644 --- a/src/libsyntax/util/parser_testing.rs +++ b/src/libsyntax/util/parser_testing.rs @@ -69,13 +69,6 @@ pub fn string_to_stmt(source_str : String) -> P { }) } -/// Parse a string, return a view item -pub fn string_to_view_item (source_str : String) -> ast::ViewItem { - with_error_checking_parse(source_str, |p| { - p.parse_view_item(Vec::new()) - }) -} - /// Parse a string, return a pat. Uses "irrefutable"... which doesn't /// (currently) affect parsing. pub fn string_to_pat(source_str: String) -> P { diff --git a/src/libsyntax/visit.rs b/src/libsyntax/visit.rs index 7778b4fa34a..eb906788aa7 100644 --- a/src/libsyntax/visit.rs +++ b/src/libsyntax/visit.rs @@ -62,7 +62,6 @@ pub trait Visitor<'v> : Sized { self.visit_name(span, ident.name); } fn visit_mod(&mut self, m: &'v Mod, _s: Span, _n: NodeId) { walk_mod(self, m) } - fn visit_view_item(&mut self, i: &'v ViewItem) { walk_view_item(self, i) } fn visit_foreign_item(&mut self, i: &'v ForeignItem) { walk_foreign_item(self, i) } fn visit_item(&mut self, i: &'v Item) { walk_item(self, i) } fn visit_local(&mut self, l: &'v Local) { walk_local(self, l) } @@ -166,51 +165,11 @@ pub fn walk_crate<'v, V: Visitor<'v>>(visitor: &mut V, krate: &'v Crate) { } pub fn walk_mod<'v, V: Visitor<'v>>(visitor: &mut V, module: &'v Mod) { - for view_item in module.view_items.iter() { - visitor.visit_view_item(view_item) - } - for item in module.items.iter() { visitor.visit_item(&**item) } } -pub fn walk_view_item<'v, V: Visitor<'v>>(visitor: &mut V, vi: &'v ViewItem) { - match vi.node { - ViewItemExternCrate(name, _, _) => { - visitor.visit_ident(vi.span, name) - } - ViewItemUse(ref vp) => { - match vp.node { - ViewPathSimple(ident, ref path, id) => { - visitor.visit_ident(vp.span, ident); - visitor.visit_path(path, id); - } - ViewPathGlob(ref path, id) => { - visitor.visit_path(path, id); - } - ViewPathList(ref prefix, ref list, _) => { - for id in list.iter() { - match id.node { - PathListIdent { name, .. } => { - visitor.visit_ident(id.span, name); - } - PathListMod { .. } => () - } - } - - // Note that the `prefix` here is not a complete - // path, so we don't use `visit_path`. - walk_path(visitor, prefix); - } - } - } - } - for attr in vi.attrs.iter() { - visitor.visit_attribute(attr); - } -} - pub fn walk_local<'v, V: Visitor<'v>>(visitor: &mut V, local: &'v Local) { visitor.visit_pat(&*local.pat); walk_ty_opt(visitor, &local.ty); @@ -269,6 +228,32 @@ pub fn walk_trait_ref<'v,V>(visitor: &mut V, pub fn walk_item<'v, V: Visitor<'v>>(visitor: &mut V, item: &'v Item) { visitor.visit_ident(item.span, item.ident); match item.node { + ItemExternCrate(..) => {} + ItemUse(ref vp) => { + match vp.node { + ViewPathSimple(ident, ref path) => { + visitor.visit_ident(vp.span, ident); + visitor.visit_path(path, item.id); + } + ViewPathGlob(ref path) => { + visitor.visit_path(path, item.id); + } + ViewPathList(ref prefix, ref list) => { + for id in list.iter() { + match id.node { + PathListIdent { name, .. } => { + visitor.visit_ident(id.span, name); + } + PathListMod { .. } => () + } + } + + // Note that the `prefix` here is not a complete + // path, so we don't use `visit_path`. + walk_path(visitor, prefix); + } + } + } ItemStatic(ref typ, _, ref expr) | ItemConst(ref typ, ref expr) => { visitor.visit_ty(&**typ); @@ -285,9 +270,6 @@ pub fn walk_item<'v, V: Visitor<'v>>(visitor: &mut V, item: &'v Item) { visitor.visit_mod(module, item.span, item.id) } ItemForeignMod(ref foreign_module) => { - for view_item in foreign_module.view_items.iter() { - visitor.visit_view_item(view_item) - } for foreign_item in foreign_module.items.iter() { visitor.visit_foreign_item(&**foreign_item) } @@ -732,9 +714,6 @@ pub fn walk_struct_field<'v, V: Visitor<'v>>(visitor: &mut V, } pub fn walk_block<'v, V: Visitor<'v>>(visitor: &mut V, block: &'v Block) { - for view_item in block.view_items.iter() { - visitor.visit_view_item(view_item) - } for statement in block.stmts.iter() { visitor.visit_stmt(&**statement) } -- cgit 1.4.1-3-g733a5 From 139346adb6632dd1507b263f1702f5f6df66e682 Mon Sep 17 00:00:00 2001 From: Eduard Burtescu Date: Fri, 26 Dec 2014 23:36:21 +0200 Subject: tests: fix fallout of merging ast::ViewItem into ast::Item. --- src/librustc_driver/test.rs | 1 + src/libsyntax/parse/mod.rs | 16 ++++++++-------- src/test/compile-fail-fulldeps/gated-plugin.rs | 3 +-- src/test/compile-fail/issue-9957.rs | 15 --------------- src/test/compile-fail/unnecessary-private.rs | 3 ++- src/test/compile-fail/view-items-at-top.rs | 19 ------------------- src/test/pretty/issue-4264.pp | 4 ++-- 7 files changed, 14 insertions(+), 47 deletions(-) delete mode 100644 src/test/compile-fail/issue-9957.rs delete mode 100644 src/test/compile-fail/view-items-at-top.rs (limited to 'src/libsyntax/parse') diff --git a/src/librustc_driver/test.rs b/src/librustc_driver/test.rs index f68c76f4c44..cd28a27f988 100644 --- a/src/librustc_driver/test.rs +++ b/src/librustc_driver/test.rs @@ -200,6 +200,7 @@ impl<'a, 'tcx> Env<'a, 'tcx> { } return match it.node { + ast::ItemUse(..) | ast::ItemExternCrate(..) | ast::ItemConst(..) | ast::ItemStatic(..) | ast::ItemFn(..) | ast::ItemForeignMod(..) | ast::ItemTy(..) => { None diff --git a/src/libsyntax/parse/mod.rs b/src/libsyntax/parse/mod.rs index 49dca01ef50..8ac94a38273 100644 --- a/src/libsyntax/parse/mod.rs +++ b/src/libsyntax/parse/mod.rs @@ -1109,25 +1109,25 @@ mod test { #[test] fn parse_use() { let use_s = "use foo::bar::baz;"; - let vitem = string_to_item(use_s.to_string()); - let vitem_s = item_to_string(&vitem); + let vitem = string_to_item(use_s.to_string()).unwrap(); + let vitem_s = item_to_string(&*vitem); assert_eq!(&vitem_s[], use_s); let use_s = "use foo::bar as baz;"; - let vitem = string_to_item(use_s.to_string()); - let vitem_s = item_to_string(&vitem); + let vitem = string_to_item(use_s.to_string()).unwrap(); + let vitem_s = item_to_string(&*vitem); assert_eq!(&vitem_s[], use_s); } #[test] fn parse_extern_crate() { let ex_s = "extern crate foo;"; - let vitem = string_to_item(ex_s.to_string()); - let vitem_s = item_to_string(&vitem); + let vitem = string_to_item(ex_s.to_string()).unwrap(); + let vitem_s = item_to_string(&*vitem); assert_eq!(&vitem_s[], ex_s); let ex_s = "extern crate \"foo\" as bar;"; - let vitem = string_to_item(ex_s.to_string()); - let vitem_s = item_to_string(&vitem); + let vitem = string_to_item(ex_s.to_string()).unwrap(); + let vitem_s = item_to_string(&*vitem); assert_eq!(&vitem_s[], ex_s); } diff --git a/src/test/compile-fail-fulldeps/gated-plugin.rs b/src/test/compile-fail-fulldeps/gated-plugin.rs index 89090d5f38a..be9e57e2d19 100644 --- a/src/test/compile-fail-fulldeps/gated-plugin.rs +++ b/src/test/compile-fail-fulldeps/gated-plugin.rs @@ -11,8 +11,7 @@ // aux-build:macro_crate_test.rs // ignore-stage1 -#[plugin] #[no_link] +#[plugin] #[no_link] extern crate macro_crate_test; //~^ ERROR compiler plugins are experimental and possibly buggy -extern crate macro_crate_test; fn main() {} diff --git a/src/test/compile-fail/issue-9957.rs b/src/test/compile-fail/issue-9957.rs deleted file mode 100644 index b1204e82890..00000000000 --- a/src/test/compile-fail/issue-9957.rs +++ /dev/null @@ -1,15 +0,0 @@ -// 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. - -pub extern crate core; //~ ERROR: `pub` visibility is not allowed - -fn main() { - pub use std::usize; //~ ERROR: imports in functions are never reachable -} diff --git a/src/test/compile-fail/unnecessary-private.rs b/src/test/compile-fail/unnecessary-private.rs index 6e6ffd23c4a..e3707292f24 100644 --- a/src/test/compile-fail/unnecessary-private.rs +++ b/src/test/compile-fail/unnecessary-private.rs @@ -1,4 +1,4 @@ -// Copyright 2013 The Rust Project Developers. See the COPYRIGHT +// Copyright 2013-2014 The Rust Project Developers. See the COPYRIGHT // file at the top-level directory of this distribution and at // http://rust-lang.org/COPYRIGHT. // @@ -9,6 +9,7 @@ // except according to those terms. fn main() { + pub use std::uint; //~ ERROR: visibility has no effect pub struct A; //~ ERROR: visibility has no effect pub enum B {} //~ ERROR: visibility has no effect pub trait C { //~ ERROR: visibility has no effect diff --git a/src/test/compile-fail/view-items-at-top.rs b/src/test/compile-fail/view-items-at-top.rs deleted file mode 100644 index 7b78a8d932b..00000000000 --- a/src/test/compile-fail/view-items-at-top.rs +++ /dev/null @@ -1,19 +0,0 @@ -// Copyright 2012-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. - -extern crate test; - -fn f() { -} - -use test::net; //~ ERROR `use` and `extern crate` declarations must precede items - -fn main() { -} diff --git a/src/test/pretty/issue-4264.pp b/src/test/pretty/issue-4264.pp index dac6e628d10..c18d076c0d3 100644 --- a/src/test/pretty/issue-4264.pp +++ b/src/test/pretty/issue-4264.pp @@ -1,8 +1,8 @@ #![no_std] -#[macro_use] -extern crate "std" as std; #[prelude_import] use std::prelude::v1::*; +#[macro_use] +extern crate "std" as std; // 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. -- cgit 1.4.1-3-g733a5