Remove preconditions from libraries

Closes #1805
This commit is contained in:
Marijn Haverbeke 2012-02-22 11:44:11 +01:00
parent e57b6775c3
commit ad03761a97
16 changed files with 18 additions and 87 deletions

View File

@ -40,7 +40,6 @@ fn get_sysroot_absolute_rt_lib(sess: session::session) -> fs::path {
+ filesearch::relative_target_lib_path(
sess.opts.target_triple)
+ [os::dylib_filename("rustrt")];
check vec::is_not_empty(path);
fs::connect_many(path)
}
@ -174,7 +173,6 @@ fn get_install_prefix_rpath(cwd: fs::path, target_triple: str) -> str {
let path = [install_prefix]
+ filesearch::relative_target_lib_path(target_triple);
check vec::is_not_empty(path);
get_absolute(cwd, fs::connect_many(path))
}

View File

@ -209,7 +209,6 @@ fn visit_block(tp: block_type, cx: ctx, visit: fn()) {
cx.current = start_current;
visit();
let cx_blocks = cx.blocks;
check is_not_empty(cx_blocks);
cx.blocks = tail(cx_blocks);
let branches = if tp == func { local.exits + [cx.current] }
else { local.exits };

View File

@ -289,7 +289,6 @@ fn map_crate(e: @env, c: @ast::crate) {
alt follow_import(*e, sc, *path, vp.span) {
some(imp) {
let glob = {def: imp, path: vp};
check list::is_not_empty(sc);
alt list::head(sc) {
scope_item(i) {
e.mod_map.get(i.id).glob_imports += [glob];
@ -585,7 +584,6 @@ fn visit_block_with_scope(b: ast::blk, sc: scopes, v: vt<scopes>) {
}
fn visit_decl_with_scope(d: @decl, sc: scopes, v: vt<scopes>) {
check list::is_not_empty(sc);
let loc_pos = alt list::head(sc) {
scope_block(_, _, pos) { pos }
_ { @mutable 0u }

View File

@ -278,7 +278,6 @@ fn extract_variant_args(bcx: block, pat_id: ast::node_id,
let vdefs_tg = vdefs.enm;
let vdefs_var = vdefs.var;
while i < size {
check (valid_variant_index(i, bcx, vdefs_tg, vdefs_var));
let r =
// invariant needed:
// how do we know it even makes sense to pass in ty_param_substs

View File

@ -355,9 +355,10 @@ fn GEP_tup_like(bcx: block, t: ty::t, base: ValueRef, ixs: [int])
// is meaningless, as it will be cast away.
fn GEP_enum(cx: block, llblobptr: ValueRef, enum_id: ast::def_id,
variant_id: ast::def_id, ty_substs: [ty::t],
ix: uint) : valid_variant_index(ix, cx, enum_id, variant_id) ->
result {
ix: uint) -> result {
let variant = ty::enum_variant_with_id(cx.tcx(), enum_id, variant_id);
assert ix < variant.args.len();
// Synthesize a tuple type so that GEP_tup_like() can work its magic.
// Separately, store the type of the element we're interested in.
@ -1119,7 +1120,6 @@ fn iter_structural_ty(cx: block, av: ValueRef, t: ty::t,
let j = 0u;
let v_id = variant.id;
for a: ty::arg in args {
check (valid_variant_index(j, cx, tid, v_id));
let rslt = GEP_enum(cx, a_tup, tid, v_id, tps, j);
let llfldp_a = rslt.val;
cx = rslt.bcx;
@ -4031,7 +4031,6 @@ fn trans_enum_variant(ccx: crate_ctxt, enum_id: ast::node_id,
let t_id = local_def(enum_id);
let v_id = local_def(variant.node.id);
for va: ast::variant_arg in variant.node.args {
check (valid_variant_index(i, bcx, t_id, v_id));
let rslt = GEP_enum(bcx, llblobptr, t_id, v_id, ty_param_substs, i);
bcx = rslt.bcx;
let lldestptr = rslt.val;

View File

@ -529,7 +529,6 @@ fn add_span_comment(bcx: block, sp: span, text: str) {
fn add_comment(bcx: block, text: str) {
let ccx = bcx.ccx();
if (!ccx.sess.opts.no_asm_comments) {
check str::is_not_empty("$");
let sanitized = str::replace(text, "$", "");
let comment_text = "; " + sanitized;
let asm = str::as_buf(comment_text, {|c|

View File

@ -835,20 +835,6 @@ fn C_shape(ccx: crate_ctxt, bytes: [u8]) -> ValueRef {
ret llvm::LLVMConstPointerCast(llglobal, T_ptr(T_i8()));
}
pure fn valid_variant_index(ix: uint, cx: block, enum_id: ast::def_id,
variant_id: ast::def_id) -> bool {
// Handwaving: it's ok to pretend this code is referentially
// transparent, because the relevant parts of the type context don't
// change. (We're not adding new variants during trans.)
unchecked{
let variant =
ty::enum_variant_with_id(cx.tcx(), enum_id, variant_id);
ix < variant.args.len()
}
}
pure fn type_has_static_size(cx: crate_ctxt, t: ty::t) -> bool {
!ty::type_has_dynamic_size(cx.tcx, t)
}

View File

@ -888,7 +888,6 @@ fn parse_bottom_expr(p: parser) -> pexpr {
/* Should be a predicate (pure boolean function) applied to
arguments that are all either slot variables or literals.
but the typechecker enforces that. */
let e = parse_expr(p);
hi = e.span.hi;
ex = ast::expr_check(ast::checked_expr, e);

View File

@ -89,7 +89,6 @@ fn relative_target_lib_path(target_triple: str) -> [fs::path] {
fn make_target_lib_path(sysroot: fs::path,
target_triple: str) -> fs::path {
let path = [sysroot] + relative_target_lib_path(target_triple);
check vec::is_not_empty(path);
let path = fs::connect_many(path);
ret path;
}
@ -112,7 +111,6 @@ fn get_sysroot(maybe_sysroot: option<fs::path>) -> fs::path {
fn get_cargo_sysroot() -> result::t<fs::path, str> {
let path = [get_default_sysroot(), libdir(), "cargo"];
check vec::is_not_empty(path);
result::ok(fs::connect_many(path))
}

View File

@ -659,7 +659,8 @@ Returns:
The original string with all occurances of `from` replaced with `to`
*/
fn replace(s: str, from: str, to: str) : is_not_empty(from) -> str unsafe {
fn replace(s: str, from: str, to: str) -> str unsafe {
assert is_not_empty(from);
if len_bytes(s) == 0u {
ret "";
} else if starts_with(s, from) {
@ -1922,12 +1923,10 @@ mod tests {
#[test]
fn test_replace() {
let a = "a";
check (is_not_empty(a));
assert (replace("", a, "b") == "");
assert (replace("a", a, "b") == "b");
assert (replace("ab", a, "b") == "bb");
let test = "test";
check (is_not_empty(test));
assert (replace(" test test ", test, "toast") == " toast toast ");
assert (replace(" test test ", test, "") == " ");
}
@ -1939,7 +1938,6 @@ mod tests {
let a = "ประเ";
let A = "دولة الكويتทศไทย中华";
check is_not_empty(a);
assert (replace(data, a, repl) == A);
}
@ -1950,7 +1948,6 @@ mod tests {
let b = "ะเ";
let B = "ปรدولة الكويتทศไทย中华";
check is_not_empty(b);
assert (replace(data, b, repl) == B);
}
@ -1961,7 +1958,6 @@ mod tests {
let c = "中华";
let C = "ประเทศไทยدولة الكويت";
check is_not_empty(c);
assert (replace(data, c, repl) == C);
}
@ -1971,7 +1967,6 @@ mod tests {
let repl = "دولة الكويت";
let d = "ไท华";
check is_not_empty(d);
assert (replace(data, d, repl) == data);
}

View File

@ -180,7 +180,7 @@ Returns the first element of a vector
Predicates:
<is_not_empty> (v)
*/
pure fn head<T: copy>(v: [const T]) : is_not_empty(v) -> T { ret v[0]; }
pure fn head<T: copy>(v: [const T]) -> T { v[0] }
/*
Function: tail
@ -240,9 +240,7 @@ Returns the last element of a non-empty vector `v`
Predicates:
<is_not_empty> (v)
*/
pure fn last_total<T: copy>(v: [const T]) : is_not_empty(v) -> T {
ret v[len(v) - 1u];
}
pure fn last_total<T: copy>(v: [const T]) -> T { v[len(v) - 1u] }
/*
Function: slice
@ -886,10 +884,10 @@ Preconditions:
<same_length> (v, u)
*/
fn zip<T: copy, U: copy>(v: [T], u: [U]) : same_length(v, u) -> [(T, U)] {
fn zip<T: copy, U: copy>(v: [T], u: [U]) -> [(T, U)] {
let zipped = [];
let sz = len(v), i = 0u;
assert (sz == len(u));
assert sz == len(u);
while i < sz { zipped += [(v[i], u[i])]; i += 1u; }
ret zipped;
}
@ -940,7 +938,8 @@ Function: enum_chars
Returns a vector containing a range of chars
*/
fn enum_chars(start: u8, end: u8) : ::u8::le(start, end) -> [char] {
fn enum_chars(start: u8, end: u8) -> [char] {
assert start < end;
let i = start;
let r = [];
while i <= end { r += [i as char]; i += 1u as u8; }
@ -953,7 +952,8 @@ Function: enum_uints
Returns a vector containing a range of uints
*/
fn enum_uints(start: uint, end: uint) : uint::le(start, end) -> [uint] {
fn enum_uints(start: uint, end: uint) -> [uint] {
assert start < end;
let i = start;
let r = [];
while i <= end { r += [i]; i += 1u; }
@ -1329,18 +1329,15 @@ mod tests {
#[test]
fn test_head() {
let a = [11, 12];
check (is_not_empty(a));
assert (head(a) == 11);
}
#[test]
fn test_tail() {
let a = [11];
check (is_not_empty(a));
assert (tail(a) == []);
a = [11, 12];
check (is_not_empty(a));
assert (tail(a) == [12]);
}
@ -1667,7 +1664,6 @@ mod tests {
let v1 = [1, 2, 3];
let v2 = [4, 5, 6];
check (same_length(v1, v2)); // Silly, but what else can we do?
let z1 = zip(v1, v2);
assert ((1, 4) == z1[0]);

View File

@ -107,12 +107,11 @@ Connects a vector of path segments into a single path.
Inserts path separators as needed.
*/
fn connect_many(paths: [path]) : vec::is_not_empty(paths) -> path {
fn connect_many(paths: [path]) -> path {
ret if vec::len(paths) == 1u {
paths[0]
} else {
let rest = vec::slice(paths, 1u, vec::len(paths));
check vec::is_not_empty(rest);
connect(paths[0], connect_many(rest))
}
}

View File

@ -140,11 +140,8 @@ Function: head
Returns the first element of a list
*/
pure fn head<T: copy>(ls: list<T>) : is_not_empty(ls) -> T {
alt ls {
cons(hd, _) { ret hd; }
nil { fail "list empty" }
}
pure fn head<T: copy>(ls: list<T>) -> T {
alt check ls { cons(hd, _) { hd } }
}
/*
@ -205,15 +202,12 @@ mod tests {
fn test_from_vec() {
let l = from_vec([0, 1, 2]);
check is_not_empty(l);
assert (head(l) == 0);
let tail_l = tail(l);
check is_not_empty(tail_l);
assert (head(tail_l) == 1);
let tail_tail_l = tail(tail_l);
check is_not_empty(tail_tail_l);
assert (head(tail_tail_l) == 2);
}
@ -227,15 +221,12 @@ mod tests {
fn test_from_vec_mut() {
let l = from_vec([mutable 0, 1, 2]);
check is_not_empty(l);
assert (head(l) == 0);
let tail_l = tail(l);
check is_not_empty(tail_l);
assert (head(tail_l) == 1);
let tail_tail_l = tail(tail_l);
check is_not_empty(tail_tail_l);
assert (head(tail_tail_l) == 2);
}

View File

@ -252,8 +252,6 @@ mod test_qsort {
let immut_names = vec::from_mut(names);
// Silly, but what else can we do?
check (vec::same_length(expected, immut_names));
let pairs = vec::zip(expected, immut_names);
for (a, b) in pairs { #debug("%d %d", a, b); assert (a == b); }
}

View File

@ -47,7 +47,6 @@ type test_desc = {
// The default console test runner. It accepts the command line
// arguments and a vector of test_descs (generated at compile time).
fn test_main(args: [str], tests: [test_desc]) {
check (vec::is_not_empty(args));
let opts =
alt parse_opts(args) {
either::left(o) { o }
@ -61,8 +60,7 @@ type test_opts = {filter: option<str>, run_ignored: bool};
type opt_res = either::t<test_opts, str>;
// Parses command line arguments into test options
fn parse_opts(args: [str]) : vec::is_not_empty(args) -> opt_res {
fn parse_opts(args: [str]) -> opt_res {
let args_ = vec::tail(args);
let opts = [getopts::optflag("ignored")];
let match =
@ -407,7 +405,6 @@ mod tests {
#[test]
fn first_free_arg_should_be_a_filter() {
let args = ["progname", "filter"];
check (vec::is_not_empty(args));
let opts = alt parse_opts(args) { either::left(o) { o }
_ { fail "Malformed arg in first_free_arg_should_be_a_filter"; } };
assert (str::eq("filter", option::get(opts.filter)));
@ -416,7 +413,6 @@ mod tests {
#[test]
fn parse_ignored_flag() {
let args = ["progname", "filter", "--ignored"];
check (vec::is_not_empty(args));
let opts = alt parse_opts(args) { either::left(o) { o }
_ { fail "Malformed arg in parse_ignored_flag"; } };
assert (opts.run_ignored);
@ -469,12 +465,10 @@ mod tests {
"test::ignored_tests_result_in_ignored", "test::parse_ignored_flag",
"test::sort_tests"];
check (vec::same_length(expected, filtered));
let pairs = vec::zip(expected, filtered);
for (a, b) in pairs { assert (a == b.name); }
}
}
}

View File

@ -1,17 +0,0 @@
// error-pattern:Unsatisfied precondition constraint (for example, same_length
use std;
import uint;
import u8;
import vec::*;
fn main() {
let a = 'a' as u8, j = 'j' as u8, k = 1u, l = 10u;
// Silly, but necessary
check (u8::le(a, j));
check (uint::le(k, l));
let chars = enum_chars(a, j);
let ints = enum_uints(k, l);
let ps = zip(chars, ints);
fail "the impossible happened";
}