Skip to content

Commit 42f6608

Browse files
committed
Add visit_ty_params to visit.rs
And use it to make typechecking of bounds less error-prone.
1 parent e11d207 commit 42f6608

File tree

2 files changed

+22
-28
lines changed

2 files changed

+22
-28
lines changed

src/comp/middle/typeck.rs

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1514,7 +1514,7 @@ fn lookup_method(fcx: @fn_ctxt, isc: resolve::iscopes,
15141514
ty::bound_iface(t) {
15151515
let (iid, tps) = alt ty::struct(tcx, t) {
15161516
ty::ty_iface(i, tps) { (i, tps) }
1517-
_ { ret none; }
1517+
_ { cont; }
15181518
};
15191519
let ifce_methods = ty::iface_methods(tcx, iid);
15201520
alt vec::position_pred(*ifce_methods, {|m| m.ident == name}) {
@@ -2765,15 +2765,12 @@ fn check_item(ccx: @crate_ctxt, it: @ast::item) {
27652765
alt it.node {
27662766
ast::item_const(_, e) { check_const(ccx, it.span, e, it.id); }
27672767
ast::item_fn(decl, tps, body) {
2768-
check_ty_params(ccx, tps);
27692768
check_fn(ccx, ast::proto_bare, decl, body, it.id, none);
27702769
}
27712770
ast::item_res(decl, tps, body, dtor_id, _) {
2772-
check_ty_params(ccx, tps);
27732771
check_fn(ccx, ast::proto_bare, decl, body, dtor_id, none);
27742772
}
27752773
ast::item_obj(ob, tps, _) {
2776-
check_ty_params(ccx, tps);
27772774
// We're entering an object, so gather up the info we need.
27782775
ccx.self_infos += [self_obj(ob.fields,
27792776
ccx.tcx.tcache.get(local_def(it.id)).ty)];
@@ -2783,10 +2780,8 @@ fn check_item(ccx: @crate_ctxt, it: @ast::item) {
27832780
vec::pop(ccx.self_infos);
27842781
}
27852782
ast::item_impl(tps, ifce, ty, ms) {
2786-
check_ty_params(ccx, tps);
27872783
ccx.self_infos += [self_impl(ast_ty_to_ty(ccx.tcx, m_check, ty))];
27882784
let my_methods = vec::map(ms, {|m|
2789-
check_ty_params(ccx, m.tps);
27902785
check_method(ccx, m);
27912786
ty_of_method(ccx.tcx, m_check, m)
27922787
});
@@ -2821,20 +2816,10 @@ fn check_item(ccx: @crate_ctxt, it: @ast::item) {
28212816
_ {}
28222817
}
28232818
}
2824-
ast::item_iface(tps, _) | ast::item_ty(_, tps) | ast::item_tag(_, tps) {
2825-
check_ty_params(ccx, tps);
2826-
}
28272819
_ {/* nothing to do */ }
28282820
}
28292821
}
28302822

2831-
fn check_native_item(ccx: @crate_ctxt, it: @ast::native_item) {
2832-
alt it.node {
2833-
ast::native_item_fn(_, tps) { check_ty_params(ccx, tps); }
2834-
_ {}
2835-
}
2836-
}
2837-
28382823
fn check_ty_params(ccx: @crate_ctxt, tps: [ast::ty_param]) {
28392824
for tp in tps {
28402825
let i = 0u;
@@ -3100,11 +3085,11 @@ fn check_crate(tcx: ty::ctxt, impl_map: resolve::impl_map,
31003085
method_map: std::map::new_int_hash(),
31013086
dict_map: std::map::new_int_hash(),
31023087
tcx: tcx};
3103-
let visit =
3104-
visit::mk_simple_visitor(@{visit_item: bind check_item(ccx, _),
3105-
visit_native_item:
3106-
bind check_native_item(ccx, _)
3107-
with *visit::default_simple_visitor()});
3088+
let visit = visit::mk_simple_visitor(@{
3089+
visit_item: bind check_item(ccx, _),
3090+
visit_ty_params: bind check_ty_params(ccx, _)
3091+
with *visit::default_simple_visitor()
3092+
});
31083093
visit::visit_crate(*crate, (), visit);
31093094
check_for_main_fn(tcx, crate);
31103095
tcx.sess.abort_if_errors();

src/comp/syntax/visit.rs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ type visitor<E> =
5252
visit_decl: fn@(@decl, E, vt<E>),
5353
visit_expr: fn@(@expr, E, vt<E>),
5454
visit_ty: fn@(@ty, E, vt<E>),
55+
visit_ty_params: fn@([ty_param], E, vt<E>),
5556
visit_constr: fn@(@path, span, node_id, E, vt<E>),
5657
visit_fn: fn@(fn_kind, fn_decl, blk, span, node_id, E, vt<E>)};
5758

@@ -68,6 +69,7 @@ fn default_visitor<E>() -> visitor<E> {
6869
visit_decl: bind visit_decl::<E>(_, _, _),
6970
visit_expr: bind visit_expr::<E>(_, _, _),
7071
visit_ty: bind skip_ty::<E>(_, _, _),
72+
visit_ty_params: bind visit_ty_params::<E>(_, _, _),
7173
visit_constr: bind visit_constr::<E>(_, _, _, _, _),
7274
visit_fn: bind visit_fn::<E>(_, _, _, _, _, _, _)};
7375
}
@@ -113,27 +115,27 @@ fn visit_item<E>(i: @item, e: E, v: vt<E>) {
113115
for vi: @view_item in nm.view_items { v.visit_view_item(vi, e, v); }
114116
for ni: @native_item in nm.items { v.visit_native_item(ni, e, v); }
115117
}
116-
item_ty(t, tps) { v.visit_ty(t, e, v); visit_ty_params(tps, e, v); }
118+
item_ty(t, tps) { v.visit_ty(t, e, v); v.visit_ty_params(tps, e, v); }
117119
item_res(decl, tps, body, dtor_id, _) {
118120
v.visit_fn(fk_res(i.ident, tps), decl, body, i.span,
119121
dtor_id, e, v);
120122
}
121123
item_tag(variants, tps) {
122-
visit_ty_params(tps, e, v);
124+
v.visit_ty_params(tps, e, v);
123125
for vr: variant in variants {
124126
for va: variant_arg in vr.node.args { v.visit_ty(va.ty, e, v); }
125127
}
126128
}
127129
item_obj(ob, tps, _) {
128-
visit_ty_params(tps, e, v);
130+
v.visit_ty_params(tps, e, v);
129131
for f: obj_field in ob.fields { v.visit_ty(f.ty, e, v); }
130132
for m: @method in ob.methods {
131133
v.visit_fn(fk_method(m.ident, m.tps), m.decl, m.body, m.span,
132134
m.id, e, v);
133135
}
134136
}
135137
item_impl(tps, ifce, ty, methods) {
136-
visit_ty_params(tps, e, v);
138+
v.visit_ty_params(tps, e, v);
137139
alt ifce { some(ty) { v.visit_ty(ty, e, v); } _ {} }
138140
v.visit_ty(ty, e, v);
139141
for m in methods {
@@ -142,7 +144,7 @@ fn visit_item<E>(i: @item, e: E, v: vt<E>) {
142144
}
143145
}
144146
item_iface(tps, methods) {
145-
visit_ty_params(tps, e, v);
147+
v.visit_ty_params(tps, e, v);
146148
for m in methods {
147149
for a in m.decl.inputs { v.visit_ty(a.ty, e, v); }
148150
v.visit_ty(m.decl.output, e, v);
@@ -217,7 +219,7 @@ fn visit_pat<E>(p: @pat, e: E, v: vt<E>) {
217219
fn visit_native_item<E>(ni: @native_item, e: E, v: vt<E>) {
218220
alt ni.node {
219221
native_item_fn(fd, tps) {
220-
visit_ty_params(tps, e, v);
222+
v.visit_ty_params(tps, e, v);
221223
visit_fn_decl(fd, e, v);
222224
}
223225
native_item_ty. { }
@@ -246,7 +248,7 @@ fn visit_fn_decl<E>(fd: fn_decl, e: E, v: vt<E>) {
246248
fn visit_fn<E>(fk: fn_kind, decl: fn_decl, body: blk, _sp: span,
247249
_id: node_id, e: E, v: vt<E>) {
248250
visit_fn_decl(decl, e, v);
249-
visit_ty_params(tps_of_fn(fk), e, v);
251+
v.visit_ty_params(tps_of_fn(fk), e, v);
250252
v.visit_block(body, e, v);
251253
}
252254

@@ -414,6 +416,7 @@ type simple_visitor =
414416
visit_decl: fn@(@decl),
415417
visit_expr: fn@(@expr),
416418
visit_ty: fn@(@ty),
419+
visit_ty_params: fn@([ty_param]),
417420
visit_constr: fn@(@path, span, node_id),
418421
visit_fn: fn@(fn_kind, fn_decl, blk, span, node_id)};
419422

@@ -432,6 +435,7 @@ fn default_simple_visitor() -> simple_visitor {
432435
visit_decl: fn(_d: @decl) { },
433436
visit_expr: fn(_e: @expr) { },
434437
visit_ty: simple_ignore_ty,
438+
visit_ty_params: fn(_ps: [ty_param]) {},
435439
visit_constr: fn(_p: @path, _sp: span, _id: node_id) { },
436440
visit_fn: fn(_fk: fn_kind, _d: fn_decl, _b: blk, _sp: span,
437441
_id: node_id) { }
@@ -488,6 +492,10 @@ fn mk_simple_visitor(v: simple_visitor) -> vt<()> {
488492
f(ty);
489493
visit_ty(ty, e, v);
490494
}
495+
fn v_ty_params(f: fn@([ty_param]), ps: [ty_param], &&e: (), v: vt<()>) {
496+
f(ps);
497+
visit_ty_params(ps, e, v);
498+
}
491499
fn v_constr(f: fn@(@path, span, node_id), pt: @path, sp: span,
492500
id: node_id, &&e: (), v: vt<()>) {
493501
f(pt, sp, id);
@@ -517,6 +525,7 @@ fn mk_simple_visitor(v: simple_visitor) -> vt<()> {
517525
visit_decl: bind v_decl(v.visit_decl, _, _, _),
518526
visit_expr: bind v_expr(v.visit_expr, _, _, _),
519527
visit_ty: visit_ty,
528+
visit_ty_params: bind v_ty_params(v.visit_ty_params, _, _, _),
520529
visit_constr: bind v_constr(v.visit_constr, _, _, _, _, _),
521530
visit_fn: bind v_fn(v.visit_fn, _, _, _, _, _, _, _)
522531
});

0 commit comments

Comments
 (0)