Skip to content

Commit a0cf531

Browse files
Test that we get the proper errors
1 parent 9aaef06 commit a0cf531

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// compile-fail
2+
3+
#![feature(specialization)]
4+
5+
// Test that attempting to override a non-default method or one not in the
6+
// parent impl causes an error
7+
8+
trait Foo {
9+
fn foo(&self) -> bool { true }
10+
}
11+
12+
// Specialization tree for Foo:
13+
//
14+
// Box<T> Vec<T>
15+
// / \ / \
16+
// Box<i32> Box<i64> Vec<()> Vec<bool>
17+
18+
impl<T> Foo for Box<T> {
19+
fn foo(&self) -> bool { false }
20+
}
21+
22+
// Allowed
23+
impl Foo for Box<i32> {}
24+
25+
// Can't override a non-`default` fn
26+
impl Foo for Box<i64> {
27+
fn foo(&self) -> bool { true }
28+
//~^ error: `foo` specializes an item from a parent `impl`, but that item is not marked `default`
29+
}
30+
31+
32+
// Doesn't mention the method = provided body is used and the method is final
33+
impl<T> Foo for Vec<T> {}
34+
35+
// Allowed
36+
impl Foo for Vec<()> {}
37+
38+
impl Foo for Vec<bool> {
39+
fn foo(&self) -> bool { true }
40+
//~^ error: `foo` specializes an item from a parent `impl`, but that item is not marked `default`
41+
}
42+
43+
fn main() {}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
error[E0520]: `foo` specializes an item from a parent `impl`, but that item is not marked `default`
2+
--> $DIR/specialization-default-methods-fail.rs:27:5
3+
|
4+
LL | / impl<T> Foo for Box<T> {
5+
LL | | fn foo(&self) -> bool { false }
6+
LL | | }
7+
| |_- parent `impl` is here
8+
...
9+
LL | fn foo(&self) -> bool { true }
10+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot specialize default item `foo`
11+
|
12+
= note: to specialize, `foo` in the parent `impl` must be marked `default`
13+
14+
error[E0520]: `foo` specializes an item from a parent `impl`, but that item is not marked `default`
15+
--> $DIR/specialization-default-methods-fail.rs:39:5
16+
|
17+
LL | impl<T> Foo for Vec<T> {}
18+
| ------------------------- parent `impl` is here
19+
...
20+
LL | fn foo(&self) -> bool { true }
21+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot specialize default item `foo`
22+
|
23+
= note: to specialize, `foo` in the parent `impl` must be marked `default`
24+
25+
error: aborting due to 2 previous errors
26+
27+
For more information about this error, try `rustc --explain E0520`.

0 commit comments

Comments
 (0)