about summary refs log tree commit diff
path: root/src/test
diff options
context:
space:
mode:
authorNiko Matsakis <niko@alum.mit.edu>2015-03-17 15:22:11 -0400
committerNiko Matsakis <niko@alum.mit.edu>2015-03-17 17:29:07 -0400
commit0947f4076ddbf5c0db63d60c19f28b6b79023638 (patch)
treecd4568b62d70c02218711c0fe73d2b1ebf0aef56 /src/test
parent1b0f0ad28070c072f68ea0ab10bbae61b52706a8 (diff)
downloadrust-0947f4076ddbf5c0db63d60c19f28b6b79023638.tar.gz
rust-0947f4076ddbf5c0db63d60c19f28b6b79023638.zip
Move unsafety out of the subtyping relation and into coercion.
Diffstat (limited to 'src/test')
-rw-r--r--src/test/compile-fail/unsafe-subtyping.rs21
-rw-r--r--src/test/compile-fail/unsafe-trait-impl.rs22
-rw-r--r--src/test/compile-fail/variadic-ffi.rs4
-rw-r--r--src/test/run-pass/unsafe-coercion.rs25
4 files changed, 70 insertions, 2 deletions
diff --git a/src/test/compile-fail/unsafe-subtyping.rs b/src/test/compile-fail/unsafe-subtyping.rs
new file mode 100644
index 00000000000..f4867636819
--- /dev/null
+++ b/src/test/compile-fail/unsafe-subtyping.rs
@@ -0,0 +1,21 @@
+// Copyright 2013-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 safe fns are not a subtype of unsafe fns.
+
+fn foo(x: Option<fn(i32)>) -> Option<unsafe fn(i32)> {
+    x //~ ERROR mismatched types
+}
+
+fn bar(x: fn(i32)) -> unsafe fn(i32) {
+    x // OK, coercion!
+}
+
+fn main() { }
diff --git a/src/test/compile-fail/unsafe-trait-impl.rs b/src/test/compile-fail/unsafe-trait-impl.rs
new file mode 100644
index 00000000000..71da2f7633f
--- /dev/null
+++ b/src/test/compile-fail/unsafe-trait-impl.rs
@@ -0,0 +1,22 @@
+// Copyright 2013-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 safe fns are not a subtype of unsafe fns.
+
+trait Foo {
+    unsafe fn len(&self) -> u32;
+}
+
+impl Foo for u32 {
+    fn len(&self) -> u32 { *self }
+    //~^ ERROR incompatible type for trait: expected unsafe fn, found normal fn
+}
+
+fn main() { }
diff --git a/src/test/compile-fail/variadic-ffi.rs b/src/test/compile-fail/variadic-ffi.rs
index 86271f670ce..2a62ac2ac30 100644
--- a/src/test/compile-fail/variadic-ffi.rs
+++ b/src/test/compile-fail/variadic-ffi.rs
@@ -30,9 +30,9 @@ fn main() {
         //~| expected non-variadic fn
         //~| found variadic function
 
-        let y: unsafe extern "C" fn(f: isize, x: u8, ...) = bar;
+        let y: extern "C" fn(f: isize, x: u8, ...) = bar;
         //~^ ERROR: mismatched types
-        //~| expected `unsafe extern "C" fn(isize, u8, ...)`
+        //~| expected `extern "C" fn(isize, u8, ...)`
         //~| found `extern "C" fn(isize, u8) {bar}`
         //~| expected variadic fn
         //~| found non-variadic function
diff --git a/src/test/run-pass/unsafe-coercion.rs b/src/test/run-pass/unsafe-coercion.rs
new file mode 100644
index 00000000000..06980e162c8
--- /dev/null
+++ b/src/test/run-pass/unsafe-coercion.rs
@@ -0,0 +1,25 @@
+// Copyright 2013-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 safe fns are not a subtype of unsafe fns.
+
+fn foo(x: i32) -> i32 {
+    x * 22
+}
+
+fn bar(x: fn(i32) -> i32) -> unsafe fn(i32) -> i32 {
+    x // OK, coercion!
+}
+
+fn main() {
+    let f = bar(foo);
+    let x = unsafe { f(2) };
+    assert_eq!(x, 44);
+}