From 5e3ffb6cbd90793041d612d3a1a7941c8ee0cf80 Mon Sep 17 00:00:00 2001 From: Haitao Li Date: Tue, 20 Dec 2011 03:56:11 +0800 Subject: [PATCH] rustc: Re-export the same name in different namespaces Issue #1115. --- src/comp/metadata/encoder.rs | 3 ++- src/comp/middle/resolve.rs | 40 ++++++++++++++++++++++++--------- src/comp/middle/trans_common.rs | 4 ++-- 3 files changed, 34 insertions(+), 13 deletions(-) diff --git a/src/comp/metadata/encoder.rs b/src/comp/metadata/encoder.rs index 26f55caec684..1e844d39bca5 100644 --- a/src/comp/metadata/encoder.rs +++ b/src/comp/metadata/encoder.rs @@ -155,7 +155,8 @@ fn encode_item_paths(ebml_w: ebml::writer, ecx: @encode_ctxt, crate: @crate) fn encode_reexport_paths(ebml_w: ebml::writer, ecx: @encode_ctxt, &index: [entry]) { - ecx.ccx.exp_map.items {|path, def| + ecx.ccx.exp_map.items {|key, def| + let path = key.path; index += [{val: path, pos: ebml_w.writer.tell()}]; ebml::start_tag(ebml_w, tag_paths_data_item); encode_name(ebml_w, path); diff --git a/src/comp/middle/resolve.rs b/src/comp/middle/resolve.rs index 9620931995b1..0082b9f0d617 100644 --- a/src/comp/middle/resolve.rs +++ b/src/comp/middle/resolve.rs @@ -79,6 +79,22 @@ fn new_ext_hash() -> ext_hash { ret std::map::mk_hashmap::(hash, eq); } +fn new_exp_hash() -> exp_map { + type key = {path: str, ns: namespace}; + fn hash(v: key) -> uint { + ret str::hash(v.path) + + alt v.ns { + ns_value. { 1u } + ns_type. { 2u } + ns_module. { 3u } + }; + } + fn eq(v1: key, v2: key) -> bool { + ret str::eq(v1.path, v2.path) && v1.ns == v2.ns; + } + ret std::map::mk_hashmap::(hash, eq); +} + tag mod_index_entry { mie_view_item(@ast::view_item); mie_import_ident(node_id, codemap::span); @@ -105,7 +121,7 @@ type indexed_mod = { type def_map = hashmap; type ext_map = hashmap; -type exp_map = hashmap; +type exp_map = hashmap<{path: str, ns: namespace}, def>; type impl_map = hashmap; type impl_cache = hashmap; @@ -142,7 +158,7 @@ fn resolve_crate(sess: session, amap: ast_map::map, crate: @ast::crate) -> def_map: new_int_hash(), ast_map: amap, imports: new_int_hash(), - exp_map: new_str_hash(), + exp_map: new_exp_hash(), mod_map: new_int_hash(), block_map: new_int_hash(), ext_map: new_def_hash(), @@ -1363,6 +1379,9 @@ fn lookup_external(e: env, cnum: int, ids: [ident], ns: namespace) -> let cname = cstore::get_crate_data(e.cstore, did.crate).name; let name = csearch::get_item_name(e.cstore, did.crate, did.node); + log #fmt("lookup_external: %s %d, %d, %s, %s", cname, + did.crate, did.node, + str::connect(ids, "::"), name); e.ext_map.insert(did, vec::init(ids) + [name]); } else { e.ext_map.insert(did, ids); @@ -1615,15 +1634,16 @@ fn check_exports(e: @env) { let (m, v, t) = (lookup(ns_module), lookup(ns_value), lookup(ns_type)); - maybe_add_reexport(e, path + ident, m); - maybe_add_reexport(e, path + ident, v); - maybe_add_reexport(e, path + ident, t); + maybe_add_reexport(e, path + ident, ns_module, m); + maybe_add_reexport(e, path + ident, ns_value, v); + maybe_add_reexport(e, path + ident, ns_type, t); ret is_some(m) || is_some(v) || is_some(t); } - fn maybe_add_reexport(e: @env, path: str, def: option::t) { + fn maybe_add_reexport(e: @env, path: str, ns: namespace, + def: option::t) { if option::is_some(def) { - e.exp_map.insert(path, option::get(def)); + e.exp_map.insert({path: path, ns: ns}, option::get(def)); } } @@ -1635,9 +1655,9 @@ fn check_exports(e: @env) { mie_import_ident(id, _) { alt e.imports.get(id) { resolved(v, t, m, _, rid, _) { - maybe_add_reexport(e, val.path + rid, v); - maybe_add_reexport(e, val.path + rid, t); - maybe_add_reexport(e, val.path + rid, m); + maybe_add_reexport(e, val.path + rid, ns_value, v); + maybe_add_reexport(e, val.path + rid, ns_type, t); + maybe_add_reexport(e, val.path + rid, ns_module, m); } _ { } } diff --git a/src/comp/middle/trans_common.rs b/src/comp/middle/trans_common.rs index f30184124b05..9d0f14e6bd1c 100644 --- a/src/comp/middle/trans_common.rs +++ b/src/comp/middle/trans_common.rs @@ -9,7 +9,7 @@ import std::map::hashmap; import option::some; import syntax::ast; import driver::session; -import middle::ty; +import middle::{resolve, ty}; import back::{link, abi, upcall}; import util::common::*; import syntax::codemap::span; @@ -84,7 +84,7 @@ type crate_ctxt = intrinsics: hashmap, item_ids: hashmap, ast_map: ast_map::map, - exp_map: hashmap, + exp_map: resolve::exp_map, item_symbols: hashmap, mutable main_fn: option::t, link_meta: link::link_meta,