Skip to content

Commit 587d8a5

Browse files
committed
Misc code cleanups using list::each for list iteration
1 parent a61f107 commit 587d8a5

File tree

2 files changed

+46
-71
lines changed

2 files changed

+46
-71
lines changed

src/rustc/middle/resolve.rs

Lines changed: 13 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -70,18 +70,13 @@ type ext_hash = hashmap<{did: def_id, ident: str, ns: namespace}, def>;
7070
fn new_ext_hash() -> ext_hash {
7171
type key = {did: def_id, ident: str, ns: namespace};
7272
fn hash(v: key) -> uint {
73-
ret str::hash(v.ident) + util::common::hash_def(v.did) +
74-
alt v.ns {
75-
ns_val { 1u }
76-
ns_type { 2u }
77-
ns_module { 3u }
78-
};
73+
str::hash(v.ident) + util::common::hash_def(v.did) + v.ns as uint
7974
}
8075
fn eq(v1: key, v2: key) -> bool {
8176
ret util::common::def_eq(v1.did, v2.did) &&
82-
str::eq(v1.ident, v2.ident) && v1.ns == v2.ns;
77+
str::eq(v1.ident, v2.ident) && v1.ns == v2.ns;
8378
}
84-
ret std::map::hashmap::<key, def>(hash, eq);
79+
std::map::hashmap(hash, {|a, b| a == b})
8580
}
8681

8782
enum mod_index_entry {
@@ -851,22 +846,16 @@ enum ctxt { in_mod(def), in_scope(scopes), }
851846

852847
fn unresolved_err(e: env, cx: ctxt, sp: span, name: ident, kind: str) {
853848
fn find_fn_or_mod_scope(sc: scopes) -> option<scope> {
854-
let mut sc = sc;
855-
loop {
856-
alt sc {
857-
cons(cur, rest) {
858-
alt cur {
859-
scope_crate | scope_bare_fn(_, _, _) |
860-
scope_fn_expr(_, _, _) |
861-
scope_item(@{node: ast::item_mod(_), _}) {
862-
ret some(cur);
863-
}
864-
_ { sc = *rest; }
865-
}
849+
for list::each(sc) {|cur|
850+
alt cur {
851+
scope_crate | scope_bare_fn(_, _, _) | scope_fn_expr(_, _, _) |
852+
scope_item(@{node: ast::item_mod(_), _}) {
853+
ret some(cur);
866854
}
867-
_ { ret none; }
855+
_ {}
868856
}
869-
};
857+
}
858+
ret none;
870859
}
871860
let mut path = name;
872861
alt cx {
@@ -887,9 +876,7 @@ fn unresolved_err(e: env, cx: ctxt, sp: span, name: ident, kind: str) {
887876
path = e.mod_map.get(did.node).path + path;
888877
} else if did.node != ast::crate_node_id {
889878
let paths = e.ext_map.get(did);
890-
if vec::len(paths) > 0u {
891-
path = str::connect(paths, "::") + "::" + path;
892-
}
879+
path = str::connect(paths + [path], "::");
893880
}
894881
}
895882
}
@@ -1673,18 +1660,12 @@ fn ns_for_def(d: def) -> namespace {
16731660
}
16741661
}
16751662

1676-
// if we're searching for a value, it's ok if we found
1677-
// a enum
1678-
fn ns_ok(wanted:namespace, actual:namespace) -> bool {
1679-
wanted == actual
1680-
}
1681-
16821663
fn lookup_external(e: env, cnum: int, ids: [ident], ns: namespace) ->
16831664
option<def> {
16841665
let mut result = none;
16851666
for csearch::lookup_defs(e.sess.cstore, cnum, ids).each {|d|
16861667
e.ext_map.insert(def_id_of_def(d), ids);
1687-
if ns_ok(ns, ns_for_def(d)) { result = some(d); }
1668+
if ns == ns_for_def(d) { result = some(d); }
16881669
}
16891670
ret result;
16901671
}

src/rustc/middle/typeck.rs

Lines changed: 33 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -4418,50 +4418,44 @@ mod vtable {
44184418
ret vtable_iface(did, tps);
44194419
}
44204420
_ {
4421-
let mut found = none;
4422-
list::iter(isc) {|impls|
4423-
if option::is_none(found) {
4424-
for vec::each(*impls) {|im|
4425-
let match = alt ty::impl_iface(tcx, im.did) {
4426-
some(ity) {
4427-
alt check ty::get(ity).struct {
4428-
ty::ty_iface(id, _) { id == iface_id }
4421+
for list::each(isc) {|impls|
4422+
let mut found = none;
4423+
for vec::each(*impls) {|im|
4424+
let match = alt ty::impl_iface(tcx, im.did) {
4425+
some(ity) {
4426+
alt check ty::get(ity).struct {
4427+
ty::ty_iface(id, _) { id == iface_id }
4428+
}
4429+
}
4430+
_ { false }
4431+
};
4432+
if match {
4433+
let {substs: substs, ty: self_ty} =
4434+
impl_self_ty(fcx, im.did);
4435+
let im_bs = ty::lookup_item_type(tcx, im.did).bounds;
4436+
alt unify::unify(fcx, ty, self_ty) {
4437+
result::ok(_) {
4438+
if option::is_some(found) {
4439+
tcx.sess.span_err(
4440+
sp, "multiple applicable implementations \
4441+
in scope");
4442+
} else {
4443+
let vars = substs.tps;
4444+
connect_iface_tps(fcx, sp, vars,
4445+
iface_tps, im.did);
4446+
let params = vec::map(vars, {|t|
4447+
fixup_ty(fcx, sp, t)});
4448+
let subres = lookup_vtables(
4449+
fcx, isc, sp, im_bs, params, false);
4450+
found = some(vtable_static(im.did, params,
4451+
subres));
44294452
}
44304453
}
4431-
_ { false }
4432-
};
4433-
if match {
4434-
let {substs: substs, ty: self_ty} =
4435-
impl_self_ty(fcx, im.did);
4436-
let im_bs =
4437-
ty::lookup_item_type(tcx, im.did).bounds;
4438-
alt unify::unify(fcx, ty, self_ty) {
4439-
result::ok(_) {
4440-
if option::is_some(found) {
4441-
tcx.sess.span_err(
4442-
sp, "multiple applicable implemen\
4443-
tations in scope");
4444-
} else {
4445-
let vars = substs.tps;
4446-
connect_iface_tps(fcx, sp, vars,
4447-
iface_tps, im.did);
4448-
let params = vec::map(vars, {|t|
4449-
fixup_ty(fcx, sp, t)});
4450-
let subres = lookup_vtables(
4451-
fcx, isc, sp, im_bs, params, false);
4452-
found = some(vtable_static(im.did, params,
4453-
subres));
4454-
}
4455-
}
4456-
result::err(_) {}
4457-
}
4454+
result::err(_) {}
44584455
}
44594456
}
44604457
}
4461-
}
4462-
alt found {
4463-
some(rslt) { ret rslt; }
4464-
_ {}
4458+
alt found { some(x) { ret x; } _ {} }
44654459
}
44664460
}
44674461
}

0 commit comments

Comments
 (0)