Skip to content

Commit a1e2a55

Browse files
committed
Auto merge of #26421 - nham:fix_21546, r=pnkfelix
Fixes #21546.
2 parents 540fd3a + f0af1eb commit a1e2a55

File tree

3 files changed

+110
-15
lines changed

3 files changed

+110
-15
lines changed

src/librustc_resolve/build_reduced_graph.rs

+45
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,29 @@ impl<'a, 'b:'a, 'tcx:'b> GraphBuilder<'a, 'b, 'tcx> {
404404
}
405405

406406
ItemMod(..) => {
407+
let child = parent.children.borrow().get(&name).cloned();
408+
if let Some(child) = child {
409+
// check if there's struct of the same name already defined
410+
if child.defined_in_namespace(TypeNS)
411+
&& child.get_module_if_available().is_none() {
412+
self.session.span_warn(sp, &format!(
413+
"duplicate definition of {} `{}`. \
414+
Defining a module and a struct with \
415+
the same name will be disallowed \
416+
soon.",
417+
namespace_error_to_string(TypeError),
418+
name));
419+
{
420+
let r = child.span_for_namespace(TypeNS);
421+
if let Some(sp) = r {
422+
self.session.span_note(sp,
423+
&format!("first definition of {} `{}` here",
424+
namespace_error_to_string(TypeError),
425+
name));
426+
}
427+
}
428+
}
429+
}
407430
let name_bindings = self.add_child(name, parent, ForbidDuplicateModules, sp);
408431

409432
let parent_link = self.get_parent_link(parent, name);
@@ -495,6 +518,28 @@ impl<'a, 'b:'a, 'tcx:'b> GraphBuilder<'a, 'b, 'tcx> {
495518
let (forbid, ctor_id) = if struct_def.is_struct() {
496519
(ForbidDuplicateTypesAndModules, None)
497520
} else {
521+
let child = parent.children.borrow().get(&name).cloned();
522+
if let Some(child) = child {
523+
// check if theres a DefMod
524+
if let Some(DefMod(_)) = child.def_for_namespace(TypeNS) {
525+
self.session.span_warn(sp, &format!(
526+
"duplicate definition of {} `{}`. \
527+
Defining a module and a struct with \
528+
the same name will be disallowed \
529+
soon.",
530+
namespace_error_to_string(TypeError),
531+
name));
532+
{
533+
let r = child.span_for_namespace(TypeNS);
534+
if let Some(sp) = r {
535+
self.session.span_note(sp,
536+
&format!("first definition of {} `{}` here",
537+
namespace_error_to_string(TypeError),
538+
name));
539+
}
540+
}
541+
}
542+
}
498543
(ForbidDuplicateTypesAndValues, Some(struct_def.id()))
499544
};
500545

src/test/compile-fail/issue-21546.rs

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// Copyright 2015 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+
// Also works as a test for #14564
12+
13+
#[allow(non_snake_case)]
14+
mod Foo { }
15+
//~^ NOTE first definition of type or module `Foo`
16+
17+
#[allow(dead_code)]
18+
struct Foo;
19+
//~^ WARNING duplicate definition of type or module `Foo`
20+
21+
22+
#[allow(non_snake_case)]
23+
mod Bar { }
24+
//~^ NOTE first definition of type or module `Bar`
25+
26+
#[allow(dead_code)]
27+
struct Bar(i32);
28+
//~^ WARNING duplicate definition of type or module `Bar`
29+
30+
31+
#[allow(dead_code)]
32+
struct Baz(i32);
33+
//~^ NOTE first definition of type or module
34+
35+
#[allow(non_snake_case)]
36+
mod Baz { }
37+
//~^ WARNING duplicate definition of type or module `Baz`
38+
39+
40+
#[allow(dead_code)]
41+
struct Qux { x: bool }
42+
//~^ NOTE first definition of type or module
43+
44+
#[allow(non_snake_case)]
45+
mod Qux { }
46+
//~^ WARNING duplicate definition of type or module `Qux`
47+
48+
49+
#[allow(dead_code)]
50+
struct Quux;
51+
//~^ NOTE first definition of type or module
52+
53+
#[allow(non_snake_case)]
54+
mod Quux { }
55+
//~^ WARNING duplicate definition of type or module `Quux`
56+
57+
58+
#[allow(dead_code)]
59+
enum Corge { A, B }
60+
61+
#[allow(non_snake_case)]
62+
mod Corge { }
63+
//~^ ERROR duplicate definition of type or module `Corge`
64+
65+
fn main() { }

src/test/run-pass/issue-14564.rs

-15
This file was deleted.

0 commit comments

Comments
 (0)