Start checking name collisions in modules

(And remove a boatload of duplicate imports from the typestate files.)

Name collision checking in local scopes will follow soon.
This commit is contained in:
Marijn Haverbeke 2011-05-18 22:23:19 +02:00
parent 843739ee98
commit 28c4a5586e
3 changed files with 75 additions and 26 deletions

View File

@ -112,6 +112,7 @@ fn resolve_crate(session sess, @ast::crate crate) -> def_map {
sess = sess);
map_crate(e, *crate);
resolve_imports(*e);
check_for_collisions(e, *crate);
resolve_names(e, *crate);
ret e.def_map;
}
@ -948,6 +949,80 @@ fn lookup_external(&env e, int cnum, vec[ident] ids, namespace ns)
ret none[def];
}
// Collision detection
fn check_for_collisions(&@env e, &ast::crate c) {
auto lim = lookup_in_mie;
auto msp = mie_span;
for each (@tup(ast::def_num, @indexed_mod) m in e.mod_map.items()) {
for each (@tup(ident, list[mod_index_entry]) name in
m._1.index.items()) {
check_mod_name(*e, name._0, name._1, lim, msp);
}
}
auto linm = lookup_in_nmie;
auto nmsp = nmie_span;
for each (@tup(ast::def_num, @indexed_nmod) m in e.nmod_map.items()) {
for each (@tup(ident, list[nmod_index_entry]) name in
m._1.index.items()) {
check_mod_name(*e, name._0, name._1, linm, nmsp);
}
}
/*
auto v = rec(visit_item_pre = bind visit_item(e, _),
with walk::default_visitor());
walk::walk_crate(v, c);
fn visit_item(@env e, &@ast::item i) {
}*/
}
fn check_mod_name[T](&env e, &ident name, &list[T] entries,
fn(&env, &T, namespace) -> option::t[def] test,
fn(&T) -> span get_span) {
auto saw_mod = false; auto saw_type = false; auto saw_value = false;
fn dup(&env e, &span sp, &str word, &ident name) {
e.sess.span_err(sp, "duplicate definition of " + word + name);
}
while (true) {
alt (entries) {
case (cons[T](?entry, ?rest)) {
if (!option::is_none(test(e, entry, ns_value))) {
if (saw_value) { dup(e, get_span(entry), "", name); }
else { saw_value = true; }
}
if (!option::is_none(test(e, entry, ns_type))) {
if (saw_type) { dup(e, get_span(entry), "type ", name); }
else { saw_type = true; }
}
if (!option::is_none(test(e, entry, ns_module))) {
if (saw_mod) { dup(e, get_span(entry), "module ", name); }
else { saw_mod = true; }
}
entries = *rest;
}
case (nil[T]) { break; }
}
}
}
fn mie_span(&mod_index_entry mie) -> span {
alt (mie) {
case (mie_view_item(?item)) { ret item.span; }
case (mie_item(?item)) { ret item.span; }
case (mie_tag_variant(?item, _)) { ret item.span; }
}
}
fn nmie_span(&nmod_index_entry nmie) -> span {
alt (nmie) {
case (nmie_view_item(?item)) { ret item.span; }
case (nmie_item(?item)) { ret item.span; }
}
}
// Local Variables:
// mode: rust
// fill-column: 78;

View File

@ -47,7 +47,6 @@ import front::ast::method;
import front::ast::ann;
import front::ast::ty;
import front::ast::mutability;
import front::ast::item;
import front::ast::item_const;
import front::ast::item_mod;
import front::ast::item_ty;
@ -56,19 +55,15 @@ import front::ast::item_native_mod;
import front::ast::obj_field;
import front::ast::stmt;
import front::ast::stmt_;
import front::ast::stmt_decl;
import front::ast::ident;
import front::ast::def_id;
import front::ast::ann;
import front::ast::expr;
import front::ast::path;
import front::ast::crate_directive;
import front::ast::fn_decl;
import front::ast::_obj;
import front::ast::native_mod;
import front::ast::variant;
import front::ast::ty_param;
import front::ast::ty;
import front::ast::proto;
import front::ast::pat;
import front::ast::binop;
@ -81,18 +76,12 @@ import front::ast::return;
import front::ast::noreturn;
import front::ast::_fn;
import front::ast::_obj;
import front::ast::_mod;
import front::ast::crate;
import front::ast::item_fn;
import front::ast::item_obj;
import front::ast::def_local;
import front::ast::def_fn;
import front::ast::ident;
import front::ast::def_id;
import front::ast::ann;
import front::ast::item;
import front::ast::item_fn;
import front::ast::expr;
import front::ast::elt;
import front::ast::field;
import front::ast::decl;
@ -138,12 +127,10 @@ import front::ast::expr_assert;
import front::ast::expr_cast;
import front::ast::expr_for;
import front::ast::expr_for_each;
import front::ast::stmt;
import front::ast::stmt_decl;
import front::ast::stmt_expr;
import front::ast::block;
import front::ast::block_;
import front::ast::method;
import middle::fold::span;
import middle::fold::respan;

View File

@ -58,19 +58,15 @@ import bitvectors::declare_var;
import bitvectors::bit_num;
import bitvectors::gen_poststate;
import front::ast::_mod;
import front::ast;
import front::ast::_fn;
import front::ast::method;
import front::ast::ann;
import front::ast::ty;
import front::ast::mutability;
import front::ast::item;
import front::ast::obj_field;
import front::ast::stmt;
import front::ast::stmt_;
import front::ast::stmt_decl;
import front::ast::ident;
import front::ast::def_id;
import front::ast::ann;
import front::ast::expr;
@ -81,7 +77,6 @@ import front::ast::_obj;
import front::ast::native_mod;
import front::ast::variant;
import front::ast::ty_param;
import front::ast::ty;
import front::ast::proto;
import front::ast::pat;
import front::ast::binop;
@ -92,7 +87,6 @@ import front::ast::init_op;
import front::ast::controlflow;
import front::ast::return;
import front::ast::noreturn;
import front::ast::_obj;
import front::ast::_mod;
import front::ast::crate;
import front::ast::item_fn;
@ -105,11 +99,6 @@ import front::ast::item_const;
import front::ast::def_local;
import front::ast::def_fn;
import front::ast::ident;
import front::ast::def_id;
import front::ast::ann;
import front::ast::item;
import front::ast::item_fn;
import front::ast::expr;
import front::ast::elt;
import front::ast::field;
import front::ast::decl;
@ -155,12 +144,10 @@ import front::ast::expr_assert;
import front::ast::expr_cast;
import front::ast::expr_for;
import front::ast::expr_for_each;
import front::ast::stmt;
import front::ast::stmt_decl;
import front::ast::stmt_expr;
import front::ast::block;
import front::ast::block_;
import front::ast::method;
import middle::fold::span;
import middle::fold::respan;