Skip to content

Commit

Permalink
auto merge of #15443 : pcwalton/rust/module-and-type-with-same-name, …
Browse files Browse the repository at this point in the history
…r=nick29581

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]

r? @nick29581
  • Loading branch information
bors committed Jul 8, 2014
2 parents a325780 + 3c9443b commit 6f46621
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 15 deletions.
33 changes: 22 additions & 11 deletions src/librustc/middle/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ enum BareIdentifierPatternResolution {
#[deriving(PartialEq)]
enum DuplicateCheckingMode {
ForbidDuplicateModules,
ForbidDuplicateTypes,
ForbidDuplicateTypesAndModules,
ForbidDuplicateValues,
ForbidDuplicateTypesAndValues,
OverwriteDuplicates
Expand Down Expand Up @@ -792,10 +792,9 @@ impl PrimitiveTypeTable {

fn namespace_error_to_str(ns: NamespaceError) -> &'static str {
match ns {
NoError => "",
ModuleError => "module",
TypeError => "type",
ValueError => "value",
NoError => "",
ModuleError | TypeError => "type or module",
ValueError => "value",
}
}

Expand Down Expand Up @@ -1033,9 +1032,12 @@ impl<'a> Resolver<'a> {
}
Some(TypeNS)
}
ForbidDuplicateTypes => {
ForbidDuplicateTypesAndModules => {
match child.def_for_namespace(TypeNS) {
Some(DefMod(_)) | None => {}
None => {}
Some(_) if child.get_module_if_available()
.map(|m| m.kind.get()) ==
Some(ImplModuleKind) => {}
Some(_) => duplicate_type = TypeError
}
Some(TypeNS)
Expand Down Expand Up @@ -1177,7 +1179,10 @@ impl<'a> Resolver<'a> {
// These items live in the type namespace.
ItemTy(..) => {
let name_bindings =
self.add_child(ident, parent.clone(), ForbidDuplicateTypes, sp);
self.add_child(ident,
parent.clone(),
ForbidDuplicateTypesAndModules,
sp);

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

ItemEnum(ref enum_definition, _) => {
let name_bindings =
self.add_child(ident, parent.clone(), ForbidDuplicateTypes, sp);
self.add_child(ident,
parent.clone(),
ForbidDuplicateTypesAndModules,
sp);

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

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

ItemTrait(_, _, _, ref methods) => {
let name_bindings =
self.add_child(ident, parent.clone(), ForbidDuplicateTypes, sp);
self.add_child(ident,
parent.clone(),
ForbidDuplicateTypesAndModules,
sp);

// Add all the methods within to a new module.
let parent_link = self.get_parent_link(parent.clone(), ident);
Expand Down
4 changes: 2 additions & 2 deletions src/test/compile-fail/dup-struct-enum-struct-variant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
#![feature(struct_variant)]

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

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

fn main() {}
19 changes: 19 additions & 0 deletions src/test/compile-fail/enum-and-module-in-same-scope.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

mod Foo {
pub static X: int = 42;
}

enum Foo { //~ ERROR duplicate definition of type or module `Foo`
X
}

fn main() {}
2 changes: 1 addition & 1 deletion src/test/compile-fail/issue-3099-a.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@

enum a { b, c }

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

fn main() {}
2 changes: 1 addition & 1 deletion src/test/compile-fail/issue-3099-b.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@

pub mod a {}

pub mod a {} //~ ERROR duplicate definition of module `a`
pub mod a {} //~ ERROR duplicate definition of type or module `a`

fn main() {}

0 comments on commit 6f46621

Please sign in to comment.