Skip to content

Commit adc5eef

Browse files
committed
auto merge of #11168 : sfackler/rust/de-at-extctxt, r=alexcrichton
* Pass `&ExtCtxt` instead of `@ExtCtxt`. * Stop passing duplicate parameters around in `expand`. * Make `ast_fold` methods take `&mut self`. After these, it should be possible to remove the `@mut` boxes from `ExtCtxt` altogether, though #11167 is doing some of that so I'm holding off on that for now. This will probably conflict with that PR, so I'm guessing that one will have to be rebased on top of the other. r? @pcwalton
2 parents 0cbb44a + dc83034 commit adc5eef

40 files changed

+399
-443
lines changed

src/librustc/front/assign_node_ids.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@ struct NodeIdAssigner {
1818
}
1919

2020
impl ast_fold for NodeIdAssigner {
21-
fn new_id(&self, old_id: ast::NodeId) -> ast::NodeId {
21+
fn new_id(&mut self, old_id: ast::NodeId) -> ast::NodeId {
2222
assert_eq!(old_id, ast::DUMMY_NODE_ID);
2323
self.sess.next_node_id()
2424
}
2525
}
2626

2727
pub fn assign_node_ids(sess: Session, crate: ast::Crate) -> ast::Crate {
28-
let fold = NodeIdAssigner {
28+
let mut fold = NodeIdAssigner {
2929
sess: sess,
3030
};
3131
fold.fold_crate(crate)

src/librustc/front/config.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -24,25 +24,25 @@ pub fn strip_unconfigured_items(crate: ast::Crate) -> ast::Crate {
2424
}
2525

2626
impl<'a> fold::ast_fold for Context<'a> {
27-
fn fold_mod(&self, module: &ast::_mod) -> ast::_mod {
27+
fn fold_mod(&mut self, module: &ast::_mod) -> ast::_mod {
2828
fold_mod(self, module)
2929
}
30-
fn fold_block(&self, block: ast::P<ast::Block>) -> ast::P<ast::Block> {
30+
fn fold_block(&mut self, block: ast::P<ast::Block>) -> ast::P<ast::Block> {
3131
fold_block(self, block)
3232
}
33-
fn fold_foreign_mod(&self, foreign_module: &ast::foreign_mod)
33+
fn fold_foreign_mod(&mut self, foreign_module: &ast::foreign_mod)
3434
-> ast::foreign_mod {
3535
fold_foreign_mod(self, foreign_module)
3636
}
37-
fn fold_item_underscore(&self, item: &ast::item_) -> ast::item_ {
37+
fn fold_item_underscore(&mut self, item: &ast::item_) -> ast::item_ {
3838
fold_item_underscore(self, item)
3939
}
4040
}
4141

4242
pub fn strip_items(crate: ast::Crate,
4343
in_cfg: |attrs: &[ast::Attribute]| -> bool)
4444
-> ast::Crate {
45-
let ctxt = Context {
45+
let mut ctxt = Context {
4646
in_cfg: in_cfg,
4747
};
4848
ctxt.fold_crate(crate)
@@ -57,7 +57,7 @@ fn filter_view_item<'r>(cx: &Context, view_item: &'r ast::view_item)
5757
}
5858
}
5959

60-
fn fold_mod(cx: &Context, m: &ast::_mod) -> ast::_mod {
60+
fn fold_mod(cx: &mut Context, m: &ast::_mod) -> ast::_mod {
6161
let filtered_items = m.items.iter()
6262
.filter(|&a| item_in_cfg(cx, *a))
6363
.flat_map(|&x| cx.fold_item(x).move_iter())
@@ -80,7 +80,7 @@ fn filter_foreign_item(cx: &Context, item: @ast::foreign_item)
8080
}
8181
}
8282

83-
fn fold_foreign_mod(cx: &Context, nm: &ast::foreign_mod) -> ast::foreign_mod {
83+
fn fold_foreign_mod(cx: &mut Context, nm: &ast::foreign_mod) -> ast::foreign_mod {
8484
let filtered_items = nm.items
8585
.iter()
8686
.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 {
9595
}
9696
}
9797

98-
fn fold_item_underscore(cx: &Context, item: &ast::item_) -> ast::item_ {
98+
fn fold_item_underscore(cx: &mut Context, item: &ast::item_) -> ast::item_ {
9999
let item = match *item {
100100
ast::item_impl(ref a, ref b, c, ref methods) => {
101101
let methods = methods.iter().filter(|m| method_in_cfg(cx, **m))
@@ -129,7 +129,7 @@ fn retain_stmt(cx: &Context, stmt: @ast::Stmt) -> bool {
129129
}
130130
}
131131

132-
fn fold_block(cx: &Context, b: ast::P<ast::Block>) -> ast::P<ast::Block> {
132+
fn fold_block(cx: &mut Context, b: ast::P<ast::Block>) -> ast::P<ast::Block> {
133133
let resulting_stmts = b.stmts.iter()
134134
.filter(|&a| retain_stmt(cx, *a))
135135
.flat_map(|&stmt| cx.fold_stmt(stmt).move_iter())

src/librustc/front/std_inject.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ struct StandardLibraryInjector {
5656
}
5757

5858
impl fold::ast_fold for StandardLibraryInjector {
59-
fn fold_crate(&self, crate: ast::Crate) -> ast::Crate {
59+
fn fold_crate(&mut self, crate: ast::Crate) -> ast::Crate {
6060
let version = STD_VERSION.to_managed();
6161
let vers_item = attr::mk_name_value_item_str(@"vers", version);
6262
let mut vis = ~[ast::view_item {
@@ -108,7 +108,7 @@ impl fold::ast_fold for StandardLibraryInjector {
108108
}
109109
}
110110

111-
fn fold_item(&self, item: @ast::item) -> SmallVector<@ast::item> {
111+
fn fold_item(&mut self, item: @ast::item) -> SmallVector<@ast::item> {
112112
if !no_prelude(item.attrs) {
113113
// only recur if there wasn't `#[no_implicit_prelude];`
114114
// on this item, i.e. this means that the prelude is not
@@ -119,7 +119,7 @@ impl fold::ast_fold for StandardLibraryInjector {
119119
}
120120
}
121121

122-
fn fold_mod(&self, module: &ast::_mod) -> ast::_mod {
122+
fn fold_mod(&mut self, module: &ast::_mod) -> ast::_mod {
123123
let prelude_path = ast::Path {
124124
span: dummy_sp(),
125125
global: false,
@@ -158,7 +158,7 @@ impl fold::ast_fold for StandardLibraryInjector {
158158
}
159159

160160
fn inject_libstd_ref(sess: Session, crate: ast::Crate) -> ast::Crate {
161-
let fold = StandardLibraryInjector {
161+
let mut fold = StandardLibraryInjector {
162162
sess: sess,
163163
};
164164
fold.fold_crate(crate)

src/librustc/front/test.rs

+25-26
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ struct Test {
4040
struct TestCtxt {
4141
sess: session::Session,
4242
path: RefCell<~[ast::Ident]>,
43-
ext_cx: @ExtCtxt,
43+
ext_cx: ExtCtxt,
4444
testfns: RefCell<~[Test]>,
4545
is_extra: bool,
4646
config: ast::CrateConfig,
@@ -63,30 +63,30 @@ pub fn modify_for_testing(sess: session::Session,
6363
}
6464

6565
struct TestHarnessGenerator {
66-
cx: @TestCtxt,
66+
cx: TestCtxt,
6767
}
6868

6969
impl fold::ast_fold for TestHarnessGenerator {
70-
fn fold_crate(&self, c: ast::Crate) -> ast::Crate {
70+
fn fold_crate(&mut self, c: ast::Crate) -> ast::Crate {
7171
let folded = fold::noop_fold_crate(c, self);
7272

7373
// Add a special __test module to the crate that will contain code
7474
// generated for the test harness
7575
ast::Crate {
76-
module: add_test_module(self.cx, &folded.module),
76+
module: add_test_module(&self.cx, &folded.module),
7777
.. folded
7878
}
7979
}
8080

81-
fn fold_item(&self, i: @ast::item) -> SmallVector<@ast::item> {
81+
fn fold_item(&mut self, i: @ast::item) -> SmallVector<@ast::item> {
8282
{
8383
let mut path = self.cx.path.borrow_mut();
8484
path.get().push(i.ident);
8585
}
8686
debug!("current path: {}",
8787
ast_util::path_name_i(self.cx.path.get()));
8888

89-
if is_test_fn(self.cx, i) || is_bench_fn(i) {
89+
if is_test_fn(&self.cx, i) || is_bench_fn(i) {
9090
match i.node {
9191
ast::item_fn(_, purity, _, _, _)
9292
if purity == ast::unsafe_fn => {
@@ -101,7 +101,7 @@ impl fold::ast_fold for TestHarnessGenerator {
101101
span: i.span,
102102
path: self.cx.path.get(),
103103
bench: is_bench_fn(i),
104-
ignore: is_ignored(self.cx, i),
104+
ignore: is_ignored(&self.cx, i),
105105
should_fail: should_fail(i)
106106
};
107107
{
@@ -122,11 +122,11 @@ impl fold::ast_fold for TestHarnessGenerator {
122122
res
123123
}
124124

125-
fn fold_mod(&self, m: &ast::_mod) -> ast::_mod {
125+
fn fold_mod(&mut self, m: &ast::_mod) -> ast::_mod {
126126
// Remove any #[main] from the AST so it doesn't clash with
127127
// the one we're going to add. Only if compiling an executable.
128128

129-
fn nomain(cx: @TestCtxt, item: @ast::item) -> @ast::item {
129+
fn nomain(cx: &TestCtxt, item: @ast::item) -> @ast::item {
130130
if !cx.sess.building_library.get() {
131131
@ast::item {
132132
attrs: item.attrs.iter().filter_map(|attr| {
@@ -145,7 +145,7 @@ impl fold::ast_fold for TestHarnessGenerator {
145145

146146
let mod_nomain = ast::_mod {
147147
view_items: m.view_items.clone(),
148-
items: m.items.iter().map(|i| nomain(self.cx, *i)).collect(),
148+
items: m.items.iter().map(|i| nomain(&self.cx, *i)).collect(),
149149
};
150150

151151
fold::noop_fold_mod(&mod_nomain, self)
@@ -154,7 +154,7 @@ impl fold::ast_fold for TestHarnessGenerator {
154154

155155
fn generate_test_harness(sess: session::Session, crate: ast::Crate)
156156
-> ast::Crate {
157-
let cx: @TestCtxt = @TestCtxt {
157+
let mut cx: TestCtxt = TestCtxt {
158158
sess: sess,
159159
ext_cx: ExtCtxt::new(sess.parse_sess, sess.opts.cfg.clone()),
160160
path: RefCell::new(~[]),
@@ -163,8 +163,7 @@ fn generate_test_harness(sess: session::Session, crate: ast::Crate)
163163
config: crate.config.clone(),
164164
};
165165

166-
let ext_cx = cx.ext_cx;
167-
ext_cx.bt_push(ExpnInfo {
166+
cx.ext_cx.bt_push(ExpnInfo {
168167
call_site: dummy_sp(),
169168
callee: NameAndSpan {
170169
name: @"test",
@@ -173,11 +172,11 @@ fn generate_test_harness(sess: session::Session, crate: ast::Crate)
173172
}
174173
});
175174

176-
let fold = TestHarnessGenerator {
175+
let mut fold = TestHarnessGenerator {
177176
cx: cx
178177
};
179178
let res = fold.fold_crate(crate);
180-
ext_cx.bt_pop();
179+
fold.cx.ext_cx.bt_pop();
181180
return res;
182181
}
183182

@@ -190,7 +189,7 @@ fn strip_test_functions(crate: ast::Crate) -> ast::Crate {
190189
})
191190
}
192191

193-
fn is_test_fn(cx: @TestCtxt, i: @ast::item) -> bool {
192+
fn is_test_fn(cx: &TestCtxt, i: @ast::item) -> bool {
194193
let has_test_attr = attr::contains_name(i.attrs, "test");
195194

196195
fn has_test_signature(i: @ast::item) -> bool {
@@ -243,7 +242,7 @@ fn is_bench_fn(i: @ast::item) -> bool {
243242
return has_bench_attr && has_test_signature(i);
244243
}
245244

246-
fn is_ignored(cx: @TestCtxt, i: @ast::item) -> bool {
245+
fn is_ignored(cx: &TestCtxt, i: @ast::item) -> bool {
247246
i.attrs.iter().any(|attr| {
248247
// check ignore(cfg(foo, bar))
249248
"ignore" == attr.name() && match attr.meta_item_list() {
@@ -313,7 +312,7 @@ fn mk_test_module(cx: &TestCtxt) -> @ast::item {
313312

314313
// The synthesized main function which will call the console test runner
315314
// with our list of tests
316-
let mainfn = (quote_item!(cx.ext_cx,
315+
let mainfn = (quote_item!(&cx.ext_cx,
317316
pub fn main() {
318317
#[main];
319318
extra::test::test_main_static(::std::os::args(), TESTS);
@@ -377,7 +376,7 @@ fn mk_tests(cx: &TestCtxt) -> @ast::item {
377376
// The vector of test_descs for this crate
378377
let test_descs = mk_test_descs(cx);
379378

380-
(quote_item!(cx.ext_cx,
379+
(quote_item!(&cx.ext_cx,
381380
pub static TESTS : &'static [self::extra::test::TestDescAndFn] =
382381
$test_descs
383382
;
@@ -438,24 +437,24 @@ fn mk_test_desc_and_fn_rec(cx: &TestCtxt, test: &Test) -> @ast::Expr {
438437
};
439438

440439
let t_expr = if test.bench {
441-
quote_expr!(cx.ext_cx, self::extra::test::StaticBenchFn($fn_expr) )
440+
quote_expr!(&cx.ext_cx, self::extra::test::StaticBenchFn($fn_expr) )
442441
} else {
443-
quote_expr!(cx.ext_cx, self::extra::test::StaticTestFn($fn_expr) )
442+
quote_expr!(&cx.ext_cx, self::extra::test::StaticTestFn($fn_expr) )
444443
};
445444

446445
let ignore_expr = if test.ignore {
447-
quote_expr!(cx.ext_cx, true )
446+
quote_expr!(&cx.ext_cx, true )
448447
} else {
449-
quote_expr!(cx.ext_cx, false )
448+
quote_expr!(&cx.ext_cx, false )
450449
};
451450

452451
let fail_expr = if test.should_fail {
453-
quote_expr!(cx.ext_cx, true )
452+
quote_expr!(&cx.ext_cx, true )
454453
} else {
455-
quote_expr!(cx.ext_cx, false )
454+
quote_expr!(&cx.ext_cx, false )
456455
};
457456

458-
let e = quote_expr!(cx.ext_cx,
457+
let e = quote_expr!(&cx.ext_cx,
459458
self::extra::test::TestDescAndFn {
460459
desc: self::extra::test::TestDesc {
461460
name: self::extra::test::StaticTestName($name_expr),

src/librustc/middle/astencode.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ struct NestedItemsDropper {
301301
}
302302

303303
impl fold::ast_fold for NestedItemsDropper {
304-
fn fold_block(&self, blk: ast::P<ast::Block>) -> ast::P<ast::Block> {
304+
fn fold_block(&mut self, blk: ast::P<ast::Block>) -> ast::P<ast::Block> {
305305
let stmts_sans_items = blk.stmts.iter().filter_map(|stmt| {
306306
match stmt.node {
307307
ast::StmtExpr(_, _) | ast::StmtSemi(_, _) |
@@ -340,7 +340,7 @@ impl fold::ast_fold for NestedItemsDropper {
340340
// nested items, as otherwise it would get confused when translating
341341
// inlined items.
342342
fn simplify_ast(ii: &ast::inlined_item) -> ast::inlined_item {
343-
let fld = NestedItemsDropper {
343+
let mut fld = NestedItemsDropper {
344344
contents: (),
345345
};
346346

@@ -365,17 +365,17 @@ struct AstRenumberer {
365365
}
366366

367367
impl fold::ast_fold for AstRenumberer {
368-
fn new_id(&self, id: ast::NodeId) -> ast::NodeId {
368+
fn new_id(&mut self, id: ast::NodeId) -> ast::NodeId {
369369
self.xcx.tr_id(id)
370370
}
371-
fn new_span(&self, span: Span) -> Span {
371+
fn new_span(&mut self, span: Span) -> Span {
372372
self.xcx.tr_span(span)
373373
}
374374
}
375375

376376
fn renumber_ast(xcx: @ExtendedDecodeContext, ii: ast::inlined_item)
377377
-> ast::inlined_item {
378-
let fld = AstRenumberer {
378+
let mut fld = AstRenumberer {
379379
xcx: xcx,
380380
};
381381
match ii {

src/librustpkg/util.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,12 @@ struct ListenerFn {
7575

7676
struct ReadyCtx {
7777
sess: session::Session,
78-
ext_cx: @ExtCtxt,
78+
ext_cx: ExtCtxt,
7979
path: ~[ast::Ident],
8080
fns: ~[ListenerFn]
8181
}
8282

83-
fn fold_mod(_ctx: @mut ReadyCtx, m: &ast::_mod, fold: &CrateSetup)
83+
fn fold_mod(_ctx: @mut ReadyCtx, m: &ast::_mod, fold: &mut CrateSetup)
8484
-> ast::_mod {
8585
fn strip_main(item: @ast::item) -> @ast::item {
8686
@ast::item {
@@ -101,7 +101,7 @@ fn fold_mod(_ctx: @mut ReadyCtx, m: &ast::_mod, fold: &CrateSetup)
101101
}, fold)
102102
}
103103

104-
fn fold_item(ctx: @mut ReadyCtx, item: @ast::item, fold: &CrateSetup)
104+
fn fold_item(ctx: @mut ReadyCtx, item: @ast::item, fold: &mut CrateSetup)
105105
-> SmallVector<@ast::item> {
106106
ctx.path.push(item.ident);
107107

@@ -145,10 +145,10 @@ struct CrateSetup {
145145
}
146146

147147
impl fold::ast_fold for CrateSetup {
148-
fn fold_item(&self, item: @ast::item) -> SmallVector<@ast::item> {
148+
fn fold_item(&mut self, item: @ast::item) -> SmallVector<@ast::item> {
149149
fold_item(self.ctx, item, self)
150150
}
151-
fn fold_mod(&self, module: &ast::_mod) -> ast::_mod {
151+
fn fold_mod(&mut self, module: &ast::_mod) -> ast::_mod {
152152
fold_mod(self.ctx, module, self)
153153
}
154154
}
@@ -162,7 +162,7 @@ pub fn ready_crate(sess: session::Session,
162162
path: ~[],
163163
fns: ~[]
164164
};
165-
let fold = CrateSetup {
165+
let mut fold = CrateSetup {
166166
ctx: ctx,
167167
};
168168
fold.fold_crate(crate)

src/libsyntax/ext/asm.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ fn next_state(s: State) -> Option<State> {
3737
}
3838
}
3939

40-
pub fn expand_asm(cx: @ExtCtxt, sp: Span, tts: &[ast::token_tree])
40+
pub fn expand_asm(cx: &mut ExtCtxt, sp: Span, tts: &[ast::token_tree])
4141
-> base::MacResult {
4242
let p = parse::new_parser_from_tts(cx.parse_sess(),
4343
cx.cfg(),

0 commit comments

Comments
 (0)