Skip to content

Commit 3c9443b

Browse files
committed
librustc: Disallow modules and types from having the same name.
This will break code that looks like: struct Foo { ... } mod Foo { ... } Change this code to: struct Foo { ... } impl Foo { ... } Or rename the module. Closes #15205. [breaking-change]
1 parent 4f120e6 commit 3c9443b

File tree

5 files changed

+45
-15
lines changed

5 files changed

+45
-15
lines changed

src/librustc/middle/resolve.rs

+22-11
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ enum BareIdentifierPatternResolution {
307307
#[deriving(PartialEq)]
308308
enum DuplicateCheckingMode {
309309
ForbidDuplicateModules,
310-
ForbidDuplicateTypes,
310+
ForbidDuplicateTypesAndModules,
311311
ForbidDuplicateValues,
312312
ForbidDuplicateTypesAndValues,
313313
OverwriteDuplicates
@@ -792,10 +792,9 @@ impl PrimitiveTypeTable {
792792

793793
fn namespace_error_to_str(ns: NamespaceError) -> &'static str {
794794
match ns {
795-
NoError => "",
796-
ModuleError => "module",
797-
TypeError => "type",
798-
ValueError => "value",
795+
NoError => "",
796+
ModuleError | TypeError => "type or module",
797+
ValueError => "value",
799798
}
800799
}
801800

@@ -1033,9 +1032,12 @@ impl<'a> Resolver<'a> {
10331032
}
10341033
Some(TypeNS)
10351034
}
1036-
ForbidDuplicateTypes => {
1035+
ForbidDuplicateTypesAndModules => {
10371036
match child.def_for_namespace(TypeNS) {
1038-
Some(DefMod(_)) | None => {}
1037+
None => {}
1038+
Some(_) if child.get_module_if_available()
1039+
.map(|m| m.kind.get()) ==
1040+
Some(ImplModuleKind) => {}
10391041
Some(_) => duplicate_type = TypeError
10401042
}
10411043
Some(TypeNS)
@@ -1177,7 +1179,10 @@ impl<'a> Resolver<'a> {
11771179
// These items live in the type namespace.
11781180
ItemTy(..) => {
11791181
let name_bindings =
1180-
self.add_child(ident, parent.clone(), ForbidDuplicateTypes, sp);
1182+
self.add_child(ident,
1183+
parent.clone(),
1184+
ForbidDuplicateTypesAndModules,
1185+
sp);
11811186

11821187
name_bindings.define_type
11831188
(DefTy(local_def(item.id)), sp, is_public);
@@ -1186,7 +1191,10 @@ impl<'a> Resolver<'a> {
11861191

11871192
ItemEnum(ref enum_definition, _) => {
11881193
let name_bindings =
1189-
self.add_child(ident, parent.clone(), ForbidDuplicateTypes, sp);
1194+
self.add_child(ident,
1195+
parent.clone(),
1196+
ForbidDuplicateTypesAndModules,
1197+
sp);
11901198

11911199
name_bindings.define_type
11921200
(DefTy(local_def(item.id)), sp, is_public);
@@ -1206,7 +1214,7 @@ impl<'a> Resolver<'a> {
12061214
// Adding to both Type and Value namespaces or just Type?
12071215
let (forbid, ctor_id) = match struct_def.ctor_id {
12081216
Some(ctor_id) => (ForbidDuplicateTypesAndValues, Some(ctor_id)),
1209-
None => (ForbidDuplicateTypes, None)
1217+
None => (ForbidDuplicateTypesAndModules, None)
12101218
};
12111219

12121220
let name_bindings = self.add_child(ident, parent.clone(), forbid, sp);
@@ -1327,7 +1335,10 @@ impl<'a> Resolver<'a> {
13271335

13281336
ItemTrait(_, _, _, ref methods) => {
13291337
let name_bindings =
1330-
self.add_child(ident, parent.clone(), ForbidDuplicateTypes, sp);
1338+
self.add_child(ident,
1339+
parent.clone(),
1340+
ForbidDuplicateTypesAndModules,
1341+
sp);
13311342

13321343
// Add all the methods within to a new module.
13331344
let parent_link = self.get_parent_link(parent.clone(), ident);

src/test/compile-fail/dup-struct-enum-struct-variant.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
#![feature(struct_variant)]
1212

1313
enum Foo { C { a: int, b: int } }
14-
struct C { a: int, b: int } //~ ERROR error: duplicate definition of type `C`
14+
struct C { a: int, b: int } //~ ERROR error: duplicate definition of type or module `C`
1515

1616
struct A { x: int }
17-
enum Bar { A { x: int } } //~ ERROR error: duplicate definition of type `A`
17+
enum Bar { A { x: int } } //~ ERROR error: duplicate definition of type or module `A`
1818

1919
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
mod Foo {
12+
pub static X: int = 42;
13+
}
14+
15+
enum Foo { //~ ERROR duplicate definition of type or module `Foo`
16+
X
17+
}
18+
19+
fn main() {}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@
1010

1111
enum a { b, c }
1212

13-
enum a { d, e } //~ ERROR duplicate definition of type `a`
13+
enum a { d, e } //~ ERROR duplicate definition of type or module `a`
1414

1515
fn main() {}

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

+1-1
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 module `a`
13+
pub mod a {} //~ ERROR duplicate definition of type or module `a`
1414

1515
fn main() {}

0 commit comments

Comments
 (0)