about summary refs log tree commit diff
path: root/src/lib
diff options
context:
space:
mode:
authorTim Chevalier <chevalier@alum.wellesley.edu>2011-06-20 17:29:54 -0700
committerTim Chevalier <chevalier@alum.wellesley.edu>2011-06-21 14:37:33 -0700
commit7fb35ecf84c3fd5f92c2af287d478eb70cbacdbd (patch)
tree58823f0d3ba4216b2e0ef5c372bf8a5ec965a7f4 /src/lib
parent3b6d94d4894e06feeb9b48f291a76ff02d0c31ff (diff)
downloadrust-7fb35ecf84c3fd5f92c2af287d478eb70cbacdbd.tar.gz
rust-7fb35ecf84c3fd5f92c2af287d478eb70cbacdbd.zip
Serialize constraints in types (literal arguments still not supported)
This involved, in part, changing the ast::def type so that a def_fn
has a "purity" field. This lets the typechecker determine whether
functions defined in other crates are pure.

It also required updating some error messages in tests. As a test
for cross-crate constrained functions, I added a safe_slice function
to std::str (slice(), with one of the asserts replaced with a
function precondition) and some test cases (various versions of
fn-constraint.rs) that call it. Also, I changed "fn" to "pred" for
some of the boolean functions in std::uint.
Diffstat (limited to 'src/lib')
-rw-r--r--src/lib/str.rs8
-rw-r--r--src/lib/uint.rs12
2 files changed, 14 insertions, 6 deletions
diff --git a/src/lib/str.rs b/src/lib/str.rs
index 51b876e3253..3ad63093ddc 100644
--- a/src/lib/str.rs
+++ b/src/lib/str.rs
@@ -1,6 +1,7 @@
 
 import rustrt::sbuf;
 import vec::rustrt::vbuf;
+import uint::le;
 export sbuf;
 export rustrt;
 export eq;
@@ -45,6 +46,7 @@ export split;
 export concat;
 export connect;
 export to_upper;
+export safe_slice;
 
 native "rust" mod rustrt {
     type sbuf;
@@ -381,6 +383,12 @@ fn slice(str s, uint begin, uint end) -> str {
     ret rustrt::str_slice(s, begin, end);
 }
 
+fn safe_slice(str s, uint begin, uint end) : le(begin, end) -> str {
+    assert (end <= str::byte_len(s)); // would need some magic to
+                                      // make this a precondition
+    ret rustrt::str_slice(s, begin, end);
+}
+
 fn shift_byte(&mutable str s) -> u8 {
     auto len = byte_len(s);
     assert (len > 0u);
diff --git a/src/lib/uint.rs b/src/lib/uint.rs
index 27c0a0e458e..ca632914a45 100644
--- a/src/lib/uint.rs
+++ b/src/lib/uint.rs
@@ -10,17 +10,17 @@ fn div(uint x, uint y) -> uint { ret x / y; }
 
 fn rem(uint x, uint y) -> uint { ret x % y; }
 
-fn lt(uint x, uint y) -> bool { ret x < y; }
+pred lt(uint x, uint y) -> bool { ret x < y; }
 
-fn le(uint x, uint y) -> bool { ret x <= y; }
+pred le(uint x, uint y) -> bool { ret x <= y; }
 
-fn eq(uint x, uint y) -> bool { ret x == y; }
+pred eq(uint x, uint y) -> bool { ret x == y; }
 
-fn ne(uint x, uint y) -> bool { ret x != y; }
+pred ne(uint x, uint y) -> bool { ret x != y; }
 
-fn ge(uint x, uint y) -> bool { ret x >= y; }
+pred ge(uint x, uint y) -> bool { ret x >= y; }
 
-fn gt(uint x, uint y) -> bool { ret x > y; }
+pred gt(uint x, uint y) -> bool { ret x > y; }
 
 fn max(uint x, uint y) -> uint { if (x > y) { ret x; } ret y; }