From 587d8a5d4f503ec07a8b964c04376078d14bf307 Mon Sep 17 00:00:00 2001 From: Marijn Haverbeke Date: Mon, 23 Apr 2012 12:10:06 +0200 Subject: [PATCH] Misc code cleanups using list::each for list iteration --- src/rustc/middle/resolve.rs | 45 +++++++---------------- src/rustc/middle/typeck.rs | 72 +++++++++++++++++-------------------- 2 files changed, 46 insertions(+), 71 deletions(-) diff --git a/src/rustc/middle/resolve.rs b/src/rustc/middle/resolve.rs index 4fff72fcc8bb..da125214cb66 100644 --- a/src/rustc/middle/resolve.rs +++ b/src/rustc/middle/resolve.rs @@ -70,18 +70,13 @@ type ext_hash = hashmap<{did: def_id, ident: str, ns: namespace}, def>; fn new_ext_hash() -> ext_hash { type key = {did: def_id, ident: str, ns: namespace}; fn hash(v: key) -> uint { - ret str::hash(v.ident) + util::common::hash_def(v.did) + - alt v.ns { - ns_val { 1u } - ns_type { 2u } - ns_module { 3u } - }; + str::hash(v.ident) + util::common::hash_def(v.did) + v.ns as uint } fn eq(v1: key, v2: key) -> bool { ret util::common::def_eq(v1.did, v2.did) && - str::eq(v1.ident, v2.ident) && v1.ns == v2.ns; + str::eq(v1.ident, v2.ident) && v1.ns == v2.ns; } - ret std::map::hashmap::(hash, eq); + std::map::hashmap(hash, {|a, b| a == b}) } enum mod_index_entry { @@ -851,22 +846,16 @@ enum ctxt { in_mod(def), in_scope(scopes), } fn unresolved_err(e: env, cx: ctxt, sp: span, name: ident, kind: str) { fn find_fn_or_mod_scope(sc: scopes) -> option { - let mut sc = sc; - loop { - alt sc { - cons(cur, rest) { - alt cur { - scope_crate | scope_bare_fn(_, _, _) | - scope_fn_expr(_, _, _) | - scope_item(@{node: ast::item_mod(_), _}) { - ret some(cur); - } - _ { sc = *rest; } - } + for list::each(sc) {|cur| + alt cur { + scope_crate | scope_bare_fn(_, _, _) | scope_fn_expr(_, _, _) | + scope_item(@{node: ast::item_mod(_), _}) { + ret some(cur); } - _ { ret none; } + _ {} } - }; + } + ret none; } let mut path = name; alt cx { @@ -887,9 +876,7 @@ fn unresolved_err(e: env, cx: ctxt, sp: span, name: ident, kind: str) { path = e.mod_map.get(did.node).path + path; } else if did.node != ast::crate_node_id { let paths = e.ext_map.get(did); - if vec::len(paths) > 0u { - path = str::connect(paths, "::") + "::" + path; - } + path = str::connect(paths + [path], "::"); } } } @@ -1673,18 +1660,12 @@ fn ns_for_def(d: def) -> namespace { } } -// if we're searching for a value, it's ok if we found -// a enum -fn ns_ok(wanted:namespace, actual:namespace) -> bool { - wanted == actual -} - fn lookup_external(e: env, cnum: int, ids: [ident], ns: namespace) -> option { let mut result = none; for csearch::lookup_defs(e.sess.cstore, cnum, ids).each {|d| e.ext_map.insert(def_id_of_def(d), ids); - if ns_ok(ns, ns_for_def(d)) { result = some(d); } + if ns == ns_for_def(d) { result = some(d); } } ret result; } diff --git a/src/rustc/middle/typeck.rs b/src/rustc/middle/typeck.rs index 18997266d81b..9574d5918469 100644 --- a/src/rustc/middle/typeck.rs +++ b/src/rustc/middle/typeck.rs @@ -4418,50 +4418,44 @@ mod vtable { ret vtable_iface(did, tps); } _ { - let mut found = none; - list::iter(isc) {|impls| - if option::is_none(found) { - for vec::each(*impls) {|im| - let match = alt ty::impl_iface(tcx, im.did) { - some(ity) { - alt check ty::get(ity).struct { - ty::ty_iface(id, _) { id == iface_id } + for list::each(isc) {|impls| + let mut found = none; + for vec::each(*impls) {|im| + let match = alt ty::impl_iface(tcx, im.did) { + some(ity) { + alt check ty::get(ity).struct { + ty::ty_iface(id, _) { id == iface_id } + } + } + _ { false } + }; + if match { + let {substs: substs, ty: self_ty} = + impl_self_ty(fcx, im.did); + let im_bs = ty::lookup_item_type(tcx, im.did).bounds; + alt unify::unify(fcx, ty, self_ty) { + result::ok(_) { + if option::is_some(found) { + tcx.sess.span_err( + sp, "multiple applicable implementations \ + in scope"); + } else { + let vars = substs.tps; + connect_iface_tps(fcx, sp, vars, + iface_tps, im.did); + let params = vec::map(vars, {|t| + fixup_ty(fcx, sp, t)}); + let subres = lookup_vtables( + fcx, isc, sp, im_bs, params, false); + found = some(vtable_static(im.did, params, + subres)); } } - _ { false } - }; - if match { - let {substs: substs, ty: self_ty} = - impl_self_ty(fcx, im.did); - let im_bs = - ty::lookup_item_type(tcx, im.did).bounds; - alt unify::unify(fcx, ty, self_ty) { - result::ok(_) { - if option::is_some(found) { - tcx.sess.span_err( - sp, "multiple applicable implemen\ - tations in scope"); - } else { - let vars = substs.tps; - connect_iface_tps(fcx, sp, vars, - iface_tps, im.did); - let params = vec::map(vars, {|t| - fixup_ty(fcx, sp, t)}); - let subres = lookup_vtables( - fcx, isc, sp, im_bs, params, false); - found = some(vtable_static(im.did, params, - subres)); - } - } - result::err(_) {} - } + result::err(_) {} } } } - } - alt found { - some(rslt) { ret rslt; } - _ {} + alt found { some(x) { ret x; } _ {} } } } }