Skip to content

Commit ba2e892

Browse files
authored
Auto merge of #37447 - estebank:non-duplicate-definition-error, r=nrc
Show one error for duplicated type definitions For the following code: ``` rustc struct Bar; struct Bar; fn main () { } ``` show ``` nocode error[E0428]: a type named `Bar` has already been defined in this module --> src/test/compile-fail/E0428.rs:12:1 | 11 | struct Bar; | ----------- previous definition of `Bar` here 12 | struct Bar; | ^^^^^^^^^^^ error: aborting due to previous error ``` instead of ``` nocode error[E0428]: a type named `Bar` has already been defined in this module --> src/test/compile-fail/E0428.rs:12:1 | 11 | struct Bar; | ----------- previous definition of `Bar` here 12 | struct Bar; | ^^^^^^^^^^^ error[E0428]: a value named `Bar` has already been defined in this module --> src/test/compile-fail/E0428.rs:12:1 | 11 | struct Bar; | ----------- previous definition of `Bar` here 12 | struct Bar; | ^^^^^^^^^^^ error: aborting due to 2 previous errors ``` Fixes #35767.
2 parents 4da129d + 43aed32 commit ba2e892

File tree

5 files changed

+14
-13
lines changed

5 files changed

+14
-13
lines changed

src/librustc_resolve/lib.rs

+13-1
Original file line numberDiff line numberDiff line change
@@ -1118,6 +1118,9 @@ pub struct Resolver<'a> {
11181118

11191119
// Maps the `Mark` of an expansion to its containing module or block.
11201120
invocations: FxHashMap<Mark, &'a InvocationData<'a>>,
1121+
1122+
// Avoid duplicated errors for "name already defined".
1123+
name_already_seen: FxHashMap<Name, Span>,
11211124
}
11221125

11231126
pub struct ResolverArenas<'a> {
@@ -1310,6 +1313,7 @@ impl<'a> Resolver<'a> {
13101313
macro_map: FxHashMap(),
13111314
macro_exports: Vec::new(),
13121315
invocations: invocations,
1316+
name_already_seen: FxHashMap(),
13131317
}
13141318
}
13151319

@@ -3396,7 +3400,7 @@ impl<'a> Resolver<'a> {
33963400
}
33973401
}
33983402

3399-
fn report_conflict(&self,
3403+
fn report_conflict(&mut self,
34003404
parent: Module,
34013405
name: Name,
34023406
ns: Namespace,
@@ -3420,6 +3424,13 @@ impl<'a> Resolver<'a> {
34203424
};
34213425

34223426
let span = binding.span;
3427+
3428+
if let Some(s) = self.name_already_seen.get(&name) {
3429+
if s == &span {
3430+
return;
3431+
}
3432+
}
3433+
34233434
let msg = {
34243435
let kind = match (ns, old_binding.module()) {
34253436
(ValueNS, _) => "a value",
@@ -3472,6 +3483,7 @@ impl<'a> Resolver<'a> {
34723483
err.span_label(old_binding.span, &format!("previous {} of `{}` here", noun, name));
34733484
}
34743485
err.emit();
3486+
self.name_already_seen.insert(name, span);
34753487
}
34763488
}
34773489

src/test/compile-fail/E0428.rs

-3
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,8 @@
99
// except according to those terms.
1010

1111
struct Bar; //~ previous definition of `Bar` here
12-
//~| previous definition of `Bar` here
1312
struct Bar; //~ ERROR E0428
1413
//~| NOTE already defined
15-
//~| ERROR E0428
16-
//~| NOTE already defined
1714

1815
fn main () {
1916
}

src/test/compile-fail/blind-item-block-item-shadow.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ fn main() {
1414
{
1515
struct Bar;
1616
use foo::Bar;
17-
//~^ ERROR a type named `Bar` has already been defined in this block
18-
//~^^ ERROR a value named `Bar` has already been defined in this block
17+
//~^ ERROR a value named `Bar` has already been defined in this block
1918
}
2019
}

src/test/compile-fail/double-type-import.rs

-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ mod foo {
1212
pub use self::bar::X;
1313
use self::bar::X;
1414
//~^ ERROR a value named `X` has already been imported in this module
15-
//~| ERROR a type named `X` has already been imported in this module
1615

1716
mod bar {
1817
pub struct X;

src/test/compile-fail/variant-namespacing.rs

-6
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,11 @@ const XUnit: u8 = 0;
3333
extern crate variant_namespacing;
3434
pub use variant_namespacing::XE::*;
3535
//~^ ERROR `XStruct` has already been defined
36-
//~| ERROR `XStruct` has already been defined
3736
//~| ERROR `XTuple` has already been defined
38-
//~| ERROR `XTuple` has already been defined
39-
//~| ERROR `XUnit` has already been defined
4037
//~| ERROR `XUnit` has already been defined
4138
pub use E::*;
4239
//~^ ERROR `Struct` has already been defined
43-
//~| ERROR `Struct` has already been defined
4440
//~| ERROR `Tuple` has already been defined
45-
//~| ERROR `Tuple` has already been defined
46-
//~| ERROR `Unit` has already been defined
4741
//~| ERROR `Unit` has already been defined
4842

4943
fn main() {}

0 commit comments

Comments
 (0)