Skip to content

Commit

Permalink
librustc: Better errors for duplicate definitions.
Browse files Browse the repository at this point in the history
  • Loading branch information
luqmana committed Jun 10, 2013
1 parent 5a3e1cd commit a95232a
Showing 1 changed file with 16 additions and 22 deletions.
38 changes: 16 additions & 22 deletions src/librustc/middle/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,18 +294,6 @@ pub enum DuplicateCheckingMode {
OverwriteDuplicates
}

// Returns the namespace associated with the given duplicate checking mode,
// or fails for OverwriteDuplicates. This is used for error messages.
pub fn namespace_for_duplicate_checking_mode(mode: DuplicateCheckingMode)
-> Namespace {
match mode {
ForbidDuplicateModules | ForbidDuplicateTypes |
ForbidDuplicateTypesAndValues => TypeNS,
ForbidDuplicateValues => ValueNS,
OverwriteDuplicates => fail!("OverwriteDuplicates has no namespace")
}
}

/// One local scope.
pub struct Rib {
bindings: @mut HashMap<ident,def_like>,
Expand Down Expand Up @@ -1007,37 +995,43 @@ impl Resolver {
// nothing.

let mut is_duplicate = false;
match duplicate_checking_mode {
let ns = match duplicate_checking_mode {
ForbidDuplicateModules => {
is_duplicate =
child.get_module_if_available().is_some();
is_duplicate = child.get_module_if_available().is_some();
Some(TypeNS)
}
ForbidDuplicateTypes => {
match child.def_for_namespace(TypeNS) {
Some(def_mod(_)) | None => {}
Some(_) => is_duplicate = true
}
Some(TypeNS)
}
ForbidDuplicateValues => {
is_duplicate = child.defined_in_namespace(ValueNS);
Some(ValueNS)
}
ForbidDuplicateTypesAndValues => {
let mut n = None;
match child.def_for_namespace(TypeNS) {
Some(def_mod(_)) | None => {}
Some(_) => is_duplicate = true
Some(_) => {
n = Some(TypeNS);
is_duplicate = true;
}
};
if child.defined_in_namespace(ValueNS) {
is_duplicate = true;
n = Some(ValueNS);
}
n
}
OverwriteDuplicates => {}
}
if duplicate_checking_mode != OverwriteDuplicates &&
is_duplicate {
OverwriteDuplicates => None
};
if is_duplicate {
// Return an error here by looking up the namespace that
// had the duplicate.
let ns = namespace_for_duplicate_checking_mode(
duplicate_checking_mode);
let ns = ns.unwrap();
self.session.span_err(sp,
fmt!("duplicate definition of %s `%s`",
namespace_to_str(ns),
Expand Down

0 comments on commit a95232a

Please sign in to comment.