Skip to content

Commit

Permalink
Test that we get the proper errors
Browse files Browse the repository at this point in the history
  • Loading branch information
jonas-schievink committed Oct 5, 2019
1 parent 9aaef06 commit a0cf531
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
43 changes: 43 additions & 0 deletions src/test/ui/specialization/specialization-default-methods-fail.rs
@@ -0,0 +1,43 @@
// compile-fail

#![feature(specialization)]

// Test that attempting to override a non-default method or one not in the
// parent impl causes an error

trait Foo {
fn foo(&self) -> bool { true }
}

// Specialization tree for Foo:
//
// Box<T> Vec<T>
// / \ / \
// Box<i32> Box<i64> Vec<()> Vec<bool>

impl<T> Foo for Box<T> {
fn foo(&self) -> bool { false }
}

// Allowed
impl Foo for Box<i32> {}

// Can't override a non-`default` fn
impl Foo for Box<i64> {
fn foo(&self) -> bool { true }
//~^ error: `foo` specializes an item from a parent `impl`, but that item is not marked `default`
}


// Doesn't mention the method = provided body is used and the method is final
impl<T> Foo for Vec<T> {}

// Allowed
impl Foo for Vec<()> {}

impl Foo for Vec<bool> {
fn foo(&self) -> bool { true }
//~^ error: `foo` specializes an item from a parent `impl`, but that item is not marked `default`
}

fn main() {}
@@ -0,0 +1,27 @@
error[E0520]: `foo` specializes an item from a parent `impl`, but that item is not marked `default`
--> $DIR/specialization-default-methods-fail.rs:27:5
|
LL | / impl<T> Foo for Box<T> {
LL | | fn foo(&self) -> bool { false }
LL | | }
| |_- parent `impl` is here
...
LL | fn foo(&self) -> bool { true }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot specialize default item `foo`
|
= note: to specialize, `foo` in the parent `impl` must be marked `default`

error[E0520]: `foo` specializes an item from a parent `impl`, but that item is not marked `default`
--> $DIR/specialization-default-methods-fail.rs:39:5
|
LL | impl<T> Foo for Vec<T> {}
| ------------------------- parent `impl` is here
...
LL | fn foo(&self) -> bool { true }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot specialize default item `foo`
|
= note: to specialize, `foo` in the parent `impl` must be marked `default`

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0520`.

0 comments on commit a0cf531

Please sign in to comment.