Skip to content

Commit 4127c67

Browse files
committed
Ported check_const from oldvisit to <V:Visitor> trait API.
1 parent ef854c9 commit 4127c67

File tree

1 file changed

+60
-36
lines changed

1 file changed

+60
-36
lines changed

src/librustc/middle/check_const.rs

Lines changed: 60 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -17,47 +17,71 @@ use util::ppaux;
1717

1818
use syntax::ast::*;
1919
use syntax::codemap;
20-
use syntax::{oldvisit, ast_util, ast_map};
20+
use syntax::{ast_util, ast_map};
21+
use syntax::visit::Visitor;
22+
use syntax::visit;
23+
24+
struct CheckCrateVisitor {
25+
sess: Session,
26+
ast_map: ast_map::map,
27+
def_map: resolve::DefMap,
28+
method_map: typeck::method_map,
29+
tcx: ty::ctxt,
30+
}
31+
32+
impl Visitor<bool> for CheckCrateVisitor {
33+
fn visit_item(&mut self, i:@item, env:bool) {
34+
check_item(self, self.sess, self.ast_map, self.def_map, i, env);
35+
}
36+
fn visit_pat(&mut self, p:@pat, env:bool) {
37+
check_pat(self, p, env);
38+
}
39+
fn visit_expr(&mut self, ex:@expr, env:bool) {
40+
check_expr(self, self.sess, self.def_map, self.method_map,
41+
self.tcx, ex, env);
42+
}
43+
}
2144

2245
pub fn check_crate(sess: Session,
2346
crate: &Crate,
2447
ast_map: ast_map::map,
2548
def_map: resolve::DefMap,
2649
method_map: typeck::method_map,
2750
tcx: ty::ctxt) {
28-
oldvisit::visit_crate(crate, (false, oldvisit::mk_vt(@oldvisit::Visitor {
29-
visit_item: |a,b| check_item(sess, ast_map, def_map, a, b),
30-
visit_pat: check_pat,
31-
visit_expr: |a,b|
32-
check_expr(sess, def_map, method_map, tcx, a, b),
33-
.. *oldvisit::default_visitor()
34-
})));
51+
let mut v = CheckCrateVisitor {
52+
sess: sess,
53+
ast_map: ast_map,
54+
def_map: def_map,
55+
method_map: method_map,
56+
tcx: tcx,
57+
};
58+
visit::walk_crate(&mut v, crate, false);
3559
sess.abort_if_errors();
3660
}
3761

38-
pub fn check_item(sess: Session,
62+
pub fn check_item(v: &mut CheckCrateVisitor,
63+
sess: Session,
3964
ast_map: ast_map::map,
4065
def_map: resolve::DefMap,
4166
it: @item,
42-
(_is_const, v): (bool,
43-
oldvisit::vt<bool>)) {
67+
_is_const: bool) {
4468
match it.node {
4569
item_static(_, _, ex) => {
46-
(v.visit_expr)(ex, (true, v));
70+
v.visit_expr(ex, true);
4771
check_item_recursion(sess, ast_map, def_map, it);
4872
}
4973
item_enum(ref enum_definition, _) => {
5074
for var in (*enum_definition).variants.iter() {
5175
for ex in var.node.disr_expr.iter() {
52-
(v.visit_expr)(*ex, (true, v));
76+
v.visit_expr(*ex, true);
5377
}
5478
}
5579
}
56-
_ => oldvisit::visit_item(it, (false, v))
80+
_ => visit::walk_item(v, it, false)
5781
}
5882
}
5983

60-
pub fn check_pat(p: @pat, (_is_const, v): (bool, oldvisit::vt<bool>)) {
84+
pub fn check_pat(v: &mut CheckCrateVisitor, p: @pat, _is_const: bool) {
6185
fn is_str(e: @expr) -> bool {
6286
match e.node {
6387
expr_vstore(
@@ -72,22 +96,22 @@ pub fn check_pat(p: @pat, (_is_const, v): (bool, oldvisit::vt<bool>)) {
7296
}
7397
match p.node {
7498
// Let through plain ~-string literals here
75-
pat_lit(a) => if !is_str(a) { (v.visit_expr)(a, (true, v)); },
99+
pat_lit(a) => if !is_str(a) { v.visit_expr(a, true); },
76100
pat_range(a, b) => {
77-
if !is_str(a) { (v.visit_expr)(a, (true, v)); }
78-
if !is_str(b) { (v.visit_expr)(b, (true, v)); }
101+
if !is_str(a) { v.visit_expr(a, true); }
102+
if !is_str(b) { v.visit_expr(b, true); }
79103
}
80-
_ => oldvisit::visit_pat(p, (false, v))
104+
_ => visit::walk_pat(v, p, false)
81105
}
82106
}
83107

84-
pub fn check_expr(sess: Session,
108+
pub fn check_expr(v: &mut CheckCrateVisitor,
109+
sess: Session,
85110
def_map: resolve::DefMap,
86111
method_map: typeck::method_map,
87112
tcx: ty::ctxt,
88113
e: @expr,
89-
(is_const, v): (bool,
90-
oldvisit::vt<bool>)) {
114+
is_const: bool) {
91115
if is_const {
92116
match e.node {
93117
expr_unary(_, deref, _) => { }
@@ -152,8 +176,8 @@ pub fn check_expr(sess: Session,
152176
}
153177
}
154178
}
155-
expr_paren(e) => { check_expr(sess, def_map, method_map,
156-
tcx, e, (is_const, v)); }
179+
expr_paren(e) => { check_expr(v, sess, def_map, method_map,
180+
tcx, e, is_const); }
157181
expr_vstore(_, expr_vstore_slice) |
158182
expr_vec(_, m_imm) |
159183
expr_addr_of(m_imm, _) |
@@ -191,7 +215,7 @@ pub fn check_expr(sess: Session,
191215
}
192216
_ => ()
193217
}
194-
oldvisit::visit_expr(e, (is_const, v));
218+
visit::walk_expr(v, e, is_const);
195219
}
196220

197221
#[deriving(Clone)]
@@ -203,6 +227,8 @@ struct env {
203227
idstack: @mut ~[NodeId]
204228
}
205229

230+
struct CheckItemRecursionVisitor;
231+
206232
// Make sure a const item doesn't recursively refer to itself
207233
// FIXME: Should use the dependency graph when it's available (#1356)
208234
pub fn check_item_recursion(sess: Session,
@@ -217,36 +243,34 @@ pub fn check_item_recursion(sess: Session,
217243
idstack: @mut ~[]
218244
};
219245

220-
let visitor = oldvisit::mk_vt(@oldvisit::Visitor {
221-
visit_item: visit_item,
222-
visit_expr: visit_expr,
223-
.. *oldvisit::default_visitor()
224-
});
225-
(visitor.visit_item)(it, (env, visitor));
246+
let mut visitor = CheckItemRecursionVisitor;
247+
visitor.visit_item(it, env);
248+
}
226249

227-
fn visit_item(it: @item, (env, v): (env, oldvisit::vt<env>)) {
250+
impl Visitor<env> for CheckItemRecursionVisitor {
251+
fn visit_item(&mut self, it: @item, env: env) {
228252
if env.idstack.iter().any(|x| x == &(it.id)) {
229253
env.sess.span_fatal(env.root_it.span, "recursive constant");
230254
}
231255
env.idstack.push(it.id);
232-
oldvisit::visit_item(it, (env, v));
256+
visit::walk_item(self, it, env);
233257
env.idstack.pop();
234258
}
235259

236-
fn visit_expr(e: @expr, (env, v): (env, oldvisit::vt<env>)) {
260+
fn visit_expr(&mut self, e: @expr, env: env) {
237261
match e.node {
238262
expr_path(*) => match env.def_map.find(&e.id) {
239263
Some(&def_static(def_id, _)) if ast_util::is_local(def_id) =>
240264
match env.ast_map.get_copy(&def_id.node) {
241265
ast_map::node_item(it, _) => {
242-
(v.visit_item)(it, (env, v));
266+
self.visit_item(it, env);
243267
}
244268
_ => fail!("const not bound to an item")
245269
},
246270
_ => ()
247271
},
248272
_ => ()
249273
}
250-
oldvisit::visit_expr(e, (env, v));
274+
visit::walk_expr(self, e, env);
251275
}
252276
}

0 commit comments

Comments
 (0)