diff options
| author | Tim Chevalier <chevalier@alum.wellesley.edu> | 2011-08-24 17:24:58 -0700 |
|---|---|---|
| committer | Tim Chevalier <chevalier@alum.wellesley.edu> | 2011-08-25 17:23:35 -0700 |
| commit | e241f2996dcf99f3d9fd2f9e277e435782c65a61 (patch) | |
| tree | cc86000e235ab08c5b0ac9a3f3b719736df1d068 /src/test | |
| parent | 4dd23f24d65e05169594bfcb45b84adfe4e52516 (diff) | |
| download | rust-e241f2996dcf99f3d9fd2f9e277e435782c65a61.tar.gz rust-e241f2996dcf99f3d9fd2f9e277e435782c65a61.zip | |
Allow pure fns to have any return type
Diffstat (limited to 'src/test')
| -rw-r--r-- | src/test/compile-fail/not-a-pred-check.rs | 2 | ||||
| -rw-r--r-- | src/test/run-pass/non-boolean-pure-fns.rs | 32 | ||||
| -rw-r--r-- | src/test/run-pass/pred-not-bool.rs (renamed from src/test/compile-fail/pred-not-bool.rs) | 2 |
3 files changed, 34 insertions, 2 deletions
diff --git a/src/test/compile-fail/not-a-pred-check.rs b/src/test/compile-fail/not-a-pred-check.rs index 8ba91d0e26e..9891360af9e 100644 --- a/src/test/compile-fail/not-a-pred-check.rs +++ b/src/test/compile-fail/not-a-pred-check.rs @@ -1,5 +1,5 @@ // -*- rust -*- -// error-pattern: non-predicate +// error-pattern: Impure function as operator fn f(q: int) -> bool { ret true; } diff --git a/src/test/run-pass/non-boolean-pure-fns.rs b/src/test/run-pass/non-boolean-pure-fns.rs new file mode 100644 index 00000000000..61477258bb4 --- /dev/null +++ b/src/test/run-pass/non-boolean-pure-fns.rs @@ -0,0 +1,32 @@ +use std; + +import std::list::*; + +pure fn pure_length_go<@T>(ls: &list<T>, acc: uint) -> uint { + alt ls { + nil. { acc } + cons(_, tl) { pure_length_go(*tl, acc + 1u) } + } +} + +pure fn pure_length<@T>(ls: &list<T>) -> uint { + pure_length_go(ls, 0u) +} + +pure fn nonempty_list<@T>(ls: &list<T>) -> bool { + pure_length(ls) > 0u +} + + // Of course, the compiler can't take advantage of the + // knowledge that ls is a cons node. Future work. + // Also, this is pretty contrived since nonempty_list + // could be a "tag refinement", if we implement those. +fn safe_head<@T>(ls: &list<T>) : nonempty_list(ls) -> T { car(ls) } + +fn main() { + let mylist = cons(@1u, @nil); + // Again, a way to eliminate such "obvious" checks seems + // desirable. (Tags could have postconditions.) + check(nonempty_list(mylist)); + assert (*(safe_head(mylist)) == 1u); +} \ No newline at end of file diff --git a/src/test/compile-fail/pred-not-bool.rs b/src/test/run-pass/pred-not-bool.rs index 50344120a98..1f70cb73f60 100644 --- a/src/test/compile-fail/pred-not-bool.rs +++ b/src/test/run-pass/pred-not-bool.rs @@ -5,6 +5,6 @@ // this checks that a pred with a non-bool return // type is rejected, even if the pred is never used -pred bad(a: int) -> int { ret 37; } +pure fn bad(a: int) -> int { ret 37; } fn main() { } |
