about summary refs log tree commit diff
diff options
context:
space:
mode:
authorJonas Schievink <jonasschievink@gmail.com>2019-09-19 00:37:28 +0200
committerJonas Schievink <jonasschievink@gmail.com>2019-10-05 15:33:26 +0200
commita0cf5315ef516c4f79bea39c5446a3a6ef6a0d24 (patch)
treeedf954faddb1bde4f618d5a9480700e2f9feafab
parent9aaef06cf6a0c4e671c51f0d13a4ae6cf8b8dec5 (diff)
downloadrust-a0cf5315ef516c4f79bea39c5446a3a6ef6a0d24.tar.gz
rust-a0cf5315ef516c4f79bea39c5446a3a6ef6a0d24.zip
Test that we get the proper errors
-rw-r--r--src/test/ui/specialization/specialization-default-methods-fail.rs43
-rw-r--r--src/test/ui/specialization/specialization-default-methods-fail.stderr27
2 files changed, 70 insertions, 0 deletions
diff --git a/src/test/ui/specialization/specialization-default-methods-fail.rs b/src/test/ui/specialization/specialization-default-methods-fail.rs
new file mode 100644
index 00000000000..3bd2d31ef63
--- /dev/null
+++ b/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() {}
diff --git a/src/test/ui/specialization/specialization-default-methods-fail.stderr b/src/test/ui/specialization/specialization-default-methods-fail.stderr
new file mode 100644
index 00000000000..f962ccca83d
--- /dev/null
+++ b/src/test/ui/specialization/specialization-default-methods-fail.stderr
@@ -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`.