From 77cc5764b9d8b53e01788886d3b3882dffc0001e Mon Sep 17 00:00:00 2001 From: Vadim Petrochenkov Date: Thu, 11 Feb 2016 23:33:09 +0300 Subject: [PATCH] Remove some unnecessary indirection from AST structures --- src/librustc_driver/pretty.rs | 4 +- src/librustc_trans/save/dump_csv.rs | 4 +- src/libsyntax/ast.rs | 14 +- src/libsyntax/config.rs | 31 ++--- src/libsyntax/ext/base.rs | 32 ++--- src/libsyntax/ext/build.rs | 46 +++---- src/libsyntax/ext/expand.rs | 89 ++++++------ src/libsyntax/ext/quote.rs | 26 ++-- src/libsyntax/ext/tt/macro_parser.rs | 2 +- src/libsyntax/ext/tt/macro_rules.rs | 4 +- src/libsyntax/fold.rs | 130 +++++++++--------- src/libsyntax/parse/mod.rs | 10 +- src/libsyntax/parse/parser.rs | 83 ++++++----- src/libsyntax/print/pprust.rs | 8 +- src/libsyntax/ptr.rs | 4 + src/libsyntax/util/parser_testing.rs | 2 +- src/libsyntax_ext/deriving/debug.rs | 4 +- src/libsyntax_ext/deriving/generic/mod.rs | 20 +-- src/libsyntax_ext/format.rs | 2 +- src/test/auxiliary/macro_crate_test.rs | 4 +- .../run-pass-fulldeps/ast_stmt_expr_attr.rs | 2 +- src/test/run-pass-fulldeps/qquote.rs | 2 +- src/test/run-pass-fulldeps/quote-tokens.rs | 2 +- 23 files changed, 271 insertions(+), 254 deletions(-) diff --git a/src/librustc_driver/pretty.rs b/src/librustc_driver/pretty.rs index 71f9d3c7e74..170ae65f919 100644 --- a/src/librustc_driver/pretty.rs +++ b/src/librustc_driver/pretty.rs @@ -614,7 +614,7 @@ impl fold::Folder for ReplaceBodyWithLoop { } } - fn fold_trait_item(&mut self, i: P) -> SmallVector> { + fn fold_trait_item(&mut self, i: ast::TraitItem) -> SmallVector { match i.node { ast::TraitItemKind::Const(..) => { self.within_static_or_const = true; @@ -626,7 +626,7 @@ impl fold::Folder for ReplaceBodyWithLoop { } } - fn fold_impl_item(&mut self, i: P) -> SmallVector> { + fn fold_impl_item(&mut self, i: ast::ImplItem) -> SmallVector { match i.node { ast::ImplItemKind::Const(..) => { self.within_static_or_const = true; diff --git a/src/librustc_trans/save/dump_csv.rs b/src/librustc_trans/save/dump_csv.rs index 34d806ab0a8..72b65f2e45f 100644 --- a/src/librustc_trans/save/dump_csv.rs +++ b/src/librustc_trans/save/dump_csv.rs @@ -561,7 +561,7 @@ impl <'l, 'tcx> DumpCsvVisitor<'l, 'tcx> { type_parameters: &ast::Generics, trait_ref: &Option, typ: &ast::Ty, - impl_items: &[P]) { + impl_items: &[ast::ImplItem]) { let mut has_self_ref = false; if let Some(impl_data) = self.save_ctxt.get_item_data(item) { down_cast_data!(impl_data, ImplData, self, item.span); @@ -602,7 +602,7 @@ impl <'l, 'tcx> DumpCsvVisitor<'l, 'tcx> { item: &ast::Item, generics: &ast::Generics, trait_refs: &ast::TyParamBounds, - methods: &[P]) { + methods: &[ast::TraitItem]) { let qualname = format!("::{}", self.tcx.map.path_to_string(item.id)); let val = self.span.snippet(item.span); let sub_span = self.span.sub_span_after_keyword(item.span, keywords::Trait); diff --git a/src/libsyntax/ast.rs b/src/libsyntax/ast.rs index bae6d780b5e..d220508a741 100644 --- a/src/libsyntax/ast.rs +++ b/src/libsyntax/ast.rs @@ -287,7 +287,7 @@ impl PathParameters { } } - pub fn bindings(&self) -> Vec<&P> { + pub fn bindings(&self) -> Vec<&TypeBinding> { match *self { PathParameters::AngleBracketed(ref data) => { data.bindings.iter().collect() @@ -308,7 +308,7 @@ pub struct AngleBracketedParameterData { pub types: P<[P]>, /// Bindings (equality constraints) on associated types, if present. /// e.g., `Foo`. - pub bindings: P<[P]>, + pub bindings: P<[TypeBinding]>, } impl AngleBracketedParameterData { @@ -508,7 +508,7 @@ impl PartialEq for MetaItemKind { #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)] pub struct Block { /// Statements in a block - pub stmts: Vec>, + pub stmts: Vec, /// An expression at the end of the block /// without a semicolon, if any pub expr: Option>, @@ -1716,12 +1716,12 @@ pub struct Mod { #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)] pub struct ForeignMod { pub abi: Abi, - pub items: Vec>, + pub items: Vec, } #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)] pub struct EnumDef { - pub variants: Vec>, + pub variants: Vec, } #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)] @@ -1988,7 +1988,7 @@ pub enum ItemKind { Trait(Unsafety, Generics, TyParamBounds, - Vec>), + Vec), // Default trait implementations /// @@ -2000,7 +2000,7 @@ pub enum ItemKind { Generics, Option, // (optional) trait this impl implements P, // self - Vec>), + Vec), /// A macro invocation (which includes macro definition) Mac(Mac), } diff --git a/src/libsyntax/config.rs b/src/libsyntax/config.rs index 09408f68dfd..9acb1805cdd 100644 --- a/src/libsyntax/config.rs +++ b/src/libsyntax/config.rs @@ -72,7 +72,7 @@ impl<'a, F> fold::Folder for Context<'a, F> where F: FnMut(&[ast::Attribute]) -> fn fold_opt_expr(&mut self, expr: P) -> Option> { fold_opt_expr(self, expr) } - fn fold_stmt(&mut self, stmt: P) -> SmallVector> { + fn fold_stmt(&mut self, stmt: ast::Stmt) -> SmallVector { fold_stmt(self, stmt) } fn fold_mac(&mut self, mac: ast::Mac) -> ast::Mac { @@ -95,8 +95,8 @@ pub fn strip_items<'a, F>(diagnostic: &'a Handler, } fn filter_foreign_item(cx: &mut Context, - item: P) - -> Option> where + item: ast::ForeignItem) + -> Option where F: FnMut(&[ast::Attribute]) -> bool { if foreign_item_in_cfg(cx, &item) { @@ -153,18 +153,15 @@ fn fold_item_kind(cx: &mut Context, item: ast::ItemKind) -> ast::ItemKind if !(cx.in_cfg)(&v.node.attrs) { None } else { - Some(v.map(|Spanned {node: ast::Variant_ {name, attrs, data, - disr_expr}, span}| { - Spanned { - node: ast::Variant_ { - name: name, - attrs: attrs, - data: fold_struct(cx, data), - disr_expr: disr_expr, - }, - span: span - } - })) + Some(Spanned { + node: ast::Variant_ { + name: v.node.name, + attrs: v.node.attrs, + data: fold_struct(cx, v.node.data), + disr_expr: v.node.disr_expr, + }, + span: v.span + }) } }); ast::ItemKind::Enum(ast::EnumDef { @@ -225,11 +222,11 @@ fn fold_expr(cx: &mut Context, expr: P) -> P where }) } -fn fold_stmt(cx: &mut Context, stmt: P) -> SmallVector> +fn fold_stmt(cx: &mut Context, stmt: ast::Stmt) -> SmallVector where F: FnMut(&[ast::Attribute]) -> bool { if stmt_in_cfg(cx, &stmt) { - stmt.and_then(|s| fold::noop_fold_stmt(s, cx)) + fold::noop_fold_stmt(stmt, cx) } else { SmallVector::zero() } diff --git a/src/libsyntax/ext/base.rs b/src/libsyntax/ext/base.rs index 381d952ea88..b4e86e4cfd3 100644 --- a/src/libsyntax/ext/base.rs +++ b/src/libsyntax/ext/base.rs @@ -82,16 +82,16 @@ impl Annotatable { } } - pub fn expect_trait_item(self) -> P { + pub fn expect_trait_item(self) -> ast::TraitItem { match self { - Annotatable::TraitItem(i) => i, + Annotatable::TraitItem(i) => i.unwrap(), _ => panic!("expected Item") } } - pub fn expect_impl_item(self) -> P { + pub fn expect_impl_item(self) -> ast::ImplItem { match self { - Annotatable::ImplItem(i) => i, + Annotatable::ImplItem(i) => i.unwrap(), _ => panic!("expected Item") } } @@ -204,8 +204,8 @@ impl IdentMacroExpander for F macro_rules! make_stmts_default { ($me:expr) => { $me.make_expr().map(|e| { - SmallVector::one(P(codemap::respan( - e.span, ast::StmtKind::Expr(e, ast::DUMMY_NODE_ID)))) + SmallVector::one(codemap::respan( + e.span, ast::StmtKind::Expr(e, ast::DUMMY_NODE_ID))) }) } } @@ -223,7 +223,7 @@ pub trait MacResult { } /// Create zero or more impl items. - fn make_impl_items(self: Box) -> Option>> { + fn make_impl_items(self: Box) -> Option> { None } @@ -236,7 +236,7 @@ pub trait MacResult { /// /// By default this attempts to create an expression statement, /// returning None if that fails. - fn make_stmts(self: Box) -> Option>> { + fn make_stmts(self: Box) -> Option> { make_stmts_default!(self) } @@ -273,8 +273,8 @@ make_MacEager! { expr: P, pat: P, items: SmallVector>, - impl_items: SmallVector>, - stmts: SmallVector>, + impl_items: SmallVector, + stmts: SmallVector, ty: P, } @@ -287,11 +287,11 @@ impl MacResult for MacEager { self.items } - fn make_impl_items(self: Box) -> Option>> { + fn make_impl_items(self: Box) -> Option> { self.impl_items } - fn make_stmts(self: Box) -> Option>> { + fn make_stmts(self: Box) -> Option> { match self.stmts.as_ref().map_or(0, |s| s.len()) { 0 => make_stmts_default!(self), _ => self.stmts, @@ -391,7 +391,7 @@ impl MacResult for DummyResult { } } - fn make_impl_items(self: Box) -> Option>> { + fn make_impl_items(self: Box) -> Option> { if self.expr_only { None } else { @@ -399,11 +399,11 @@ impl MacResult for DummyResult { } } - fn make_stmts(self: Box) -> Option>> { - Some(SmallVector::one(P( + fn make_stmts(self: Box) -> Option> { + Some(SmallVector::one( codemap::respan(self.span, ast::StmtKind::Expr(DummyResult::raw_expr(self.span), - ast::DUMMY_NODE_ID))))) + ast::DUMMY_NODE_ID)))) } } diff --git a/src/libsyntax/ext/build.rs b/src/libsyntax/ext/build.rs index 31d5521799e..38af8353aea 100644 --- a/src/libsyntax/ext/build.rs +++ b/src/libsyntax/ext/build.rs @@ -34,7 +34,7 @@ pub trait AstBuilder { idents: Vec , lifetimes: Vec, types: Vec>, - bindings: Vec> ) + bindings: Vec ) -> ast::Path; fn qpath(&self, self_type: P, @@ -46,7 +46,7 @@ pub trait AstBuilder { ident: ast::Ident, lifetimes: Vec, types: Vec>, - bindings: Vec>) + bindings: Vec) -> (ast::QSelf, ast::Path); // types @@ -88,8 +88,8 @@ pub trait AstBuilder { -> ast::LifetimeDef; // statements - fn stmt_expr(&self, expr: P) -> P; - fn stmt_let(&self, sp: Span, mutbl: bool, ident: ast::Ident, ex: P) -> P; + fn stmt_expr(&self, expr: P) -> ast::Stmt; + fn stmt_let(&self, sp: Span, mutbl: bool, ident: ast::Ident, ex: P) -> ast::Stmt; fn stmt_let_typed(&self, sp: Span, mutbl: bool, @@ -97,14 +97,14 @@ pub trait AstBuilder { typ: P, ex: P) -> P; - fn stmt_item(&self, sp: Span, item: P) -> P; + fn stmt_item(&self, sp: Span, item: P) -> ast::Stmt; // blocks - fn block(&self, span: Span, stmts: Vec>, + fn block(&self, span: Span, stmts: Vec, expr: Option>) -> P; fn block_expr(&self, expr: P) -> P; fn block_all(&self, span: Span, - stmts: Vec>, + stmts: Vec, expr: Option>) -> P; // expressions @@ -206,9 +206,9 @@ pub trait AstBuilder { fn lambda_expr_1(&self, span: Span, expr: P, ident: ast::Ident) -> P; fn lambda_stmts(&self, span: Span, ids: Vec, - blk: Vec>) -> P; - fn lambda_stmts_0(&self, span: Span, stmts: Vec>) -> P; - fn lambda_stmts_1(&self, span: Span, stmts: Vec>, + blk: Vec) -> P; + fn lambda_stmts_0(&self, span: Span, stmts: Vec) -> P; + fn lambda_stmts_1(&self, span: Span, stmts: Vec, ident: ast::Ident) -> P; // items @@ -315,7 +315,7 @@ impl<'a> AstBuilder for ExtCtxt<'a> { mut idents: Vec , lifetimes: Vec, types: Vec>, - bindings: Vec> ) + bindings: Vec ) -> ast::Path { let last_identifier = idents.pop().unwrap(); let mut segments: Vec = idents.into_iter() @@ -360,7 +360,7 @@ impl<'a> AstBuilder for ExtCtxt<'a> { ident: ast::Ident, lifetimes: Vec, types: Vec>, - bindings: Vec>) + bindings: Vec) -> (ast::QSelf, ast::Path) { let mut path = trait_path; path.segments.push(ast::PathSegment { @@ -505,12 +505,12 @@ impl<'a> AstBuilder for ExtCtxt<'a> { } } - fn stmt_expr(&self, expr: P) -> P { - P(respan(expr.span, ast::StmtKind::Semi(expr, ast::DUMMY_NODE_ID))) + fn stmt_expr(&self, expr: P) -> ast::Stmt { + respan(expr.span, ast::StmtKind::Semi(expr, ast::DUMMY_NODE_ID)) } fn stmt_let(&self, sp: Span, mutbl: bool, ident: ast::Ident, - ex: P) -> P { + ex: P) -> ast::Stmt { let pat = if mutbl { let binding_mode = ast::BindingMode::ByValue(ast::Mutability::Mutable); self.pat_ident_binding_mode(sp, ident, binding_mode) @@ -526,7 +526,7 @@ impl<'a> AstBuilder for ExtCtxt<'a> { attrs: None, }); let decl = respan(sp, ast::DeclKind::Local(local)); - P(respan(sp, ast::StmtKind::Decl(P(decl), ast::DUMMY_NODE_ID))) + respan(sp, ast::StmtKind::Decl(P(decl), ast::DUMMY_NODE_ID)) } fn stmt_let_typed(&self, @@ -554,14 +554,14 @@ impl<'a> AstBuilder for ExtCtxt<'a> { P(respan(sp, ast::StmtKind::Decl(P(decl), ast::DUMMY_NODE_ID))) } - fn block(&self, span: Span, stmts: Vec>, + fn block(&self, span: Span, stmts: Vec, expr: Option>) -> P { self.block_all(span, stmts, expr) } - fn stmt_item(&self, sp: Span, item: P) -> P { + fn stmt_item(&self, sp: Span, item: P) -> ast::Stmt { let decl = respan(sp, ast::DeclKind::Item(item)); - P(respan(sp, ast::StmtKind::Decl(P(decl), ast::DUMMY_NODE_ID))) + respan(sp, ast::StmtKind::Decl(P(decl), ast::DUMMY_NODE_ID)) } fn block_expr(&self, expr: P) -> P { @@ -569,7 +569,7 @@ impl<'a> AstBuilder for ExtCtxt<'a> { } fn block_all(&self, span: Span, - stmts: Vec>, + stmts: Vec, expr: Option>) -> P { P(ast::Block { stmts: stmts, @@ -923,14 +923,14 @@ impl<'a> AstBuilder for ExtCtxt<'a> { fn lambda_stmts(&self, span: Span, ids: Vec, - stmts: Vec>) + stmts: Vec) -> P { self.lambda(span, ids, self.block(span, stmts, None)) } - fn lambda_stmts_0(&self, span: Span, stmts: Vec>) -> P { + fn lambda_stmts_0(&self, span: Span, stmts: Vec) -> P { self.lambda0(span, self.block(span, stmts, None)) } - fn lambda_stmts_1(&self, span: Span, stmts: Vec>, + fn lambda_stmts_1(&self, span: Span, stmts: Vec, ident: ast::Ident) -> P { self.lambda1(span, self.block(span, stmts, None), ident) } diff --git a/src/libsyntax/ext/expand.rs b/src/libsyntax/ext/expand.rs index c4bbe709f34..1ee108217c6 100644 --- a/src/libsyntax/ext/expand.rs +++ b/src/libsyntax/ext/expand.rs @@ -503,8 +503,7 @@ pub fn expand_item_mac(it: P, } /// Expand a stmt -fn expand_stmt(stmt: P, fld: &mut MacroExpander) -> SmallVector> { - let stmt = stmt.and_then(|stmt| stmt); +fn expand_stmt(stmt: Stmt, fld: &mut MacroExpander) -> SmallVector { let (mac, style, attrs) = match stmt.node { StmtKind::Mac(mac, style, attrs) => (mac, style, attrs), _ => return expand_non_macro_stmt(stmt, fld) @@ -514,7 +513,7 @@ fn expand_stmt(stmt: P, fld: &mut MacroExpander) -> SmallVector> { drop(attrs); let maybe_new_items = - expand_mac_invoc(mac.and_then(|m| m), stmt.span, + expand_mac_invoc(mac.unwrap(), stmt.span, |r| r.make_stmts(), |stmts, mark| stmts.move_map(|m| mark_stmt(m, mark)), fld); @@ -535,15 +534,13 @@ fn expand_stmt(stmt: P, fld: &mut MacroExpander) -> SmallVector> { // semicolon to the final statement produced by expansion. if style == MacStmtStyle::Semicolon { if let Some(stmt) = fully_expanded.pop() { - let new_stmt = stmt.map(|Spanned {node, span}| { - Spanned { - node: match node { - StmtKind::Expr(e, stmt_id) => StmtKind::Semi(e, stmt_id), - _ => node /* might already have a semi */ - }, - span: span - } - }); + let new_stmt = Spanned { + node: match stmt.node { + StmtKind::Expr(e, stmt_id) => StmtKind::Semi(e, stmt_id), + _ => stmt.node /* might already have a semi */ + }, + span: stmt.span + }; fully_expanded.push(new_stmt); } } @@ -554,7 +551,7 @@ fn expand_stmt(stmt: P, fld: &mut MacroExpander) -> SmallVector> { // expand a non-macro stmt. this is essentially the fallthrough for // expand_stmt, above. fn expand_non_macro_stmt(Spanned {node, span: stmt_span}: Stmt, fld: &mut MacroExpander) - -> SmallVector> { + -> SmallVector { // is it a let? match node { StmtKind::Decl(decl, node_id) => decl.and_then(|Spanned {node: decl, span}| match decl { @@ -594,14 +591,14 @@ fn expand_non_macro_stmt(Spanned {node, span: stmt_span}: Stmt, fld: &mut MacroE attrs: fold::fold_thin_attrs(attrs, fld), } }); - SmallVector::one(P(Spanned { + SmallVector::one(Spanned { node: StmtKind::Decl(P(Spanned { node: DeclKind::Local(rewritten_local), span: span }), node_id), span: stmt_span - })) + }) } _ => { noop_fold_stmt(Spanned { @@ -919,24 +916,28 @@ fn expand_annotatable(a: Annotatable, }, Annotatable::TraitItem(it) => match it.node { - ast::TraitItemKind::Method(_, Some(_)) => SmallVector::one(it.map(|ti| ast::TraitItem { - id: ti.id, - ident: ti.ident, - attrs: ti.attrs, - node: match ti.node { - ast::TraitItemKind::Method(sig, Some(body)) => { - let (sig, body) = expand_and_rename_method(sig, body, fld); - ast::TraitItemKind::Method(sig, Some(body)) - } - _ => unreachable!() - }, - span: fld.new_span(ti.span) - })), - _ => fold::noop_fold_trait_item(it, fld) - }.into_iter().map(Annotatable::TraitItem).collect(), + ast::TraitItemKind::Method(_, Some(_)) => { + let ti = it.unwrap(); + SmallVector::one(ast::TraitItem { + id: ti.id, + ident: ti.ident, + attrs: ti.attrs, + node: match ti.node { + ast::TraitItemKind::Method(sig, Some(body)) => { + let (sig, body) = expand_and_rename_method(sig, body, fld); + ast::TraitItemKind::Method(sig, Some(body)) + } + _ => unreachable!() + }, + span: fld.new_span(ti.span) + }) + } + _ => fold::noop_fold_trait_item(it.unwrap(), fld) + }.into_iter().map(|ti| Annotatable::TraitItem(P(ti))).collect(), Annotatable::ImplItem(ii) => { - expand_impl_item(ii, fld).into_iter().map(Annotatable::ImplItem).collect() + expand_impl_item(ii.unwrap(), fld).into_iter(). + map(|ii| Annotatable::ImplItem(P(ii))).collect() } }; @@ -1052,10 +1053,10 @@ fn expand_item_multi_modifier(mut it: Annotatable, expand_item_multi_modifier(it, fld) } -fn expand_impl_item(ii: P, fld: &mut MacroExpander) - -> SmallVector> { +fn expand_impl_item(ii: ast::ImplItem, fld: &mut MacroExpander) + -> SmallVector { match ii.node { - ast::ImplItemKind::Method(..) => SmallVector::one(ii.map(|ii| ast::ImplItem { + ast::ImplItemKind::Method(..) => SmallVector::one(ast::ImplItem { id: ii.id, ident: ii.ident, attrs: ii.attrs, @@ -1068,12 +1069,12 @@ fn expand_impl_item(ii: P, fld: &mut MacroExpander) _ => unreachable!() }, span: fld.new_span(ii.span) - })), + }), ast::ImplItemKind::Macro(_) => { - let (span, mac) = ii.and_then(|ii| match ii.node { + let (span, mac) = match ii.node { ast::ImplItemKind::Macro(mac) => (ii.span, mac), _ => unreachable!() - }); + }; let maybe_new_items = expand_mac_invoc(mac, span, |r| r.make_impl_items(), @@ -1198,7 +1199,7 @@ impl<'a, 'b> Folder for MacroExpander<'a, 'b> { expand_item_kind(item, self) } - fn fold_stmt(&mut self, stmt: P) -> SmallVector> { + fn fold_stmt(&mut self, stmt: ast::Stmt) -> SmallVector { expand_stmt(stmt, self) } @@ -1210,13 +1211,13 @@ impl<'a, 'b> Folder for MacroExpander<'a, 'b> { expand_arm(arm, self) } - fn fold_trait_item(&mut self, i: P) -> SmallVector> { - expand_annotatable(Annotatable::TraitItem(i), self) + fn fold_trait_item(&mut self, i: ast::TraitItem) -> SmallVector { + expand_annotatable(Annotatable::TraitItem(P(i)), self) .into_iter().map(|i| i.expect_trait_item()).collect() } - fn fold_impl_item(&mut self, i: P) -> SmallVector> { - expand_annotatable(Annotatable::ImplItem(i), self) + fn fold_impl_item(&mut self, i: ast::ImplItem) -> SmallVector { + expand_annotatable(Annotatable::ImplItem(P(i)), self) .into_iter().map(|i| i.expect_impl_item()).collect() } @@ -1359,7 +1360,7 @@ fn mark_pat(pat: P, m: Mrk) -> P { } // apply a given mark to the given stmt. Used following the expansion of a macro. -fn mark_stmt(stmt: P, m: Mrk) -> P { +fn mark_stmt(stmt: ast::Stmt, m: Mrk) -> ast::Stmt { Marker{mark:m}.fold_stmt(stmt) .expect_one("marking a stmt didn't return exactly one stmt") } @@ -1371,7 +1372,7 @@ fn mark_item(expr: P, m: Mrk) -> P { } // apply a given mark to the given item. Used following the expansion of a macro. -fn mark_impl_item(ii: P, m: Mrk) -> P { +fn mark_impl_item(ii: ast::ImplItem, m: Mrk) -> ast::ImplItem { Marker{mark:m}.fold_impl_item(ii) .expect_one("marking an impl item didn't return exactly one impl item") } diff --git a/src/libsyntax/ext/quote.rs b/src/libsyntax/ext/quote.rs index 57db1347021..d0eaa89e4ae 100644 --- a/src/libsyntax/ext/quote.rs +++ b/src/libsyntax/ext/quote.rs @@ -114,22 +114,24 @@ pub mod rt { } } - impl ToTokens for P { + impl ToTokens for ast::ImplItem { fn to_tokens(&self, _cx: &ExtCtxt) -> Vec { - vec![TokenTree::Token(self.span, token::Interpolated(token::NtImplItem(self.clone())))] + vec![TokenTree::Token(self.span, + token::Interpolated(token::NtImplItem(P(self.clone()))))] } } - impl ToTokens for P { + impl ToTokens for ast::TraitItem { fn to_tokens(&self, _cx: &ExtCtxt) -> Vec { - vec![TokenTree::Token(self.span, token::Interpolated(token::NtTraitItem(self.clone())))] + vec![TokenTree::Token(self.span, + token::Interpolated(token::NtTraitItem(P(self.clone()))))] } } - impl ToTokens for P { + impl ToTokens for ast::Stmt { fn to_tokens(&self, _cx: &ExtCtxt) -> Vec { let mut tts = vec![ - TokenTree::Token(self.span, token::Interpolated(token::NtStmt(self.clone()))) + TokenTree::Token(self.span, token::Interpolated(token::NtStmt(P(self.clone())))) ]; // Some statements require a trailing semicolon. @@ -312,7 +314,7 @@ pub mod rt { pub trait ExtParseUtils { fn parse_item(&self, s: String) -> P; fn parse_expr(&self, s: String) -> P; - fn parse_stmt(&self, s: String) -> P; + fn parse_stmt(&self, s: String) -> ast::Stmt; fn parse_tts(&self, s: String) -> Vec; } @@ -326,7 +328,7 @@ pub mod rt { self.parse_sess()).expect("parse error") } - fn parse_stmt(&self, s: String) -> P { + fn parse_stmt(&self, s: String) -> ast::Stmt { parse::parse_stmt_from_source_str("".to_string(), s, self.cfg(), @@ -371,7 +373,7 @@ pub fn parse_ty_panic(parser: &mut Parser) -> P { panictry!(parser.parse_ty()) } -pub fn parse_stmt_panic(parser: &mut Parser) -> Option> { +pub fn parse_stmt_panic(parser: &mut Parser) -> Option { panictry!(parser.parse_stmt()) } @@ -710,7 +712,7 @@ fn expr_mk_token(cx: &ExtCtxt, sp: Span, tok: &token::Token) -> P { mk_token_path(cx, sp, name) } -fn statements_mk_tt(cx: &ExtCtxt, tt: &TokenTree, matcher: bool) -> Vec> { +fn statements_mk_tt(cx: &ExtCtxt, tt: &TokenTree, matcher: bool) -> Vec { match *tt { TokenTree::Token(sp, SubstNt(ident, _)) => { // tt.extend($ident.to_tokens(ext_cx)) @@ -831,7 +833,7 @@ fn parse_arguments_to_quote(cx: &ExtCtxt, tts: &[TokenTree]) (cx_expr, tts) } -fn mk_stmts_let(cx: &ExtCtxt, sp: Span) -> Vec> { +fn mk_stmts_let(cx: &ExtCtxt, sp: Span) -> Vec { // We also bind a single value, sp, to ext_cx.call_site() // // This causes every span in a token-tree quote to be attributed to the @@ -872,7 +874,7 @@ fn mk_stmts_let(cx: &ExtCtxt, sp: Span) -> Vec> { vec!(stmt_let_sp, stmt_let_tt) } -fn statements_mk_tts(cx: &ExtCtxt, tts: &[TokenTree], matcher: bool) -> Vec> { +fn statements_mk_tts(cx: &ExtCtxt, tts: &[TokenTree], matcher: bool) -> Vec { let mut ss = Vec::new(); for tt in tts { ss.extend(statements_mk_tt(cx, tt, matcher)); diff --git a/src/libsyntax/ext/tt/macro_parser.rs b/src/libsyntax/ext/tt/macro_parser.rs index 9c8ae9460e4..c4e1f32a52c 100644 --- a/src/libsyntax/ext/tt/macro_parser.rs +++ b/src/libsyntax/ext/tt/macro_parser.rs @@ -523,7 +523,7 @@ pub fn parse_nt<'a>(p: &mut Parser<'a>, sp: Span, name: &str) -> Nonterminal { }, "block" => token::NtBlock(panictry!(p.parse_block())), "stmt" => match panictry!(p.parse_stmt()) { - Some(s) => token::NtStmt(s), + Some(s) => token::NtStmt(P(s)), None => { p.fatal("expected a statement").emit(); panic!(FatalError); diff --git a/src/libsyntax/ext/tt/macro_rules.rs b/src/libsyntax/ext/tt/macro_rules.rs index 1e9178a55c5..c641c478a6b 100644 --- a/src/libsyntax/ext/tt/macro_rules.rs +++ b/src/libsyntax/ext/tt/macro_rules.rs @@ -87,7 +87,7 @@ impl<'a> MacResult for ParserAnyMacro<'a> { } fn make_impl_items(self: Box>) - -> Option>> { + -> Option> { let mut ret = SmallVector::zero(); loop { let mut parser = self.parser.borrow_mut(); @@ -101,7 +101,7 @@ impl<'a> MacResult for ParserAnyMacro<'a> { } fn make_stmts(self: Box>) - -> Option>> { + -> Option> { let mut ret = SmallVector::zero(); loop { let mut parser = self.parser.borrow_mut(); diff --git a/src/libsyntax/fold.rs b/src/libsyntax/fold.rs index 5ae24e6fb24..d347899ca2e 100644 --- a/src/libsyntax/fold.rs +++ b/src/libsyntax/fold.rs @@ -55,7 +55,7 @@ pub trait Folder : Sized { noop_fold_view_path(view_path, self) } - fn fold_foreign_item(&mut self, ni: P) -> P { + fn fold_foreign_item(&mut self, ni: ForeignItem) -> ForeignItem { noop_fold_foreign_item(ni, self) } @@ -75,11 +75,11 @@ pub trait Folder : Sized { noop_fold_item_kind(i, self) } - fn fold_trait_item(&mut self, i: P) -> SmallVector> { + fn fold_trait_item(&mut self, i: TraitItem) -> SmallVector { noop_fold_trait_item(i, self) } - fn fold_impl_item(&mut self, i: P) -> SmallVector> { + fn fold_impl_item(&mut self, i: ImplItem) -> SmallVector { noop_fold_impl_item(i, self) } @@ -91,8 +91,8 @@ pub trait Folder : Sized { noop_fold_block(b, self) } - fn fold_stmt(&mut self, s: P) -> SmallVector> { - s.and_then(|s| noop_fold_stmt(s, self)) + fn fold_stmt(&mut self, s: Stmt) -> SmallVector { + noop_fold_stmt(s, self) } fn fold_arm(&mut self, a: Arm) -> Arm { @@ -123,7 +123,7 @@ pub trait Folder : Sized { noop_fold_ty(t, self) } - fn fold_ty_binding(&mut self, t: P) -> P { + fn fold_ty_binding(&mut self, t: TypeBinding) -> TypeBinding { noop_fold_ty_binding(t, self) } @@ -135,7 +135,7 @@ pub trait Folder : Sized { noop_fold_foreign_mod(nm, self) } - fn fold_variant(&mut self, v: P) -> P { + fn fold_variant(&mut self, v: Variant) -> Variant { noop_fold_variant(v, self) } @@ -367,13 +367,13 @@ pub fn noop_fold_decl(d: P, fld: &mut T) -> SmallVector }) } -pub fn noop_fold_ty_binding(b: P, fld: &mut T) -> P { - b.map(|TypeBinding { id, ident, ty, span }| TypeBinding { - id: fld.new_id(id), - ident: ident, - ty: fld.fold_ty(ty), - span: fld.new_span(span), - }) +pub fn noop_fold_ty_binding(b: TypeBinding, fld: &mut T) -> TypeBinding { + TypeBinding { + id: fld.new_id(b.id), + ident: b.ident, + ty: fld.fold_ty(b.ty), + span: fld.new_span(b.span), + } } pub fn noop_fold_ty(t: P, fld: &mut T) -> P { @@ -434,16 +434,16 @@ pub fn noop_fold_foreign_mod(ForeignMod {abi, items}: ForeignMod, } } -pub fn noop_fold_variant(v: P, fld: &mut T) -> P { - v.map(|Spanned {node: Variant_ {name, attrs, data, disr_expr}, span}| Spanned { +pub fn noop_fold_variant(v: Variant, fld: &mut T) -> Variant { + Spanned { node: Variant_ { - name: name, - attrs: fold_attrs(attrs, fld), - data: fld.fold_variant_data(data), - disr_expr: disr_expr.map(|e| fld.fold_expr(e)), + name: v.node.name, + attrs: fold_attrs(v.node.attrs, fld), + data: fld.fold_variant_data(v.node.data), + disr_expr: v.node.disr_expr.map(|e| fld.fold_expr(e)), }, - span: fld.new_span(span), - }) + span: fld.new_span(v.span), + } } pub fn noop_fold_ident(i: Ident, _: &mut T) -> Ident { @@ -653,11 +653,11 @@ pub fn noop_fold_interpolated(nt: token::Nonterminal, fld: &mut T) .expect_one("expected fold to produce exactly one item")), token::NtBlock(block) => token::NtBlock(fld.fold_block(block)), token::NtStmt(stmt) => - token::NtStmt(fld.fold_stmt(stmt) + token::NtStmt(stmt.map(|stmt| fld.fold_stmt(stmt) // this is probably okay, because the only folds likely // to peek inside interpolated nodes will be renamings/markings, // which map single items to single items - .expect_one("expected fold to produce exactly one statement")), + .expect_one("expected fold to produce exactly one statement"))), token::NtPat(pat) => token::NtPat(fld.fold_pat(pat)), token::NtExpr(expr) => token::NtExpr(fld.fold_expr(expr)), token::NtTy(ty) => token::NtTy(fld.fold_ty(ty)), @@ -669,11 +669,11 @@ pub fn noop_fold_interpolated(nt: token::Nonterminal, fld: &mut T) token::NtTT(tt) => token::NtTT(P(fld.fold_tt(&tt))), token::NtArm(arm) => token::NtArm(fld.fold_arm(arm)), token::NtImplItem(arm) => - token::NtImplItem(fld.fold_impl_item(arm) - .expect_one("expected fold to produce exactly one item")), + token::NtImplItem(arm.map(|arm| fld.fold_impl_item(arm) + .expect_one("expected fold to produce exactly one item"))), token::NtTraitItem(arm) => - token::NtTraitItem(fld.fold_trait_item(arm) - .expect_one("expected fold to produce exactly one item")), + token::NtTraitItem(arm.map(|arm| fld.fold_trait_item(arm) + .expect_one("expected fold to produce exactly one item"))), token::NtGenerics(generics) => token::NtGenerics(fld.fold_generics(generics)), token::NtWhereClause(where_clause) => token::NtWhereClause(fld.fold_where_clause(where_clause)), @@ -962,13 +962,13 @@ pub fn noop_fold_item_kind(i: ItemKind, folder: &mut T) -> ItemKind { } } -pub fn noop_fold_trait_item(i: P, folder: &mut T) - -> SmallVector> { - SmallVector::one(i.map(|TraitItem {id, ident, attrs, node, span}| TraitItem { - id: folder.new_id(id), - ident: folder.fold_ident(ident), - attrs: fold_attrs(attrs, folder), - node: match node { +pub fn noop_fold_trait_item(i: TraitItem, folder: &mut T) + -> SmallVector { + SmallVector::one(TraitItem { + id: folder.new_id(i.id), + ident: folder.fold_ident(i.ident), + attrs: fold_attrs(i.attrs, folder), + node: match i.node { TraitItemKind::Const(ty, default) => { TraitItemKind::Const(folder.fold_ty(ty), default.map(|x| folder.fold_expr(x))) @@ -982,18 +982,18 @@ pub fn noop_fold_trait_item(i: P, folder: &mut T) default.map(|x| folder.fold_ty(x))) } }, - span: folder.new_span(span) - })) + span: folder.new_span(i.span) + }) } -pub fn noop_fold_impl_item(i: P, folder: &mut T) - -> SmallVector> { - SmallVector::one(i.map(|ImplItem {id, ident, attrs, node, vis, span}| ImplItem { - id: folder.new_id(id), - ident: folder.fold_ident(ident), - attrs: fold_attrs(attrs, folder), - vis: vis, - node: match node { +pub fn noop_fold_impl_item(i: ImplItem, folder: &mut T) + -> SmallVector { + SmallVector::one(ImplItem { + id: folder.new_id(i.id), + ident: folder.fold_ident(i.ident), + attrs: fold_attrs(i.attrs, folder), + vis: i.vis, + node: match i.node { ast::ImplItemKind::Const(ty, expr) => { ast::ImplItemKind::Const(folder.fold_ty(ty), folder.fold_expr(expr)) } @@ -1004,8 +1004,8 @@ pub fn noop_fold_impl_item(i: P, folder: &mut T) ast::ImplItemKind::Type(ty) => ast::ImplItemKind::Type(folder.fold_ty(ty)), ast::ImplItemKind::Macro(mac) => ast::ImplItemKind::Macro(folder.fold_mac(mac)) }, - span: folder.new_span(span) - })) + span: folder.new_span(i.span) + }) } pub fn noop_fold_mod(Mod {inner, items}: Mod, folder: &mut T) -> Mod { @@ -1086,12 +1086,12 @@ pub fn noop_fold_item_simple(Item {id, ident, attrs, node, vis, span} } } -pub fn noop_fold_foreign_item(ni: P, folder: &mut T) -> P { - ni.map(|ForeignItem {id, ident, attrs, node, span, vis}| ForeignItem { - id: folder.new_id(id), - ident: folder.fold_ident(ident), - attrs: fold_attrs(attrs, folder), - node: match node { +pub fn noop_fold_foreign_item(ni: ForeignItem, folder: &mut T) -> ForeignItem { + ForeignItem { + id: folder.new_id(ni.id), + ident: folder.fold_ident(ni.ident), + attrs: fold_attrs(ni.attrs, folder), + node: match ni.node { ForeignItemKind::Fn(fdec, generics) => { ForeignItemKind::Fn(folder.fold_fn_decl(fdec), folder.fold_generics(generics)) } @@ -1099,9 +1099,9 @@ pub fn noop_fold_foreign_item(ni: P, folder: &mut T) -> ForeignItemKind::Static(folder.fold_ty(t), m) } }, - vis: vis, - span: folder.new_span(span) - }) + vis: ni.vis, + span: folder.new_span(ni.span) + } } pub fn noop_fold_method_sig(sig: MethodSig, folder: &mut T) -> MethodSig { @@ -1344,23 +1344,23 @@ pub fn noop_fold_exprs(es: Vec>, folder: &mut T) -> Vec(Spanned {node, span}: Stmt, folder: &mut T) - -> SmallVector> { + -> SmallVector { let span = folder.new_span(span); match node { StmtKind::Decl(d, id) => { let id = folder.new_id(id); - folder.fold_decl(d).into_iter().map(|d| P(Spanned { + folder.fold_decl(d).into_iter().map(|d| Spanned { node: StmtKind::Decl(d, id), span: span - })).collect() + }).collect() } StmtKind::Expr(e, id) => { let id = folder.new_id(id); if let Some(e) = folder.fold_opt_expr(e) { - SmallVector::one(P(Spanned { + SmallVector::one(Spanned { node: StmtKind::Expr(e, id), span: span - })) + }) } else { SmallVector::zero() } @@ -1368,20 +1368,20 @@ pub fn noop_fold_stmt(Spanned {node, span}: Stmt, folder: &mut T) StmtKind::Semi(e, id) => { let id = folder.new_id(id); if let Some(e) = folder.fold_opt_expr(e) { - SmallVector::one(P(Spanned { + SmallVector::one(Spanned { node: StmtKind::Semi(e, id), span: span - })) + }) } else { SmallVector::zero() } } - StmtKind::Mac(mac, semi, attrs) => SmallVector::one(P(Spanned { + StmtKind::Mac(mac, semi, attrs) => SmallVector::one(Spanned { node: StmtKind::Mac(mac.map(|m| folder.fold_mac(m)), semi, attrs.map_thin_attrs(|v| fold_attrs(v, folder))), span: span - })) + }) } } diff --git a/src/libsyntax/parse/mod.rs b/src/libsyntax/parse/mod.rs index f7060296f1a..02844c35408 100644 --- a/src/libsyntax/parse/mod.rs +++ b/src/libsyntax/parse/mod.rs @@ -144,7 +144,7 @@ pub fn parse_stmt_from_source_str(name: String, source: String, cfg: ast::CrateConfig, sess: &ParseSess) - -> Option> { + -> Option { let mut p = new_parser_from_source_str( sess, cfg, @@ -866,7 +866,7 @@ mod tests { #[test] fn parse_stmt_1 () { assert!(string_to_stmt("b;".to_string()) == - Some(P(Spanned{ + Some(Spanned{ node: ast::StmtKind::Expr(P(ast::Expr { id: ast::DUMMY_NODE_ID, node: ast::ExprKind::Path(None, ast::Path { @@ -882,7 +882,7 @@ mod tests { span: sp(0,1), attrs: None}), ast::DUMMY_NODE_ID), - span: sp(0,1)}))) + span: sp(0,1)})) } @@ -957,7 +957,7 @@ mod tests { } }, P(ast::Block { - stmts: vec!(P(Spanned{ + stmts: vec!(Spanned{ node: ast::StmtKind::Semi(P(ast::Expr{ id: ast::DUMMY_NODE_ID, node: ast::ExprKind::Path(None, @@ -977,7 +977,7 @@ mod tests { span: sp(17,18), attrs: None,}), ast::DUMMY_NODE_ID), - span: sp(17,19)})), + span: sp(17,19)}), expr: None, id: ast::DUMMY_NODE_ID, rules: ast::BlockCheckMode::Default, // no idea diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index e985bfd37b0..0d5bdfbbc3b 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -179,6 +179,19 @@ macro_rules! maybe_whole { } } ); + (no_clone_from_p $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 Ok(x.unwrap()); + } + } + ); (deref $p:expr, $constructor:ident) => ( { let found = match ($p).token { @@ -1174,13 +1187,13 @@ impl<'a> Parser<'a> { } /// Parse the items in a trait declaration - pub fn parse_trait_items(&mut self) -> PResult<'a, Vec>> { + pub fn parse_trait_items(&mut self) -> PResult<'a, Vec> { self.parse_unspanned_seq( &token::OpenDelim(token::Brace), &token::CloseDelim(token::Brace), seq_sep_none(), - |p| -> PResult<'a, P> { - maybe_whole!(no_clone p, NtTraitItem); + |p| -> PResult<'a, TraitItem> { + maybe_whole!(no_clone_from_p p, NtTraitItem); let mut attrs = try!(p.parse_outer_attributes()); let lo = p.span.lo; @@ -1249,13 +1262,13 @@ impl<'a> Parser<'a> { (ident, ast::TraitItemKind::Method(sig, body)) }; - Ok(P(TraitItem { + Ok(TraitItem { id: ast::DUMMY_NODE_ID, ident: name, attrs: attrs, node: node, span: mk_sp(lo, p.last_span.hi), - })) + }) }) } @@ -3661,8 +3674,8 @@ impl<'a> Parser<'a> { } /// Parse a statement. may include decl. - pub fn parse_stmt(&mut self) -> PResult<'a, Option>> { - Ok(try!(self.parse_stmt_()).map(P)) + pub fn parse_stmt(&mut self) -> PResult<'a, Option> { + Ok(try!(self.parse_stmt_())) } fn parse_stmt_(&mut self) -> PResult<'a, Option> { @@ -3846,10 +3859,10 @@ impl<'a> Parser<'a> { // expr depending on whether a semicolon follows match self.token { token::Semi => { - stmts.push(P(Spanned { + stmts.push(Spanned { node: StmtKind::Mac(mac, MacStmtStyle::Semicolon, attrs), span: mk_sp(span.lo, self.span.hi), - })); + }); self.bump(); } _ => { @@ -3871,10 +3884,10 @@ impl<'a> Parser<'a> { // statement macro; might be an expr match self.token { token::Semi => { - stmts.push(P(Spanned { + stmts.push(Spanned { node: StmtKind::Mac(m, MacStmtStyle::Semicolon, attrs), span: mk_sp(span.lo, self.span.hi), - })); + }); self.bump(); } token::CloseDelim(token::Brace) => { @@ -3885,10 +3898,10 @@ impl<'a> Parser<'a> { attrs)); } _ => { - stmts.push(P(Spanned { + stmts.push(Spanned { node: StmtKind::Mac(m, style, attrs), span: span - })); + }); } } } @@ -3899,10 +3912,10 @@ impl<'a> Parser<'a> { hi = self.last_span.hi; } - stmts.push(P(Spanned { + stmts.push(Spanned { node: node, span: mk_sp(span.lo, hi) - })); + }); } } } @@ -3920,7 +3933,7 @@ impl<'a> Parser<'a> { &mut self, e: P, span: Span, - stmts: &mut Vec>, + stmts: &mut Vec, last_block_expr: &mut Option>) -> PResult<'a, ()> { // expression without semicolon if classify::expr_requires_semi_to_be_stmt(&*e) { @@ -3937,17 +3950,17 @@ impl<'a> Parser<'a> { hi: self.last_span.hi, expn_id: span.expn_id, }; - stmts.push(P(Spanned { + stmts.push(Spanned { node: StmtKind::Semi(e, ast::DUMMY_NODE_ID), span: span_with_semi, - })); + }); } token::CloseDelim(token::Brace) => *last_block_expr = Some(e), _ => { - stmts.push(P(Spanned { + stmts.push(Spanned { node: StmtKind::Expr(e, ast::DUMMY_NODE_ID), span: span - })); + }); } } Ok(()) @@ -4080,7 +4093,7 @@ impl<'a> Parser<'a> { fn parse_generic_values_after_lt(&mut self) -> PResult<'a, (Vec, Vec>, - Vec>)> { + Vec)> { let span_lo = self.span.lo; let lifetimes = try!(self.parse_lifetimes(token::Comma)); @@ -4146,11 +4159,11 @@ impl<'a> Parser<'a> { let ty = try!(p.parse_ty()); let hi = ty.span.hi; let span = mk_sp(lo, hi); - return Ok(P(TypeBinding{id: ast::DUMMY_NODE_ID, + return Ok(TypeBinding{id: ast::DUMMY_NODE_ID, ident: ident, ty: ty, span: span, - })); + }); } )); Ok((lifetimes, types.into_vec(), bindings.into_vec())) @@ -4647,8 +4660,8 @@ impl<'a> Parser<'a> { } /// Parse an impl item. - pub fn parse_impl_item(&mut self) -> PResult<'a, P> { - maybe_whole!(no_clone self, NtImplItem); + pub fn parse_impl_item(&mut self) -> PResult<'a, ImplItem> { + maybe_whole!(no_clone_from_p self, NtImplItem); let mut attrs = try!(self.parse_outer_attributes()); let lo = self.span.lo; @@ -4674,14 +4687,14 @@ impl<'a> Parser<'a> { (name, node) }; - Ok(P(ImplItem { + Ok(ImplItem { id: ast::DUMMY_NODE_ID, span: mk_sp(lo, self.last_span.hi), ident: name, vis: vis, attrs: attrs, node: node - })) + }) } fn complain_if_pub_macro(&mut self, visa: Visibility, span: Span) { @@ -5243,7 +5256,7 @@ impl<'a> Parser<'a> { /// Parse a function declaration from a foreign module fn parse_item_foreign_fn(&mut self, vis: ast::Visibility, lo: BytePos, - attrs: Vec) -> PResult<'a, P> { + attrs: Vec) -> PResult<'a, ForeignItem> { try!(self.expect_keyword(keywords::Fn)); let (ident, mut generics) = try!(self.parse_fn_header()); @@ -5251,19 +5264,19 @@ impl<'a> Parser<'a> { generics.where_clause = try!(self.parse_where_clause()); let hi = self.span.hi; try!(self.expect(&token::Semi)); - Ok(P(ast::ForeignItem { + Ok(ast::ForeignItem { ident: ident, attrs: attrs, node: ForeignItemKind::Fn(decl, generics), id: ast::DUMMY_NODE_ID, span: mk_sp(lo, hi), vis: vis - })) + }) } /// Parse a static item from a foreign module fn parse_item_foreign_static(&mut self, vis: ast::Visibility, lo: BytePos, - attrs: Vec) -> PResult<'a, P> { + attrs: Vec) -> PResult<'a, ForeignItem> { try!(self.expect_keyword(keywords::Static)); let mutbl = self.eat_keyword(keywords::Mut); @@ -5272,14 +5285,14 @@ impl<'a> Parser<'a> { let ty = try!(self.parse_ty_sum()); let hi = self.span.hi; try!(self.expect(&token::Semi)); - Ok(P(ForeignItem { + Ok(ForeignItem { ident: ident, attrs: attrs, node: ForeignItemKind::Static(ty, mutbl), id: ast::DUMMY_NODE_ID, span: mk_sp(lo, hi), vis: vis - })) + }) } /// Parse extern crate links @@ -5405,7 +5418,7 @@ impl<'a> Parser<'a> { data: struct_def, disr_expr: disr_expr, }; - variants.push(P(spanned(vlo, self.last_span.hi, vr))); + variants.push(spanned(vlo, self.last_span.hi, vr)); if !self.eat(&token::Comma) { break; } } @@ -5729,7 +5742,7 @@ impl<'a> Parser<'a> { } /// Parse a foreign item. - fn parse_foreign_item(&mut self) -> PResult<'a, Option>> { + fn parse_foreign_item(&mut self) -> PResult<'a, Option> { let attrs = try!(self.parse_outer_attributes()); let lo = self.span.lo; let visibility = try!(self.parse_visibility()); diff --git a/src/libsyntax/print/pprust.rs b/src/libsyntax/print/pprust.rs index ab218971a51..7e58fd9c3a1 100644 --- a/src/libsyntax/print/pprust.rs +++ b/src/libsyntax/print/pprust.rs @@ -939,7 +939,7 @@ impl<'a> State<'a> { attrs: &[ast::Attribute]) -> io::Result<()> { try!(self.print_inner_attributes(attrs)); for item in &nmod.items { - try!(self.print_foreign_item(&**item)); + try!(self.print_foreign_item(item)); } Ok(()) } @@ -1370,7 +1370,7 @@ impl<'a> State<'a> { } pub fn print_variants(&mut self, - variants: &[P], + variants: &[ast::Variant], span: codemap::Span) -> io::Result<()> { try!(self.bopen()); for v in variants { @@ -1378,7 +1378,7 @@ impl<'a> State<'a> { try!(self.maybe_print_comment(v.span.lo)); try!(self.print_outer_attributes(&v.node.attrs)); try!(self.ibox(INDENT_UNIT)); - try!(self.print_variant(&**v)); + try!(self.print_variant(v)); try!(word(&mut self.s, ",")); try!(self.end()); try!(self.maybe_print_trailing_comment(v.span, None)); @@ -1686,7 +1686,7 @@ impl<'a> State<'a> { try!(self.print_inner_attributes(attrs)); for st in &blk.stmts { - try!(self.print_stmt(&**st)); + try!(self.print_stmt(st)); } match blk.expr { Some(ref expr) => { diff --git a/src/libsyntax/ptr.rs b/src/libsyntax/ptr.rs index 6190cf73464..27f5700cad5 100644 --- a/src/libsyntax/ptr.rs +++ b/src/libsyntax/ptr.rs @@ -65,6 +65,10 @@ impl P { { f(*self.ptr) } + /// Equivalent to and_then(|x| x) + pub fn unwrap(self) -> T { + *self.ptr + } /// Transform the inner value, consuming `self` and producing a new `P`. pub fn map(mut self, f: F) -> P where diff --git a/src/libsyntax/util/parser_testing.rs b/src/libsyntax/util/parser_testing.rs index 454b925a494..b0f4c2dcba5 100644 --- a/src/libsyntax/util/parser_testing.rs +++ b/src/libsyntax/util/parser_testing.rs @@ -64,7 +64,7 @@ pub fn string_to_item (source_str : String) -> Option> { } /// Parse a string, return a stmt -pub fn string_to_stmt(source_str : String) -> Option> { +pub fn string_to_stmt(source_str : String) -> Option { let ps = ParseSess::new(); with_error_checking_parse(source_str, &ps, |p| { p.parse_stmt() diff --git a/src/libsyntax_ext/deriving/debug.rs b/src/libsyntax_ext/deriving/debug.rs index 858066cb626..6439e9aa498 100644 --- a/src/libsyntax_ext/deriving/debug.rs +++ b/src/libsyntax_ext/deriving/debug.rs @@ -141,7 +141,7 @@ fn show_substructure(cx: &mut ExtCtxt, span: Span, fn stmt_let_undescore(cx: &mut ExtCtxt, sp: Span, - expr: P) -> P { + expr: P) -> ast::Stmt { let local = P(ast::Local { pat: cx.pat_wild(sp), ty: None, @@ -151,5 +151,5 @@ fn stmt_let_undescore(cx: &mut ExtCtxt, attrs: None, }); let decl = respan(sp, ast::DeclKind::Local(local)); - P(respan(sp, ast::StmtKind::Decl(P(decl), ast::DUMMY_NODE_ID))) + respan(sp, ast::StmtKind::Decl(P(decl), ast::DUMMY_NODE_ID)) } diff --git a/src/libsyntax_ext/deriving/generic/mod.rs b/src/libsyntax_ext/deriving/generic/mod.rs index 1e4babfac1e..160d230f86b 100644 --- a/src/libsyntax_ext/deriving/generic/mod.rs +++ b/src/libsyntax_ext/deriving/generic/mod.rs @@ -312,7 +312,7 @@ pub enum SubstructureFields<'a> { /// variants for the enum itself, and the third component is a list of /// `Ident`s bound to the variant index values for each of the actual /// input `Self` arguments. - EnumNonMatchingCollapsed(Vec, &'a [P], &'a [Ident]), + EnumNonMatchingCollapsed(Vec, &'a [ast::Variant], &'a [Ident]), /// A static method where `Self` is a struct. StaticStruct(&'a ast::VariantData, StaticFields), @@ -466,12 +466,12 @@ impl<'a> TraitDef<'a> { type_ident: Ident, generics: &Generics, field_tys: Vec>, - methods: Vec>) -> P { + methods: Vec) -> P { let trait_path = self.path.to_path(cx, self.span, type_ident, generics); // Transform associated types from `deriving::ty::Ty` into `ast::ImplItem` let associated_types = self.associated_types.iter().map(|&(ident, ref type_def)| { - P(ast::ImplItem { + ast::ImplItem { id: ast::DUMMY_NODE_ID, span: self.span, ident: ident, @@ -482,7 +482,7 @@ impl<'a> TraitDef<'a> { type_ident, generics )), - }) + } }); let Generics { mut lifetimes, ty_params, mut where_clause } = @@ -857,7 +857,7 @@ impl<'a> MethodDef<'a> { abi: Abi, explicit_self: ast::ExplicitSelf, arg_types: Vec<(Ident, P)> , - body: P) -> P { + body: P) -> ast::ImplItem { // create the generics that aren't for Self let fn_generics = self.generics.to_generics(cx, trait_.span, type_ident, generics); @@ -888,7 +888,7 @@ impl<'a> MethodDef<'a> { }; // Create the method. - P(ast::ImplItem { + ast::ImplItem { id: ast::DUMMY_NODE_ID, attrs: self.attributes.clone(), span: trait_.span, @@ -902,7 +902,7 @@ impl<'a> MethodDef<'a> { constness: ast::Constness::NotConst, decl: fn_decl }, body_block) - }) + } } /// ```ignore @@ -1139,7 +1139,7 @@ impl<'a> MethodDef<'a> { let mk_self_pat = |cx: &mut ExtCtxt, self_arg_name: &str| { let (p, idents) = trait_.create_enum_variant_pattern( cx, type_ident, - &**variant, + variant, self_arg_name, ast::Mutability::Immutable); (cx.pat(sp, ast::PatRegion(p, ast::Mutability::Immutable)), idents) @@ -1209,7 +1209,7 @@ impl<'a> MethodDef<'a> { // Self arg, assuming all are instances of VariantK. // Build up code associated with such a case. let substructure = EnumMatching(index, - &**variant, + variant, field_tuples); let arm_expr = self.call_substructure_method( cx, trait_, type_ident, &self_args[..], nonself_args, @@ -1250,7 +1250,7 @@ impl<'a> MethodDef<'a> { // let __self2_vi = unsafe { // std::intrinsics::discriminant_value(&__arg2) } as i32; // ``` - let mut index_let_stmts: Vec> = Vec::new(); + let mut index_let_stmts: Vec = Vec::new(); //We also build an expression which checks whether all discriminants are equal // discriminant_test = __self0_vi == __self1_vi && __self0_vi == __self2_vi && ... diff --git a/src/libsyntax_ext/format.rs b/src/libsyntax_ext/format.rs index 4e24eb9f6d7..fd68ba73427 100644 --- a/src/libsyntax_ext/format.rs +++ b/src/libsyntax_ext/format.rs @@ -461,7 +461,7 @@ impl<'a, 'b> Context<'a, 'b> { // Wrap the declaration in a block so that it forms a single expression. ecx.expr_block(ecx.block(sp, - vec![P(respan(sp, ast::StmtKind::Decl(P(decl), ast::DUMMY_NODE_ID)))], + vec![respan(sp, ast::StmtKind::Decl(P(decl), ast::DUMMY_NODE_ID))], Some(ecx.expr_ident(sp, name)))) } diff --git a/src/test/auxiliary/macro_crate_test.rs b/src/test/auxiliary/macro_crate_test.rs index c4cfa36542f..3516f566e8a 100644 --- a/src/test/auxiliary/macro_crate_test.rs +++ b/src/test/auxiliary/macro_crate_test.rs @@ -74,7 +74,7 @@ fn expand_into_foo_multi(cx: &mut ExtCtxt, quote_item!(cx, impl X { fn foo(&self) -> i32 { 42 } }).unwrap().and_then(|i| { match i.node { ItemKind::Impl(_, _, _, _, _, mut items) => { - Annotatable::ImplItem(items.pop().expect("impl method not found")) + Annotatable::ImplItem(P(items.pop().expect("impl method not found"))) } _ => unreachable!("impl parsed to something other than impl") } @@ -84,7 +84,7 @@ fn expand_into_foo_multi(cx: &mut ExtCtxt, quote_item!(cx, trait X { fn foo(&self) -> i32 { 0 } }).unwrap().and_then(|i| { match i.node { ItemKind::Trait(_, _, _, mut items) => { - Annotatable::TraitItem(items.pop().expect("trait method not found")) + Annotatable::TraitItem(P(items.pop().expect("trait method not found"))) } _ => unreachable!("trait parsed to something other than trait") } diff --git a/src/test/run-pass-fulldeps/ast_stmt_expr_attr.rs b/src/test/run-pass-fulldeps/ast_stmt_expr_attr.rs index b64e5778d90..ed971faf8c6 100644 --- a/src/test/run-pass-fulldeps/ast_stmt_expr_attr.rs +++ b/src/test/run-pass-fulldeps/ast_stmt_expr_attr.rs @@ -59,7 +59,7 @@ fn expr<'a>(s: &str, ps: &'a ParseSess) -> PResult<'a, P> { }) } -fn stmt<'a>(s: &str, ps: &'a ParseSess) -> PResult<'a, P> { +fn stmt<'a>(s: &str, ps: &'a ParseSess) -> PResult<'a, ast::Stmt> { with_error_checking_parse(s.to_string(), ps, |p| { p.parse_stmt().map(|s| s.unwrap()) }) diff --git a/src/test/run-pass-fulldeps/qquote.rs b/src/test/run-pass-fulldeps/qquote.rs index edaa88452c4..0bb3e610020 100644 --- a/src/test/run-pass-fulldeps/qquote.rs +++ b/src/test/run-pass-fulldeps/qquote.rs @@ -52,7 +52,7 @@ fn main() { let twenty: u16 = 20; let stmt = quote_stmt!(cx, let x = $twenty;).unwrap(); - check!(stmt_to_string, stmt, *quote_stmt!(cx, $stmt).unwrap(); "let x = 20u16;"); + check!(stmt_to_string, stmt, quote_stmt!(cx, $stmt).unwrap(); "let x = 20u16;"); let pat = quote_pat!(cx, Some(_)); check!(pat_to_string, pat, *quote_pat!(cx, $pat); "Some(_)"); diff --git a/src/test/run-pass-fulldeps/quote-tokens.rs b/src/test/run-pass-fulldeps/quote-tokens.rs index 5182f274255..4397da35d7a 100644 --- a/src/test/run-pass-fulldeps/quote-tokens.rs +++ b/src/test/run-pass-fulldeps/quote-tokens.rs @@ -26,7 +26,7 @@ fn syntax_extension(cx: &ExtCtxt) { let a: P = quote_expr!(cx, 1 + 2); let _b: Option> = quote_item!(cx, static foo : isize = $e_toks; ); let _c: P = quote_pat!(cx, (x, 1 .. 4, *) ); - let _d: Option> = quote_stmt!(cx, let x = $a; ); + let _d: Option = quote_stmt!(cx, let x = $a; ); let _d: syntax::ast::Arm = quote_arm!(cx, (ref x, ref y) = (x, y) ); let _e: P = quote_expr!(cx, match foo { $p_toks => 10 } );