Skip to content

Commit 4967bd0

Browse files
committed
auto merge of #7325 : artagnon/rust/resolve-module, r=cmr
Fix #7322. I started out with a band-aid approach to special-case the duplicate module error using `is_duplicate_module`, but thought this would be better in the long term.
2 parents 032dcc5 + f982f42 commit 4967bd0

File tree

2 files changed

+28
-14
lines changed

2 files changed

+28
-14
lines changed

src/librustc/middle/resolve.rs

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,14 @@ pub enum Namespace {
101101
ValueNS
102102
}
103103

104+
#[deriving(Eq)]
105+
pub enum NamespaceError {
106+
NoError,
107+
ModuleError,
108+
TypeError,
109+
ValueError
110+
}
111+
104112
/// A NamespaceResult represents the result of resolving an import in
105113
/// a particular namespace. The result is either definitely-resolved,
106114
/// definitely- unresolved, or unknown.
@@ -759,10 +767,12 @@ pub fn PrimitiveTypeTable() -> PrimitiveTypeTable {
759767
}
760768

761769

762-
pub fn namespace_to_str(ns: Namespace) -> ~str {
770+
pub fn namespace_error_to_str(ns: NamespaceError) -> &'static str {
763771
match ns {
764-
TypeNS => ~"type",
765-
ValueNS => ~"value",
772+
NoError => "",
773+
ModuleError => "module",
774+
TypeError => "type",
775+
ValueError => "value",
766776
}
767777
}
768778

@@ -993,21 +1003,25 @@ impl Resolver {
9931003
// * If no duplicate checking was requested at all, do
9941004
// nothing.
9951005

996-
let mut is_duplicate = false;
1006+
let mut duplicate_type = NoError;
9971007
let ns = match duplicate_checking_mode {
9981008
ForbidDuplicateModules => {
999-
is_duplicate = child.get_module_if_available().is_some();
1009+
if (child.get_module_if_available().is_some()) {
1010+
duplicate_type = ModuleError;
1011+
}
10001012
Some(TypeNS)
10011013
}
10021014
ForbidDuplicateTypes => {
10031015
match child.def_for_namespace(TypeNS) {
10041016
Some(def_mod(_)) | None => {}
1005-
Some(_) => is_duplicate = true
1017+
Some(_) => duplicate_type = TypeError
10061018
}
10071019
Some(TypeNS)
10081020
}
10091021
ForbidDuplicateValues => {
1010-
is_duplicate = child.defined_in_namespace(ValueNS);
1022+
if child.defined_in_namespace(ValueNS) {
1023+
duplicate_type = ValueError;
1024+
}
10111025
Some(ValueNS)
10121026
}
10131027
ForbidDuplicateTypesAndValues => {
@@ -1016,31 +1030,31 @@ impl Resolver {
10161030
Some(def_mod(_)) | None => {}
10171031
Some(_) => {
10181032
n = Some(TypeNS);
1019-
is_duplicate = true;
1033+
duplicate_type = TypeError;
10201034
}
10211035
};
10221036
if child.defined_in_namespace(ValueNS) {
1023-
is_duplicate = true;
1037+
duplicate_type = ValueError;
10241038
n = Some(ValueNS);
10251039
}
10261040
n
10271041
}
10281042
OverwriteDuplicates => None
10291043
};
1030-
if is_duplicate {
1044+
if (duplicate_type != NoError) {
10311045
// Return an error here by looking up the namespace that
10321046
// had the duplicate.
10331047
let ns = ns.unwrap();
10341048
self.session.span_err(sp,
10351049
fmt!("duplicate definition of %s `%s`",
1036-
namespace_to_str(ns),
1050+
namespace_error_to_str(duplicate_type),
10371051
self.session.str_of(name)));
10381052
{
10391053
let r = child.span_for_namespace(ns);
10401054
for r.iter().advance |sp| {
10411055
self.session.span_note(*sp,
1042-
fmt!("first definition of %s %s here:",
1043-
namespace_to_str(ns),
1056+
fmt!("first definition of %s `%s` here",
1057+
namespace_error_to_str(duplicate_type),
10441058
self.session.str_of(name)));
10451059
}
10461060
}

src/test/compile-fail/issue-3099-b.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@
1010

1111
pub mod a {}
1212

13-
pub mod a {} //~ ERROR duplicate definition of type `a`
13+
pub mod a {} //~ ERROR duplicate definition of module `a`
1414

1515
fn main() {}

0 commit comments

Comments
 (0)