Skip to content

Commit 887c656

Browse files
committed
librustc: Introduce a new visitor type based on traits and port syntax to it.
This is preparation for removing `@fn`. This does *not* use default methods yet, because I don't know whether they work. If they do, a forthcoming PR will use them. This also changes the precedence of `as`.
1 parent efd6eaf commit 887c656

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+2639
-1382
lines changed

doc/tutorial.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1455,7 +1455,7 @@ compiler needs assistance, though, the arguments and return types may be
14551455
annotated.
14561456

14571457
~~~~
1458-
let square = |x: int| -> uint { x * x as uint };
1458+
let square = |x: int| -> uint { (x * x) as uint };
14591459
~~~~
14601460

14611461
There are several forms of closure, each with its own role. The most

src/librustc/metadata/creader.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@ use metadata::filesearch::FileSearch;
1717
use metadata::loader;
1818

1919
use std::hashmap::HashMap;
20+
use syntax::ast;
2021
use syntax::attr;
2122
use syntax::attr::AttrMetaMethods;
2223
use syntax::codemap::{span, dummy_sp};
2324
use syntax::diagnostic::span_handler;
2425
use syntax::parse::token;
2526
use syntax::parse::token::ident_interner;
26-
use syntax::visit;
27-
use syntax::ast;
27+
use syntax::oldvisit;
2828

2929
// Traverses an AST, reading all the information about use'd crates and extern
3030
// libraries necessary for later resolving, typechecking, linking, etc.
@@ -46,12 +46,12 @@ pub fn read_crates(diag: @span_handler,
4646
intr: intr
4747
};
4848
let v =
49-
visit::mk_simple_visitor(@visit::SimpleVisitor {
49+
oldvisit::mk_simple_visitor(@oldvisit::SimpleVisitor {
5050
visit_view_item: |a| visit_view_item(e, a),
5151
visit_item: |a| visit_item(e, a),
52-
.. *visit::default_simple_visitor()});
52+
.. *oldvisit::default_simple_visitor()});
5353
visit_crate(e, crate);
54-
visit::visit_crate(crate, ((), v));
54+
oldvisit::visit_crate(crate, ((), v));
5555
dump_crates(*e.crate_cache);
5656
warn_if_multiple_versions(e, diag, *e.crate_cache);
5757
}

src/librustc/metadata/encoder.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ use syntax::attr;
3939
use syntax::attr::AttrMetaMethods;
4040
use syntax::diagnostic::span_handler;
4141
use syntax::parse::token::special_idents;
42-
use syntax::{ast_util, visit};
42+
use syntax::{ast_util, oldvisit};
4343
use syntax::parse::token;
4444
use syntax;
4545
use writer = extra::ebml::writer;
@@ -1199,12 +1199,12 @@ fn encode_info_for_items(ecx: &EncodeContext,
11991199
// See comment in `encode_side_tables_for_ii` in astencode
12001200
let ecx_ptr : *() = unsafe { cast::transmute(ecx) };
12011201

1202-
visit::visit_crate(crate, ((), visit::mk_vt(@visit::Visitor {
1202+
oldvisit::visit_crate(crate, ((), oldvisit::mk_vt(@oldvisit::Visitor {
12031203
visit_expr: |_e, (_cx, _v)| { },
12041204
visit_item: {
12051205
let ebml_w = (*ebml_w).clone();
12061206
|i, (cx, v)| {
1207-
visit::visit_item(i, (cx, v));
1207+
oldvisit::visit_item(i, (cx, v));
12081208
match items.get_copy(&i.id) {
12091209
ast_map::node_item(_, pt) => {
12101210
let mut ebml_w = ebml_w.clone();
@@ -1219,7 +1219,7 @@ fn encode_info_for_items(ecx: &EncodeContext,
12191219
visit_foreign_item: {
12201220
let ebml_w = (*ebml_w).clone();
12211221
|ni, (cx, v)| {
1222-
visit::visit_foreign_item(ni, (cx, v));
1222+
oldvisit::visit_foreign_item(ni, (cx, v));
12231223
match items.get_copy(&ni.id) {
12241224
ast_map::node_foreign_item(_, abi, _, pt) => {
12251225
debug!("writing foreign item %s::%s",
@@ -1243,7 +1243,7 @@ fn encode_info_for_items(ecx: &EncodeContext,
12431243
}
12441244
}
12451245
},
1246-
..*visit::default_visitor()
1246+
..*oldvisit::default_visitor()
12471247
})));
12481248
ebml_w.end_tag();
12491249
return /*bad*/(*index).clone();

src/librustc/middle/borrowck/check_loans.rs

+24-22
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use syntax::ast::{m_mutbl, m_imm, m_const};
2727
use syntax::ast;
2828
use syntax::ast_util;
2929
use syntax::codemap::span;
30-
use syntax::visit;
30+
use syntax::oldvisit;
3131
use util::ppaux::Repr;
3232

3333
#[deriving(Clone)]
@@ -54,12 +54,14 @@ pub fn check_loans(bccx: @BorrowckCtxt,
5454
reported: @mut HashSet::new(),
5555
};
5656

57-
let vt = visit::mk_vt(@visit::Visitor {visit_expr: check_loans_in_expr,
58-
visit_local: check_loans_in_local,
59-
visit_block: check_loans_in_block,
60-
visit_pat: check_loans_in_pat,
61-
visit_fn: check_loans_in_fn,
62-
.. *visit::default_visitor()});
57+
let vt = oldvisit::mk_vt(@oldvisit::Visitor {
58+
visit_expr: check_loans_in_expr,
59+
visit_local: check_loans_in_local,
60+
visit_block: check_loans_in_block,
61+
visit_pat: check_loans_in_pat,
62+
visit_fn: check_loans_in_fn,
63+
.. *oldvisit::default_visitor()
64+
});
6365
(vt.visit_block)(body, (clcx, vt));
6466
}
6567

@@ -612,27 +614,27 @@ impl<'self> CheckLoanCtxt<'self> {
612614
}
613615
}
614616

615-
fn check_loans_in_fn<'a>(fk: &visit::fn_kind,
617+
fn check_loans_in_fn<'a>(fk: &oldvisit::fn_kind,
616618
decl: &ast::fn_decl,
617619
body: &ast::Block,
618620
sp: span,
619621
id: ast::NodeId,
620622
(this, visitor): (CheckLoanCtxt<'a>,
621-
visit::vt<CheckLoanCtxt<'a>>)) {
623+
oldvisit::vt<CheckLoanCtxt<'a>>)) {
622624
match *fk {
623-
visit::fk_item_fn(*) |
624-
visit::fk_method(*) => {
625+
oldvisit::fk_item_fn(*) |
626+
oldvisit::fk_method(*) => {
625627
// Don't process nested items.
626628
return;
627629
}
628630

629-
visit::fk_anon(*) |
630-
visit::fk_fn_block(*) => {
631+
oldvisit::fk_anon(*) |
632+
oldvisit::fk_fn_block(*) => {
631633
check_captured_variables(this, id, sp);
632634
}
633635
}
634636

635-
visit::visit_fn(fk, decl, body, sp, id, (this, visitor));
637+
oldvisit::visit_fn(fk, decl, body, sp, id, (this, visitor));
636638

637639
fn check_captured_variables(this: CheckLoanCtxt,
638640
closure_id: ast::NodeId,
@@ -677,14 +679,14 @@ fn check_loans_in_fn<'a>(fk: &visit::fn_kind,
677679

678680
fn check_loans_in_local<'a>(local: @ast::Local,
679681
(this, vt): (CheckLoanCtxt<'a>,
680-
visit::vt<CheckLoanCtxt<'a>>)) {
681-
visit::visit_local(local, (this, vt));
682+
oldvisit::vt<CheckLoanCtxt<'a>>)) {
683+
oldvisit::visit_local(local, (this, vt));
682684
}
683685

684686
fn check_loans_in_expr<'a>(expr: @ast::expr,
685687
(this, vt): (CheckLoanCtxt<'a>,
686-
visit::vt<CheckLoanCtxt<'a>>)) {
687-
visit::visit_expr(expr, (this, vt));
688+
oldvisit::vt<CheckLoanCtxt<'a>>)) {
689+
oldvisit::visit_expr(expr, (this, vt));
688690

689691
debug!("check_loans_in_expr(expr=%s)",
690692
expr.repr(this.tcx()));
@@ -737,17 +739,17 @@ fn check_loans_in_expr<'a>(expr: @ast::expr,
737739

738740
fn check_loans_in_pat<'a>(pat: @ast::pat,
739741
(this, vt): (CheckLoanCtxt<'a>,
740-
visit::vt<CheckLoanCtxt<'a>>))
742+
oldvisit::vt<CheckLoanCtxt<'a>>))
741743
{
742744
this.check_for_conflicting_loans(pat.id);
743745
this.check_move_out_from_id(pat.id, pat.span);
744-
visit::visit_pat(pat, (this, vt));
746+
oldvisit::visit_pat(pat, (this, vt));
745747
}
746748

747749
fn check_loans_in_block<'a>(blk: &ast::Block,
748750
(this, vt): (CheckLoanCtxt<'a>,
749-
visit::vt<CheckLoanCtxt<'a>>))
751+
oldvisit::vt<CheckLoanCtxt<'a>>))
750752
{
751-
visit::visit_block(blk, (this, vt));
753+
oldvisit::visit_block(blk, (this, vt));
752754
this.check_for_conflicting_loans(blk.id);
753755
}

src/librustc/middle/borrowck/gather_loans/mod.rs

+31-29
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ use syntax::ast;
3131
use syntax::ast_util::id_range;
3232
use syntax::codemap::span;
3333
use syntax::print::pprust;
34-
use syntax::visit;
34+
use syntax::oldvisit;
3535

3636
mod lifetime;
3737
mod restrictions;
@@ -85,46 +85,48 @@ pub fn gather_loans(bccx: @BorrowckCtxt,
8585
move_data: @mut MoveData::new()
8686
};
8787
glcx.gather_fn_arg_patterns(decl, body);
88-
let v = visit::mk_vt(@visit::Visitor {visit_expr: gather_loans_in_expr,
89-
visit_block: gather_loans_in_block,
90-
visit_fn: gather_loans_in_fn,
91-
visit_stmt: add_stmt_to_map,
92-
visit_pat: add_pat_to_id_range,
93-
visit_local: gather_loans_in_local,
94-
.. *visit::default_visitor()});
88+
let v = oldvisit::mk_vt(@oldvisit::Visitor {
89+
visit_expr: gather_loans_in_expr,
90+
visit_block: gather_loans_in_block,
91+
visit_fn: gather_loans_in_fn,
92+
visit_stmt: add_stmt_to_map,
93+
visit_pat: add_pat_to_id_range,
94+
visit_local: gather_loans_in_local,
95+
.. *oldvisit::default_visitor()
96+
});
9597
(v.visit_block)(body, (glcx, v));
9698
return (glcx.id_range, glcx.all_loans, glcx.move_data);
9799
}
98100

99101
fn add_pat_to_id_range(p: @ast::pat,
100102
(this, v): (@mut GatherLoanCtxt,
101-
visit::vt<@mut GatherLoanCtxt>)) {
103+
oldvisit::vt<@mut GatherLoanCtxt>)) {
102104
// NB: This visitor function just adds the pat ids into the id
103105
// range. We gather loans that occur in patterns using the
104106
// `gather_pat()` method below. Eventually these two should be
105107
// brought together.
106108
this.id_range.add(p.id);
107-
visit::visit_pat(p, (this, v));
109+
oldvisit::visit_pat(p, (this, v));
108110
}
109111

110-
fn gather_loans_in_fn(fk: &visit::fn_kind,
112+
fn gather_loans_in_fn(fk: &oldvisit::fn_kind,
111113
decl: &ast::fn_decl,
112114
body: &ast::Block,
113115
sp: span,
114116
id: ast::NodeId,
115117
(this, v): (@mut GatherLoanCtxt,
116-
visit::vt<@mut GatherLoanCtxt>)) {
118+
oldvisit::vt<@mut GatherLoanCtxt>)) {
117119
match fk {
118120
// Do not visit items here, the outer loop in borrowck/mod
119121
// will visit them for us in turn.
120-
&visit::fk_item_fn(*) | &visit::fk_method(*) => {
122+
&oldvisit::fk_item_fn(*) | &oldvisit::fk_method(*) => {
121123
return;
122124
}
123125

124126
// Visit closures as part of the containing item.
125-
&visit::fk_anon(*) | &visit::fk_fn_block(*) => {
127+
&oldvisit::fk_anon(*) | &oldvisit::fk_fn_block(*) => {
126128
this.push_repeating_id(body.id);
127-
visit::visit_fn(fk, decl, body, sp, id, (this, v));
129+
oldvisit::visit_fn(fk, decl, body, sp, id, (this, v));
128130
this.pop_repeating_id(body.id);
129131
this.gather_fn_arg_patterns(decl, body);
130132
}
@@ -133,14 +135,14 @@ fn gather_loans_in_fn(fk: &visit::fn_kind,
133135

134136
fn gather_loans_in_block(blk: &ast::Block,
135137
(this, vt): (@mut GatherLoanCtxt,
136-
visit::vt<@mut GatherLoanCtxt>)) {
138+
oldvisit::vt<@mut GatherLoanCtxt>)) {
137139
this.id_range.add(blk.id);
138-
visit::visit_block(blk, (this, vt));
140+
oldvisit::visit_block(blk, (this, vt));
139141
}
140142

141143
fn gather_loans_in_local(local: @ast::Local,
142144
(this, vt): (@mut GatherLoanCtxt,
143-
visit::vt<@mut GatherLoanCtxt>)) {
145+
oldvisit::vt<@mut GatherLoanCtxt>)) {
144146
match local.init {
145147
None => {
146148
// Variable declarations without initializers are considered "moves":
@@ -171,12 +173,12 @@ fn gather_loans_in_local(local: @ast::Local,
171173
}
172174
}
173175

174-
visit::visit_local(local, (this, vt));
176+
oldvisit::visit_local(local, (this, vt));
175177
}
176178

177179
fn gather_loans_in_expr(ex: @ast::expr,
178180
(this, vt): (@mut GatherLoanCtxt,
179-
visit::vt<@mut GatherLoanCtxt>)) {
181+
oldvisit::vt<@mut GatherLoanCtxt>)) {
180182
let bccx = this.bccx;
181183
let tcx = bccx.tcx;
182184

@@ -216,7 +218,7 @@ fn gather_loans_in_expr(ex: @ast::expr,
216218
// for the lifetime `scope_r` of the resulting ptr:
217219
let scope_r = ty_region(tcx, ex.span, ty::expr_ty(tcx, ex));
218220
this.guarantee_valid(ex.id, ex.span, base_cmt, mutbl, scope_r);
219-
visit::visit_expr(ex, (this, vt));
221+
oldvisit::visit_expr(ex, (this, vt));
220222
}
221223

222224
ast::expr_assign(l, _) | ast::expr_assign_op(_, _, l, _) => {
@@ -233,7 +235,7 @@ fn gather_loans_in_expr(ex: @ast::expr,
233235
// with moves etc, just ignore.
234236
}
235237
}
236-
visit::visit_expr(ex, (this, vt));
238+
oldvisit::visit_expr(ex, (this, vt));
237239
}
238240

239241
ast::expr_match(ex_v, ref arms) => {
@@ -243,7 +245,7 @@ fn gather_loans_in_expr(ex: @ast::expr,
243245
this.gather_pat(cmt, *pat, Some((arm.body.id, ex.id)));
244246
}
245247
}
246-
visit::visit_expr(ex, (this, vt));
248+
oldvisit::visit_expr(ex, (this, vt));
247249
}
248250

249251
ast::expr_index(_, _, arg) |
@@ -257,7 +259,7 @@ fn gather_loans_in_expr(ex: @ast::expr,
257259
let scope_r = ty::re_scope(ex.id);
258260
let arg_cmt = this.bccx.cat_expr(arg);
259261
this.guarantee_valid(arg.id, arg.span, arg_cmt, m_imm, scope_r);
260-
visit::visit_expr(ex, (this, vt));
262+
oldvisit::visit_expr(ex, (this, vt));
261263
}
262264

263265
// see explanation attached to the `root_ub` field:
@@ -276,17 +278,17 @@ fn gather_loans_in_expr(ex: @ast::expr,
276278
// see explanation attached to the `root_ub` field:
277279
ast::expr_loop(ref body, _) => {
278280
this.push_repeating_id(body.id);
279-
visit::visit_expr(ex, (this, vt));
281+
oldvisit::visit_expr(ex, (this, vt));
280282
this.pop_repeating_id(body.id);
281283
}
282284

283285
ast::expr_fn_block(*) => {
284286
gather_moves::gather_captures(this.bccx, this.move_data, ex);
285-
visit::visit_expr(ex, (this, vt));
287+
oldvisit::visit_expr(ex, (this, vt));
286288
}
287289

288290
_ => {
289-
visit::visit_expr(ex, (this, vt));
291+
oldvisit::visit_expr(ex, (this, vt));
290292
}
291293
}
292294
}
@@ -762,12 +764,12 @@ impl GatherLoanCtxt {
762764
// This is just the most convenient place to do it.
763765
fn add_stmt_to_map(stmt: @ast::stmt,
764766
(this, vt): (@mut GatherLoanCtxt,
765-
visit::vt<@mut GatherLoanCtxt>)) {
767+
oldvisit::vt<@mut GatherLoanCtxt>)) {
766768
match stmt.node {
767769
ast::stmt_expr(_, id) | ast::stmt_semi(_, id) => {
768770
this.bccx.stmt_map.insert(id);
769771
}
770772
_ => ()
771773
}
772-
visit::visit_stmt(stmt, (this, vt));
774+
oldvisit::visit_stmt(stmt, (this, vt));
773775
}

0 commit comments

Comments
 (0)