about summary refs log tree commit diff
path: root/src/test
diff options
context:
space:
mode:
authorNiko Matsakis <niko@alum.mit.edu>2014-12-15 21:11:09 -0500
committerNiko Matsakis <niko@alum.mit.edu>2015-01-02 12:08:36 -0500
commit19dcecb2258dcbc6f02be9beac105b57fd43472f (patch)
treec07a4e631f2b9d9ea461cbc11838f8e2f35fe3a6 /src/test
parent2c1d7a7caa279ae3dc284940eebf08cbac75df1e (diff)
downloadrust-19dcecb2258dcbc6f02be9beac105b57fd43472f.tar.gz
rust-19dcecb2258dcbc6f02be9beac105b57fd43472f.zip
Refactor object-safety into its own (cached) module so that we can
check it more easily; also extend object safety to cover sized types
as well as static methods.  This makes it sufficient so that we can
always ensure that `Foo : Foo` holds for any trait `Foo`.
Diffstat (limited to 'src/test')
-rw-r--r--src/test/compile-fail/issue-18959.rs2
-rw-r--r--src/test/compile-fail/object-safety-by-value-self.rs47
-rw-r--r--src/test/compile-fail/object-safety-generics.rs (renamed from src/test/run-pass/trait-object-safety.rs)21
-rw-r--r--src/test/compile-fail/object-safety-mentions-Self.rs47
-rw-r--r--src/test/compile-fail/object-safety-no-static.rs31
-rw-r--r--src/test/compile-fail/object-safety-sized-2.rs33
-rw-r--r--src/test/compile-fail/object-safety-sized.rs31
-rw-r--r--src/test/compile-fail/trait-objects.rs43
-rw-r--r--src/test/run-pass/issue-7320.rs18
9 files changed, 203 insertions, 70 deletions
diff --git a/src/test/compile-fail/issue-18959.rs b/src/test/compile-fail/issue-18959.rs
index 3d126790335..1a792eb6e76 100644
--- a/src/test/compile-fail/issue-18959.rs
+++ b/src/test/compile-fail/issue-18959.rs
@@ -21,6 +21,6 @@ impl Foo for Thing {
 
 fn main() {
     let mut thing = Thing;
-    let test: &Bar = &mut thing; //~ ERROR cannot convert to a trait object because trait `Foo`
+    let test: &Bar = &mut thing; //~ ERROR cannot convert to a trait object
     foo(test);
 }
diff --git a/src/test/compile-fail/object-safety-by-value-self.rs b/src/test/compile-fail/object-safety-by-value-self.rs
new file mode 100644
index 00000000000..5ebcc8516ca
--- /dev/null
+++ b/src/test/compile-fail/object-safety-by-value-self.rs
@@ -0,0 +1,47 @@
+// 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.
+
+// Check that we correctly prevent users from making trait objects
+// from traits with a `fn(self)` method.
+
+trait Bar {
+    fn bar(self);
+}
+
+trait Baz {
+    fn baz(self: Self);
+}
+
+fn make_bar<T:Bar>(t: &T) -> &Bar {
+    t
+        //~^ ERROR `Bar` is not object-safe
+        //~| NOTE method `bar` has a receiver type of `Self`
+}
+
+fn make_bar_explicit<T:Bar>(t: &T) -> &Bar {
+    t as &Bar
+        //~^ ERROR `Bar` is not object-safe
+        //~| NOTE method `bar` has a receiver type of `Self`
+}
+
+fn make_baz<T:Baz>(t: &T) -> &Baz {
+    t
+        //~^ ERROR `Baz` is not object-safe
+        //~| NOTE method `baz` has a receiver type of `Self`
+}
+
+fn make_baz_explicit<T:Baz>(t: &T) -> &Baz {
+    t as &Baz
+        //~^ ERROR `Baz` is not object-safe
+        //~| NOTE method `baz` has a receiver type of `Self`
+}
+
+fn main() {
+}
diff --git a/src/test/run-pass/trait-object-safety.rs b/src/test/compile-fail/object-safety-generics.rs
index ed7284a8353..0ca706404c1 100644
--- a/src/test/run-pass/trait-object-safety.rs
+++ b/src/test/compile-fail/object-safety-generics.rs
@@ -8,19 +8,24 @@
 // option. This file may not be copied, modified, or distributed
 // except according to those terms.
 
-// Check that object-safe methods are identified as such.
+// Check that we correctly prevent users from making trait objects
+// from traits with generic methods.
 
-trait Tr {
-    fn foo(&self);
+trait Bar {
+    fn bar<T>(&self, t: T);
 }
 
-struct St;
+fn make_bar<T:Bar>(t: &T) -> &Bar {
+    t
+        //~^ ERROR `Bar` is not object-safe
+        //~| NOTE method `bar` has generic type parameters
+}
 
-impl Tr for St {
-    fn foo(&self) {}
+fn make_bar_explicit<T:Bar>(t: &T) -> &Bar {
+    t as &Bar
+        //~^ ERROR `Bar` is not object-safe
+        //~| NOTE method `bar` has generic type parameters
 }
 
 fn main() {
-    let s: &Tr = &St;
-    s.foo();
 }
diff --git a/src/test/compile-fail/object-safety-mentions-Self.rs b/src/test/compile-fail/object-safety-mentions-Self.rs
new file mode 100644
index 00000000000..df0f44c1391
--- /dev/null
+++ b/src/test/compile-fail/object-safety-mentions-Self.rs
@@ -0,0 +1,47 @@
+// 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.
+
+// Check that we correctly prevent users from making trait objects
+// form traits that make use of `Self` in an argument or return position.
+
+trait Bar {
+    fn bar(&self, x: &Self);
+}
+
+trait Baz {
+    fn bar(&self) -> Self;
+}
+
+fn make_bar<T:Bar>(t: &T) -> &Bar {
+    t
+        //~^ ERROR `Bar` is not object-safe
+        //~| NOTE method `bar` references the `Self` type in its arguments or return type
+}
+
+fn make_bar_explicit<T:Bar>(t: &T) -> &Bar {
+    t as &Bar
+        //~^ ERROR `Bar` is not object-safe
+        //~| NOTE method `bar` references the `Self` type in its arguments or return type
+}
+
+fn make_baz<T:Baz>(t: &T) -> &Baz {
+    t
+        //~^ ERROR `Baz` is not object-safe
+        //~| NOTE method `bar` references the `Self` type in its arguments or return type
+}
+
+fn make_baz_explicit<T:Baz>(t: &T) -> &Baz {
+    t as &Baz
+        //~^ ERROR `Baz` is not object-safe
+        //~| NOTE method `bar` references the `Self` type in its arguments or return type
+}
+
+fn main() {
+}
diff --git a/src/test/compile-fail/object-safety-no-static.rs b/src/test/compile-fail/object-safety-no-static.rs
new file mode 100644
index 00000000000..6a010d49692
--- /dev/null
+++ b/src/test/compile-fail/object-safety-no-static.rs
@@ -0,0 +1,31 @@
+// 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.
+
+// Check that we correctly prevent users from making trait objects
+// from traits with static methods.
+
+trait Foo {
+    fn foo();
+}
+
+fn foo_implicit<T:Foo+'static>(b: Box<T>) -> Box<Foo+'static> {
+    b
+        //~^ ERROR cannot convert to a trait object
+        //~| NOTE method `foo` has no receiver
+}
+
+fn foo_explicit<T:Foo+'static>(b: Box<T>) -> Box<Foo+'static> {
+    b as Box<Foo>
+        //~^ ERROR cannot convert to a trait object
+        //~| NOTE method `foo` has no receiver
+}
+
+fn main() {
+}
diff --git a/src/test/compile-fail/object-safety-sized-2.rs b/src/test/compile-fail/object-safety-sized-2.rs
new file mode 100644
index 00000000000..3a02461bbb2
--- /dev/null
+++ b/src/test/compile-fail/object-safety-sized-2.rs
@@ -0,0 +1,33 @@
+// 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.
+
+// Check that we correctly prevent users from making trait objects
+// from traits where `Self : Sized`.
+
+trait Bar
+    where Self : Sized
+{
+    fn bar<T>(&self, t: T);
+}
+
+fn make_bar<T:Bar>(t: &T) -> &Bar {
+    t
+        //~^ ERROR `Bar` is not object-safe
+        //~| NOTE the trait cannot require that `Self : Sized`
+}
+
+fn make_bar_explicit<T:Bar>(t: &T) -> &Bar {
+    t as &Bar
+        //~^ ERROR `Bar` is not object-safe
+        //~| NOTE the trait cannot require that `Self : Sized`
+}
+
+fn main() {
+}
diff --git a/src/test/compile-fail/object-safety-sized.rs b/src/test/compile-fail/object-safety-sized.rs
new file mode 100644
index 00000000000..bc214f6f3d9
--- /dev/null
+++ b/src/test/compile-fail/object-safety-sized.rs
@@ -0,0 +1,31 @@
+// 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.
+
+// Check that we correctly prevent users from making trait objects
+// from traits where `Self : Sized`.
+
+trait Bar : Sized {
+    fn bar<T>(&self, t: T);
+}
+
+fn make_bar<T:Bar>(t: &T) -> &Bar {
+    t
+        //~^ ERROR `Bar` is not object-safe
+        //~| NOTE the trait cannot require that `Self : Sized`
+}
+
+fn make_bar_explicit<T:Bar>(t: &T) -> &Bar {
+    t as &Bar
+        //~^ ERROR `Bar` is not object-safe
+        //~| NOTE the trait cannot require that `Self : Sized`
+}
+
+fn main() {
+}
diff --git a/src/test/compile-fail/trait-objects.rs b/src/test/compile-fail/trait-objects.rs
deleted file mode 100644
index 88b907a5cb9..00000000000
--- a/src/test/compile-fail/trait-objects.rs
+++ /dev/null
@@ -1,43 +0,0 @@
-// 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 Foo {
-    fn foo(self);
-}
-
-trait Bar {
-    fn bar(&self, x: &Self);
-}
-
-trait Baz {
-    fn baz<T>(&self, x: &T);
-}
-
-impl Foo for int {
-    fn foo(self) {}
-}
-
-impl Bar for int {
-    fn bar(&self, _x: &int) {}
-}
-
-impl Baz for int {
-    fn baz<T>(&self, _x: &T) {}
-}
-
-fn main() {
-    let _: &Foo = &42i; //~ ERROR cannot convert to a trait object
-    let _: &Bar = &42i; //~ ERROR cannot convert to a trait object
-    let _: &Baz = &42i; //~ ERROR cannot convert to a trait object
-
-    let _ = &42i as &Foo; //~ ERROR cannot convert to a trait object
-    let _ = &42i as &Bar; //~ ERROR cannot convert to a trait object
-    let _ = &42i as &Baz; //~ ERROR cannot convert to a trait object
-}
diff --git a/src/test/run-pass/issue-7320.rs b/src/test/run-pass/issue-7320.rs
deleted file mode 100644
index bd57a3956c7..00000000000
--- a/src/test/run-pass/issue-7320.rs
+++ /dev/null
@@ -1,18 +0,0 @@
-// 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 Foo : Sized {
-    fn foo(self: Box<Self>) { bar(self as Box<Foo>); }
-}
-
-fn bar(_b: Box<Foo>) { }
-
-fn main() {}