about summary refs log tree commit diff
path: root/src/test
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2014-07-04 04:56:16 +0000
committerbors <bors@rust-lang.org>2014-07-04 04:56:16 +0000
commit1bff1ff810dcfa8064c11e2b84473f053d1f69f1 (patch)
tree17705fe81d6cf0a5365c0a870c1260b5e56a2403 /src/test
parentd623a8bf3c530b900a69c059568079d5cbb4d6b8 (diff)
parentc3ae64a5cff47edcbe73885bc00e715d082ef5ec (diff)
downloadrust-1bff1ff810dcfa8064c11e2b84473f053d1f69f1.tar.gz
rust-1bff1ff810dcfa8064c11e2b84473f053d1f69f1.zip
auto merge of #15356 : pcwalton/rust/wrong-implementor, r=alexcrichton
parameters.

This can break code that mistakenly used type parameters in place of
`Self`. For example, this will break:

    trait Foo {
        fn bar<X>(u: X) -> Self {
            u
        }
    }

Change this code to not contain a type error. For example:

    trait Foo {
        fn bar<X>(_: X) -> Self {
            self
        }
    }

Closes #15172.

[breaking-change]

r? @alexcrichton
Diffstat (limited to 'src/test')
-rw-r--r--src/test/compile-fail/type-params-in-different-spaces-1.rs25
-rw-r--r--src/test/compile-fail/type-params-in-different-spaces-2.rs35
-rw-r--r--src/test/compile-fail/type-params-in-different-spaces-3.rs18
3 files changed, 78 insertions, 0 deletions
diff --git a/src/test/compile-fail/type-params-in-different-spaces-1.rs b/src/test/compile-fail/type-params-in-different-spaces-1.rs
new file mode 100644
index 00000000000..6e32e6e4835
--- /dev/null
+++ b/src/test/compile-fail/type-params-in-different-spaces-1.rs
@@ -0,0 +1,25 @@
+// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+use std::num::Num;
+
+trait BrokenAdd: Num {
+    fn broken_add<T>(&self, rhs: T) -> Self {
+        *self + rhs //~ ERROR mismatched types
+    }
+}
+
+impl<T: Num> BrokenAdd for T {}
+
+pub fn main() {
+    let foo: u8 = 0u8;
+    let x: u8 = foo.broken_add("hello darkness my old friend".to_string());
+    println!("{}", x);
+}
diff --git a/src/test/compile-fail/type-params-in-different-spaces-2.rs b/src/test/compile-fail/type-params-in-different-spaces-2.rs
new file mode 100644
index 00000000000..955efeef30d
--- /dev/null
+++ b/src/test/compile-fail/type-params-in-different-spaces-2.rs
@@ -0,0 +1,35 @@
+// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+trait Tr<T> {
+    fn op(T) -> Self;
+}
+
+// these compile as if Self: Tr<U>, even tho only Self: Tr<Self or T>
+trait A:    Tr<Self> {
+    fn test<U>(u: U) -> Self {
+        Tr::op(u)   //~ ERROR expected Tr<U>, but found Tr<Self>
+    }
+}
+trait B<T>: Tr<T> {
+    fn test<U>(u: U) -> Self {
+        Tr::op(u)   //~ ERROR expected Tr<U>, but found Tr<T>
+    }
+}
+
+impl<T> Tr<T> for T {
+    fn op(t: T) -> T { t }
+}
+impl<T> A for T {}
+
+fn main() {
+    std::io::println(A::test((&7306634593706211700, 8)));
+}
+
diff --git a/src/test/compile-fail/type-params-in-different-spaces-3.rs b/src/test/compile-fail/type-params-in-different-spaces-3.rs
new file mode 100644
index 00000000000..a3d69d53ba9
--- /dev/null
+++ b/src/test/compile-fail/type-params-in-different-spaces-3.rs
@@ -0,0 +1,18 @@
+// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+trait Tr {
+    fn test<X>(u: X) -> Self {
+        u   //~ ERROR mismatched types
+    }
+}
+
+fn main() {}
+