Skip to content

Commit a69fcfa

Browse files
author
Ariel Ben-Yehuda
authored
Rollup merge of rust-lang#41052 - topecongiro:overlapping_inherent_impls, r=estebank
Make 'overlapping_inherent_impls' lint a hard error This is ought to be implemented in PR rust-lang#40728. Unfortunately, when I rebased the PR to resolve merge conflict, the "hard error" code disappeared. This PR complements the initial PR. Now the following rust code gives the following error: ```rust struct Foo; impl Foo { fn id() {} } impl Foo { fn id() {} } fn main() {} ``` ``` error[E0592]: duplicate definitions with name `id` --> /home/topecongiro/test.rs:4:5 | 4 | fn id() {} | ^^^^^^^^^^ duplicate definitions for `id` ... 8 | fn id() {} | ---------- other definition for `id` error: aborting due to previous error ```
2 parents 5e410ba + db60b0b commit a69fcfa

5 files changed

+53
-11
lines changed

src/librustc_typeck/coherence/inherent_impls_overlap.rs

+10-7
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
use rustc::hir::def_id::{CrateNum, DefId, LOCAL_CRATE};
1212
use rustc::hir;
1313
use rustc::hir::itemlikevisit::ItemLikeVisitor;
14-
use rustc::lint;
1514
use rustc::traits::{self, Reveal};
1615
use rustc::ty::{self, TyCtxt};
1716

@@ -53,12 +52,16 @@ impl<'a, 'tcx> InherentOverlapChecker<'a, 'tcx> {
5352

5453
for &item2 in &impl_items2[..] {
5554
if (name, namespace) == name_and_namespace(item2) {
56-
let msg = format!("duplicate definitions with name `{}`", name);
57-
let node_id = self.tcx.hir.as_local_node_id(item1).unwrap();
58-
self.tcx.sess.add_lint(lint::builtin::OVERLAPPING_INHERENT_IMPLS,
59-
node_id,
60-
self.tcx.span_of_impl(item1).unwrap(),
61-
msg);
55+
struct_span_err!(self.tcx.sess,
56+
self.tcx.span_of_impl(item1).unwrap(),
57+
E0592,
58+
"duplicate definitions with name `{}`",
59+
name)
60+
.span_label(self.tcx.span_of_impl(item1).unwrap(),
61+
&format!("duplicate definitions for `{}`", name))
62+
.span_label(self.tcx.span_of_impl(item2).unwrap(),
63+
&format!("other definition for `{}`", name))
64+
.emit();
6265
}
6366
}
6467
}

src/test/compile-fail/coherence-overlapping-inherent-impl-trait.rs renamed to src/test/ui/codemap_tests/coherence-overlapping-inherent-impl-trait.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@
1111
#![allow(dead_code)]
1212

1313
trait C {}
14-
impl C { fn f() {} } //~ ERROR duplicate definitions with name `f`
14+
impl C { fn f() {} }
1515
impl C { fn f() {} }
1616
fn main() { }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
error[E0592]: duplicate definitions with name `f`
2+
--> $DIR/coherence-overlapping-inherent-impl-trait.rs:14:10
3+
|
4+
14 | impl C { fn f() {} }
5+
| ^^^^^^^^^ duplicate definitions for `f`
6+
15 | impl C { fn f() {} }
7+
| --------- other definition for `f`
8+
9+
error: aborting due to previous error
10+

src/test/compile-fail/inherent-overlap.rs renamed to src/test/ui/codemap_tests/overlapping_inherent_impls.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
struct Foo;
1717

1818
impl Foo {
19-
fn id() {} //~ ERROR duplicate definitions
19+
fn id() {}
2020
}
2121

2222
impl Foo {
@@ -26,7 +26,7 @@ impl Foo {
2626
struct Bar<T>(T);
2727

2828
impl<T> Bar<T> {
29-
fn bar(&self) {} //~ ERROR duplicate definitions
29+
fn bar(&self) {}
3030
}
3131

3232
impl Bar<u32> {
@@ -36,7 +36,7 @@ impl Bar<u32> {
3636
struct Baz<T>(T);
3737

3838
impl<T: Copy> Baz<T> {
39-
fn baz(&self) {} //~ ERROR duplicate definitions
39+
fn baz(&self) {}
4040
}
4141

4242
impl<T> Baz<Vec<T>> {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
error[E0592]: duplicate definitions with name `id`
2+
--> $DIR/overlapping_inherent_impls.rs:19:5
3+
|
4+
19 | fn id() {}
5+
| ^^^^^^^^^^ duplicate definitions for `id`
6+
...
7+
23 | fn id() {}
8+
| ---------- other definition for `id`
9+
10+
error[E0592]: duplicate definitions with name `bar`
11+
--> $DIR/overlapping_inherent_impls.rs:29:5
12+
|
13+
29 | fn bar(&self) {}
14+
| ^^^^^^^^^^^^^^^^ duplicate definitions for `bar`
15+
...
16+
33 | fn bar(&self) {}
17+
| ---------------- other definition for `bar`
18+
19+
error[E0592]: duplicate definitions with name `baz`
20+
--> $DIR/overlapping_inherent_impls.rs:39:5
21+
|
22+
39 | fn baz(&self) {}
23+
| ^^^^^^^^^^^^^^^^ duplicate definitions for `baz`
24+
...
25+
43 | fn baz(&self) {}
26+
| ---------------- other definition for `baz`
27+
28+
error: aborting due to 3 previous errors
29+

0 commit comments

Comments
 (0)