summary refs log tree commit diff
path: root/src/test
diff options
context:
space:
mode:
authorPatrick Walton <pcwalton@mimiga.net>2013-10-29 15:06:13 -0700
committerPatrick Walton <pcwalton@mimiga.net>2013-10-31 10:30:32 -0700
commitf27272d60f193c6d27cc283f48c5be0e41562814 (patch)
tree6647a01e6d2a80a05213cedf75b651d7accc9828 /src/test
parente976de32dc590f759e6c0c72d286844ca373e775 (diff)
downloadrust-f27272d60f193c6d27cc283f48c5be0e41562814.tar.gz
rust-f27272d60f193c6d27cc283f48c5be0e41562814.zip
librustc: Implement `|A| -> B` syntax for closures and make bare `fn`
work
Diffstat (limited to 'src/test')
-rw-r--r--src/test/compile-fail/block-coerce-no-2.rs2
-rw-r--r--src/test/compile-fail/closure-reform-bad.rs13
-rw-r--r--src/test/pretty/closure-reform-pretty.rs17
-rw-r--r--src/test/pretty/disamb-stmt-expr.rs2
-rw-r--r--src/test/pretty/do1.rs2
-rw-r--r--src/test/pretty/fn-types.rs4
-rw-r--r--src/test/run-pass/closure-reform.rs52
7 files changed, 87 insertions, 5 deletions
diff --git a/src/test/compile-fail/block-coerce-no-2.rs b/src/test/compile-fail/block-coerce-no-2.rs
index 85ef09cc2a6..3e38ce9ab35 100644
--- a/src/test/compile-fail/block-coerce-no-2.rs
+++ b/src/test/compile-fail/block-coerce-no-2.rs
@@ -19,5 +19,5 @@ fn main() {
     }
 
     f(g);
-    //~^ ERROR mismatched types: expected `extern "Rust" fn(extern "Rust" fn(extern "Rust" fn()))`
+    //~^ ERROR mismatched types: expected `fn(fn(fn()))`
 }
diff --git a/src/test/compile-fail/closure-reform-bad.rs b/src/test/compile-fail/closure-reform-bad.rs
new file mode 100644
index 00000000000..3da709942e0
--- /dev/null
+++ b/src/test/compile-fail/closure-reform-bad.rs
@@ -0,0 +1,13 @@
+/* Any copyright is dedicated to the Public Domain.
+ * http://creativecommons.org/publicdomain/zero/1.0/ */
+
+fn call_bare(f: fn(&str)) {
+    f("Hello ");
+}
+
+fn main() {
+    let string = "world!";
+    let f: |&str| = |s| println(s + string);
+    call_bare(f)    //~ ERROR mismatched types
+}
+
diff --git a/src/test/pretty/closure-reform-pretty.rs b/src/test/pretty/closure-reform-pretty.rs
new file mode 100644
index 00000000000..5169652a6c0
--- /dev/null
+++ b/src/test/pretty/closure-reform-pretty.rs
@@ -0,0 +1,17 @@
+// Any copyright is dedicated to the Public Domain.
+// http://creativecommons.org/publicdomain/zero/1.0/
+
+// pp-exact
+
+fn call_it(f: proc(~str) -> ~str) { }
+
+fn call_this(f: |&str|: Send) { }
+
+fn call_that(f: <'a>|&'a int, &'a int|: -> int) { }
+
+fn call_extern(f: fn() -> int) { }
+
+fn call_abid_extern(f: extern "C" fn() -> int) { }
+
+pub fn main() { }
+
diff --git a/src/test/pretty/disamb-stmt-expr.rs b/src/test/pretty/disamb-stmt-expr.rs
index f6787fa9c3d..d3d6f1c0e35 100644
--- a/src/test/pretty/disamb-stmt-expr.rs
+++ b/src/test/pretty/disamb-stmt-expr.rs
@@ -14,7 +14,7 @@
 // preserved.  They are needed to disambiguate `{return n+1}; - 0` from
 // `({return n+1}-0)`.
 
-fn id(f: &fn() -> int) -> int { f() }
+fn id(f: || -> int) -> int { f() }
 
 fn wsucc(_n: int) -> int { (do id || { 1 }) - 0 }
 fn main() { }
diff --git a/src/test/pretty/do1.rs b/src/test/pretty/do1.rs
index 751aedb39a3..1fb2359da53 100644
--- a/src/test/pretty/do1.rs
+++ b/src/test/pretty/do1.rs
@@ -10,6 +10,6 @@
 
 // pp-exact
 
-fn f(f: &fn(int)) { f(10) }
+fn f(f: |int|) { f(10) }
 
 fn main() { do f |i| { assert!(i == 10) } }
diff --git a/src/test/pretty/fn-types.rs b/src/test/pretty/fn-types.rs
index b000c9f9137..27e56fb6074 100644
--- a/src/test/pretty/fn-types.rs
+++ b/src/test/pretty/fn-types.rs
@@ -10,7 +10,7 @@
 
 // pp-exact
 
-fn from_foreign_fn(_x: extern "Rust" fn()) { }
-fn from_stack_closure(_x: &fn()) { }
+fn from_foreign_fn(_x: fn()) { }
+fn from_stack_closure(_x: ||) { }
 fn from_unique_closure(_x: ~fn()) { }
 fn main() { }
diff --git a/src/test/run-pass/closure-reform.rs b/src/test/run-pass/closure-reform.rs
index c765ebe9643..18ca64d0f27 100644
--- a/src/test/run-pass/closure-reform.rs
+++ b/src/test/run-pass/closure-reform.rs
@@ -1,11 +1,42 @@
 /* Any copyright is dedicated to the Public Domain.
  * http://creativecommons.org/publicdomain/zero/1.0/ */
 
+use std::cast;
+
 fn call_it(f: proc(~str) -> ~str) {
     println(f(~"Fred"))
 }
 
+fn call_a_thunk(f: ||) {
+    f();
+}
+
+fn call_this(f: |&str|:Send) {
+    f("Hello!");
+}
+
+fn call_that(f: <'a>|&'a int, &'a int|: -> int) {
+    let (ten, forty_two) = (10, 42);
+    println!("Your lucky number is {}", f(&ten, &forty_two));
+}
+
+fn call_cramped(f:||->uint,g:<'a>||->&'a uint) {
+    let number = f();
+    let other_number = *g();
+    println!("Ticket {} wins an all-expenses-paid trip to Mountain View", number + other_number);
+}
+
+fn call_bare(f: fn(&str)) {
+    f("Hello world!")
+}
+
+fn call_bare_again(f: extern "Rust" fn(&str)) {
+    f("Goodbye world!")
+}
+
 pub fn main() {
+    // Procs
+
     let greeting = ~"Hi ";
     do call_it |s| {
         greeting + s
@@ -23,5 +54,26 @@ pub fn main() {
     call_it(proc(s: ~str) -> ~str {
         greeting + s
     });
+
+    // Closures
+
+    call_a_thunk(|| println("Hello world!"));
+
+    call_this(|s| println(s));
+
+    call_that(|x, y| *x + *y);
+
+    let z = 100;
+    call_that(|x, y| *x + *y - z);
+
+    call_cramped(|| 1, || unsafe {
+        cast::transmute(&100)
+    });
+
+    // External functions
+
+    call_bare(println);
+
+    call_bare_again(println);
 }