about summary refs log tree commit diff
path: root/tests/ui/traits/impl-inherent-prefer-over-trait.rs
diff options
context:
space:
mode:
Diffstat (limited to 'tests/ui/traits/impl-inherent-prefer-over-trait.rs')
-rw-r--r--tests/ui/traits/impl-inherent-prefer-over-trait.rs30
1 files changed, 30 insertions, 0 deletions
diff --git a/tests/ui/traits/impl-inherent-prefer-over-trait.rs b/tests/ui/traits/impl-inherent-prefer-over-trait.rs
new file mode 100644
index 00000000000..82760788897
--- /dev/null
+++ b/tests/ui/traits/impl-inherent-prefer-over-trait.rs
@@ -0,0 +1,30 @@
+// run-pass
+
+struct Foo;
+
+trait Trait {
+    fn bar(&self);
+}
+
+// Inherent impls should be preferred over trait ones.
+impl Foo {
+    fn bar(&self) {}
+}
+
+impl dyn Trait {
+    fn baz(_: &Foo) {}
+}
+
+impl Trait for Foo {
+    fn bar(&self) { panic!("wrong method called!") }
+}
+
+fn main() {
+    Foo.bar();
+    Foo::bar(&Foo);
+    <Foo>::bar(&Foo);
+
+    // Should work even if Trait::baz doesn't exist.
+    // N.B: `<Trait>::bar` would be ambiguous.
+    <dyn Trait>::baz(&Foo);
+}