From 0607c138caf991c7dbe4fef57a08958a5e7fb621 Mon Sep 17 00:00:00 2001 From: Steven Fackler Date: Fri, 27 Dec 2013 17:17:36 -0700 Subject: [PATCH 1/6] Stop using @ExtCtxt --- src/librustc/front/test.rs | 25 +++++++------- src/librustpkg/util.rs | 2 +- src/libsyntax/ext/asm.rs | 2 +- src/libsyntax/ext/base.rs | 30 ++++++++--------- src/libsyntax/ext/build.rs | 18 +++++----- src/libsyntax/ext/bytes.rs | 2 +- src/libsyntax/ext/cfg.rs | 2 +- src/libsyntax/ext/concat.rs | 2 +- src/libsyntax/ext/concat_idents.rs | 2 +- src/libsyntax/ext/deriving/clone.rs | 6 ++-- src/libsyntax/ext/deriving/cmp/eq.rs | 6 ++-- src/libsyntax/ext/deriving/cmp/ord.rs | 4 +-- src/libsyntax/ext/deriving/cmp/totaleq.rs | 4 +-- src/libsyntax/ext/deriving/cmp/totalord.rs | 6 ++-- src/libsyntax/ext/deriving/decodable.rs | 6 ++-- src/libsyntax/ext/deriving/default.rs | 4 +-- src/libsyntax/ext/deriving/encodable.rs | 4 +-- src/libsyntax/ext/deriving/generic.rs | 24 +++++++------- src/libsyntax/ext/deriving/iter_bytes.rs | 4 +-- src/libsyntax/ext/deriving/mod.rs | 6 ++-- src/libsyntax/ext/deriving/primitive.rs | 4 +-- src/libsyntax/ext/deriving/rand.rs | 6 ++-- src/libsyntax/ext/deriving/to_str.rs | 4 +-- src/libsyntax/ext/deriving/ty.rs | 18 +++++----- src/libsyntax/ext/deriving/zero.rs | 4 +-- src/libsyntax/ext/env.rs | 4 +-- src/libsyntax/ext/expand.rs | 28 ++++++++-------- src/libsyntax/ext/fmt.rs | 2 +- src/libsyntax/ext/format.rs | 8 ++--- src/libsyntax/ext/log_syntax.rs | 2 +- src/libsyntax/ext/quote.rs | 38 +++++++++++----------- src/libsyntax/ext/source_util.rs | 18 +++++----- src/libsyntax/ext/trace_macros.rs | 2 +- src/libsyntax/ext/tt/macro_rules.rs | 6 ++-- src/test/run-pass-fulldeps/quote-tokens.rs | 2 +- 35 files changed, 152 insertions(+), 153 deletions(-) diff --git a/src/librustc/front/test.rs b/src/librustc/front/test.rs index 47ef4b94058ba..ed899561bc919 100644 --- a/src/librustc/front/test.rs +++ b/src/librustc/front/test.rs @@ -40,7 +40,7 @@ struct Test { struct TestCtxt { sess: session::Session, path: RefCell<~[ast::Ident]>, - ext_cx: @ExtCtxt, + ext_cx: ExtCtxt, testfns: RefCell<~[Test]>, is_extra: bool, config: ast::CrateConfig, @@ -163,8 +163,7 @@ fn generate_test_harness(sess: session::Session, crate: ast::Crate) config: crate.config.clone(), }; - let ext_cx = cx.ext_cx; - ext_cx.bt_push(ExpnInfo { + cx.ext_cx.bt_push(ExpnInfo { call_site: dummy_sp(), callee: NameAndSpan { name: @"test", @@ -177,7 +176,7 @@ fn generate_test_harness(sess: session::Session, crate: ast::Crate) cx: cx }; let res = fold.fold_crate(crate); - ext_cx.bt_pop(); + cx.ext_cx.bt_pop(); return res; } @@ -313,7 +312,7 @@ fn mk_test_module(cx: &TestCtxt) -> @ast::item { // The synthesized main function which will call the console test runner // with our list of tests - let mainfn = (quote_item!(cx.ext_cx, + let mainfn = (quote_item!(&cx.ext_cx, pub fn main() { #[main]; extra::test::test_main_static(::std::os::args(), TESTS); @@ -377,7 +376,7 @@ fn mk_tests(cx: &TestCtxt) -> @ast::item { // The vector of test_descs for this crate let test_descs = mk_test_descs(cx); - (quote_item!(cx.ext_cx, + (quote_item!(&cx.ext_cx, pub static TESTS : &'static [self::extra::test::TestDescAndFn] = $test_descs ; @@ -438,24 +437,24 @@ fn mk_test_desc_and_fn_rec(cx: &TestCtxt, test: &Test) -> @ast::Expr { }; let t_expr = if test.bench { - quote_expr!(cx.ext_cx, self::extra::test::StaticBenchFn($fn_expr) ) + quote_expr!(&cx.ext_cx, self::extra::test::StaticBenchFn($fn_expr) ) } else { - quote_expr!(cx.ext_cx, self::extra::test::StaticTestFn($fn_expr) ) + quote_expr!(&cx.ext_cx, self::extra::test::StaticTestFn($fn_expr) ) }; let ignore_expr = if test.ignore { - quote_expr!(cx.ext_cx, true ) + quote_expr!(&cx.ext_cx, true ) } else { - quote_expr!(cx.ext_cx, false ) + quote_expr!(&cx.ext_cx, false ) }; let fail_expr = if test.should_fail { - quote_expr!(cx.ext_cx, true ) + quote_expr!(&cx.ext_cx, true ) } else { - quote_expr!(cx.ext_cx, false ) + quote_expr!(&cx.ext_cx, false ) }; - let e = quote_expr!(cx.ext_cx, + let e = quote_expr!(&cx.ext_cx, self::extra::test::TestDescAndFn { desc: self::extra::test::TestDesc { name: self::extra::test::StaticTestName($name_expr), diff --git a/src/librustpkg/util.rs b/src/librustpkg/util.rs index eb7dbe4e4a8a7..b8d01f56e4db5 100644 --- a/src/librustpkg/util.rs +++ b/src/librustpkg/util.rs @@ -75,7 +75,7 @@ struct ListenerFn { struct ReadyCtx { sess: session::Session, - ext_cx: @ExtCtxt, + ext_cx: ExtCtxt, path: ~[ast::Ident], fns: ~[ListenerFn] } diff --git a/src/libsyntax/ext/asm.rs b/src/libsyntax/ext/asm.rs index f4b1c7f1f06df..fdde336ffa724 100644 --- a/src/libsyntax/ext/asm.rs +++ b/src/libsyntax/ext/asm.rs @@ -37,7 +37,7 @@ fn next_state(s: State) -> Option { } } -pub fn expand_asm(cx: @ExtCtxt, sp: Span, tts: &[ast::token_tree]) +pub fn expand_asm(cx: &ExtCtxt, sp: Span, tts: &[ast::token_tree]) -> base::MacResult { let p = parse::new_parser_from_tts(cx.parse_sess(), cx.cfg(), diff --git a/src/libsyntax/ext/base.rs b/src/libsyntax/ext/base.rs index d5a64a5edbaea..87d09ea3f8ed8 100644 --- a/src/libsyntax/ext/base.rs +++ b/src/libsyntax/ext/base.rs @@ -35,7 +35,7 @@ pub struct MacroDef { ext: SyntaxExtension } -pub type ItemDecorator = extern "Rust" fn(@ExtCtxt, +pub type ItemDecorator = extern "Rust" fn(&ExtCtxt, Span, @ast::MetaItem, ~[@ast::item]) @@ -48,7 +48,7 @@ pub struct SyntaxExpanderTT { pub trait SyntaxExpanderTTTrait { fn expand(&self, - ecx: @ExtCtxt, + ecx: &ExtCtxt, span: Span, token_tree: &[ast::token_tree], context: ast::SyntaxContext) @@ -56,7 +56,7 @@ pub trait SyntaxExpanderTTTrait { } pub type SyntaxExpanderTTFunNoCtxt = - extern "Rust" fn(ecx: @ExtCtxt, + extern "Rust" fn(ecx: &ExtCtxt, span: codemap::Span, token_tree: &[ast::token_tree]) -> MacResult; @@ -67,7 +67,7 @@ enum SyntaxExpanderTTExpander { impl SyntaxExpanderTTTrait for SyntaxExpanderTT { fn expand(&self, - ecx: @ExtCtxt, + ecx: &ExtCtxt, span: Span, token_tree: &[ast::token_tree], _: ast::SyntaxContext) @@ -92,7 +92,7 @@ pub struct SyntaxExpanderTTItem { pub trait SyntaxExpanderTTItemTrait { fn expand(&self, - cx: @ExtCtxt, + cx: &ExtCtxt, sp: Span, ident: ast::Ident, token_tree: ~[ast::token_tree], @@ -102,7 +102,7 @@ pub trait SyntaxExpanderTTItemTrait { impl SyntaxExpanderTTItemTrait for SyntaxExpanderTTItem { fn expand(&self, - cx: @ExtCtxt, + cx: &ExtCtxt, sp: Span, ident: ast::Ident, token_tree: ~[ast::token_tree], @@ -119,7 +119,7 @@ impl SyntaxExpanderTTItemTrait for SyntaxExpanderTTItem { } } -pub type SyntaxExpanderTTItemFun = extern "Rust" fn(@ExtCtxt, +pub type SyntaxExpanderTTItemFun = extern "Rust" fn(&ExtCtxt, Span, ast::Ident, ~[ast::token_tree], @@ -127,7 +127,7 @@ pub type SyntaxExpanderTTItemFun = extern "Rust" fn(@ExtCtxt, -> MacResult; pub type SyntaxExpanderTTItemFunNoCtxt = - extern "Rust" fn(@ExtCtxt, Span, ast::Ident, ~[ast::token_tree]) + extern "Rust" fn(&ExtCtxt, Span, ast::Ident, ~[ast::token_tree]) -> MacResult; pub trait AnyMacro { @@ -319,8 +319,8 @@ pub struct ExtCtxt { impl ExtCtxt { pub fn new(parse_sess: @mut parse::ParseSess, cfg: ast::CrateConfig) - -> @ExtCtxt { - @ExtCtxt { + -> ExtCtxt { + ExtCtxt { parse_sess: parse_sess, cfg: cfg, backtrace: @mut None, @@ -329,7 +329,7 @@ impl ExtCtxt { } } - pub fn expand_expr(@self, mut e: @ast::Expr) -> @ast::Expr { + pub fn expand_expr(&self, mut e: @ast::Expr) -> @ast::Expr { loop { match e.node { ast::ExprMac(..) => { @@ -417,7 +417,7 @@ impl ExtCtxt { } } -pub fn expr_to_str(cx: @ExtCtxt, expr: @ast::Expr, err_msg: &str) -> (@str, ast::StrStyle) { +pub fn expr_to_str(cx: &ExtCtxt, expr: @ast::Expr, err_msg: &str) -> (@str, ast::StrStyle) { match expr.node { ast::ExprLit(l) => match l.node { ast::lit_str(s, style) => (s, style), @@ -427,14 +427,14 @@ pub fn expr_to_str(cx: @ExtCtxt, expr: @ast::Expr, err_msg: &str) -> (@str, ast: } } -pub fn check_zero_tts(cx: @ExtCtxt, sp: Span, tts: &[ast::token_tree], +pub fn check_zero_tts(cx: &ExtCtxt, sp: Span, tts: &[ast::token_tree], name: &str) { if tts.len() != 0 { cx.span_fatal(sp, format!("{} takes no arguments", name)); } } -pub fn get_single_str_from_tts(cx: @ExtCtxt, +pub fn get_single_str_from_tts(cx: &ExtCtxt, sp: Span, tts: &[ast::token_tree], name: &str) @@ -450,7 +450,7 @@ pub fn get_single_str_from_tts(cx: @ExtCtxt, } } -pub fn get_exprs_from_tts(cx: @ExtCtxt, +pub fn get_exprs_from_tts(cx: &ExtCtxt, sp: Span, tts: &[ast::token_tree]) -> ~[@ast::Expr] { let p = parse::new_parser_from_tts(cx.parse_sess(), diff --git a/src/libsyntax/ext/build.rs b/src/libsyntax/ext/build.rs index aa7e0d0eced8d..007c06938c60e 100644 --- a/src/libsyntax/ext/build.rs +++ b/src/libsyntax/ext/build.rs @@ -15,7 +15,7 @@ use ast_util; use codemap::{Span, respan, dummy_sp}; use ext::base::ExtCtxt; use ext::quote::rt::*; -use fold; +use fold::ast_fold; use opt_vec; use opt_vec::OptVec; @@ -236,7 +236,7 @@ pub trait AstBuilder { vis: ast::visibility, path: ~[ast::Ident]) -> ast::view_item; } -impl AstBuilder for @ExtCtxt { +impl<'a> AstBuilder for &'a ExtCtxt { fn path(&self, span: Span, strs: ~[ast::Ident]) -> ast::Path { self.path_all(span, false, strs, opt_vec::Empty, ~[]) } @@ -903,11 +903,11 @@ impl AstBuilder for @ExtCtxt { } } -struct Duplicator { - cx: @ExtCtxt, +struct Duplicator<'a> { + cx: &'a ExtCtxt, } -impl fold::ast_fold for Duplicator { +impl<'a> ast_fold for Duplicator<'a> { fn new_id(&self, _: NodeId) -> NodeId { ast::DUMMY_NODE_ID } @@ -920,14 +920,14 @@ pub trait Duplicate { // These functions just duplicate AST nodes. // - fn duplicate(&self, cx: @ExtCtxt) -> Self; + fn duplicate(&self, cx: &ExtCtxt) -> Self; } impl Duplicate for @ast::Expr { - fn duplicate(&self, cx: @ExtCtxt) -> @ast::Expr { - let folder = @Duplicator { + fn duplicate(&self, cx: &ExtCtxt) -> @ast::Expr { + let folder = Duplicator { cx: cx, - } as @fold::ast_fold; + }; folder.fold_expr(*self) } } diff --git a/src/libsyntax/ext/bytes.rs b/src/libsyntax/ext/bytes.rs index 933f4b33fe3df..1ceb2a1668eaf 100644 --- a/src/libsyntax/ext/bytes.rs +++ b/src/libsyntax/ext/bytes.rs @@ -18,7 +18,7 @@ use ext::build::AstBuilder; use std::char; -pub fn expand_syntax_ext(cx: @ExtCtxt, sp: Span, tts: &[ast::token_tree]) -> base::MacResult { +pub fn expand_syntax_ext(cx: &ExtCtxt, sp: Span, tts: &[ast::token_tree]) -> base::MacResult { // Gather all argument expressions let exprs = get_exprs_from_tts(cx, sp, tts); let mut bytes = ~[]; diff --git a/src/libsyntax/ext/cfg.rs b/src/libsyntax/ext/cfg.rs index 89e7994530d73..2ca8917ed8fc6 100644 --- a/src/libsyntax/ext/cfg.rs +++ b/src/libsyntax/ext/cfg.rs @@ -25,7 +25,7 @@ use parse; use parse::token; use parse::attr::parser_attr; -pub fn expand_cfg(cx: @ExtCtxt, sp: Span, tts: &[ast::token_tree]) -> base::MacResult { +pub fn expand_cfg(cx: &ExtCtxt, sp: Span, tts: &[ast::token_tree]) -> base::MacResult { let p = parse::new_parser_from_tts(cx.parse_sess(), cx.cfg(), tts.to_owned()); let mut cfgs = ~[]; diff --git a/src/libsyntax/ext/concat.rs b/src/libsyntax/ext/concat.rs index 11e316e3e6bd6..b6a81a8bea543 100644 --- a/src/libsyntax/ext/concat.rs +++ b/src/libsyntax/ext/concat.rs @@ -15,7 +15,7 @@ use codemap; use ext::base; use ext::build::AstBuilder; -pub fn expand_syntax_ext(cx: @base::ExtCtxt, +pub fn expand_syntax_ext(cx: &base::ExtCtxt, sp: codemap::Span, tts: &[ast::token_tree]) -> base::MacResult { let es = base::get_exprs_from_tts(cx, sp, tts); diff --git a/src/libsyntax/ext/concat_idents.rs b/src/libsyntax/ext/concat_idents.rs index 216bc3097ce94..6b204a243cb1e 100644 --- a/src/libsyntax/ext/concat_idents.rs +++ b/src/libsyntax/ext/concat_idents.rs @@ -16,7 +16,7 @@ use opt_vec; use parse::token; use parse::token::{str_to_ident}; -pub fn expand_syntax_ext(cx: @ExtCtxt, sp: Span, tts: &[ast::token_tree]) +pub fn expand_syntax_ext(cx: &ExtCtxt, sp: Span, tts: &[ast::token_tree]) -> base::MacResult { let mut res_str = ~""; for (i, e) in tts.iter().enumerate() { diff --git a/src/libsyntax/ext/deriving/clone.rs b/src/libsyntax/ext/deriving/clone.rs index 9285defe69ea1..c3c57082684b6 100644 --- a/src/libsyntax/ext/deriving/clone.rs +++ b/src/libsyntax/ext/deriving/clone.rs @@ -14,7 +14,7 @@ use ext::base::ExtCtxt; use ext::build::AstBuilder; use ext::deriving::generic::*; -pub fn expand_deriving_clone(cx: @ExtCtxt, +pub fn expand_deriving_clone(cx: &ExtCtxt, span: Span, mitem: @MetaItem, in_items: ~[@item]) @@ -42,7 +42,7 @@ pub fn expand_deriving_clone(cx: @ExtCtxt, trait_def.expand(mitem, in_items) } -pub fn expand_deriving_deep_clone(cx: @ExtCtxt, +pub fn expand_deriving_deep_clone(cx: &ExtCtxt, span: Span, mitem: @MetaItem, in_items: ~[@item]) @@ -74,7 +74,7 @@ pub fn expand_deriving_deep_clone(cx: @ExtCtxt, fn cs_clone( name: &str, - cx: @ExtCtxt, span: Span, + cx: &ExtCtxt, span: Span, substr: &Substructure) -> @Expr { let clone_ident = substr.method_ident; let ctor_ident; diff --git a/src/libsyntax/ext/deriving/cmp/eq.rs b/src/libsyntax/ext/deriving/cmp/eq.rs index 74680266cb709..f7b249fc8d8bb 100644 --- a/src/libsyntax/ext/deriving/cmp/eq.rs +++ b/src/libsyntax/ext/deriving/cmp/eq.rs @@ -14,17 +14,17 @@ use ext::base::ExtCtxt; use ext::build::AstBuilder; use ext::deriving::generic::*; -pub fn expand_deriving_eq(cx: @ExtCtxt, +pub fn expand_deriving_eq(cx: &ExtCtxt, span: Span, mitem: @MetaItem, in_items: ~[@item]) -> ~[@item] { // structures are equal if all fields are equal, and non equal, if // any fields are not equal or if the enum variants are different - fn cs_eq(cx: @ExtCtxt, span: Span, substr: &Substructure) -> @Expr { + fn cs_eq(cx: &ExtCtxt, span: Span, substr: &Substructure) -> @Expr { cs_and(|cx, span, _, _| cx.expr_bool(span, false), cx, span, substr) } - fn cs_ne(cx: @ExtCtxt, span: Span, substr: &Substructure) -> @Expr { + fn cs_ne(cx: &ExtCtxt, span: Span, substr: &Substructure) -> @Expr { cs_or(|cx, span, _, _| cx.expr_bool(span, true), cx, span, substr) } diff --git a/src/libsyntax/ext/deriving/cmp/ord.rs b/src/libsyntax/ext/deriving/cmp/ord.rs index d48cfbd7dd707..c1e4e1e2b641d 100644 --- a/src/libsyntax/ext/deriving/cmp/ord.rs +++ b/src/libsyntax/ext/deriving/cmp/ord.rs @@ -15,7 +15,7 @@ use ext::base::ExtCtxt; use ext::build::AstBuilder; use ext::deriving::generic::*; -pub fn expand_deriving_ord(cx: @ExtCtxt, +pub fn expand_deriving_ord(cx: &ExtCtxt, span: Span, mitem: @MetaItem, in_items: ~[@item]) -> ~[@item] { @@ -51,7 +51,7 @@ pub fn expand_deriving_ord(cx: @ExtCtxt, } /// Strict inequality. -fn cs_op(less: bool, equal: bool, cx: @ExtCtxt, span: Span, substr: &Substructure) -> @Expr { +fn cs_op(less: bool, equal: bool, cx: &ExtCtxt, span: Span, substr: &Substructure) -> @Expr { let op = if less {ast::BiLt} else {ast::BiGt}; cs_fold( false, // need foldr, diff --git a/src/libsyntax/ext/deriving/cmp/totaleq.rs b/src/libsyntax/ext/deriving/cmp/totaleq.rs index ab822d14b4845..0b4d2659fd1fe 100644 --- a/src/libsyntax/ext/deriving/cmp/totaleq.rs +++ b/src/libsyntax/ext/deriving/cmp/totaleq.rs @@ -14,11 +14,11 @@ use ext::base::ExtCtxt; use ext::build::AstBuilder; use ext::deriving::generic::*; -pub fn expand_deriving_totaleq(cx: @ExtCtxt, +pub fn expand_deriving_totaleq(cx: &ExtCtxt, span: Span, mitem: @MetaItem, in_items: ~[@item]) -> ~[@item] { - fn cs_equals(cx: @ExtCtxt, span: Span, substr: &Substructure) -> @Expr { + fn cs_equals(cx: &ExtCtxt, span: Span, substr: &Substructure) -> @Expr { cs_and(|cx, span, _, _| cx.expr_bool(span, false), cx, span, substr) } diff --git a/src/libsyntax/ext/deriving/cmp/totalord.rs b/src/libsyntax/ext/deriving/cmp/totalord.rs index 2ace39a348688..3c58bfa01a96f 100644 --- a/src/libsyntax/ext/deriving/cmp/totalord.rs +++ b/src/libsyntax/ext/deriving/cmp/totalord.rs @@ -16,7 +16,7 @@ use ext::build::AstBuilder; use ext::deriving::generic::*; use std::cmp::{Ordering, Equal, Less, Greater}; -pub fn expand_deriving_totalord(cx: @ExtCtxt, +pub fn expand_deriving_totalord(cx: &ExtCtxt, span: Span, mitem: @MetaItem, in_items: ~[@item]) -> ~[@item] { @@ -44,7 +44,7 @@ pub fn expand_deriving_totalord(cx: @ExtCtxt, } -pub fn ordering_const(cx: @ExtCtxt, span: Span, cnst: Ordering) -> ast::Path { +pub fn ordering_const(cx: &ExtCtxt, span: Span, cnst: Ordering) -> ast::Path { let cnst = match cnst { Less => "Less", Equal => "Equal", @@ -56,7 +56,7 @@ pub fn ordering_const(cx: @ExtCtxt, span: Span, cnst: Ordering) -> ast::Path { cx.ident_of(cnst)]) } -pub fn cs_cmp(cx: @ExtCtxt, span: Span, +pub fn cs_cmp(cx: &ExtCtxt, span: Span, substr: &Substructure) -> @Expr { let test_id = cx.ident_of("__test"); let equals_path = ordering_const(cx, span, Equal); diff --git a/src/libsyntax/ext/deriving/decodable.rs b/src/libsyntax/ext/deriving/decodable.rs index 0aade760b7ba6..de995af9ae3a5 100644 --- a/src/libsyntax/ext/deriving/decodable.rs +++ b/src/libsyntax/ext/deriving/decodable.rs @@ -19,7 +19,7 @@ use ext::base::ExtCtxt; use ext::build::AstBuilder; use ext::deriving::generic::*; -pub fn expand_deriving_decodable(cx: @ExtCtxt, +pub fn expand_deriving_decodable(cx: &ExtCtxt, span: Span, mitem: @MetaItem, in_items: ~[@item]) -> ~[@item] { @@ -51,7 +51,7 @@ pub fn expand_deriving_decodable(cx: @ExtCtxt, trait_def.expand(mitem, in_items) } -fn decodable_substructure(cx: @ExtCtxt, span: Span, +fn decodable_substructure(cx: &ExtCtxt, span: Span, substr: &Substructure) -> @Expr { let decoder = substr.nonself_args[0]; let recurse = ~[cx.ident_of("extra"), @@ -132,7 +132,7 @@ fn decodable_substructure(cx: @ExtCtxt, span: Span, /// Create a decoder for a single enum variant/struct: /// - `outer_pat_ident` is the name of this enum variant/struct /// - `getarg` should retrieve the `uint`-th field with name `@str`. -fn decode_static_fields(cx: @ExtCtxt, +fn decode_static_fields(cx: &ExtCtxt, outer_span: Span, outer_pat_ident: Ident, fields: &StaticFields, diff --git a/src/libsyntax/ext/deriving/default.rs b/src/libsyntax/ext/deriving/default.rs index 393d808a0254e..ba2df120796c3 100644 --- a/src/libsyntax/ext/deriving/default.rs +++ b/src/libsyntax/ext/deriving/default.rs @@ -14,7 +14,7 @@ use ext::base::ExtCtxt; use ext::build::AstBuilder; use ext::deriving::generic::*; -pub fn expand_deriving_default(cx: @ExtCtxt, +pub fn expand_deriving_default(cx: &ExtCtxt, span: Span, mitem: @MetaItem, in_items: ~[@item]) @@ -41,7 +41,7 @@ pub fn expand_deriving_default(cx: @ExtCtxt, trait_def.expand(mitem, in_items) } -fn default_substructure(cx: @ExtCtxt, span: Span, substr: &Substructure) -> @Expr { +fn default_substructure(cx: &ExtCtxt, span: Span, substr: &Substructure) -> @Expr { let default_ident = ~[ cx.ident_of("std"), cx.ident_of("default"), diff --git a/src/libsyntax/ext/deriving/encodable.rs b/src/libsyntax/ext/deriving/encodable.rs index 09210116c3767..a6cba59aea518 100644 --- a/src/libsyntax/ext/deriving/encodable.rs +++ b/src/libsyntax/ext/deriving/encodable.rs @@ -81,7 +81,7 @@ use ext::base::ExtCtxt; use ext::build::AstBuilder; use ext::deriving::generic::*; -pub fn expand_deriving_encodable(cx: @ExtCtxt, +pub fn expand_deriving_encodable(cx: &ExtCtxt, span: Span, mitem: @MetaItem, in_items: ~[@item]) -> ~[@item] { @@ -113,7 +113,7 @@ pub fn expand_deriving_encodable(cx: @ExtCtxt, trait_def.expand(mitem, in_items) } -fn encodable_substructure(cx: @ExtCtxt, span: Span, +fn encodable_substructure(cx: &ExtCtxt, span: Span, substr: &Substructure) -> @Expr { let encoder = substr.nonself_args[0]; // throw an underscore in front to suppress unused variable warnings diff --git a/src/libsyntax/ext/deriving/generic.rs b/src/libsyntax/ext/deriving/generic.rs index b3e2503f8c460..2eb0fe30b4b07 100644 --- a/src/libsyntax/ext/deriving/generic.rs +++ b/src/libsyntax/ext/deriving/generic.rs @@ -190,7 +190,7 @@ mod ty; pub struct TraitDef<'a> { /// The extension context - cx: @ExtCtxt, + cx: &'a ExtCtxt, /// The span for the current #[deriving(Foo)] header. span: Span, @@ -300,7 +300,7 @@ Combine the values of all the fields together. The last argument is all the fields of all the structures, see above for details. */ pub type CombineSubstructureFunc<'a> = - 'a |@ExtCtxt, Span, &Substructure| -> @Expr; + 'a |&ExtCtxt, Span, &Substructure| -> @Expr; /** Deal with non-matching enum variants, the arguments are a list @@ -308,7 +308,7 @@ representing each variant: (variant index, ast::variant instance, [variant fields]), and a list of the nonself args of the type */ pub type EnumNonMatchFunc<'a> = - 'a |@ExtCtxt, + 'a |&ExtCtxt, Span, &[(uint, P, ~[(Span, Option, @Expr)])], &[@Expr]| @@ -1076,10 +1076,10 @@ Fold the fields. `use_foldl` controls whether this is done left-to-right (`true`) or right-to-left (`false`). */ pub fn cs_fold(use_foldl: bool, - f: |@ExtCtxt, Span, @Expr, @Expr, &[@Expr]| -> @Expr, + f: |&ExtCtxt, Span, @Expr, @Expr, &[@Expr]| -> @Expr, base: @Expr, enum_nonmatch_f: EnumNonMatchFunc, - cx: @ExtCtxt, + cx: &ExtCtxt, trait_span: Span, substructure: &Substructure) -> @Expr { @@ -1115,9 +1115,9 @@ f(cx, span, ~[self_1.method(__arg_1_1, __arg_2_1), ~~~ */ #[inline] -pub fn cs_same_method(f: |@ExtCtxt, Span, ~[@Expr]| -> @Expr, +pub fn cs_same_method(f: |&ExtCtxt, Span, ~[@Expr]| -> @Expr, enum_nonmatch_f: EnumNonMatchFunc, - cx: @ExtCtxt, + cx: &ExtCtxt, trait_span: Span, substructure: &Substructure) -> @Expr { @@ -1149,10 +1149,10 @@ fields. `use_foldl` controls whether this is done left-to-right */ #[inline] pub fn cs_same_method_fold(use_foldl: bool, - f: |@ExtCtxt, Span, @Expr, @Expr| -> @Expr, + f: |&ExtCtxt, Span, @Expr, @Expr| -> @Expr, base: @Expr, enum_nonmatch_f: EnumNonMatchFunc, - cx: @ExtCtxt, + cx: &ExtCtxt, trait_span: Span, substructure: &Substructure) -> @Expr { @@ -1179,7 +1179,7 @@ on all the fields. #[inline] pub fn cs_binop(binop: ast::BinOp, base: @Expr, enum_nonmatch_f: EnumNonMatchFunc, - cx: @ExtCtxt, trait_span: Span, + cx: &ExtCtxt, trait_span: Span, substructure: &Substructure) -> @Expr { cs_same_method_fold( true, // foldl is good enough @@ -1197,7 +1197,7 @@ pub fn cs_binop(binop: ast::BinOp, base: @Expr, /// cs_binop with binop == or #[inline] pub fn cs_or(enum_nonmatch_f: EnumNonMatchFunc, - cx: @ExtCtxt, span: Span, + cx: &ExtCtxt, span: Span, substructure: &Substructure) -> @Expr { cs_binop(ast::BiOr, cx.expr_bool(span, false), enum_nonmatch_f, @@ -1207,7 +1207,7 @@ pub fn cs_or(enum_nonmatch_f: EnumNonMatchFunc, /// cs_binop with binop == and #[inline] pub fn cs_and(enum_nonmatch_f: EnumNonMatchFunc, - cx: @ExtCtxt, span: Span, + cx: &ExtCtxt, span: Span, substructure: &Substructure) -> @Expr { cs_binop(ast::BiAnd, cx.expr_bool(span, true), enum_nonmatch_f, diff --git a/src/libsyntax/ext/deriving/iter_bytes.rs b/src/libsyntax/ext/deriving/iter_bytes.rs index 837b8bd59ec9a..8b2ad95e45c5b 100644 --- a/src/libsyntax/ext/deriving/iter_bytes.rs +++ b/src/libsyntax/ext/deriving/iter_bytes.rs @@ -15,7 +15,7 @@ use ext::build::AstBuilder; use ext::deriving::generic::*; -pub fn expand_deriving_iter_bytes(cx: @ExtCtxt, +pub fn expand_deriving_iter_bytes(cx: &ExtCtxt, span: Span, mitem: @MetaItem, in_items: ~[@item]) -> ~[@item] { @@ -45,7 +45,7 @@ pub fn expand_deriving_iter_bytes(cx: @ExtCtxt, trait_def.expand(mitem, in_items) } -fn iter_bytes_substructure(cx: @ExtCtxt, span: Span, substr: &Substructure) -> @Expr { +fn iter_bytes_substructure(cx: &ExtCtxt, span: Span, substr: &Substructure) -> @Expr { let (lsb0, f)= match substr.nonself_args { [l, f] => (l, f), _ => cx.span_bug(span, "Incorrect number of arguments in `deriving(IterBytes)`") diff --git a/src/libsyntax/ext/deriving/mod.rs b/src/libsyntax/ext/deriving/mod.rs index d38f9b4d1b79e..1e8b79c8f350b 100644 --- a/src/libsyntax/ext/deriving/mod.rs +++ b/src/libsyntax/ext/deriving/mod.rs @@ -46,20 +46,20 @@ pub mod totalord; pub mod generic; -pub type ExpandDerivingStructDefFn<'a> = 'a |@ExtCtxt, +pub type ExpandDerivingStructDefFn<'a> = 'a |&ExtCtxt, Span, x: &struct_def, Ident, y: &Generics| -> @item; -pub type ExpandDerivingEnumDefFn<'a> = 'a |@ExtCtxt, +pub type ExpandDerivingEnumDefFn<'a> = 'a |&ExtCtxt, Span, x: &enum_def, Ident, y: &Generics| -> @item; -pub fn expand_meta_deriving(cx: @ExtCtxt, +pub fn expand_meta_deriving(cx: &ExtCtxt, _span: Span, mitem: @MetaItem, in_items: ~[@item]) diff --git a/src/libsyntax/ext/deriving/primitive.rs b/src/libsyntax/ext/deriving/primitive.rs index 73ea627ff5457..ba2d85f357a7d 100644 --- a/src/libsyntax/ext/deriving/primitive.rs +++ b/src/libsyntax/ext/deriving/primitive.rs @@ -15,7 +15,7 @@ use ext::base::ExtCtxt; use ext::build::AstBuilder; use ext::deriving::generic::*; -pub fn expand_deriving_from_primitive(cx: @ExtCtxt, +pub fn expand_deriving_from_primitive(cx: &ExtCtxt, span: Span, mitem: @MetaItem, in_items: ~[@item]) -> ~[@item] { @@ -64,7 +64,7 @@ pub fn expand_deriving_from_primitive(cx: @ExtCtxt, trait_def.expand(mitem, in_items) } -fn cs_from(name: &str, cx: @ExtCtxt, span: Span, substr: &Substructure) -> @Expr { +fn cs_from(name: &str, cx: &ExtCtxt, span: Span, substr: &Substructure) -> @Expr { let n = match substr.nonself_args { [n] => n, _ => cx.span_bug(span, "Incorrect number of arguments in `deriving(FromPrimitive)`") diff --git a/src/libsyntax/ext/deriving/rand.rs b/src/libsyntax/ext/deriving/rand.rs index 91ed60324cf1a..1491797c8083f 100644 --- a/src/libsyntax/ext/deriving/rand.rs +++ b/src/libsyntax/ext/deriving/rand.rs @@ -16,7 +16,7 @@ use ext::build::{AstBuilder}; use ext::deriving::generic::*; use opt_vec; -pub fn expand_deriving_rand(cx: @ExtCtxt, +pub fn expand_deriving_rand(cx: &ExtCtxt, span: Span, mitem: @MetaItem, in_items: ~[@item]) @@ -50,7 +50,7 @@ pub fn expand_deriving_rand(cx: @ExtCtxt, trait_def.expand(mitem, in_items) } -fn rand_substructure(cx: @ExtCtxt, span: Span, substr: &Substructure) -> @Expr { +fn rand_substructure(cx: &ExtCtxt, span: Span, substr: &Substructure) -> @Expr { let rng = match substr.nonself_args { [rng] => ~[ rng ], _ => cx.bug("Incorrect number of arguments to `rand` in `deriving(Rand)`") @@ -130,7 +130,7 @@ fn rand_substructure(cx: @ExtCtxt, span: Span, substr: &Substructure) -> @Expr { _ => cx.bug("Non-static method in `deriving(Rand)`") }; - fn rand_thing(cx: @ExtCtxt, + fn rand_thing(cx: &ExtCtxt, span: Span, ctor_ident: Ident, summary: &StaticFields, diff --git a/src/libsyntax/ext/deriving/to_str.rs b/src/libsyntax/ext/deriving/to_str.rs index fd8c21f4f9867..25700f93a33cd 100644 --- a/src/libsyntax/ext/deriving/to_str.rs +++ b/src/libsyntax/ext/deriving/to_str.rs @@ -15,7 +15,7 @@ use ext::base::ExtCtxt; use ext::build::AstBuilder; use ext::deriving::generic::*; -pub fn expand_deriving_to_str(cx: @ExtCtxt, +pub fn expand_deriving_to_str(cx: &ExtCtxt, span: Span, mitem: @MetaItem, in_items: ~[@item]) @@ -47,7 +47,7 @@ pub fn expand_deriving_to_str(cx: @ExtCtxt, // doesn't invoke the to_str() method on each field. Hence we mirror // the logic of the repr_to_str() method, but with tweaks to call to_str() // on sub-fields. -fn to_str_substructure(cx: @ExtCtxt, span: Span, +fn to_str_substructure(cx: &ExtCtxt, span: Span, substr: &Substructure) -> @Expr { let to_str = cx.ident_of("to_str"); diff --git a/src/libsyntax/ext/deriving/ty.rs b/src/libsyntax/ext/deriving/ty.rs index e606cebc415c4..10e07520a8490 100644 --- a/src/libsyntax/ext/deriving/ty.rs +++ b/src/libsyntax/ext/deriving/ty.rs @@ -58,7 +58,7 @@ impl<'a> Path<'a> { } pub fn to_ty(&self, - cx: @ExtCtxt, + cx: &ExtCtxt, span: Span, self_ty: Ident, self_generics: &Generics) @@ -66,7 +66,7 @@ impl<'a> Path<'a> { cx.ty_path(self.to_path(cx, span, self_ty, self_generics), None) } pub fn to_path(&self, - cx: @ExtCtxt, + cx: &ExtCtxt, span: Span, self_ty: Ident, self_generics: &Generics) @@ -110,14 +110,14 @@ pub fn nil_ty() -> Ty<'static> { Tuple(~[]) } -fn mk_lifetime(cx: @ExtCtxt, span: Span, lt: &Option<&str>) -> Option { +fn mk_lifetime(cx: &ExtCtxt, span: Span, lt: &Option<&str>) -> Option { match *lt { Some(ref s) => Some(cx.lifetime(span, cx.ident_of(*s))), None => None } } -fn mk_lifetimes(cx: @ExtCtxt, span: Span, lt: &Option<&str>) -> OptVec { +fn mk_lifetimes(cx: &ExtCtxt, span: Span, lt: &Option<&str>) -> OptVec { match *lt { Some(ref s) => opt_vec::with(cx.lifetime(span, cx.ident_of(*s))), None => opt_vec::Empty @@ -126,7 +126,7 @@ fn mk_lifetimes(cx: @ExtCtxt, span: Span, lt: &Option<&str>) -> OptVec Ty<'a> { pub fn to_ty(&self, - cx: @ExtCtxt, + cx: &ExtCtxt, span: Span, self_ty: Ident, self_generics: &Generics) @@ -164,7 +164,7 @@ impl<'a> Ty<'a> { } pub fn to_path(&self, - cx: @ExtCtxt, + cx: &ExtCtxt, span: Span, self_ty: Ident, self_generics: &Generics) @@ -189,7 +189,7 @@ impl<'a> Ty<'a> { } -fn mk_ty_param(cx: @ExtCtxt, span: Span, name: &str, bounds: &[Path], +fn mk_ty_param(cx: &ExtCtxt, span: Span, name: &str, bounds: &[Path], self_ident: Ident, self_generics: &Generics) -> ast::TyParam { let bounds = opt_vec::from( bounds.map(|b| { @@ -219,7 +219,7 @@ impl<'a> LifetimeBounds<'a> { } } pub fn to_generics(&self, - cx: @ExtCtxt, + cx: &ExtCtxt, span: Span, self_ty: Ident, self_generics: &Generics) @@ -239,7 +239,7 @@ impl<'a> LifetimeBounds<'a> { } -pub fn get_explicit_self(cx: @ExtCtxt, span: Span, self_ptr: &Option) +pub fn get_explicit_self(cx: &ExtCtxt, span: Span, self_ptr: &Option) -> (@Expr, ast::explicit_self) { let self_path = cx.expr_self(span); match *self_ptr { diff --git a/src/libsyntax/ext/deriving/zero.rs b/src/libsyntax/ext/deriving/zero.rs index 67f14a8d48645..c406dbf0d0117 100644 --- a/src/libsyntax/ext/deriving/zero.rs +++ b/src/libsyntax/ext/deriving/zero.rs @@ -14,7 +14,7 @@ use ext::base::ExtCtxt; use ext::build::AstBuilder; use ext::deriving::generic::*; -pub fn expand_deriving_zero(cx: @ExtCtxt, +pub fn expand_deriving_zero(cx: &ExtCtxt, span: Span, mitem: @MetaItem, in_items: ~[@item]) @@ -57,7 +57,7 @@ pub fn expand_deriving_zero(cx: @ExtCtxt, trait_def.expand(mitem, in_items) } -fn zero_substructure(cx: @ExtCtxt, span: Span, substr: &Substructure) -> @Expr { +fn zero_substructure(cx: &ExtCtxt, span: Span, substr: &Substructure) -> @Expr { let zero_ident = ~[ cx.ident_of("std"), cx.ident_of("num"), diff --git a/src/libsyntax/ext/env.rs b/src/libsyntax/ext/env.rs index 15630e37eadd5..b5f5f8c41d023 100644 --- a/src/libsyntax/ext/env.rs +++ b/src/libsyntax/ext/env.rs @@ -22,7 +22,7 @@ use ext::build::AstBuilder; use std::os; -pub fn expand_option_env(cx: @ExtCtxt, sp: Span, tts: &[ast::token_tree]) +pub fn expand_option_env(cx: &ExtCtxt, sp: Span, tts: &[ast::token_tree]) -> base::MacResult { let var = get_single_str_from_tts(cx, sp, tts, "option_env!"); @@ -33,7 +33,7 @@ pub fn expand_option_env(cx: @ExtCtxt, sp: Span, tts: &[ast::token_tree]) MRExpr(e) } -pub fn expand_env(cx: @ExtCtxt, sp: Span, tts: &[ast::token_tree]) +pub fn expand_env(cx: &ExtCtxt, sp: Span, tts: &[ast::token_tree]) -> base::MacResult { let exprs = get_exprs_from_tts(cx, sp, tts); diff --git a/src/libsyntax/ext/expand.rs b/src/libsyntax/ext/expand.rs index 2c2669e914cca..9f9c245fe481d 100644 --- a/src/libsyntax/ext/expand.rs +++ b/src/libsyntax/ext/expand.rs @@ -32,7 +32,7 @@ use util::small_vector::SmallVector; use std::vec; pub fn expand_expr(extsbox: @mut SyntaxEnv, - cx: @ExtCtxt, + cx: &ExtCtxt, e: @ast::Expr, fld: &MacroExpander) -> @ast::Expr { @@ -214,7 +214,7 @@ pub fn expand_expr(extsbox: @mut SyntaxEnv, // NB: there is some redundancy between this and expand_item, below, and // they might benefit from some amount of semantic and language-UI merger. pub fn expand_mod_items(extsbox: @mut SyntaxEnv, - cx: @ExtCtxt, + cx: &ExtCtxt, module_: &ast::_mod, fld: &MacroExpander) -> ast::_mod { @@ -271,7 +271,7 @@ static special_block_name : &'static str = " block"; // When we enter a module, record it, for the sake of `module!` pub fn expand_item(extsbox: @mut SyntaxEnv, - cx: @ExtCtxt, + cx: &ExtCtxt, it: @ast::item, fld: &MacroExpander) -> SmallVector<@ast::item> { @@ -298,7 +298,7 @@ pub fn contains_macro_escape(attrs: &[ast::Attribute]) -> bool { // Support for item-position macro invocations, exactly the same // logic as for expression-position macro invocations. pub fn expand_item_mac(extsbox: @mut SyntaxEnv, - cx: @ExtCtxt, + cx: &ExtCtxt, it: @ast::item, fld: &MacroExpander) -> SmallVector<@ast::item> { @@ -407,7 +407,7 @@ fn insert_macro(exts: SyntaxEnv, name: ast::Name, transformer: @Transformer) { // expand a stmt pub fn expand_stmt(extsbox: @mut SyntaxEnv, - cx: @ExtCtxt, + cx: &ExtCtxt, s: &Stmt, fld: &MacroExpander) -> SmallVector<@Stmt> { @@ -625,7 +625,7 @@ pub fn new_name_finder(idents: ~[ast::Ident]) -> NewNameFinderContext { // expand a block. pushes a new exts_frame, then calls expand_block_elts pub fn expand_block(extsbox: @mut SyntaxEnv, - _: @ExtCtxt, + _: &ExtCtxt, blk: &Block, fld: &MacroExpander) -> P { @@ -690,7 +690,7 @@ pub fn renames_to_fold(renames: @mut ~[(ast::Ident,ast::Name)]) -> @ast_fold { } as @ast_fold } -pub fn new_span(cx: @ExtCtxt, sp: Span) -> Span { +pub fn new_span(cx: &ExtCtxt, sp: Span) -> Span { /* this discards information in the case of macro-defining macros */ Span { lo: sp.lo, @@ -949,12 +949,12 @@ pub fn inject_std_macros(parse_sess: @mut parse::ParseSess, injector.fold_crate(c) } -pub struct MacroExpander { +pub struct MacroExpander<'a> { extsbox: @mut SyntaxEnv, - cx: @ExtCtxt, + cx: &'a ExtCtxt, } -impl ast_fold for MacroExpander { +impl<'a> ast_fold for MacroExpander<'a> { fn fold_expr(&self, expr: @ast::Expr) -> @ast::Expr { expand_expr(self.extsbox, self.cx, @@ -1005,10 +1005,10 @@ pub fn expand_crate(parse_sess: @mut parse::ParseSess, // every method/element of AstFoldFns in fold.rs. let extsbox = syntax_expander_table(); let cx = ExtCtxt::new(parse_sess, cfg.clone()); - let expander = @MacroExpander { + let expander = MacroExpander { extsbox: @mut extsbox, - cx: cx, - } as @ast_fold; + cx: &cx, + }; let ret = expander.fold_crate(c); parse_sess.span_diagnostic.handler().abort_if_errors(); @@ -1159,7 +1159,7 @@ pub fn mtwt_cancel_outer_mark(tts: &[ast::token_tree], ctxt: ast::SyntaxContext) mark_tts(tts,outer_mark) } -fn original_span(cx: @ExtCtxt) -> @codemap::ExpnInfo { +fn original_span(cx: &ExtCtxt) -> @codemap::ExpnInfo { let mut relevant_info = cx.backtrace(); let mut einfo = relevant_info.unwrap(); loop { diff --git a/src/libsyntax/ext/fmt.rs b/src/libsyntax/ext/fmt.rs index 5e3e4e15d8e74..f2eab3f1cbc4c 100644 --- a/src/libsyntax/ext/fmt.rs +++ b/src/libsyntax/ext/fmt.rs @@ -15,7 +15,7 @@ use codemap::Span; use ext::base; use ext::build::AstBuilder; -pub fn expand_syntax_ext(ecx: @base::ExtCtxt, sp: Span, +pub fn expand_syntax_ext(ecx: &base::ExtCtxt, sp: Span, _tts: &[ast::token_tree]) -> base::MacResult { ecx.span_err(sp, "`fmt!` is deprecated, use `format!` instead"); ecx.parse_sess.span_diagnostic.span_note(sp, diff --git a/src/libsyntax/ext/format.rs b/src/libsyntax/ext/format.rs index 98ebc24427d9a..2b51203eca5bb 100644 --- a/src/libsyntax/ext/format.rs +++ b/src/libsyntax/ext/format.rs @@ -28,8 +28,8 @@ enum ArgumentType { String, } -struct Context { - ecx: @ExtCtxt, +struct Context<'a> { + ecx: &'a ExtCtxt, fmtsp: Span, // Parsed argument expressions and the types that we've found so far for @@ -50,7 +50,7 @@ struct Context { next_arg: uint, } -impl Context { +impl<'a> Context<'a> { /// Parses the arguments from the given list of tokens, returning None if /// there's a parse error so we can continue parsing other format! expressions. fn parse_args(&mut self, sp: Span, @@ -722,7 +722,7 @@ impl Context { } } -pub fn expand_args(ecx: @ExtCtxt, sp: Span, +pub fn expand_args(ecx: &ExtCtxt, sp: Span, tts: &[ast::token_tree]) -> base::MacResult { let mut cx = Context { ecx: ecx, diff --git a/src/libsyntax/ext/log_syntax.rs b/src/libsyntax/ext/log_syntax.rs index 2007abcc81cf7..7dfd487b379fc 100644 --- a/src/libsyntax/ext/log_syntax.rs +++ b/src/libsyntax/ext/log_syntax.rs @@ -15,7 +15,7 @@ use ext::base; use print; use parse::token::{get_ident_interner}; -pub fn expand_syntax_ext(cx: @ExtCtxt, +pub fn expand_syntax_ext(cx: &ExtCtxt, sp: codemap::Span, tt: &[ast::token_tree]) -> base::MacResult { diff --git a/src/libsyntax/ext/quote.rs b/src/libsyntax/ext/quote.rs index 77cb02522a7e0..f6086734003e8 100644 --- a/src/libsyntax/ext/quote.rs +++ b/src/libsyntax/ext/quote.rs @@ -40,11 +40,11 @@ pub mod rt { pub use codemap::{BytePos, Span, dummy_spanned}; pub trait ToTokens { - fn to_tokens(&self, _cx: @ExtCtxt) -> ~[token_tree]; + fn to_tokens(&self, _cx: &ExtCtxt) -> ~[token_tree]; } impl ToTokens for ~[token_tree] { - fn to_tokens(&self, _cx: @ExtCtxt) -> ~[token_tree] { + fn to_tokens(&self, _cx: &ExtCtxt) -> ~[token_tree] { (*self).clone() } } @@ -56,7 +56,7 @@ pub mod rt { pub fn to_source() -> ~str; // If you can make source, you can definitely make tokens. - pub fn to_tokens(cx: @ExtCtxt) -> ~[token_tree] { + pub fn to_tokens(cx: &ExtCtxt) -> ~[token_tree] { cx.parse_tts(self.to_source()) } } @@ -199,7 +199,7 @@ pub mod rt { macro_rules! impl_to_tokens( ($t:ty) => ( impl ToTokens for $t { - fn to_tokens(&self, cx: @ExtCtxt) -> ~[token_tree] { + fn to_tokens(&self, cx: &ExtCtxt) -> ~[token_tree] { cx.parse_tts(self.to_source()) } } @@ -209,7 +209,7 @@ pub mod rt { macro_rules! impl_to_tokens_self( ($t:ty) => ( impl<'a> ToTokens for $t { - fn to_tokens(&self, cx: @ExtCtxt) -> ~[token_tree] { + fn to_tokens(&self, cx: &ExtCtxt) -> ~[token_tree] { cx.parse_tts(self.to_source()) } } @@ -289,7 +289,7 @@ pub mod rt { } -pub fn expand_quote_tokens(cx: @ExtCtxt, +pub fn expand_quote_tokens(cx: &ExtCtxt, sp: Span, tts: &[ast::token_tree]) -> base::MacResult { let (cx_expr, expr) = expand_tts(cx, sp, tts); @@ -297,14 +297,14 @@ pub fn expand_quote_tokens(cx: @ExtCtxt, base::MRExpr(expanded) } -pub fn expand_quote_expr(cx: @ExtCtxt, +pub fn expand_quote_expr(cx: &ExtCtxt, sp: Span, tts: &[ast::token_tree]) -> base::MacResult { let expanded = expand_parse_call(cx, sp, "parse_expr", ~[], tts); base::MRExpr(expanded) } -pub fn expand_quote_item(cx: @ExtCtxt, +pub fn expand_quote_item(cx: &ExtCtxt, sp: Span, tts: &[ast::token_tree]) -> base::MacResult { let e_attrs = cx.expr_vec_uniq(sp, ~[]); @@ -313,7 +313,7 @@ pub fn expand_quote_item(cx: @ExtCtxt, base::MRExpr(expanded) } -pub fn expand_quote_pat(cx: @ExtCtxt, +pub fn expand_quote_pat(cx: &ExtCtxt, sp: Span, tts: &[ast::token_tree]) -> base::MacResult { let e_refutable = cx.expr_lit(sp, ast::lit_bool(true)); @@ -322,7 +322,7 @@ pub fn expand_quote_pat(cx: @ExtCtxt, base::MRExpr(expanded) } -pub fn expand_quote_ty(cx: @ExtCtxt, +pub fn expand_quote_ty(cx: &ExtCtxt, sp: Span, tts: &[ast::token_tree]) -> base::MacResult { let e_param_colons = cx.expr_lit(sp, ast::lit_bool(false)); @@ -331,7 +331,7 @@ pub fn expand_quote_ty(cx: @ExtCtxt, base::MRExpr(expanded) } -pub fn expand_quote_stmt(cx: @ExtCtxt, +pub fn expand_quote_stmt(cx: &ExtCtxt, sp: Span, tts: &[ast::token_tree]) -> base::MacResult { let e_attrs = cx.expr_vec_uniq(sp, ~[]); @@ -349,7 +349,7 @@ fn id_ext(str: &str) -> ast::Ident { } // Lift an ident to the expr that evaluates to that ident. -fn mk_ident(cx: @ExtCtxt, sp: Span, ident: ast::Ident) -> @ast::Expr { +fn mk_ident(cx: &ExtCtxt, sp: Span, ident: ast::Ident) -> @ast::Expr { let e_str = cx.expr_str(sp, cx.str_of(ident)); cx.expr_method_call(sp, cx.expr_ident(sp, id_ext("ext_cx")), @@ -357,7 +357,7 @@ fn mk_ident(cx: @ExtCtxt, sp: Span, ident: ast::Ident) -> @ast::Expr { ~[e_str]) } -fn mk_binop(cx: @ExtCtxt, sp: Span, bop: token::binop) -> @ast::Expr { +fn mk_binop(cx: &ExtCtxt, sp: Span, bop: token::binop) -> @ast::Expr { let name = match bop { PLUS => "PLUS", MINUS => "MINUS", @@ -373,7 +373,7 @@ fn mk_binop(cx: @ExtCtxt, sp: Span, bop: token::binop) -> @ast::Expr { cx.expr_ident(sp, id_ext(name)) } -fn mk_token(cx: @ExtCtxt, sp: Span, tok: &token::Token) -> @ast::Expr { +fn mk_token(cx: &ExtCtxt, sp: Span, tok: &token::Token) -> @ast::Expr { match *tok { BINOP(binop) => { @@ -528,7 +528,7 @@ fn mk_token(cx: @ExtCtxt, sp: Span, tok: &token::Token) -> @ast::Expr { } -fn mk_tt(cx: @ExtCtxt, sp: Span, tt: &ast::token_tree) +fn mk_tt(cx: &ExtCtxt, sp: Span, tt: &ast::token_tree) -> ~[@ast::Stmt] { match *tt { @@ -570,7 +570,7 @@ fn mk_tt(cx: @ExtCtxt, sp: Span, tt: &ast::token_tree) } } -fn mk_tts(cx: @ExtCtxt, sp: Span, tts: &[ast::token_tree]) +fn mk_tts(cx: &ExtCtxt, sp: Span, tts: &[ast::token_tree]) -> ~[@ast::Stmt] { let mut ss = ~[]; for tt in tts.iter() { @@ -579,7 +579,7 @@ fn mk_tts(cx: @ExtCtxt, sp: Span, tts: &[ast::token_tree]) ss } -fn expand_tts(cx: @ExtCtxt, +fn expand_tts(cx: &ExtCtxt, sp: Span, tts: &[ast::token_tree]) -> (@ast::Expr, @ast::Expr) { @@ -652,7 +652,7 @@ fn expand_tts(cx: @ExtCtxt, (cx_expr, block) } -fn expand_wrapper(cx: @ExtCtxt, +fn expand_wrapper(cx: &ExtCtxt, sp: Span, cx_expr: @ast::Expr, expr: @ast::Expr) -> @ast::Expr { @@ -667,7 +667,7 @@ fn expand_wrapper(cx: @ExtCtxt, cx.expr_block(cx.block_all(sp, uses, ~[stmt_let_ext_cx], Some(expr))) } -fn expand_parse_call(cx: @ExtCtxt, +fn expand_parse_call(cx: &ExtCtxt, sp: Span, parse_method: &str, arg_exprs: ~[@ast::Expr], diff --git a/src/libsyntax/ext/source_util.rs b/src/libsyntax/ext/source_util.rs index 2fcf6f1cad025..cf11bdcce64fa 100644 --- a/src/libsyntax/ext/source_util.rs +++ b/src/libsyntax/ext/source_util.rs @@ -28,7 +28,7 @@ use std::str; // a given file into the current one. /* line!(): expands to the current line number */ -pub fn expand_line(cx: @ExtCtxt, sp: Span, tts: &[ast::token_tree]) +pub fn expand_line(cx: &ExtCtxt, sp: Span, tts: &[ast::token_tree]) -> base::MacResult { base::check_zero_tts(cx, sp, tts, "line!"); @@ -39,7 +39,7 @@ pub fn expand_line(cx: @ExtCtxt, sp: Span, tts: &[ast::token_tree]) } /* col!(): expands to the current column number */ -pub fn expand_col(cx: @ExtCtxt, sp: Span, tts: &[ast::token_tree]) +pub fn expand_col(cx: &ExtCtxt, sp: Span, tts: &[ast::token_tree]) -> base::MacResult { base::check_zero_tts(cx, sp, tts, "col!"); @@ -51,7 +51,7 @@ pub fn expand_col(cx: @ExtCtxt, sp: Span, tts: &[ast::token_tree]) /* file!(): expands to the current filename */ /* The filemap (`loc.file`) contains a bunch more information we could spit * out if we wanted. */ -pub fn expand_file(cx: @ExtCtxt, sp: Span, tts: &[ast::token_tree]) +pub fn expand_file(cx: &ExtCtxt, sp: Span, tts: &[ast::token_tree]) -> base::MacResult { base::check_zero_tts(cx, sp, tts, "file!"); @@ -61,13 +61,13 @@ pub fn expand_file(cx: @ExtCtxt, sp: Span, tts: &[ast::token_tree]) base::MRExpr(cx.expr_str(topmost.call_site, filename)) } -pub fn expand_stringify(cx: @ExtCtxt, sp: Span, tts: &[ast::token_tree]) +pub fn expand_stringify(cx: &ExtCtxt, sp: Span, tts: &[ast::token_tree]) -> base::MacResult { let s = pprust::tts_to_str(tts, get_ident_interner()); base::MRExpr(cx.expr_str(sp, s.to_managed())) } -pub fn expand_mod(cx: @ExtCtxt, sp: Span, tts: &[ast::token_tree]) +pub fn expand_mod(cx: &ExtCtxt, sp: Span, tts: &[ast::token_tree]) -> base::MacResult { base::check_zero_tts(cx, sp, tts, "module_path!"); base::MRExpr(cx.expr_str(sp, @@ -77,7 +77,7 @@ pub fn expand_mod(cx: @ExtCtxt, sp: Span, tts: &[ast::token_tree]) // include! : parse the given file as an expr // This is generally a bad idea because it's going to behave // unhygienically. -pub fn expand_include(cx: @ExtCtxt, sp: Span, tts: &[ast::token_tree]) +pub fn expand_include(cx: &ExtCtxt, sp: Span, tts: &[ast::token_tree]) -> base::MacResult { let file = get_single_str_from_tts(cx, sp, tts, "include!"); // The file will be added to the code map by the parser @@ -88,7 +88,7 @@ pub fn expand_include(cx: @ExtCtxt, sp: Span, tts: &[ast::token_tree]) } // include_str! : read the given file, insert it as a literal string expr -pub fn expand_include_str(cx: @ExtCtxt, sp: Span, tts: &[ast::token_tree]) +pub fn expand_include_str(cx: &ExtCtxt, sp: Span, tts: &[ast::token_tree]) -> base::MacResult { let file = get_single_str_from_tts(cx, sp, tts, "include_str!"); let file = res_rel_file(cx, sp, &Path::new(file)); @@ -120,7 +120,7 @@ pub fn expand_include_str(cx: @ExtCtxt, sp: Span, tts: &[ast::token_tree]) } } -pub fn expand_include_bin(cx: @ExtCtxt, sp: Span, tts: &[ast::token_tree]) +pub fn expand_include_bin(cx: &ExtCtxt, sp: Span, tts: &[ast::token_tree]) -> base::MacResult { use std::at_vec; @@ -167,7 +167,7 @@ fn topmost_expn_info(expn_info: @codemap::ExpnInfo) -> @codemap::ExpnInfo { // resolve a file-system path to an absolute file-system path (if it // isn't already) -fn res_rel_file(cx: @ExtCtxt, sp: codemap::Span, arg: &Path) -> Path { +fn res_rel_file(cx: &ExtCtxt, sp: codemap::Span, arg: &Path) -> Path { // NB: relative paths are resolved relative to the compilation unit if !arg.is_absolute() { let mut cu = Path::new(cx.codemap().span_to_filename(sp)); diff --git a/src/libsyntax/ext/trace_macros.rs b/src/libsyntax/ext/trace_macros.rs index 1862d4a88fd4a..cd172495d705b 100644 --- a/src/libsyntax/ext/trace_macros.rs +++ b/src/libsyntax/ext/trace_macros.rs @@ -16,7 +16,7 @@ use parse::lexer::{new_tt_reader, reader}; use parse::parser::Parser; use parse::token::keywords; -pub fn expand_trace_macros(cx: @ExtCtxt, +pub fn expand_trace_macros(cx: &ExtCtxt, sp: Span, tt: &[ast::token_tree]) -> base::MacResult { diff --git a/src/libsyntax/ext/tt/macro_rules.rs b/src/libsyntax/ext/tt/macro_rules.rs index 8291a76fab65d..63517ca2269d8 100644 --- a/src/libsyntax/ext/tt/macro_rules.rs +++ b/src/libsyntax/ext/tt/macro_rules.rs @@ -83,7 +83,7 @@ struct MacroRulesSyntaxExpanderTTFun { impl SyntaxExpanderTTTrait for MacroRulesSyntaxExpanderTTFun { fn expand(&self, - cx: @ExtCtxt, + cx: &ExtCtxt, sp: Span, arg: &[ast::token_tree], _: ast::SyntaxContext) @@ -93,7 +93,7 @@ impl SyntaxExpanderTTTrait for MacroRulesSyntaxExpanderTTFun { } // Given `lhses` and `rhses`, this is the new macro we create -fn generic_extension(cx: @ExtCtxt, +fn generic_extension(cx: &ExtCtxt, sp: Span, name: Ident, arg: &[ast::token_tree], @@ -168,7 +168,7 @@ fn generic_extension(cx: @ExtCtxt, // this procedure performs the expansion of the // macro_rules! macro. It parses the RHS and adds // an extension to the current context. -pub fn add_new_extension(cx: @ExtCtxt, +pub fn add_new_extension(cx: &ExtCtxt, sp: Span, name: Ident, arg: ~[ast::token_tree], diff --git a/src/test/run-pass-fulldeps/quote-tokens.rs b/src/test/run-pass-fulldeps/quote-tokens.rs index 2e426c0413e74..dc95faa198647 100644 --- a/src/test/run-pass-fulldeps/quote-tokens.rs +++ b/src/test/run-pass-fulldeps/quote-tokens.rs @@ -16,7 +16,7 @@ extern mod syntax; use syntax::ext::base::ExtCtxt; -fn syntax_extension(cx: @ExtCtxt) { +fn syntax_extension(cx: &ExtCtxt) { let e_toks : ~[syntax::ast::token_tree] = quote_tokens!(cx, 1 + 2); let p_toks : ~[syntax::ast::token_tree] = quote_tokens!(cx, (x, 1 .. 4, *)); From 711d5106bcd5e2b42d59b12f646f406c572def20 Mon Sep 17 00:00:00 2001 From: Steven Fackler Date: Fri, 27 Dec 2013 17:21:15 -0700 Subject: [PATCH 2/6] Remove unecessary extern "Rust" specifiers --- src/libsyntax/ext/base.rs | 25 ++++++++----------------- 1 file changed, 8 insertions(+), 17 deletions(-) diff --git a/src/libsyntax/ext/base.rs b/src/libsyntax/ext/base.rs index 87d09ea3f8ed8..83e4506d2579c 100644 --- a/src/libsyntax/ext/base.rs +++ b/src/libsyntax/ext/base.rs @@ -35,11 +35,8 @@ pub struct MacroDef { ext: SyntaxExtension } -pub type ItemDecorator = extern "Rust" fn(&ExtCtxt, - Span, - @ast::MetaItem, - ~[@ast::item]) - -> ~[@ast::item]; +pub type ItemDecorator = + fn(&ExtCtxt, Span, @ast::MetaItem, ~[@ast::item]) -> ~[@ast::item]; pub struct SyntaxExpanderTT { expander: SyntaxExpanderTTExpander, @@ -56,10 +53,8 @@ pub trait SyntaxExpanderTTTrait { } pub type SyntaxExpanderTTFunNoCtxt = - extern "Rust" fn(ecx: &ExtCtxt, - span: codemap::Span, - token_tree: &[ast::token_tree]) - -> MacResult; + fn(ecx: &ExtCtxt, span: codemap::Span, token_tree: &[ast::token_tree]) + -> MacResult; enum SyntaxExpanderTTExpander { SyntaxExpanderTTExpanderWithoutContext(SyntaxExpanderTTFunNoCtxt), @@ -119,16 +114,12 @@ impl SyntaxExpanderTTItemTrait for SyntaxExpanderTTItem { } } -pub type SyntaxExpanderTTItemFun = extern "Rust" fn(&ExtCtxt, - Span, - ast::Ident, - ~[ast::token_tree], - ast::SyntaxContext) - -> MacResult; +pub type SyntaxExpanderTTItemFun = + fn(&ExtCtxt, Span, ast::Ident, ~[ast::token_tree], ast::SyntaxContext) + -> MacResult; pub type SyntaxExpanderTTItemFunNoCtxt = - extern "Rust" fn(&ExtCtxt, Span, ast::Ident, ~[ast::token_tree]) - -> MacResult; + fn(&ExtCtxt, Span, ast::Ident, ~[ast::token_tree]) -> MacResult; pub trait AnyMacro { fn make_expr(&self) -> @ast::Expr; From 933def408c48e9b20c478cf4cad9ae2d20b3c5df Mon Sep 17 00:00:00 2001 From: Steven Fackler Date: Fri, 27 Dec 2013 17:43:01 -0700 Subject: [PATCH 3/6] Stop passing duplicate parameters in expand --- src/libsyntax/ext/base.rs | 5 +- src/libsyntax/ext/expand.rs | 215 +++++++++++++++--------------------- 2 files changed, 92 insertions(+), 128 deletions(-) diff --git a/src/libsyntax/ext/base.rs b/src/libsyntax/ext/base.rs index 83e4506d2579c..37a1ae6884dda 100644 --- a/src/libsyntax/ext/base.rs +++ b/src/libsyntax/ext/base.rs @@ -324,12 +324,11 @@ impl ExtCtxt { loop { match e.node { ast::ExprMac(..) => { - let extsbox = @mut syntax_expander_table(); let expander = expand::MacroExpander { - extsbox: extsbox, + extsbox: @mut syntax_expander_table(), cx: self, }; - e = expand::expand_expr(extsbox, self, e, &expander); + e = expand::expand_expr(e, &expander); } _ => return e } diff --git a/src/libsyntax/ext/expand.rs b/src/libsyntax/ext/expand.rs index 9f9c245fe481d..31c44b7e5744b 100644 --- a/src/libsyntax/ext/expand.rs +++ b/src/libsyntax/ext/expand.rs @@ -31,11 +31,7 @@ use util::small_vector::SmallVector; use std::vec; -pub fn expand_expr(extsbox: @mut SyntaxEnv, - cx: &ExtCtxt, - e: @ast::Expr, - fld: &MacroExpander) - -> @ast::Expr { +pub fn expand_expr(e: @ast::Expr, fld: &MacroExpander) -> @ast::Expr { match e.node { // expr_mac should really be expr_ext or something; it's the // entry-point for all syntax extensions. @@ -49,7 +45,7 @@ pub fn expand_expr(extsbox: @mut SyntaxEnv, // Token-tree macros: mac_invoc_tt(ref pth, ref tts, ctxt) => { if (pth.segments.len() > 1u) { - cx.span_fatal( + fld.cx.span_fatal( pth.span, format!("expected macro name without module \ separators")); @@ -57,14 +53,14 @@ pub fn expand_expr(extsbox: @mut SyntaxEnv, let extname = &pth.segments[0].identifier; let extnamestr = ident_to_str(extname); // leaving explicit deref here to highlight unbox op: - match (*extsbox).find(&extname.name) { + match (*fld.extsbox).find(&extname.name) { None => { - cx.span_fatal( + fld.cx.span_fatal( pth.span, format!("macro undefined: '{}'", extnamestr)) } Some(@SE(NormalTT(expandfun, exp_span))) => { - cx.bt_push(ExpnInfo { + fld.cx.bt_push(ExpnInfo { call_site: e.span, callee: NameAndSpan { name: extnamestr, @@ -81,17 +77,17 @@ pub fn expand_expr(extsbox: @mut SyntaxEnv, // be the root of the call stack. That's the most // relevant span and it's the actual invocation of // the macro. - let mac_span = original_span(cx); + let mac_span = original_span(fld.cx); let expanded = - match expandfun.expand(cx, + match expandfun.expand(fld.cx, mac_span.call_site, marked_before, marked_ctxt) { MRExpr(e) => e, MRAny(any_macro) => any_macro.make_expr(), _ => { - cx.span_fatal( + fld.cx.span_fatal( pth.span, format!( "non-expr macro in expr pos: {}", @@ -109,7 +105,7 @@ pub fn expand_expr(extsbox: @mut SyntaxEnv, // node here? let fully_expanded = fld.fold_expr(marked_after).node.clone(); - cx.bt_pop(); + fld.cx.bt_pop(); @ast::Expr { id: ast::DUMMY_NODE_ID, @@ -118,7 +114,7 @@ pub fn expand_expr(extsbox: @mut SyntaxEnv, } } _ => { - cx.span_fatal( + fld.cx.span_fatal( pth.span, format!("'{}' is not a tt-style macro", extnamestr) ) @@ -152,46 +148,47 @@ pub fn expand_expr(extsbox: @mut SyntaxEnv, // } let local_ident = token::gensym_ident("i"); - let next_ident = cx.ident_of("next"); - let none_ident = cx.ident_of("None"); + let next_ident = fld.cx.ident_of("next"); + let none_ident = fld.cx.ident_of("None"); - let local_path = cx.path_ident(span, local_ident); - let some_path = cx.path_ident(span, cx.ident_of("Some")); + let local_path = fld.cx.path_ident(span, local_ident); + let some_path = fld.cx.path_ident(span, fld.cx.ident_of("Some")); // `let i = &mut ` - let iter_decl_stmt = cx.stmt_let(span, false, local_ident, - cx.expr_mut_addr_of(span, src_expr)); + let iter_decl_stmt = fld.cx.stmt_let(span, false, local_ident, + fld.cx.expr_mut_addr_of(span, src_expr)); // `None => break ['];` let none_arm = { // FIXME #6993: this map goes away: - let break_expr = cx.expr(span, ast::ExprBreak(opt_ident.map(|x| x.name))); - let none_pat = cx.pat_ident(span, none_ident); - cx.arm(span, ~[none_pat], break_expr) + let break_expr = fld.cx.expr(span, ast::ExprBreak(opt_ident.map(|x| x.name))); + let none_pat = fld.cx.pat_ident(span, none_ident); + fld.cx.arm(span, ~[none_pat], break_expr) }; // `Some() => ` let some_arm = - cx.arm(span, - ~[cx.pat_enum(span, some_path, ~[src_pat])], - cx.expr_block(src_loop_block)); + fld.cx.arm(span, + ~[fld.cx.pat_enum(span, some_path, ~[src_pat])], + fld.cx.expr_block(src_loop_block)); // `match i.next() { ... }` let match_expr = { let next_call_expr = - cx.expr_method_call(span, cx.expr_path(local_path), next_ident, ~[]); + fld.cx.expr_method_call(span, fld.cx.expr_path(local_path), next_ident, ~[]); - cx.expr_match(span, next_call_expr, ~[none_arm, some_arm]) + fld.cx.expr_match(span, next_call_expr, ~[none_arm, some_arm]) }; // ['ident:] loop { ... } - let loop_expr = cx.expr(span, - ast::ExprLoop(cx.block_expr(match_expr), opt_ident)); + let loop_expr = fld.cx.expr(span, + ast::ExprLoop(fld.cx.block_expr(match_expr), + opt_ident)); // `{ let ... ; loop { ... } }` - let block = cx.block(span, - ~[iter_decl_stmt], - Some(loop_expr)); + let block = fld.cx.block(span, + ~[iter_decl_stmt], + Some(loop_expr)); @ast::Expr { id: ast::DUMMY_NODE_ID, @@ -213,11 +210,7 @@ pub fn expand_expr(extsbox: @mut SyntaxEnv, // // NB: there is some redundancy between this and expand_item, below, and // they might benefit from some amount of semantic and language-UI merger. -pub fn expand_mod_items(extsbox: @mut SyntaxEnv, - cx: &ExtCtxt, - module_: &ast::_mod, - fld: &MacroExpander) - -> ast::_mod { +pub fn expand_mod_items(module_: &ast::_mod, fld: &MacroExpander) -> ast::_mod { // Fold the contents first: let module_ = noop_fold_mod(module_, fld); @@ -228,9 +221,9 @@ pub fn expand_mod_items(extsbox: @mut SyntaxEnv, item.attrs.rev_iter().fold(~[*item], |items, attr| { let mname = attr.name(); - match (*extsbox).find(&intern(mname)) { + match (*fld.extsbox).find(&intern(mname)) { Some(@SE(ItemDecorator(dec_fn))) => { - cx.bt_push(ExpnInfo { + fld.cx.bt_push(ExpnInfo { call_site: attr.span, callee: NameAndSpan { name: mname, @@ -238,8 +231,8 @@ pub fn expand_mod_items(extsbox: @mut SyntaxEnv, span: None } }); - let r = dec_fn(cx, attr.span, attr.node.value, items); - cx.bt_pop(); + let r = dec_fn(fld.cx, attr.span, attr.node.value, items); + fld.cx.bt_pop(); r }, _ => items, @@ -270,20 +263,16 @@ macro_rules! with_exts_frame ( static special_block_name : &'static str = " block"; // When we enter a module, record it, for the sake of `module!` -pub fn expand_item(extsbox: @mut SyntaxEnv, - cx: &ExtCtxt, - it: @ast::item, - fld: &MacroExpander) - -> SmallVector<@ast::item> { +pub fn expand_item(it: @ast::item, fld: &MacroExpander) -> SmallVector<@ast::item> { match it.node { - ast::item_mac(..) => expand_item_mac(extsbox, cx, it, fld), + ast::item_mac(..) => expand_item_mac(it, fld), ast::item_mod(_) | ast::item_foreign_mod(_) => { - cx.mod_push(it.ident); + fld.cx.mod_push(it.ident); let macro_escape = contains_macro_escape(it.attrs); - let result = with_exts_frame!(extsbox, + let result = with_exts_frame!(fld.extsbox, macro_escape, noop_fold_item(it, fld)); - cx.mod_pop(); + fld.cx.mod_pop(); result }, _ => noop_fold_item(it, fld) @@ -297,10 +286,7 @@ pub fn contains_macro_escape(attrs: &[ast::Attribute]) -> bool { // Support for item-position macro invocations, exactly the same // logic as for expression-position macro invocations. -pub fn expand_item_mac(extsbox: @mut SyntaxEnv, - cx: &ExtCtxt, - it: @ast::item, - fld: &MacroExpander) +pub fn expand_item_mac(it: @ast::item, fld: &MacroExpander) -> SmallVector<@ast::item> { let (pth, tts, ctxt) = match it.node { item_mac(codemap::Spanned { @@ -309,24 +295,24 @@ pub fn expand_item_mac(extsbox: @mut SyntaxEnv, }) => { (pth, (*tts).clone(), ctxt) } - _ => cx.span_bug(it.span, "invalid item macro invocation") + _ => fld.cx.span_bug(it.span, "invalid item macro invocation") }; let extname = &pth.segments[0].identifier; let extnamestr = ident_to_str(extname); let fm = fresh_mark(); - let expanded = match (*extsbox).find(&extname.name) { - None => cx.span_fatal(pth.span, - format!("macro undefined: '{}!'", extnamestr)), + let expanded = match (*fld.extsbox).find(&extname.name) { + None => fld.cx.span_fatal(pth.span, + format!("macro undefined: '{}!'", extnamestr)), Some(@SE(NormalTT(expander, span))) => { if it.ident.name != parse::token::special_idents::invalid.name { - cx.span_fatal(pth.span, - format!("macro {}! expects no ident argument, \ - given '{}'", extnamestr, - ident_to_str(&it.ident))); + fld.cx.span_fatal(pth.span, + format!("macro {}! expects no ident argument, \ + given '{}'", extnamestr, + ident_to_str(&it.ident))); } - cx.bt_push(ExpnInfo { + fld.cx.bt_push(ExpnInfo { call_site: it.span, callee: NameAndSpan { name: extnamestr, @@ -337,15 +323,15 @@ pub fn expand_item_mac(extsbox: @mut SyntaxEnv, // mark before expansion: let marked_before = mark_tts(tts,fm); let marked_ctxt = new_mark(fm,ctxt); - expander.expand(cx, it.span, marked_before, marked_ctxt) + expander.expand(fld.cx, it.span, marked_before, marked_ctxt) } Some(@SE(IdentTT(expander, span))) => { if it.ident.name == parse::token::special_idents::invalid.name { - cx.span_fatal(pth.span, - format!("macro {}! expects an ident argument", - extnamestr)); + fld.cx.span_fatal(pth.span, + format!("macro {}! expects an ident argument", + extnamestr)); } - cx.bt_push(ExpnInfo { + fld.cx.bt_push(ExpnInfo { call_site: it.span, callee: NameAndSpan { name: extnamestr, @@ -356,10 +342,11 @@ pub fn expand_item_mac(extsbox: @mut SyntaxEnv, // mark before expansion: let marked_tts = mark_tts(tts,fm); let marked_ctxt = new_mark(fm,ctxt); - expander.expand(cx, it.span, it.ident, marked_tts, marked_ctxt) + expander.expand(fld.cx, it.span, it.ident, marked_tts, marked_ctxt) } - _ => cx.span_fatal( - it.span, format!("{}! is not legal in item position", extnamestr)) + _ => fld.cx.span_fatal(it.span, + format!("{}! is not legal in item position", + extnamestr)) }; let items = match expanded { @@ -369,7 +356,8 @@ pub fn expand_item_mac(extsbox: @mut SyntaxEnv, .collect() } MRExpr(_) => { - cx.span_fatal(pth.span, format!("expr macro in item position: {}", extnamestr)) + fld.cx.span_fatal(pth.span, format!("expr macro in item position: {}", + extnamestr)) } MRAny(any_macro) => { any_macro.make_items().move_iter() @@ -380,11 +368,11 @@ pub fn expand_item_mac(extsbox: @mut SyntaxEnv, MRDef(ref mdef) => { // yikes... no idea how to apply the mark to this. I'm afraid // we're going to have to wait-and-see on this one. - insert_macro(*extsbox,intern(mdef.name), @SE((*mdef).ext)); + insert_macro(*fld.extsbox,intern(mdef.name), @SE((*mdef).ext)); SmallVector::zero() } }; - cx.bt_pop(); + fld.cx.bt_pop(); return items; } @@ -406,11 +394,7 @@ fn insert_macro(exts: SyntaxEnv, name: ast::Name, transformer: @Transformer) { } // expand a stmt -pub fn expand_stmt(extsbox: @mut SyntaxEnv, - cx: &ExtCtxt, - s: &Stmt, - fld: &MacroExpander) - -> SmallVector<@Stmt> { +pub fn expand_stmt(s: &Stmt, fld: &MacroExpander) -> SmallVector<@Stmt> { // why the copying here and not in expand_expr? // looks like classic changed-in-only-one-place let (pth, tts, semi, ctxt) = match s.node { @@ -421,21 +405,21 @@ pub fn expand_stmt(extsbox: @mut SyntaxEnv, } } } - _ => return expand_non_macro_stmt(*extsbox, s, fld) + _ => return expand_non_macro_stmt(s, fld) }; if (pth.segments.len() > 1u) { - cx.span_fatal(pth.span, - "expected macro name without module separators"); + fld.cx.span_fatal(pth.span, + "expected macro name without module separators"); } let extname = &pth.segments[0].identifier; let extnamestr = ident_to_str(extname); - let fully_expanded: SmallVector<@Stmt> = match (*extsbox).find(&extname.name) { + let fully_expanded: SmallVector<@Stmt> = match (*fld.extsbox).find(&extname.name) { None => { - cx.span_fatal(pth.span, format!("macro undefined: '{}'", extnamestr)) + fld.cx.span_fatal(pth.span, format!("macro undefined: '{}'", extnamestr)) } Some(@SE(NormalTT(expandfun, exp_span))) => { - cx.bt_push(ExpnInfo { + fld.cx.bt_push(ExpnInfo { call_site: s.span, callee: NameAndSpan { name: extnamestr, @@ -450,9 +434,9 @@ pub fn expand_stmt(extsbox: @mut SyntaxEnv, // See the comment in expand_expr for why we want the original span, // not the current mac.span. - let mac_span = original_span(cx); + let mac_span = original_span(fld.cx); - let expanded = match expandfun.expand(cx, + let expanded = match expandfun.expand(fld.cx, mac_span.call_site, marked_tts, marked_ctxt) { @@ -463,7 +447,7 @@ pub fn expand_stmt(extsbox: @mut SyntaxEnv, } } MRAny(any_macro) => any_macro.make_stmt(), - _ => cx.span_fatal( + _ => fld.cx.span_fatal( pth.span, format!("non-stmt macro in stmt pos: {}", extnamestr)) }; @@ -472,18 +456,19 @@ pub fn expand_stmt(extsbox: @mut SyntaxEnv, // Keep going, outside-in. let fully_expanded = fld.fold_stmt(marked_after); if fully_expanded.is_empty() { - cx.span_fatal(pth.span, + fld.cx.span_fatal(pth.span, "macro didn't expand to a statement"); } - cx.bt_pop(); + fld.cx.bt_pop(); fully_expanded.move_iter() .map(|s| @Spanned { span: s.span, node: s.node.clone() }) .collect() } _ => { - cx.span_fatal(pth.span, - format!("'{}' is not a tt-style macro", extnamestr)) + fld.cx.span_fatal(pth.span, + format!("'{}' is not a tt-style macro", + extnamestr)) } }; @@ -502,7 +487,7 @@ pub fn expand_stmt(extsbox: @mut SyntaxEnv, // expand a non-macro stmt. this is essentially the fallthrough for // expand_stmt, above. -fn expand_non_macro_stmt(exts: SyntaxEnv, s: &Stmt, fld: &MacroExpander) +fn expand_non_macro_stmt(s: &Stmt, fld: &MacroExpander) -> SmallVector<@Stmt> { // is it a let? match s.node { @@ -511,7 +496,7 @@ fn expand_non_macro_stmt(exts: SyntaxEnv, s: &Stmt, fld: &MacroExpander) span: stmt_span }, node_id) => { - let block_info = get_block_info(exts); + let block_info = get_block_info(*fld.extsbox); let pending_renames = block_info.pending_renames; // take it apart: @@ -624,20 +609,15 @@ pub fn new_name_finder(idents: ~[ast::Ident]) -> NewNameFinderContext { } // expand a block. pushes a new exts_frame, then calls expand_block_elts -pub fn expand_block(extsbox: @mut SyntaxEnv, - _: &ExtCtxt, - blk: &Block, - fld: &MacroExpander) - -> P { +pub fn expand_block(blk: &Block, fld: &MacroExpander) -> P { // see note below about treatment of exts table - with_exts_frame!(extsbox,false, - expand_block_elts(*extsbox, blk, fld)) + with_exts_frame!(fld.extsbox,false, + expand_block_elts(blk, fld)) } // expand the elements of a block. -pub fn expand_block_elts(exts: SyntaxEnv, b: &Block, fld: &MacroExpander) - -> P { - let block_info = get_block_info(exts); +pub fn expand_block_elts(b: &Block, fld: &MacroExpander) -> P { + let block_info = get_block_info(*fld.extsbox); let pending_renames = block_info.pending_renames; let rename_fld = renames_to_fold(pending_renames); let new_view_items = b.view_items.map(|x| fld.fold_view_item(x)); @@ -956,38 +936,23 @@ pub struct MacroExpander<'a> { impl<'a> ast_fold for MacroExpander<'a> { fn fold_expr(&self, expr: @ast::Expr) -> @ast::Expr { - expand_expr(self.extsbox, - self.cx, - expr, - self) + expand_expr(expr, self) } fn fold_mod(&self, module: &ast::_mod) -> ast::_mod { - expand_mod_items(self.extsbox, - self.cx, - module, - self) + expand_mod_items(module, self) } fn fold_item(&self, item: @ast::item) -> SmallVector<@ast::item> { - expand_item(self.extsbox, - self.cx, - item, - self) + expand_item(item, self) } fn fold_stmt(&self, stmt: &ast::Stmt) -> SmallVector<@ast::Stmt> { - expand_stmt(self.extsbox, - self.cx, - stmt, - self) + expand_stmt(stmt, self) } fn fold_block(&self, block: P) -> P { - expand_block(self.extsbox, - self.cx, - block, - self) + expand_block(block, self) } fn new_span(&self, span: Span) -> Span { From 3965dddf49aa30a69614e9f42a73960f1be4790e Mon Sep 17 00:00:00 2001 From: Steven Fackler Date: Fri, 27 Dec 2013 20:34:51 -0700 Subject: [PATCH 4/6] Make ast_fold take &mut self --- src/librustc/front/assign_node_ids.rs | 4 +- src/librustc/front/config.rs | 18 ++-- src/librustc/front/std_inject.rs | 8 +- src/librustc/front/test.rs | 8 +- src/librustc/middle/astencode.rs | 10 +- src/librustpkg/util.rs | 10 +- src/libsyntax/ext/base.rs | 4 +- src/libsyntax/ext/build.rs | 4 +- src/libsyntax/ext/expand.rs | 67 +++++++------- src/libsyntax/fold.rs | 128 +++++++++++++------------- 10 files changed, 131 insertions(+), 130 deletions(-) diff --git a/src/librustc/front/assign_node_ids.rs b/src/librustc/front/assign_node_ids.rs index 3d0b32fcee03a..74e8e0de1d8d0 100644 --- a/src/librustc/front/assign_node_ids.rs +++ b/src/librustc/front/assign_node_ids.rs @@ -18,14 +18,14 @@ struct NodeIdAssigner { } impl ast_fold for NodeIdAssigner { - fn new_id(&self, old_id: ast::NodeId) -> ast::NodeId { + fn new_id(&mut self, old_id: ast::NodeId) -> ast::NodeId { assert_eq!(old_id, ast::DUMMY_NODE_ID); self.sess.next_node_id() } } pub fn assign_node_ids(sess: Session, crate: ast::Crate) -> ast::Crate { - let fold = NodeIdAssigner { + let mut fold = NodeIdAssigner { sess: sess, }; fold.fold_crate(crate) diff --git a/src/librustc/front/config.rs b/src/librustc/front/config.rs index 6694bee582fbe..58e7963ca9097 100644 --- a/src/librustc/front/config.rs +++ b/src/librustc/front/config.rs @@ -24,17 +24,17 @@ pub fn strip_unconfigured_items(crate: ast::Crate) -> ast::Crate { } impl<'a> fold::ast_fold for Context<'a> { - fn fold_mod(&self, module: &ast::_mod) -> ast::_mod { + fn fold_mod(&mut self, module: &ast::_mod) -> ast::_mod { fold_mod(self, module) } - fn fold_block(&self, block: ast::P) -> ast::P { + fn fold_block(&mut self, block: ast::P) -> ast::P { fold_block(self, block) } - fn fold_foreign_mod(&self, foreign_module: &ast::foreign_mod) + fn fold_foreign_mod(&mut self, foreign_module: &ast::foreign_mod) -> ast::foreign_mod { fold_foreign_mod(self, foreign_module) } - fn fold_item_underscore(&self, item: &ast::item_) -> ast::item_ { + fn fold_item_underscore(&mut self, item: &ast::item_) -> ast::item_ { fold_item_underscore(self, item) } } @@ -42,7 +42,7 @@ impl<'a> fold::ast_fold for Context<'a> { pub fn strip_items(crate: ast::Crate, in_cfg: |attrs: &[ast::Attribute]| -> bool) -> ast::Crate { - let ctxt = Context { + let mut ctxt = Context { in_cfg: in_cfg, }; ctxt.fold_crate(crate) @@ -57,7 +57,7 @@ fn filter_view_item<'r>(cx: &Context, view_item: &'r ast::view_item) } } -fn fold_mod(cx: &Context, m: &ast::_mod) -> ast::_mod { +fn fold_mod(cx: &mut Context, m: &ast::_mod) -> ast::_mod { let filtered_items = m.items.iter() .filter(|&a| item_in_cfg(cx, *a)) .flat_map(|&x| cx.fold_item(x).move_iter()) @@ -80,7 +80,7 @@ fn filter_foreign_item(cx: &Context, item: @ast::foreign_item) } } -fn fold_foreign_mod(cx: &Context, nm: &ast::foreign_mod) -> ast::foreign_mod { +fn fold_foreign_mod(cx: &mut Context, nm: &ast::foreign_mod) -> ast::foreign_mod { let filtered_items = nm.items .iter() .filter_map(|a| filter_foreign_item(cx, *a)) @@ -95,7 +95,7 @@ fn fold_foreign_mod(cx: &Context, nm: &ast::foreign_mod) -> ast::foreign_mod { } } -fn fold_item_underscore(cx: &Context, item: &ast::item_) -> ast::item_ { +fn fold_item_underscore(cx: &mut Context, item: &ast::item_) -> ast::item_ { let item = match *item { ast::item_impl(ref a, ref b, c, ref methods) => { let methods = methods.iter().filter(|m| method_in_cfg(cx, **m)) @@ -129,7 +129,7 @@ fn retain_stmt(cx: &Context, stmt: @ast::Stmt) -> bool { } } -fn fold_block(cx: &Context, b: ast::P) -> ast::P { +fn fold_block(cx: &mut Context, b: ast::P) -> ast::P { let resulting_stmts = b.stmts.iter() .filter(|&a| retain_stmt(cx, *a)) .flat_map(|&stmt| cx.fold_stmt(stmt).move_iter()) diff --git a/src/librustc/front/std_inject.rs b/src/librustc/front/std_inject.rs index 03f9a3459a3a9..13bf867e06be1 100644 --- a/src/librustc/front/std_inject.rs +++ b/src/librustc/front/std_inject.rs @@ -56,7 +56,7 @@ struct StandardLibraryInjector { } impl fold::ast_fold for StandardLibraryInjector { - fn fold_crate(&self, crate: ast::Crate) -> ast::Crate { + fn fold_crate(&mut self, crate: ast::Crate) -> ast::Crate { let version = STD_VERSION.to_managed(); let vers_item = attr::mk_name_value_item_str(@"vers", version); let mut vis = ~[ast::view_item { @@ -108,7 +108,7 @@ impl fold::ast_fold for StandardLibraryInjector { } } - fn fold_item(&self, item: @ast::item) -> SmallVector<@ast::item> { + fn fold_item(&mut self, item: @ast::item) -> SmallVector<@ast::item> { if !no_prelude(item.attrs) { // only recur if there wasn't `#[no_implicit_prelude];` // on this item, i.e. this means that the prelude is not @@ -119,7 +119,7 @@ impl fold::ast_fold for StandardLibraryInjector { } } - fn fold_mod(&self, module: &ast::_mod) -> ast::_mod { + fn fold_mod(&mut self, module: &ast::_mod) -> ast::_mod { let prelude_path = ast::Path { span: dummy_sp(), global: false, @@ -158,7 +158,7 @@ impl fold::ast_fold for StandardLibraryInjector { } fn inject_libstd_ref(sess: Session, crate: ast::Crate) -> ast::Crate { - let fold = StandardLibraryInjector { + let mut fold = StandardLibraryInjector { sess: sess, }; fold.fold_crate(crate) diff --git a/src/librustc/front/test.rs b/src/librustc/front/test.rs index ed899561bc919..764935d530553 100644 --- a/src/librustc/front/test.rs +++ b/src/librustc/front/test.rs @@ -67,7 +67,7 @@ struct TestHarnessGenerator { } impl fold::ast_fold for TestHarnessGenerator { - fn fold_crate(&self, c: ast::Crate) -> ast::Crate { + fn fold_crate(&mut self, c: ast::Crate) -> ast::Crate { let folded = fold::noop_fold_crate(c, self); // Add a special __test module to the crate that will contain code @@ -78,7 +78,7 @@ impl fold::ast_fold for TestHarnessGenerator { } } - fn fold_item(&self, i: @ast::item) -> SmallVector<@ast::item> { + fn fold_item(&mut self, i: @ast::item) -> SmallVector<@ast::item> { { let mut path = self.cx.path.borrow_mut(); path.get().push(i.ident); @@ -122,7 +122,7 @@ impl fold::ast_fold for TestHarnessGenerator { res } - fn fold_mod(&self, m: &ast::_mod) -> ast::_mod { + fn fold_mod(&mut self, m: &ast::_mod) -> ast::_mod { // Remove any #[main] from the AST so it doesn't clash with // the one we're going to add. Only if compiling an executable. @@ -172,7 +172,7 @@ fn generate_test_harness(sess: session::Session, crate: ast::Crate) } }); - let fold = TestHarnessGenerator { + let mut fold = TestHarnessGenerator { cx: cx }; let res = fold.fold_crate(crate); diff --git a/src/librustc/middle/astencode.rs b/src/librustc/middle/astencode.rs index 3560a930237b9..90825289ac39f 100644 --- a/src/librustc/middle/astencode.rs +++ b/src/librustc/middle/astencode.rs @@ -301,7 +301,7 @@ struct NestedItemsDropper { } impl fold::ast_fold for NestedItemsDropper { - fn fold_block(&self, blk: ast::P) -> ast::P { + fn fold_block(&mut self, blk: ast::P) -> ast::P { let stmts_sans_items = blk.stmts.iter().filter_map(|stmt| { match stmt.node { ast::StmtExpr(_, _) | ast::StmtSemi(_, _) | @@ -340,7 +340,7 @@ impl fold::ast_fold for NestedItemsDropper { // nested items, as otherwise it would get confused when translating // inlined items. fn simplify_ast(ii: &ast::inlined_item) -> ast::inlined_item { - let fld = NestedItemsDropper { + let mut fld = NestedItemsDropper { contents: (), }; @@ -365,17 +365,17 @@ struct AstRenumberer { } impl fold::ast_fold for AstRenumberer { - fn new_id(&self, id: ast::NodeId) -> ast::NodeId { + fn new_id(&mut self, id: ast::NodeId) -> ast::NodeId { self.xcx.tr_id(id) } - fn new_span(&self, span: Span) -> Span { + fn new_span(&mut self, span: Span) -> Span { self.xcx.tr_span(span) } } fn renumber_ast(xcx: @ExtendedDecodeContext, ii: ast::inlined_item) -> ast::inlined_item { - let fld = AstRenumberer { + let mut fld = AstRenumberer { xcx: xcx, }; match ii { diff --git a/src/librustpkg/util.rs b/src/librustpkg/util.rs index b8d01f56e4db5..17b46b883b37f 100644 --- a/src/librustpkg/util.rs +++ b/src/librustpkg/util.rs @@ -80,7 +80,7 @@ struct ReadyCtx { fns: ~[ListenerFn] } -fn fold_mod(_ctx: @mut ReadyCtx, m: &ast::_mod, fold: &CrateSetup) +fn fold_mod(_ctx: @mut ReadyCtx, m: &ast::_mod, fold: &mut CrateSetup) -> ast::_mod { fn strip_main(item: @ast::item) -> @ast::item { @ast::item { @@ -101,7 +101,7 @@ fn fold_mod(_ctx: @mut ReadyCtx, m: &ast::_mod, fold: &CrateSetup) }, fold) } -fn fold_item(ctx: @mut ReadyCtx, item: @ast::item, fold: &CrateSetup) +fn fold_item(ctx: @mut ReadyCtx, item: @ast::item, fold: &mut CrateSetup) -> SmallVector<@ast::item> { ctx.path.push(item.ident); @@ -145,10 +145,10 @@ struct CrateSetup { } impl fold::ast_fold for CrateSetup { - fn fold_item(&self, item: @ast::item) -> SmallVector<@ast::item> { + fn fold_item(&mut self, item: @ast::item) -> SmallVector<@ast::item> { fold_item(self.ctx, item, self) } - fn fold_mod(&self, module: &ast::_mod) -> ast::_mod { + fn fold_mod(&mut self, module: &ast::_mod) -> ast::_mod { fold_mod(self.ctx, module, self) } } @@ -162,7 +162,7 @@ pub fn ready_crate(sess: session::Session, path: ~[], fns: ~[] }; - let fold = CrateSetup { + let mut fold = CrateSetup { ctx: ctx, }; fold.fold_crate(crate) diff --git a/src/libsyntax/ext/base.rs b/src/libsyntax/ext/base.rs index 37a1ae6884dda..842f2bfdffc9e 100644 --- a/src/libsyntax/ext/base.rs +++ b/src/libsyntax/ext/base.rs @@ -324,11 +324,11 @@ impl ExtCtxt { loop { match e.node { ast::ExprMac(..) => { - let expander = expand::MacroExpander { + let mut expander = expand::MacroExpander { extsbox: @mut syntax_expander_table(), cx: self, }; - e = expand::expand_expr(e, &expander); + e = expand::expand_expr(e, &mut expander); } _ => return e } diff --git a/src/libsyntax/ext/build.rs b/src/libsyntax/ext/build.rs index 007c06938c60e..f5c3c1792c96c 100644 --- a/src/libsyntax/ext/build.rs +++ b/src/libsyntax/ext/build.rs @@ -908,7 +908,7 @@ struct Duplicator<'a> { } impl<'a> ast_fold for Duplicator<'a> { - fn new_id(&self, _: NodeId) -> NodeId { + fn new_id(&mut self, _: NodeId) -> NodeId { ast::DUMMY_NODE_ID } } @@ -925,7 +925,7 @@ pub trait Duplicate { impl Duplicate for @ast::Expr { fn duplicate(&self, cx: &ExtCtxt) -> @ast::Expr { - let folder = Duplicator { + let mut folder = Duplicator { cx: cx, }; folder.fold_expr(*self) diff --git a/src/libsyntax/ext/expand.rs b/src/libsyntax/ext/expand.rs index 31c44b7e5744b..e20161ea2d093 100644 --- a/src/libsyntax/ext/expand.rs +++ b/src/libsyntax/ext/expand.rs @@ -31,7 +31,7 @@ use util::small_vector::SmallVector; use std::vec; -pub fn expand_expr(e: @ast::Expr, fld: &MacroExpander) -> @ast::Expr { +pub fn expand_expr(e: @ast::Expr, fld: &mut MacroExpander) -> @ast::Expr { match e.node { // expr_mac should really be expr_ext or something; it's the // entry-point for all syntax extensions. @@ -210,7 +210,7 @@ pub fn expand_expr(e: @ast::Expr, fld: &MacroExpander) -> @ast::Expr { // // NB: there is some redundancy between this and expand_item, below, and // they might benefit from some amount of semantic and language-UI merger. -pub fn expand_mod_items(module_: &ast::_mod, fld: &MacroExpander) -> ast::_mod { +pub fn expand_mod_items(module_: &ast::_mod, fld: &mut MacroExpander) -> ast::_mod { // Fold the contents first: let module_ = noop_fold_mod(module_, fld); @@ -263,7 +263,8 @@ macro_rules! with_exts_frame ( static special_block_name : &'static str = " block"; // When we enter a module, record it, for the sake of `module!` -pub fn expand_item(it: @ast::item, fld: &MacroExpander) -> SmallVector<@ast::item> { +pub fn expand_item(it: @ast::item, fld: &mut MacroExpander) + -> SmallVector<@ast::item> { match it.node { ast::item_mac(..) => expand_item_mac(it, fld), ast::item_mod(_) | ast::item_foreign_mod(_) => { @@ -286,7 +287,7 @@ pub fn contains_macro_escape(attrs: &[ast::Attribute]) -> bool { // Support for item-position macro invocations, exactly the same // logic as for expression-position macro invocations. -pub fn expand_item_mac(it: @ast::item, fld: &MacroExpander) +pub fn expand_item_mac(it: @ast::item, fld: &mut MacroExpander) -> SmallVector<@ast::item> { let (pth, tts, ctxt) = match it.node { item_mac(codemap::Spanned { @@ -394,7 +395,7 @@ fn insert_macro(exts: SyntaxEnv, name: ast::Name, transformer: @Transformer) { } // expand a stmt -pub fn expand_stmt(s: &Stmt, fld: &MacroExpander) -> SmallVector<@Stmt> { +pub fn expand_stmt(s: &Stmt, fld: &mut MacroExpander) -> SmallVector<@Stmt> { // why the copying here and not in expand_expr? // looks like classic changed-in-only-one-place let (pth, tts, semi, ctxt) = match s.node { @@ -487,7 +488,7 @@ pub fn expand_stmt(s: &Stmt, fld: &MacroExpander) -> SmallVector<@Stmt> { // expand a non-macro stmt. this is essentially the fallthrough for // expand_stmt, above. -fn expand_non_macro_stmt(s: &Stmt, fld: &MacroExpander) +fn expand_non_macro_stmt(s: &Stmt, fld: &mut MacroExpander) -> SmallVector<@Stmt> { // is it a let? match s.node { @@ -521,7 +522,7 @@ fn expand_non_macro_stmt(s: &Stmt, fld: &MacroExpander) let new_name = fresh_name(ident); new_pending_renames.push((*ident,new_name)); } - let rename_fld = renames_to_fold(new_pending_renames); + let mut rename_fld = renames_to_fold(new_pending_renames); // rewrite the pattern using the new names (the old ones // have already been applied): let rewritten_pat = rename_fld.fold_pat(expanded_pat); @@ -609,17 +610,17 @@ pub fn new_name_finder(idents: ~[ast::Ident]) -> NewNameFinderContext { } // expand a block. pushes a new exts_frame, then calls expand_block_elts -pub fn expand_block(blk: &Block, fld: &MacroExpander) -> P { +pub fn expand_block(blk: &Block, fld: &mut MacroExpander) -> P { // see note below about treatment of exts table with_exts_frame!(fld.extsbox,false, expand_block_elts(blk, fld)) } // expand the elements of a block. -pub fn expand_block_elts(b: &Block, fld: &MacroExpander) -> P { +pub fn expand_block_elts(b: &Block, fld: &mut MacroExpander) -> P { let block_info = get_block_info(*fld.extsbox); let pending_renames = block_info.pending_renames; - let rename_fld = renames_to_fold(pending_renames); + let mut rename_fld = renames_to_fold(pending_renames); let new_view_items = b.view_items.map(|x| fld.fold_view_item(x)); let new_stmts = b.stmts.iter() .map(|x| rename_fld.fold_stmt(*x) @@ -651,7 +652,7 @@ struct IdentRenamer { } impl ast_fold for IdentRenamer { - fn fold_ident(&self, id: ast::Ident) -> ast::Ident { + fn fold_ident(&mut self, id: ast::Ident) -> ast::Ident { let new_ctxt = self.renames.iter().fold(id.ctxt, |ctxt, &(from, to)| { new_rename(from, to, ctxt) }); @@ -664,10 +665,10 @@ impl ast_fold for IdentRenamer { // given a mutable list of renames, return a tree-folder that applies those // renames. -pub fn renames_to_fold(renames: @mut ~[(ast::Ident,ast::Name)]) -> @ast_fold { - @IdentRenamer { +pub fn renames_to_fold(renames: @mut ~[(ast::Ident,ast::Name)]) -> IdentRenamer { + IdentRenamer { renames: renames, - } as @ast_fold + } } pub fn new_span(cx: &ExtCtxt, sp: Span) -> Span { @@ -897,7 +898,7 @@ struct Injector { } impl ast_fold for Injector { - fn fold_mod(&self, module: &ast::_mod) -> ast::_mod { + fn fold_mod(&mut self, module: &ast::_mod) -> ast::_mod { // Just inject the standard macros at the start of the first module // in the crate: that is, at the start of the crate file itself. let items = vec::append(~[ self.sm ], module.items); @@ -923,9 +924,9 @@ pub fn inject_std_macros(parse_sess: @mut parse::ParseSess, None => fail!("expected core macros to parse correctly") }; - let injector = @Injector { + let mut injector = Injector { sm: sm, - } as @ast_fold; + }; injector.fold_crate(c) } @@ -935,27 +936,27 @@ pub struct MacroExpander<'a> { } impl<'a> ast_fold for MacroExpander<'a> { - fn fold_expr(&self, expr: @ast::Expr) -> @ast::Expr { + fn fold_expr(&mut self, expr: @ast::Expr) -> @ast::Expr { expand_expr(expr, self) } - fn fold_mod(&self, module: &ast::_mod) -> ast::_mod { + fn fold_mod(&mut self, module: &ast::_mod) -> ast::_mod { expand_mod_items(module, self) } - fn fold_item(&self, item: @ast::item) -> SmallVector<@ast::item> { + fn fold_item(&mut self, item: @ast::item) -> SmallVector<@ast::item> { expand_item(item, self) } - fn fold_stmt(&self, stmt: &ast::Stmt) -> SmallVector<@ast::Stmt> { + fn fold_stmt(&mut self, stmt: &ast::Stmt) -> SmallVector<@ast::Stmt> { expand_stmt(stmt, self) } - fn fold_block(&self, block: P) -> P { + fn fold_block(&mut self, block: P) -> P { expand_block(block, self) } - fn new_span(&self, span: Span) -> Span { + fn new_span(&mut self, span: Span) -> Span { new_span(self.cx, span) } } @@ -970,7 +971,7 @@ pub fn expand_crate(parse_sess: @mut parse::ParseSess, // every method/element of AstFoldFns in fold.rs. let extsbox = syntax_expander_table(); let cx = ExtCtxt::new(parse_sess, cfg.clone()); - let expander = MacroExpander { + let mut expander = MacroExpander { extsbox: @mut extsbox, cx: &cx, }; @@ -1046,7 +1047,7 @@ pub struct ContextWrapper { } impl ast_fold for ContextWrapper { - fn fold_ident(&self, id: ast::Ident) -> ast::Ident { + fn fold_ident(&mut self, id: ast::Ident) -> ast::Ident { let ast::Ident { name, ctxt @@ -1056,7 +1057,7 @@ impl ast_fold for ContextWrapper { ctxt: self.context_function.f(ctxt), } } - fn fold_mac(&self, m: &ast::mac) -> ast::mac { + fn fold_mac(&mut self, m: &ast::mac) -> ast::mac { let macro = match m.node { mac_invoc_tt(ref path, ref tts, ctxt) => { mac_invoc_tt(self.fold_path(path), @@ -1073,24 +1074,24 @@ impl ast_fold for ContextWrapper { // given a function from ctxts to ctxts, produce // an ast_fold that applies that function to all ctxts: -pub fn fun_to_ctxt_folder(cf: @T) -> @ContextWrapper { - @ContextWrapper { +pub fn fun_to_ctxt_folder(cf: @T) -> ContextWrapper { + ContextWrapper { context_function: cf as @CtxtFn, } } // just a convenience: -pub fn new_mark_folder(m: Mrk) -> @ContextWrapper { +pub fn new_mark_folder(m: Mrk) -> ContextWrapper { fun_to_ctxt_folder(@Marker{mark:m}) } -pub fn new_rename_folder(from: ast::Ident, to: ast::Name) -> @ContextWrapper { +pub fn new_rename_folder(from: ast::Ident, to: ast::Name) -> ContextWrapper { fun_to_ctxt_folder(@Renamer{from:from,to:to}) } // apply a given mark to the given token trees. Used prior to expansion of a macro. fn mark_tts(tts : &[token_tree], m : Mrk) -> ~[token_tree] { - fold_tts(tts,new_mark_folder(m)) + fold_tts(tts, &mut new_mark_folder(m)) } // apply a given mark to the given expr. Used following the expansion of a macro. @@ -1295,7 +1296,7 @@ mod test { let ident_str = @"x"; let tts = string_to_tts(ident_str); let fm = fresh_mark(); - let marked_once = fold::fold_tts(tts,new_mark_folder(fm)); + let marked_once = fold::fold_tts(tts,&mut new_mark_folder(fm)); assert_eq!(marked_once.len(),1); let marked_once_ctxt = match marked_once[0] { @@ -1317,7 +1318,7 @@ mod test { let item_ast = string_to_crate(@"fn f() -> int { a }"); let a_name = intern("a"); let a2_name = gensym("a2"); - let renamer = new_rename_folder(ast::Ident{name:a_name,ctxt:EMPTY_CTXT}, + let mut renamer = new_rename_folder(ast::Ident{name:a_name,ctxt:EMPTY_CTXT}, a2_name); let renamed_ast = renamer.fold_crate(item_ast.clone()); let mut path_finder = new_path_finder(~[]); diff --git a/src/libsyntax/fold.rs b/src/libsyntax/fold.rs index a7faeee494e2f..9edde7c9c87ad 100644 --- a/src/libsyntax/fold.rs +++ b/src/libsyntax/fold.rs @@ -17,15 +17,15 @@ use util::small_vector::SmallVector; // We may eventually want to be able to fold over type parameters, too. pub trait ast_fold { - fn fold_crate(&self, c: Crate) -> Crate { + fn fold_crate(&mut self, c: Crate) -> Crate { noop_fold_crate(c, self) } - fn fold_meta_items(&self, meta_items: &[@MetaItem]) -> ~[@MetaItem] { + fn fold_meta_items(&mut self, meta_items: &[@MetaItem]) -> ~[@MetaItem] { meta_items.map(|x| fold_meta_item_(*x, self)) } - fn fold_view_paths(&self, view_paths: &[@view_path]) -> ~[@view_path] { + fn fold_view_paths(&mut self, view_paths: &[@view_path]) -> ~[@view_path] { view_paths.map(|view_path| { let inner_view_path = match view_path.node { view_path_simple(ref ident, ref path, node_id) => { @@ -62,7 +62,7 @@ pub trait ast_fold { }) } - fn fold_view_item(&self, vi: &view_item) -> view_item { + fn fold_view_item(&mut self, vi: &view_item) -> view_item { let inner_view_item = match vi.node { view_item_extern_mod(ref ident, string, @@ -85,7 +85,7 @@ pub trait ast_fold { } } - fn fold_foreign_item(&self, ni: @foreign_item) -> @foreign_item { + fn fold_foreign_item(&mut self, ni: @foreign_item) -> @foreign_item { let fold_attribute = |x| fold_attribute_(x, self); @ast::foreign_item { @@ -114,11 +114,11 @@ pub trait ast_fold { } } - fn fold_item(&self, i: @item) -> SmallVector<@item> { + fn fold_item(&mut self, i: @item) -> SmallVector<@item> { noop_fold_item(i, self) } - fn fold_struct_field(&self, sf: &struct_field) -> struct_field { + fn fold_struct_field(&mut self, sf: &struct_field) -> struct_field { let fold_attribute = |x| fold_attribute_(x, self); Spanned { @@ -132,15 +132,15 @@ pub trait ast_fold { } } - fn fold_item_underscore(&self, i: &item_) -> item_ { + fn fold_item_underscore(&mut self, i: &item_) -> item_ { noop_fold_item_underscore(i, self) } - fn fold_type_method(&self, m: &TypeMethod) -> TypeMethod { + fn fold_type_method(&mut self, m: &TypeMethod) -> TypeMethod { noop_fold_type_method(m, self) } - fn fold_method(&self, m: @method) -> @method { + fn fold_method(&mut self, m: @method) -> @method { @ast::method { ident: self.fold_ident(m.ident), attrs: m.attrs.map(|a| fold_attribute_(*a, self)), @@ -156,15 +156,15 @@ pub trait ast_fold { } } - fn fold_block(&self, b: P) -> P { + fn fold_block(&mut self, b: P) -> P { noop_fold_block(b, self) } - fn fold_stmt(&self, s: &Stmt) -> SmallVector<@Stmt> { + fn fold_stmt(&mut self, s: &Stmt) -> SmallVector<@Stmt> { noop_fold_stmt(s, self) } - fn fold_arm(&self, a: &Arm) -> Arm { + fn fold_arm(&mut self, a: &Arm) -> Arm { Arm { pats: a.pats.map(|x| self.fold_pat(*x)), guard: a.guard.map(|x| self.fold_expr(x)), @@ -172,7 +172,7 @@ pub trait ast_fold { } } - fn fold_pat(&self, p: @Pat) -> @Pat { + fn fold_pat(&mut self, p: @Pat) -> @Pat { let node = match p.node { PatWild => PatWild, PatWildMulti => PatWildMulti, @@ -217,7 +217,7 @@ pub trait ast_fold { } } - fn fold_decl(&self, d: @Decl) -> SmallVector<@Decl> { + fn fold_decl(&mut self, d: @Decl) -> SmallVector<@Decl> { let node = match d.node { DeclLocal(ref l) => SmallVector::one(DeclLocal(self.fold_local(*l))), DeclItem(it) => { @@ -233,11 +233,11 @@ pub trait ast_fold { }).collect() } - fn fold_expr(&self, e: @Expr) -> @Expr { + fn fold_expr(&mut self, e: @Expr) -> @Expr { noop_fold_expr(e, self) } - fn fold_ty(&self, t: P) -> P { + fn fold_ty(&mut self, t: P) -> P { let node = match t.node { ty_nil | ty_bot | ty_infer => t.node.clone(), ty_box(ref mt) => ty_box(fold_mt(mt, self)), @@ -284,11 +284,11 @@ pub trait ast_fold { }) } - fn fold_mod(&self, m: &_mod) -> _mod { + fn fold_mod(&mut self, m: &_mod) -> _mod { noop_fold_mod(m, self) } - fn fold_foreign_mod(&self, nm: &foreign_mod) -> foreign_mod { + fn fold_foreign_mod(&mut self, nm: &foreign_mod) -> foreign_mod { ast::foreign_mod { abis: nm.abis, view_items: nm.view_items @@ -302,7 +302,7 @@ pub trait ast_fold { } } - fn fold_variant(&self, v: &variant) -> P { + fn fold_variant(&mut self, v: &variant) -> P { let kind; match v.node.kind { tuple_variant_kind(ref variant_args) => { @@ -339,11 +339,11 @@ pub trait ast_fold { }) } - fn fold_ident(&self, i: Ident) -> Ident { + fn fold_ident(&mut self, i: Ident) -> Ident { i } - fn fold_path(&self, p: &Path) -> Path { + fn fold_path(&mut self, p: &Path) -> Path { ast::Path { span: self.new_span(p.span), global: p.global, @@ -355,7 +355,7 @@ pub trait ast_fold { } } - fn fold_local(&self, l: @Local) -> @Local { + fn fold_local(&mut self, l: @Local) -> @Local { @Local { ty: self.fold_ty(l.ty), pat: self.fold_pat(l.pat), @@ -365,7 +365,7 @@ pub trait ast_fold { } } - fn fold_mac(&self, macro: &mac) -> mac { + fn fold_mac(&mut self, macro: &mac) -> mac { Spanned { node: match macro.node { mac_invoc_tt(ref p, ref tts, ctxt) => { @@ -382,22 +382,22 @@ pub trait ast_fold { es.map(|x| f(*x)) } - fn new_id(&self, i: NodeId) -> NodeId { + fn new_id(&mut self, i: NodeId) -> NodeId { i } - fn new_span(&self, sp: Span) -> Span { + fn new_span(&mut self, sp: Span) -> Span { sp } - fn fold_explicit_self(&self, es: &explicit_self) -> explicit_self { + fn fold_explicit_self(&mut self, es: &explicit_self) -> explicit_self { Spanned { span: self.new_span(es.span), node: self.fold_explicit_self_(&es.node) } } - fn fold_explicit_self_(&self, es: &explicit_self_) -> explicit_self_ { + fn fold_explicit_self_(&mut self, es: &explicit_self_) -> explicit_self_ { match *es { sty_static | sty_value(_) | sty_uniq(_) | sty_box(_) => { *es @@ -412,7 +412,7 @@ pub trait ast_fold { /* some little folds that probably aren't useful to have in ast_fold itself*/ //used in noop_fold_item and noop_fold_crate and noop_fold_crate_directive -fn fold_meta_item_(mi: @MetaItem, fld: &T) -> @MetaItem { +fn fold_meta_item_(mi: @MetaItem, fld: &mut T) -> @MetaItem { @Spanned { node: match mi.node { @@ -430,7 +430,7 @@ fn fold_meta_item_(mi: @MetaItem, fld: &T) -> @MetaItem { } //used in noop_fold_item and noop_fold_crate -fn fold_attribute_(at: Attribute, fld: &T) -> Attribute { +fn fold_attribute_(at: Attribute, fld: &mut T) -> Attribute { Spanned { span: fld.new_span(at.span), node: ast::Attribute_ { @@ -442,7 +442,7 @@ fn fold_attribute_(at: Attribute, fld: &T) -> Attribute { } //used in noop_fold_foreign_item and noop_fold_fn_decl -fn fold_arg_(a: &arg, fld: &T) -> arg { +fn fold_arg_(a: &arg, fld: &mut T) -> arg { ast::arg { ty: fld.fold_ty(a.ty), pat: fld.fold_pat(a.pat), @@ -452,7 +452,7 @@ fn fold_arg_(a: &arg, fld: &T) -> arg { // build a new vector of tts by appling the ast_fold's fold_ident to // all of the identifiers in the token trees. -pub fn fold_tts(tts: &[token_tree], fld: &T) -> ~[token_tree] { +pub fn fold_tts(tts: &[token_tree], fld: &mut T) -> ~[token_tree] { tts.map(|tt| { match *tt { tt_tok(span, ref tok) => @@ -470,7 +470,7 @@ pub fn fold_tts(tts: &[token_tree], fld: &T) -> ~[token_tree] { } // apply ident folder if it's an ident, otherwise leave it alone -fn maybe_fold_ident(t: &token::Token, fld: &T) -> token::Token { +fn maybe_fold_ident(t: &token::Token, fld: &mut T) -> token::Token { match *t { token::IDENT(id, followed_by_colons) => { token::IDENT(fld.fold_ident(id), followed_by_colons) @@ -479,7 +479,7 @@ fn maybe_fold_ident(t: &token::Token, fld: &T) -> token::Token { } } -pub fn fold_fn_decl(decl: &ast::fn_decl, fld: &T) +pub fn fold_fn_decl(decl: &ast::fn_decl, fld: &mut T) -> P { P(fn_decl { inputs: decl.inputs.map(|x| fold_arg_(x, fld)), // bad copy @@ -489,7 +489,7 @@ pub fn fold_fn_decl(decl: &ast::fn_decl, fld: &T) }) } -fn fold_ty_param_bound(tpb: &TyParamBound, fld: &T) +fn fold_ty_param_bound(tpb: &TyParamBound, fld: &mut T) -> TyParamBound { match *tpb { TraitTyParamBound(ref ty) => TraitTyParamBound(fold_trait_ref(ty, fld)), @@ -497,7 +497,7 @@ fn fold_ty_param_bound(tpb: &TyParamBound, fld: &T) } } -pub fn fold_ty_param(tp: &TyParam, fld: &T) -> TyParam { +pub fn fold_ty_param(tp: &TyParam, fld: &mut T) -> TyParam { TyParam { ident: tp.ident, id: fld.new_id(tp.id), @@ -505,12 +505,12 @@ pub fn fold_ty_param(tp: &TyParam, fld: &T) -> TyParam { } } -pub fn fold_ty_params(tps: &OptVec, fld: &T) +pub fn fold_ty_params(tps: &OptVec, fld: &mut T) -> OptVec { tps.map(|tp| fold_ty_param(tp, fld)) } -pub fn fold_lifetime(l: &Lifetime, fld: &T) -> Lifetime { +pub fn fold_lifetime(l: &Lifetime, fld: &mut T) -> Lifetime { Lifetime { id: fld.new_id(l.id), span: fld.new_span(l.span), @@ -518,22 +518,22 @@ pub fn fold_lifetime(l: &Lifetime, fld: &T) -> Lifetime { } } -pub fn fold_lifetimes(lts: &OptVec, fld: &T) +pub fn fold_lifetimes(lts: &OptVec, fld: &mut T) -> OptVec { lts.map(|l| fold_lifetime(l, fld)) } -pub fn fold_opt_lifetime(o_lt: &Option, fld: &T) +pub fn fold_opt_lifetime(o_lt: &Option, fld: &mut T) -> Option { o_lt.as_ref().map(|lt| fold_lifetime(lt, fld)) } -pub fn fold_generics(generics: &Generics, fld: &T) -> Generics { +pub fn fold_generics(generics: &Generics, fld: &mut T) -> Generics { Generics {ty_params: fold_ty_params(&generics.ty_params, fld), lifetimes: fold_lifetimes(&generics.lifetimes, fld)} } -fn fold_struct_def(struct_def: @ast::struct_def, fld: &T) +fn fold_struct_def(struct_def: @ast::struct_def, fld: &mut T) -> @ast::struct_def { @ast::struct_def { fields: struct_def.fields.map(|f| fold_struct_field(f, fld)), @@ -541,14 +541,14 @@ fn fold_struct_def(struct_def: @ast::struct_def, fld: &T) } } -fn fold_trait_ref(p: &trait_ref, fld: &T) -> trait_ref { +fn fold_trait_ref(p: &trait_ref, fld: &mut T) -> trait_ref { ast::trait_ref { path: fld.fold_path(&p.path), ref_id: fld.new_id(p.ref_id), } } -fn fold_struct_field(f: &struct_field, fld: &T) -> struct_field { +fn fold_struct_field(f: &struct_field, fld: &mut T) -> struct_field { Spanned { node: ast::struct_field_ { kind: f.node.kind, @@ -560,7 +560,7 @@ fn fold_struct_field(f: &struct_field, fld: &T) -> struct_field { } } -fn fold_field_(field: Field, folder: &T) -> Field { +fn fold_field_(field: Field, folder: &mut T) -> Field { ast::Field { ident: respan(field.ident.span, folder.fold_ident(field.ident.node)), expr: folder.fold_expr(field.expr), @@ -568,14 +568,14 @@ fn fold_field_(field: Field, folder: &T) -> Field { } } -fn fold_mt(mt: &mt, folder: &T) -> mt { +fn fold_mt(mt: &mt, folder: &mut T) -> mt { mt { ty: folder.fold_ty(mt.ty), mutbl: mt.mutbl, } } -fn fold_opt_bounds(b: &Option>, folder: &T) +fn fold_opt_bounds(b: &Option>, folder: &mut T) -> Option> { b.as_ref().map(|bounds| { bounds.map(|bound| { @@ -584,7 +584,7 @@ fn fold_opt_bounds(b: &Option>, folder: &T) }) } -fn fold_variant_arg_(va: &variant_arg, folder: &T) +fn fold_variant_arg_(va: &variant_arg, folder: &mut T) -> variant_arg { ast::variant_arg { ty: folder.fold_ty(va.ty), @@ -592,7 +592,7 @@ fn fold_variant_arg_(va: &variant_arg, folder: &T) } } -pub fn noop_fold_block(b: P, folder: &T) -> P { +pub fn noop_fold_block(b: P, folder: &mut T) -> P { let view_items = b.view_items.map(|x| folder.fold_view_item(x)); let stmts = b.stmts.iter().flat_map(|s| folder.fold_stmt(*s).move_iter()).collect(); P(Block { @@ -605,7 +605,7 @@ pub fn noop_fold_block(b: P, folder: &T) -> P { }) } -pub fn noop_fold_item_underscore(i: &item_, folder: &T) -> item_ { +pub fn noop_fold_item_underscore(i: &item_, folder: &mut T) -> item_ { match *i { item_static(t, m, e) => { item_static(folder.fold_ty(t), m, folder.fold_expr(e)) @@ -662,7 +662,7 @@ pub fn noop_fold_item_underscore(i: &item_, folder: &T) -> item_ { } } -pub fn noop_fold_type_method(m: &TypeMethod, fld: &T) +pub fn noop_fold_type_method(m: &TypeMethod, fld: &mut T) -> TypeMethod { TypeMethod { ident: fld.fold_ident(m.ident), @@ -676,7 +676,7 @@ pub fn noop_fold_type_method(m: &TypeMethod, fld: &T) } } -pub fn noop_fold_mod(m: &_mod, folder: &T) -> _mod { +pub fn noop_fold_mod(m: &_mod, folder: &mut T) -> _mod { ast::_mod { view_items: m.view_items .iter() @@ -685,7 +685,7 @@ pub fn noop_fold_mod(m: &_mod, folder: &T) -> _mod { } } -pub fn noop_fold_crate(c: Crate, folder: &T) -> Crate { +pub fn noop_fold_crate(c: Crate, folder: &mut T) -> Crate { let fold_meta_item = |x| fold_meta_item_(x, folder); let fold_attribute = |x| fold_attribute_(x, folder); @@ -697,7 +697,7 @@ pub fn noop_fold_crate(c: Crate, folder: &T) -> Crate { } } -pub fn noop_fold_item(i: @ast::item, folder: &T) +pub fn noop_fold_item(i: @ast::item, folder: &mut T) -> SmallVector<@ast::item> { let fold_attribute = |x| fold_attribute_(x, folder); @@ -711,7 +711,7 @@ pub fn noop_fold_item(i: @ast::item, folder: &T) }) } -pub fn noop_fold_expr(e: @ast::Expr, folder: &T) -> @ast::Expr { +pub fn noop_fold_expr(e: @ast::Expr, folder: &mut T) -> @ast::Expr { let fold_field = |x| fold_field_(x, folder); let node = match e.node { @@ -719,7 +719,7 @@ pub fn noop_fold_expr(e: @ast::Expr, folder: &T) -> @ast::Expr { ExprVstore(folder.fold_expr(e), v) } ExprVec(ref exprs, mutt) => { - ExprVec(folder.map_exprs(|x| folder.fold_expr(x), *exprs), mutt) + ExprVec(exprs.map(|&x| folder.fold_expr(x)), mutt) } ExprRepeat(expr, count, mutt) => { ExprRepeat(folder.fold_expr(expr), folder.fold_expr(count), mutt) @@ -727,7 +727,7 @@ pub fn noop_fold_expr(e: @ast::Expr, folder: &T) -> @ast::Expr { ExprTup(ref elts) => ExprTup(elts.map(|x| folder.fold_expr(*x))), ExprCall(f, ref args, blk) => { ExprCall(folder.fold_expr(f), - folder.map_exprs(|x| folder.fold_expr(x), *args), + args.map(|&x| folder.fold_expr(x)), blk) } ExprMethodCall(callee_id, f, i, ref tps, ref args, blk) => { @@ -736,7 +736,7 @@ pub fn noop_fold_expr(e: @ast::Expr, folder: &T) -> @ast::Expr { folder.fold_expr(f), folder.fold_ident(i), tps.map(|&x| folder.fold_ty(x)), - folder.map_exprs(|x| folder.fold_expr(x), *args), + args.map(|&x| folder.fold_expr(x)), blk ) } @@ -837,7 +837,7 @@ pub fn noop_fold_expr(e: @ast::Expr, folder: &T) -> @ast::Expr { } } -pub fn noop_fold_stmt(s: &Stmt, folder: &T) -> SmallVector<@Stmt> { +pub fn noop_fold_stmt(s: &Stmt, folder: &mut T) -> SmallVector<@Stmt> { let nodes = match s.node { StmtDecl(d, nid) => { folder.fold_decl(d).move_iter() @@ -876,7 +876,7 @@ mod test { struct ToZzIdentFolder; impl ast_fold for ToZzIdentFolder { - fn fold_ident(&self, _: ast::Ident) -> ast::Ident { + fn fold_ident(&mut self, _: ast::Ident) -> ast::Ident { token::str_to_ident("zz") } } @@ -898,23 +898,23 @@ mod test { // make sure idents get transformed everywhere #[test] fn ident_transformation () { - let zz_fold = ToZzIdentFolder; + let mut zz_fold = ToZzIdentFolder; let ast = string_to_crate(@"#[a] mod b {fn c (d : e, f : g) {h!(i,j,k);l;m}}"); assert_pred!(matches_codepattern, "matches_codepattern", - pprust::to_str(&zz_fold.fold_crate(ast),fake_print_crate, + pprust::to_str(&mut zz_fold.fold_crate(ast),fake_print_crate, token::get_ident_interner()), ~"#[a]mod zz{fn zz(zz:zz,zz:zz){zz!(zz,zz,zz);zz;zz}}"); } // even inside macro defs.... #[test] fn ident_transformation_in_defs () { - let zz_fold = ToZzIdentFolder; + let mut zz_fold = ToZzIdentFolder; let ast = string_to_crate(@"macro_rules! a {(b $c:expr $(d $e:token)f+ => (g $(d $d $e)+))} "); assert_pred!(matches_codepattern, "matches_codepattern", - pprust::to_str(&zz_fold.fold_crate(ast),fake_print_crate, + pprust::to_str(&mut zz_fold.fold_crate(ast),fake_print_crate, token::get_ident_interner()), ~"zz!zz((zz$zz:zz$(zz $zz:zz)zz+=>(zz$(zz$zz$zz)+)))"); } From 8143662836ccecca0b1f177f0c12528a4aa74c0d Mon Sep 17 00:00:00 2001 From: Steven Fackler Date: Sat, 28 Dec 2013 22:06:22 -0700 Subject: [PATCH 5/6] Start passing around &mut ExtCtxt --- src/libsyntax/ext/asm.rs | 2 +- src/libsyntax/ext/base.rs | 16 ++++++++-------- src/libsyntax/ext/build.rs | 6 +++--- src/libsyntax/ext/bytes.rs | 2 +- src/libsyntax/ext/cfg.rs | 2 +- src/libsyntax/ext/concat.rs | 2 +- src/libsyntax/ext/concat_idents.rs | 2 +- src/libsyntax/ext/env.rs | 4 ++-- src/libsyntax/ext/expand.rs | 6 +++--- src/libsyntax/ext/fmt.rs | 2 +- src/libsyntax/ext/format.rs | 15 ++++++++------- src/libsyntax/ext/log_syntax.rs | 2 +- src/libsyntax/ext/quote.rs | 12 ++++++------ src/libsyntax/ext/source_util.rs | 18 +++++++++--------- src/libsyntax/ext/trace_macros.rs | 2 +- src/libsyntax/ext/tt/macro_rules.rs | 4 ++-- 16 files changed, 49 insertions(+), 48 deletions(-) diff --git a/src/libsyntax/ext/asm.rs b/src/libsyntax/ext/asm.rs index fdde336ffa724..cd7953aac2067 100644 --- a/src/libsyntax/ext/asm.rs +++ b/src/libsyntax/ext/asm.rs @@ -37,7 +37,7 @@ fn next_state(s: State) -> Option { } } -pub fn expand_asm(cx: &ExtCtxt, sp: Span, tts: &[ast::token_tree]) +pub fn expand_asm(cx: &mut ExtCtxt, sp: Span, tts: &[ast::token_tree]) -> base::MacResult { let p = parse::new_parser_from_tts(cx.parse_sess(), cx.cfg(), diff --git a/src/libsyntax/ext/base.rs b/src/libsyntax/ext/base.rs index 842f2bfdffc9e..b5f8329a0b24f 100644 --- a/src/libsyntax/ext/base.rs +++ b/src/libsyntax/ext/base.rs @@ -45,7 +45,7 @@ pub struct SyntaxExpanderTT { pub trait SyntaxExpanderTTTrait { fn expand(&self, - ecx: &ExtCtxt, + ecx: &mut ExtCtxt, span: Span, token_tree: &[ast::token_tree], context: ast::SyntaxContext) @@ -53,7 +53,7 @@ pub trait SyntaxExpanderTTTrait { } pub type SyntaxExpanderTTFunNoCtxt = - fn(ecx: &ExtCtxt, span: codemap::Span, token_tree: &[ast::token_tree]) + fn(ecx: &mut ExtCtxt, span: codemap::Span, token_tree: &[ast::token_tree]) -> MacResult; enum SyntaxExpanderTTExpander { @@ -62,7 +62,7 @@ enum SyntaxExpanderTTExpander { impl SyntaxExpanderTTTrait for SyntaxExpanderTT { fn expand(&self, - ecx: &ExtCtxt, + ecx: &mut ExtCtxt, span: Span, token_tree: &[ast::token_tree], _: ast::SyntaxContext) @@ -87,7 +87,7 @@ pub struct SyntaxExpanderTTItem { pub trait SyntaxExpanderTTItemTrait { fn expand(&self, - cx: &ExtCtxt, + cx: &mut ExtCtxt, sp: Span, ident: ast::Ident, token_tree: ~[ast::token_tree], @@ -97,7 +97,7 @@ pub trait SyntaxExpanderTTItemTrait { impl SyntaxExpanderTTItemTrait for SyntaxExpanderTTItem { fn expand(&self, - cx: &ExtCtxt, + cx: &mut ExtCtxt, sp: Span, ident: ast::Ident, token_tree: ~[ast::token_tree], @@ -115,11 +115,11 @@ impl SyntaxExpanderTTItemTrait for SyntaxExpanderTTItem { } pub type SyntaxExpanderTTItemFun = - fn(&ExtCtxt, Span, ast::Ident, ~[ast::token_tree], ast::SyntaxContext) + fn(&mut ExtCtxt, Span, ast::Ident, ~[ast::token_tree], ast::SyntaxContext) -> MacResult; pub type SyntaxExpanderTTItemFunNoCtxt = - fn(&ExtCtxt, Span, ast::Ident, ~[ast::token_tree]) -> MacResult; + fn(&mut ExtCtxt, Span, ast::Ident, ~[ast::token_tree]) -> MacResult; pub trait AnyMacro { fn make_expr(&self) -> @ast::Expr; @@ -320,7 +320,7 @@ impl ExtCtxt { } } - pub fn expand_expr(&self, mut e: @ast::Expr) -> @ast::Expr { + pub fn expand_expr(&mut self, mut e: @ast::Expr) -> @ast::Expr { loop { match e.node { ast::ExprMac(..) => { diff --git a/src/libsyntax/ext/build.rs b/src/libsyntax/ext/build.rs index f5c3c1792c96c..e5f20950412ac 100644 --- a/src/libsyntax/ext/build.rs +++ b/src/libsyntax/ext/build.rs @@ -236,7 +236,7 @@ pub trait AstBuilder { vis: ast::visibility, path: ~[ast::Ident]) -> ast::view_item; } -impl<'a> AstBuilder for &'a ExtCtxt { +impl AstBuilder for ExtCtxt { fn path(&self, span: Span, strs: ~[ast::Ident]) -> ast::Path { self.path_all(span, false, strs, opt_vec::Empty, ~[]) } @@ -686,12 +686,12 @@ impl<'a> AstBuilder for &'a ExtCtxt { } fn lambda0(&self, _span: Span, blk: P) -> @ast::Expr { let blk_e = self.expr(blk.span, ast::ExprBlock(blk)); - quote_expr!(*self, || $blk_e ) + quote_expr!(self, || $blk_e ) } fn lambda1(&self, _span: Span, blk: P, ident: ast::Ident) -> @ast::Expr { let blk_e = self.expr(blk.span, ast::ExprBlock(blk)); - quote_expr!(*self, |$ident| $blk_e ) + quote_expr!(self, |$ident| $blk_e ) } fn lambda_expr(&self, span: Span, ids: ~[ast::Ident], expr: @ast::Expr) -> @ast::Expr { diff --git a/src/libsyntax/ext/bytes.rs b/src/libsyntax/ext/bytes.rs index 1ceb2a1668eaf..1878f6f3c3cbc 100644 --- a/src/libsyntax/ext/bytes.rs +++ b/src/libsyntax/ext/bytes.rs @@ -18,7 +18,7 @@ use ext::build::AstBuilder; use std::char; -pub fn expand_syntax_ext(cx: &ExtCtxt, sp: Span, tts: &[ast::token_tree]) -> base::MacResult { +pub fn expand_syntax_ext(cx: &mut ExtCtxt, sp: Span, tts: &[ast::token_tree]) -> base::MacResult { // Gather all argument expressions let exprs = get_exprs_from_tts(cx, sp, tts); let mut bytes = ~[]; diff --git a/src/libsyntax/ext/cfg.rs b/src/libsyntax/ext/cfg.rs index 2ca8917ed8fc6..d9fbc33153a60 100644 --- a/src/libsyntax/ext/cfg.rs +++ b/src/libsyntax/ext/cfg.rs @@ -25,7 +25,7 @@ use parse; use parse::token; use parse::attr::parser_attr; -pub fn expand_cfg(cx: &ExtCtxt, sp: Span, tts: &[ast::token_tree]) -> base::MacResult { +pub fn expand_cfg(cx: &mut ExtCtxt, sp: Span, tts: &[ast::token_tree]) -> base::MacResult { let p = parse::new_parser_from_tts(cx.parse_sess(), cx.cfg(), tts.to_owned()); let mut cfgs = ~[]; diff --git a/src/libsyntax/ext/concat.rs b/src/libsyntax/ext/concat.rs index b6a81a8bea543..d8be7dedef2f2 100644 --- a/src/libsyntax/ext/concat.rs +++ b/src/libsyntax/ext/concat.rs @@ -15,7 +15,7 @@ use codemap; use ext::base; use ext::build::AstBuilder; -pub fn expand_syntax_ext(cx: &base::ExtCtxt, +pub fn expand_syntax_ext(cx: &mut base::ExtCtxt, sp: codemap::Span, tts: &[ast::token_tree]) -> base::MacResult { let es = base::get_exprs_from_tts(cx, sp, tts); diff --git a/src/libsyntax/ext/concat_idents.rs b/src/libsyntax/ext/concat_idents.rs index 6b204a243cb1e..0cb3e781c26ef 100644 --- a/src/libsyntax/ext/concat_idents.rs +++ b/src/libsyntax/ext/concat_idents.rs @@ -16,7 +16,7 @@ use opt_vec; use parse::token; use parse::token::{str_to_ident}; -pub fn expand_syntax_ext(cx: &ExtCtxt, sp: Span, tts: &[ast::token_tree]) +pub fn expand_syntax_ext(cx: &mut ExtCtxt, sp: Span, tts: &[ast::token_tree]) -> base::MacResult { let mut res_str = ~""; for (i, e) in tts.iter().enumerate() { diff --git a/src/libsyntax/ext/env.rs b/src/libsyntax/ext/env.rs index b5f5f8c41d023..cebd7aac4e3ea 100644 --- a/src/libsyntax/ext/env.rs +++ b/src/libsyntax/ext/env.rs @@ -22,7 +22,7 @@ use ext::build::AstBuilder; use std::os; -pub fn expand_option_env(cx: &ExtCtxt, sp: Span, tts: &[ast::token_tree]) +pub fn expand_option_env(cx: &mut ExtCtxt, sp: Span, tts: &[ast::token_tree]) -> base::MacResult { let var = get_single_str_from_tts(cx, sp, tts, "option_env!"); @@ -33,7 +33,7 @@ pub fn expand_option_env(cx: &ExtCtxt, sp: Span, tts: &[ast::token_tree]) MRExpr(e) } -pub fn expand_env(cx: &ExtCtxt, sp: Span, tts: &[ast::token_tree]) +pub fn expand_env(cx: &mut ExtCtxt, sp: Span, tts: &[ast::token_tree]) -> base::MacResult { let exprs = get_exprs_from_tts(cx, sp, tts); diff --git a/src/libsyntax/ext/expand.rs b/src/libsyntax/ext/expand.rs index e20161ea2d093..eb07353cda3d1 100644 --- a/src/libsyntax/ext/expand.rs +++ b/src/libsyntax/ext/expand.rs @@ -932,7 +932,7 @@ pub fn inject_std_macros(parse_sess: @mut parse::ParseSess, pub struct MacroExpander<'a> { extsbox: @mut SyntaxEnv, - cx: &'a ExtCtxt, + cx: &'a mut ExtCtxt, } impl<'a> ast_fold for MacroExpander<'a> { @@ -970,10 +970,10 @@ pub fn expand_crate(parse_sess: @mut parse::ParseSess, // exts table through the fold, but that would require updating // every method/element of AstFoldFns in fold.rs. let extsbox = syntax_expander_table(); - let cx = ExtCtxt::new(parse_sess, cfg.clone()); + let mut cx = ExtCtxt::new(parse_sess, cfg.clone()); let mut expander = MacroExpander { extsbox: @mut extsbox, - cx: &cx, + cx: &mut cx, }; let ret = expander.fold_crate(c); diff --git a/src/libsyntax/ext/fmt.rs b/src/libsyntax/ext/fmt.rs index f2eab3f1cbc4c..34a8236905b0a 100644 --- a/src/libsyntax/ext/fmt.rs +++ b/src/libsyntax/ext/fmt.rs @@ -15,7 +15,7 @@ use codemap::Span; use ext::base; use ext::build::AstBuilder; -pub fn expand_syntax_ext(ecx: &base::ExtCtxt, sp: Span, +pub fn expand_syntax_ext(ecx: &mut base::ExtCtxt, sp: Span, _tts: &[ast::token_tree]) -> base::MacResult { ecx.span_err(sp, "`fmt!` is deprecated, use `format!` instead"); ecx.parse_sess.span_diagnostic.span_note(sp, diff --git a/src/libsyntax/ext/format.rs b/src/libsyntax/ext/format.rs index 2b51203eca5bb..3bfab7da9b46d 100644 --- a/src/libsyntax/ext/format.rs +++ b/src/libsyntax/ext/format.rs @@ -29,7 +29,7 @@ enum ArgumentType { } struct Context<'a> { - ecx: &'a ExtCtxt, + ecx: &'a mut ExtCtxt, fmtsp: Span, // Parsed argument expressions and the types that we've found so far for @@ -722,7 +722,7 @@ impl<'a> Context<'a> { } } -pub fn expand_args(ecx: &ExtCtxt, sp: Span, +pub fn expand_args(ecx: &mut ExtCtxt, sp: Span, tts: &[ast::token_tree]) -> base::MacResult { let mut cx = Context { ecx: ecx, @@ -739,19 +739,20 @@ pub fn expand_args(ecx: &ExtCtxt, sp: Span, }; let (extra, efmt) = match cx.parse_args(sp, tts) { (extra, Some(e)) => (extra, e), - (_, None) => { return MRExpr(ecx.expr_uint(sp, 2)); } + (_, None) => { return MRExpr(cx.ecx.expr_uint(sp, 2)); } }; cx.fmtsp = efmt.span; // Be sure to recursively expand macros just in case the format string uses // a macro to build the format expression. - let (fmt, _) = expr_to_str(ecx, ecx.expand_expr(efmt), + let expr = cx.ecx.expand_expr(efmt); + let (fmt, _) = expr_to_str(cx.ecx, expr, "format argument must be a string literal."); let mut err = false; parse::parse_error::cond.trap(|m| { if !err { err = true; - ecx.span_err(efmt.span, m); + cx.ecx.span_err(efmt.span, m); } }).inside(|| { for piece in parse::Parser::new(fmt) { @@ -767,12 +768,12 @@ pub fn expand_args(ecx: &ExtCtxt, sp: Span, // Make sure that all arguments were used and all arguments have types. for (i, ty) in cx.arg_types.iter().enumerate() { if ty.is_none() { - ecx.span_err(cx.args[i].span, "argument never used"); + cx.ecx.span_err(cx.args[i].span, "argument never used"); } } for (name, e) in cx.names.iter() { if !cx.name_types.contains_key(name) { - ecx.span_err(e.span, "named argument never used"); + cx.ecx.span_err(e.span, "named argument never used"); } } diff --git a/src/libsyntax/ext/log_syntax.rs b/src/libsyntax/ext/log_syntax.rs index 7dfd487b379fc..22b04501cfbec 100644 --- a/src/libsyntax/ext/log_syntax.rs +++ b/src/libsyntax/ext/log_syntax.rs @@ -15,7 +15,7 @@ use ext::base; use print; use parse::token::{get_ident_interner}; -pub fn expand_syntax_ext(cx: &ExtCtxt, +pub fn expand_syntax_ext(cx: &mut ExtCtxt, sp: codemap::Span, tt: &[ast::token_tree]) -> base::MacResult { diff --git a/src/libsyntax/ext/quote.rs b/src/libsyntax/ext/quote.rs index f6086734003e8..330d33d6fc6c4 100644 --- a/src/libsyntax/ext/quote.rs +++ b/src/libsyntax/ext/quote.rs @@ -289,7 +289,7 @@ pub mod rt { } -pub fn expand_quote_tokens(cx: &ExtCtxt, +pub fn expand_quote_tokens(cx: &mut ExtCtxt, sp: Span, tts: &[ast::token_tree]) -> base::MacResult { let (cx_expr, expr) = expand_tts(cx, sp, tts); @@ -297,14 +297,14 @@ pub fn expand_quote_tokens(cx: &ExtCtxt, base::MRExpr(expanded) } -pub fn expand_quote_expr(cx: &ExtCtxt, +pub fn expand_quote_expr(cx: &mut ExtCtxt, sp: Span, tts: &[ast::token_tree]) -> base::MacResult { let expanded = expand_parse_call(cx, sp, "parse_expr", ~[], tts); base::MRExpr(expanded) } -pub fn expand_quote_item(cx: &ExtCtxt, +pub fn expand_quote_item(cx: &mut ExtCtxt, sp: Span, tts: &[ast::token_tree]) -> base::MacResult { let e_attrs = cx.expr_vec_uniq(sp, ~[]); @@ -313,7 +313,7 @@ pub fn expand_quote_item(cx: &ExtCtxt, base::MRExpr(expanded) } -pub fn expand_quote_pat(cx: &ExtCtxt, +pub fn expand_quote_pat(cx: &mut ExtCtxt, sp: Span, tts: &[ast::token_tree]) -> base::MacResult { let e_refutable = cx.expr_lit(sp, ast::lit_bool(true)); @@ -322,7 +322,7 @@ pub fn expand_quote_pat(cx: &ExtCtxt, base::MRExpr(expanded) } -pub fn expand_quote_ty(cx: &ExtCtxt, +pub fn expand_quote_ty(cx: &mut ExtCtxt, sp: Span, tts: &[ast::token_tree]) -> base::MacResult { let e_param_colons = cx.expr_lit(sp, ast::lit_bool(false)); @@ -331,7 +331,7 @@ pub fn expand_quote_ty(cx: &ExtCtxt, base::MRExpr(expanded) } -pub fn expand_quote_stmt(cx: &ExtCtxt, +pub fn expand_quote_stmt(cx: &mut ExtCtxt, sp: Span, tts: &[ast::token_tree]) -> base::MacResult { let e_attrs = cx.expr_vec_uniq(sp, ~[]); diff --git a/src/libsyntax/ext/source_util.rs b/src/libsyntax/ext/source_util.rs index cf11bdcce64fa..ccf4bf2acd6f3 100644 --- a/src/libsyntax/ext/source_util.rs +++ b/src/libsyntax/ext/source_util.rs @@ -28,7 +28,7 @@ use std::str; // a given file into the current one. /* line!(): expands to the current line number */ -pub fn expand_line(cx: &ExtCtxt, sp: Span, tts: &[ast::token_tree]) +pub fn expand_line(cx: &mut ExtCtxt, sp: Span, tts: &[ast::token_tree]) -> base::MacResult { base::check_zero_tts(cx, sp, tts, "line!"); @@ -39,7 +39,7 @@ pub fn expand_line(cx: &ExtCtxt, sp: Span, tts: &[ast::token_tree]) } /* col!(): expands to the current column number */ -pub fn expand_col(cx: &ExtCtxt, sp: Span, tts: &[ast::token_tree]) +pub fn expand_col(cx: &mut ExtCtxt, sp: Span, tts: &[ast::token_tree]) -> base::MacResult { base::check_zero_tts(cx, sp, tts, "col!"); @@ -51,7 +51,7 @@ pub fn expand_col(cx: &ExtCtxt, sp: Span, tts: &[ast::token_tree]) /* file!(): expands to the current filename */ /* The filemap (`loc.file`) contains a bunch more information we could spit * out if we wanted. */ -pub fn expand_file(cx: &ExtCtxt, sp: Span, tts: &[ast::token_tree]) +pub fn expand_file(cx: &mut ExtCtxt, sp: Span, tts: &[ast::token_tree]) -> base::MacResult { base::check_zero_tts(cx, sp, tts, "file!"); @@ -61,13 +61,13 @@ pub fn expand_file(cx: &ExtCtxt, sp: Span, tts: &[ast::token_tree]) base::MRExpr(cx.expr_str(topmost.call_site, filename)) } -pub fn expand_stringify(cx: &ExtCtxt, sp: Span, tts: &[ast::token_tree]) +pub fn expand_stringify(cx: &mut ExtCtxt, sp: Span, tts: &[ast::token_tree]) -> base::MacResult { let s = pprust::tts_to_str(tts, get_ident_interner()); base::MRExpr(cx.expr_str(sp, s.to_managed())) } -pub fn expand_mod(cx: &ExtCtxt, sp: Span, tts: &[ast::token_tree]) +pub fn expand_mod(cx: &mut ExtCtxt, sp: Span, tts: &[ast::token_tree]) -> base::MacResult { base::check_zero_tts(cx, sp, tts, "module_path!"); base::MRExpr(cx.expr_str(sp, @@ -77,7 +77,7 @@ pub fn expand_mod(cx: &ExtCtxt, sp: Span, tts: &[ast::token_tree]) // include! : parse the given file as an expr // This is generally a bad idea because it's going to behave // unhygienically. -pub fn expand_include(cx: &ExtCtxt, sp: Span, tts: &[ast::token_tree]) +pub fn expand_include(cx: &mut ExtCtxt, sp: Span, tts: &[ast::token_tree]) -> base::MacResult { let file = get_single_str_from_tts(cx, sp, tts, "include!"); // The file will be added to the code map by the parser @@ -88,7 +88,7 @@ pub fn expand_include(cx: &ExtCtxt, sp: Span, tts: &[ast::token_tree]) } // include_str! : read the given file, insert it as a literal string expr -pub fn expand_include_str(cx: &ExtCtxt, sp: Span, tts: &[ast::token_tree]) +pub fn expand_include_str(cx: &mut ExtCtxt, sp: Span, tts: &[ast::token_tree]) -> base::MacResult { let file = get_single_str_from_tts(cx, sp, tts, "include_str!"); let file = res_rel_file(cx, sp, &Path::new(file)); @@ -120,7 +120,7 @@ pub fn expand_include_str(cx: &ExtCtxt, sp: Span, tts: &[ast::token_tree]) } } -pub fn expand_include_bin(cx: &ExtCtxt, sp: Span, tts: &[ast::token_tree]) +pub fn expand_include_bin(cx: &mut ExtCtxt, sp: Span, tts: &[ast::token_tree]) -> base::MacResult { use std::at_vec; @@ -167,7 +167,7 @@ fn topmost_expn_info(expn_info: @codemap::ExpnInfo) -> @codemap::ExpnInfo { // resolve a file-system path to an absolute file-system path (if it // isn't already) -fn res_rel_file(cx: &ExtCtxt, sp: codemap::Span, arg: &Path) -> Path { +fn res_rel_file(cx: &mut ExtCtxt, sp: codemap::Span, arg: &Path) -> Path { // NB: relative paths are resolved relative to the compilation unit if !arg.is_absolute() { let mut cu = Path::new(cx.codemap().span_to_filename(sp)); diff --git a/src/libsyntax/ext/trace_macros.rs b/src/libsyntax/ext/trace_macros.rs index cd172495d705b..d9b1c2bddbca8 100644 --- a/src/libsyntax/ext/trace_macros.rs +++ b/src/libsyntax/ext/trace_macros.rs @@ -16,7 +16,7 @@ use parse::lexer::{new_tt_reader, reader}; use parse::parser::Parser; use parse::token::keywords; -pub fn expand_trace_macros(cx: &ExtCtxt, +pub fn expand_trace_macros(cx: &mut ExtCtxt, sp: Span, tt: &[ast::token_tree]) -> base::MacResult { diff --git a/src/libsyntax/ext/tt/macro_rules.rs b/src/libsyntax/ext/tt/macro_rules.rs index 63517ca2269d8..ae9bbdadf2cbd 100644 --- a/src/libsyntax/ext/tt/macro_rules.rs +++ b/src/libsyntax/ext/tt/macro_rules.rs @@ -83,7 +83,7 @@ struct MacroRulesSyntaxExpanderTTFun { impl SyntaxExpanderTTTrait for MacroRulesSyntaxExpanderTTFun { fn expand(&self, - cx: &ExtCtxt, + cx: &mut ExtCtxt, sp: Span, arg: &[ast::token_tree], _: ast::SyntaxContext) @@ -168,7 +168,7 @@ fn generic_extension(cx: &ExtCtxt, // this procedure performs the expansion of the // macro_rules! macro. It parses the RHS and adds // an extension to the current context. -pub fn add_new_extension(cx: &ExtCtxt, +pub fn add_new_extension(cx: &mut ExtCtxt, sp: Span, name: Ident, arg: ~[ast::token_tree], From dc830345e840875dff41ae1a0497cbff18909712 Mon Sep 17 00:00:00 2001 From: Steven Fackler Date: Sat, 28 Dec 2013 22:35:38 -0700 Subject: [PATCH 6/6] Remove @muts from ExtCtxt --- src/librustc/front/test.rs | 20 +++++++++---------- src/libsyntax/ext/base.rs | 40 +++++++++++++++++++------------------- 2 files changed, 30 insertions(+), 30 deletions(-) diff --git a/src/librustc/front/test.rs b/src/librustc/front/test.rs index 764935d530553..6193e5e8fa1f6 100644 --- a/src/librustc/front/test.rs +++ b/src/librustc/front/test.rs @@ -63,7 +63,7 @@ pub fn modify_for_testing(sess: session::Session, } struct TestHarnessGenerator { - cx: @TestCtxt, + cx: TestCtxt, } impl fold::ast_fold for TestHarnessGenerator { @@ -73,7 +73,7 @@ impl fold::ast_fold for TestHarnessGenerator { // Add a special __test module to the crate that will contain code // generated for the test harness ast::Crate { - module: add_test_module(self.cx, &folded.module), + module: add_test_module(&self.cx, &folded.module), .. folded } } @@ -86,7 +86,7 @@ impl fold::ast_fold for TestHarnessGenerator { debug!("current path: {}", ast_util::path_name_i(self.cx.path.get())); - if is_test_fn(self.cx, i) || is_bench_fn(i) { + if is_test_fn(&self.cx, i) || is_bench_fn(i) { match i.node { ast::item_fn(_, purity, _, _, _) if purity == ast::unsafe_fn => { @@ -101,7 +101,7 @@ impl fold::ast_fold for TestHarnessGenerator { span: i.span, path: self.cx.path.get(), bench: is_bench_fn(i), - ignore: is_ignored(self.cx, i), + ignore: is_ignored(&self.cx, i), should_fail: should_fail(i) }; { @@ -126,7 +126,7 @@ impl fold::ast_fold for TestHarnessGenerator { // Remove any #[main] from the AST so it doesn't clash with // the one we're going to add. Only if compiling an executable. - fn nomain(cx: @TestCtxt, item: @ast::item) -> @ast::item { + fn nomain(cx: &TestCtxt, item: @ast::item) -> @ast::item { if !cx.sess.building_library.get() { @ast::item { attrs: item.attrs.iter().filter_map(|attr| { @@ -145,7 +145,7 @@ impl fold::ast_fold for TestHarnessGenerator { let mod_nomain = ast::_mod { view_items: m.view_items.clone(), - items: m.items.iter().map(|i| nomain(self.cx, *i)).collect(), + items: m.items.iter().map(|i| nomain(&self.cx, *i)).collect(), }; fold::noop_fold_mod(&mod_nomain, self) @@ -154,7 +154,7 @@ impl fold::ast_fold for TestHarnessGenerator { fn generate_test_harness(sess: session::Session, crate: ast::Crate) -> ast::Crate { - let cx: @TestCtxt = @TestCtxt { + let mut cx: TestCtxt = TestCtxt { sess: sess, ext_cx: ExtCtxt::new(sess.parse_sess, sess.opts.cfg.clone()), path: RefCell::new(~[]), @@ -176,7 +176,7 @@ fn generate_test_harness(sess: session::Session, crate: ast::Crate) cx: cx }; let res = fold.fold_crate(crate); - cx.ext_cx.bt_pop(); + fold.cx.ext_cx.bt_pop(); return res; } @@ -189,7 +189,7 @@ fn strip_test_functions(crate: ast::Crate) -> ast::Crate { }) } -fn is_test_fn(cx: @TestCtxt, i: @ast::item) -> bool { +fn is_test_fn(cx: &TestCtxt, i: @ast::item) -> bool { let has_test_attr = attr::contains_name(i.attrs, "test"); fn has_test_signature(i: @ast::item) -> bool { @@ -242,7 +242,7 @@ fn is_bench_fn(i: @ast::item) -> bool { return has_bench_attr && has_test_signature(i); } -fn is_ignored(cx: @TestCtxt, i: @ast::item) -> bool { +fn is_ignored(cx: &TestCtxt, i: @ast::item) -> bool { i.attrs.iter().any(|attr| { // check ignore(cfg(foo, bar)) "ignore" == attr.name() && match attr.meta_item_list() { diff --git a/src/libsyntax/ext/base.rs b/src/libsyntax/ext/base.rs index b5f8329a0b24f..ccbc533fbcc72 100644 --- a/src/libsyntax/ext/base.rs +++ b/src/libsyntax/ext/base.rs @@ -297,15 +297,15 @@ pub fn syntax_expander_table() -> SyntaxEnv { pub struct ExtCtxt { parse_sess: @mut parse::ParseSess, cfg: ast::CrateConfig, - backtrace: @mut Option<@ExpnInfo>, + backtrace: Option<@ExpnInfo>, // These two @mut's should really not be here, // but the self types for CtxtRepr are all wrong // and there are bugs in the code for object // types that make this hard to get right at the // moment. - nmatsakis - mod_path: @mut ~[ast::Ident], - trace_mac: @mut bool + mod_path: ~[ast::Ident], + trace_mac: bool } impl ExtCtxt { @@ -314,9 +314,9 @@ impl ExtCtxt { ExtCtxt { parse_sess: parse_sess, cfg: cfg, - backtrace: @mut None, - mod_path: @mut ~[], - trace_mac: @mut false + backtrace: None, + mod_path: ~[], + trace_mac: false } } @@ -339,32 +339,32 @@ impl ExtCtxt { pub fn parse_sess(&self) -> @mut parse::ParseSess { self.parse_sess } pub fn cfg(&self) -> ast::CrateConfig { self.cfg.clone() } pub fn call_site(&self) -> Span { - match *self.backtrace { + match self.backtrace { Some(@ExpnInfo {call_site: cs, ..}) => cs, None => self.bug("missing top span") } } pub fn print_backtrace(&self) { } - pub fn backtrace(&self) -> Option<@ExpnInfo> { *self.backtrace } - pub fn mod_push(&self, i: ast::Ident) { self.mod_path.push(i); } - pub fn mod_pop(&self) { self.mod_path.pop(); } - pub fn mod_path(&self) -> ~[ast::Ident] { (*self.mod_path).clone() } - pub fn bt_push(&self, ei: codemap::ExpnInfo) { + pub fn backtrace(&self) -> Option<@ExpnInfo> { self.backtrace } + pub fn mod_push(&mut self, i: ast::Ident) { self.mod_path.push(i); } + pub fn mod_pop(&mut self) { self.mod_path.pop(); } + pub fn mod_path(&self) -> ~[ast::Ident] { self.mod_path.clone() } + pub fn bt_push(&mut self, ei: codemap::ExpnInfo) { match ei { ExpnInfo {call_site: cs, callee: ref callee} => { - *self.backtrace = + self.backtrace = Some(@ExpnInfo { call_site: Span {lo: cs.lo, hi: cs.hi, - expn_info: *self.backtrace}, + expn_info: self.backtrace}, callee: *callee}); } } } - pub fn bt_pop(&self) { - match *self.backtrace { + pub fn bt_pop(&mut self) { + match self.backtrace { Some(@ExpnInfo { call_site: Span {expn_info: prev, ..}, ..}) => { - *self.backtrace = prev + self.backtrace = prev } _ => self.bug("tried to pop without a push") } @@ -394,10 +394,10 @@ impl ExtCtxt { self.parse_sess.span_diagnostic.handler().bug(msg); } pub fn trace_macros(&self) -> bool { - *self.trace_mac + self.trace_mac } - pub fn set_trace_macros(&self, x: bool) { - *self.trace_mac = x + pub fn set_trace_macros(&mut self, x: bool) { + self.trace_mac = x } pub fn str_of(&self, id: ast::Ident) -> @str { ident_to_str(&id)