Skip to content

Commit 7fe3d82

Browse files
paulstansifergraydon
authored andcommitted
Add a map from def_id s to items in resolve::env.
Use it to provide better error messages in the event of glob-importing the same name multiple times.
1 parent 54ca856 commit 7fe3d82

File tree

2 files changed

+58
-1
lines changed

2 files changed

+58
-1
lines changed

src/comp/middle/resolve.rs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ type def_map = hashmap[uint,def];
8888

8989
type env = rec(crate_map crate_map,
9090
def_map def_map,
91+
hashmap[def_id,@ast::item] ast_map,
9192
hashmap[ast::def_num,import_state] imports,
9293
hashmap[ast::def_num,@indexed_mod] mod_map,
9394
hashmap[def_id,vec[ident]] ext_map,
@@ -107,6 +108,7 @@ tag namespace {
107108
fn resolve_crate(session sess, @ast::crate crate) -> def_map {
108109
auto e = @rec(crate_map = new_uint_hash[ast::crate_num](),
109110
def_map = new_uint_hash[def](),
111+
ast_map = new_def_hash[@ast::item](),
110112
imports = new_int_hash[import_state](),
111113
mod_map = new_int_hash[@indexed_mod](),
112114
ext_map = new_def_hash[vec[ident]](),
@@ -157,14 +159,31 @@ fn map_crate(&@env e, &ast::crate c) {
157159
e.mod_map.insert(defid._1,
158160
@rec(m=some(md), index=index_mod(md),
159161
glob_imports=vec::empty[def]()));
162+
e.ast_map.insert(defid, i);
160163
}
161164
case (ast::item_native_mod(_, ?nmd, ?defid)) {
162165
e.mod_map.insert(defid._1,
163166
@rec(m=none[ast::_mod],
164167
index=index_nmod(nmd),
165168
glob_imports=vec::empty[def]()));
169+
e.ast_map.insert(defid, i);
170+
}
171+
case (ast::item_const(_, _, _, ?defid, _)) {
172+
e.ast_map.insert(defid, i);
173+
}
174+
case (ast::item_fn(_, _, _, ?defid, _)) {
175+
e.ast_map.insert(defid, i);
176+
}
177+
case (ast::item_ty(_, _, _, ?defid, _)) {
178+
e.ast_map.insert(defid, i);
179+
}
180+
case (ast::item_tag(_, _, _, ?defid, _)) {
181+
e.ast_map.insert(defid, i);
182+
}
183+
case (ast::item_obj(_, _, _, ?obj_def_ids, _)) {
184+
e.ast_map.insert(obj_def_ids.ty, i);
185+
e.ast_map.insert(obj_def_ids.ctor, i);
166186
}
167-
case (_) {}
168187
}
169188
}
170189

@@ -899,6 +918,10 @@ fn lookup_glob_in_mod(&env e, @indexed_mod info, list[def] m, &span sp,
899918
} else if (vec::len(matches) == 1u){
900919
ret some[def](matches.(0));
901920
} else {
921+
for (def match in matches) {
922+
e.sess.span_note(e.ast_map.get(ast::def_id_of_def(match)).span,
923+
"'" + id + "' is defined here.");
924+
}
902925
e.sess.span_err(sp, "'" + id + "' is glob-imported from" +
903926
" multiple different modules.");
904927
fail;
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// error-pattern: common2
2+
3+
import mod1::*;
4+
import mod2::*;
5+
6+
mod mod1 {
7+
fn f1() {
8+
log "f1";
9+
}
10+
fn common1() {
11+
log "common"
12+
}
13+
fn common2() {
14+
log "common"
15+
}
16+
}
17+
18+
mod mod2 {
19+
fn f2() {
20+
log "f1";
21+
}
22+
fn common1() {
23+
log "common"
24+
}
25+
fn common2() {
26+
log "common"
27+
}
28+
}
29+
30+
31+
32+
fn main() {
33+
common2();
34+
}

0 commit comments

Comments
 (0)