Use loop instead of while(true) in libraries and compiler itself

And remove spurious fails/unreachable() calls.
This commit is contained in:
Tim Chevalier 2012-03-10 20:34:17 -08:00
parent 98260a2a22
commit 35400e13ad
19 changed files with 81 additions and 104 deletions

View File

@ -64,7 +64,7 @@ fn parse_buf(buf: [u8], radix: uint) -> option<int> {
start = 1u;
}
let mut n = 0;
while true {
loop {
alt char::to_digit(buf[i] as char, radix) {
some(d) { n += (d as int) * power; }
none { ret none; }
@ -72,8 +72,7 @@ fn parse_buf(buf: [u8], radix: uint) -> option<int> {
power *= radix as int;
if i <= start { ret some(n); }
i -= 1u;
}
fail;
};
}
#[doc = "Parse a string to an int"]

View File

@ -70,7 +70,7 @@ fn from_str(buf: str, radix: u64) -> option<u64> {
if str::len(buf) == 0u { ret none; }
let mut i = str::len(buf) - 1u;
let mut power = 1u64, n = 0u64;
while true {
loop {
alt char::to_digit(buf[i] as char, radix as uint) {
some(d) { n += d as u64 * power; }
none { ret none; }
@ -78,8 +78,7 @@ fn from_str(buf: str, radix: u64) -> option<u64> {
power *= radix;
if i == 0u { ret some(n); }
i -= 1u;
}
fail;
};
}
#[doc = "Computes the bitwise complement"]

View File

@ -133,7 +133,7 @@ fn parse_buf(buf: [u8], radix: uint) -> option<uint> {
let mut i = vec::len(buf) - 1u;
let mut power = 1u;
let mut n = 0u;
while true {
loop {
alt char::to_digit(buf[i] as char, radix) {
some(d) { n += d * power; }
none { ret none; }
@ -141,8 +141,7 @@ fn parse_buf(buf: [u8], radix: uint) -> option<uint> {
power *= radix;
if i == 0u { ret some(n); }
i -= 1u;
}
fail;
};
}
#[doc = "Parse a string to an int"]

View File

@ -59,7 +59,7 @@ fn setenv(n: str, v: str) {
#[cfg(target_os = "win32")]
fn getenv(n: str) -> option<str> {
let nsize = 256u;
while true {
loop {
let v: [u8] = [];
vec::reserve(v, nsize);
let res =
@ -80,7 +80,6 @@ fn getenv(n: str) -> option<str> {
ret option::some(str::from_bytes(v)); // UTF-8 or fail
} else { nsize = res; }
}
core::unreachable();
}
#[cfg(target_os = "win32")]

View File

@ -104,7 +104,7 @@ impl reader_util for reader {
fn read_line() -> str {
let buf: [u8] = [];
while true {
loop {
let ch = self.read_byte();
if ch == -1 || ch == 10 { break; }
buf += [ch as u8];
@ -114,7 +114,7 @@ impl reader_util for reader {
fn read_c_str() -> str {
let buf: [u8] = [];
while true {
loop {
let ch = self.read_byte();
if ch < 1 { break; } else { buf += [ch as u8]; }
}

View File

@ -383,23 +383,23 @@ impl parser for parser {
ret ok(list(values));
}
while true {
loop {
alt self.parse_value() {
ok(v) { vec::push(values, v); }
e { ret e; }
}
self.parse_whitespace();
if self.eof() { break; }
if self.eof() {
ret self.error("EOF while parsing list");
}
alt self.ch {
',' { self.bump(); }
']' { self.bump(); ret ok(list(values)); }
_ { ret self.error("expecting ',' or ']'"); }
}
}
ret self.error("EOF while parsing list");
};
}
fn parse_object() -> result::t<json, error> {

View File

@ -43,27 +43,25 @@ is returned. If `f` matches no elements then none is returned.
fn find<T: copy, U: copy>(ls: list<T>, f: fn(T) -> option<U>)
-> option<U> {
let ls = ls;
while true {
loop {
alt ls {
cons(hd, tl) {
alt f(hd) { none { ls = *tl; } some(rs) { ret some(rs); } }
}
nil { break; }
nil { ret none; }
}
}
ret none;
};
}
#[doc = "Returns true if a list contains an element with the given value"]
fn has<T: copy>(ls: list<T>, elt: T) -> bool {
let ls = ls;
while true {
loop {
alt ls {
cons(hd, tl) { if elt == hd { ret true; } else { ls = *tl; } }
nil { break; }
nil { ret false; }
}
}
ret false;
};
}
#[doc = "Returns true if the list is empty"]
@ -113,7 +111,7 @@ fn iter<T>(l: list<T>, f: fn(T)) {
cons(hd, tl) {
f(hd);
let cur = tl;
while true {
loop {
alt *cur {
cons(hd, tl) {
f(hd);

View File

@ -96,7 +96,7 @@ mod chained {
e_root: @entry<K,V>) -> search_result<K,V> {
let e0 = e_root;
let comp = 1u; // for logging
while true {
loop {
alt e0.next {
absent {
#debug("search_tbl: absent, comp %u, hash %u, idx %u",
@ -115,8 +115,7 @@ mod chained {
}
}
}
}
core::unreachable();
};
}
fn search_tbl<K: copy, V: copy>(
@ -209,7 +208,7 @@ mod chained {
fn foreach_entry<K: copy, V: copy>(chain0: chain<K,V>,
blk: fn(@entry<K,V>)) {
let chain = chain0;
while true {
loop {
alt chain {
absent { ret; }
present(entry) {

View File

@ -800,7 +800,7 @@ mod node {
let buf = vec::to_mut(vec::init_elt(byte_len(node), 0u8));
let offset = 0u;//Current position in the buffer
let it = leaf_iterator::start(node);
while true {
loop {
alt(leaf_iterator::next(it)) {
option::none { break; }
option::some(x) {
@ -862,7 +862,7 @@ mod node {
//1. Gather all leaves as a forest
let forest = [mutable];
let it = leaf_iterator::start(node);
while true {
loop {
alt (leaf_iterator::next(it)) {
option::none { break; }
option::some(x) { forest += [mutable @leaf(x)]; }
@ -896,7 +896,7 @@ mod node {
fn sub_bytes(node: @node, byte_offset: uint, byte_len: uint) -> @node {
let node = node;
let byte_offset = byte_offset;
while true {
loop {
if byte_offset == 0u && byte_len == node::byte_len(node) {
ret node;
}
@ -932,8 +932,7 @@ mod node {
}
}
}
}
core::unreachable();
};
}
#[doc ="
@ -958,7 +957,7 @@ mod node {
fn sub_chars(node: @node, char_offset: uint, char_len: uint) -> @node {
let node = node;
let char_offset = char_offset;
while true {
loop {
alt(*node) {
node::leaf(x) {
if char_offset == 0u && char_len == x.char_len {
@ -997,8 +996,7 @@ mod node {
}
}
}
}
core::unreachable();
};
}
fn concat2(left: @node, right: @node) -> @node {
@ -1066,7 +1064,7 @@ mod node {
"]
fn loop_leaves(node: @node, it: fn(leaf) -> bool) -> bool{
let current = node;
while true {
loop {
alt(*current) {
leaf(x) {
ret it(x);
@ -1079,8 +1077,7 @@ mod node {
}
}
}
}
core::unreachable();
};
}
#[doc ="
@ -1103,7 +1100,7 @@ mod node {
fn char_at(node: @node, pos: uint) -> char {
let node = node;
let pos = pos;
while true {
loop {
alt *node {
leaf(x) {
ret str::char_at(*x.content, pos);
@ -1114,8 +1111,7 @@ mod node {
else { pos -= left_len; right };
}
}
}
core::unreachable();
};
}
mod leaf_iterator {
@ -1139,7 +1135,7 @@ mod node {
fn next(it: t) -> option<leaf> {
if it.stackpos < 0 { ret option::none; }
while true {
loop {
let current = it.stack[it.stackpos];
it.stackpos -= 1;
alt(*current) {
@ -1153,8 +1149,7 @@ mod node {
ret option::some(x);
}
}
}
core::unreachable();
};
}
}
@ -1182,7 +1177,7 @@ mod node {
}
fn next(it: t) -> option<char> {
while true {
loop {
alt(get_current_or_next_leaf(it)) {
option::none { ret option::none; }
option::some(_) {
@ -1197,8 +1192,7 @@ mod node {
}
}
}
}
core::unreachable();
};
}
fn get_current_or_next_leaf(it: t) -> option<leaf> {
@ -1326,7 +1320,7 @@ mod tests {
let len = 0u;
let it = iterator::char::start(r);
while true {
loop {
alt(node::char_iterator::next(it)) {
option::none { break; }
option::some(_) { len += 1u; }

View File

@ -100,7 +100,7 @@ fn qsort3<T: copy>(compare_func_lt: le<T>, compare_func_eq: le<T>,
let j: int = right;
let p: int = i;
let q: int = j;
while true {
loop {
i += 1;
while compare_func_lt(copy arr[i], v) { i += 1; }
j -= 1;

View File

@ -96,7 +96,7 @@ fn parse_path(st: @pstate) -> @ast::path {
let idents: [ast::ident] = [];
fn is_last(c: char) -> bool { ret c == '(' || c == ':'; }
idents += [parse_ident_(st, is_last)];
while true {
loop {
alt peek(st) {
':' { next(st); next(st); }
c {
@ -106,8 +106,7 @@ fn parse_path(st: @pstate) -> @ast::path {
} else { idents += [parse_ident_(st, is_last)]; }
}
}
}
fail "parse_path: ill-formed path";
};
}
fn parse_constr_arg(st: @pstate) -> ast::fn_constr_arg {
@ -328,28 +327,26 @@ fn parse_def(st: @pstate, conv: conv_did) -> ast::def_id {
fn parse_int(st: @pstate) -> int {
let n = 0;
while true {
loop {
let cur = peek(st);
if cur < '0' || cur > '9' { break; }
if cur < '0' || cur > '9' { ret n; }
st.pos = st.pos + 1u;
n *= 10;
n += (cur as int) - ('0' as int);
}
ret n;
};
}
fn parse_hex(st: @pstate) -> uint {
let n = 0u;
while true {
loop {
let cur = peek(st);
if (cur < '0' || cur > '9') && (cur < 'a' || cur > 'f') { break; }
if (cur < '0' || cur > '9') && (cur < 'a' || cur > 'f') { ret n; }
st.pos = st.pos + 1u;
n *= 16u;
if '0' <= cur && cur <= '9' {
n += (cur as uint) - ('0' as uint);
} else { n += 10u + (cur as uint) - ('a' as uint); }
}
ret n;
};
}
fn parse_ty_fn(st: @pstate, conv: conv_did) -> ty::fn_ty {
@ -405,7 +402,7 @@ fn parse_bounds_data(data: @[u8], start: uint,
fn parse_bounds(st: @pstate, conv: conv_did) -> @[ty::param_bound] {
let bounds = [];
while true {
loop {
bounds += [alt check next(st) {
'S' { ty::bound_send }
'C' { ty::bound_copy }

View File

@ -661,16 +661,15 @@ fn unsafe_set(from: option<unsafe_ty>) -> [unsafe_ty] {
fn find_invalid(id: node_id, lst: list<@invalid>)
-> option<@invalid> {
let cur = lst;
while true {
loop {
alt cur {
list::nil { break; }
list::nil { ret none; }
list::cons(head, tail) {
if head.node_id == id { ret some(head); }
cur = *tail;
}
}
}
ret none;
};
}
fn join_invalid(a: list<@invalid>, b: list<@invalid>) -> list<@invalid> {

View File

@ -16,7 +16,7 @@ fn expr_root(tcx: ty::ctxt, ex: @expr, autoderef: bool) ->
{ex: @expr, ds: @[deref]} {
fn maybe_auto_unbox(tcx: ty::ctxt, t: ty::t) -> {t: ty::t, ds: [deref]} {
let ds = [], t = t;
while true {
loop {
alt ty::get(t).struct {
ty::ty_box(mt) {
ds += [@{mutbl: mt.mutbl == m_mutbl,
@ -49,7 +49,7 @@ fn expr_root(tcx: ty::ctxt, ex: @expr, autoderef: bool) ->
ret {t: t, ds: ds};
}
let ds: [deref] = [], ex = ex;
while true {
loop {
alt copy ex.node {
expr_field(base, ident, _) {
let auto_unbox = maybe_auto_unbox(tcx, ty::expr_ty(tcx, base));

View File

@ -677,7 +677,7 @@ fn follow_import(e: env, sc: scopes, path: [ident], sp: span) ->
let path_len = vec::len(path);
let dcur = lookup_in_scope_strict(e, sc, sp, path[0], ns_module);
let i = 1u;
while true {
loop {
alt dcur {
some(dcur_def) {
if i == path_len { break; }
@ -790,7 +790,7 @@ fn resolve_import(e: env, defid: ast::def_id, name: ast::ident,
}
some(dcur_) {
let dcur = dcur_, i = 1u;
while true {
loop {
if i == n_idents - 1u {
let impls = [];
find_impls_in_mod(e, dcur, impls, some(end_id));
@ -846,7 +846,7 @@ enum ctxt { in_mod(def), in_scope(scopes), }
fn unresolved_err(e: env, cx: ctxt, sp: span, name: ident, kind: str) {
fn find_fn_or_mod_scope(sc: scopes) -> option<scope> {
let sc = sc;
while true {
loop {
alt sc {
cons(cur, rest) {
alt cur {
@ -860,8 +860,7 @@ fn unresolved_err(e: env, cx: ctxt, sp: span, name: ident, kind: str) {
}
_ { ret none; }
}
}
core::unreachable()
};
}
let path = name;
alt cx {
@ -1098,7 +1097,7 @@ fn lookup_in_scope(e: env, sc: scopes, sp: span, name: ident, ns: namespace)
// Used to determine whether self is in scope
let left_fn_level2 = false;
let sc = sc;
while true {
loop {
alt copy sc {
nil { ret none; }
cons(hd, tl) {
@ -1150,8 +1149,7 @@ fn lookup_in_scope(e: env, sc: scopes, sp: span, name: ident, ns: namespace)
sc = *tl;
}
}
}
e.sess.bug("reached unreachable code in lookup_in_scope"); // sigh
};
}
fn lookup_in_ty_params(e: env, name: ident, ty_params: [ast::ty_param])
@ -1766,7 +1764,7 @@ fn check_mod_name(e: env, name: ident, entries: list<mod_index_entry>) {
fn dup(e: env, sp: span, word: str, name: ident) {
e.sess.span_fatal(sp, "duplicate definition of " + word + name);
}
while true {
loop {
alt entries {
cons(entry, rest) {
if !is_none(lookup_in_mie(e, entry, ns_val(value_or_enum))) {

View File

@ -1829,7 +1829,7 @@ fn autoderef(cx: block, v: ValueRef, t: ty::t) -> result_t {
let v1: ValueRef = v;
let t1: ty::t = t;
let ccx = cx.ccx();
while true {
loop {
alt ty::get(t1).struct {
ty::ty_box(mt) {
let body = GEPi(cx, v1, [0, abi::box_field_body]);
@ -2935,7 +2935,7 @@ fn invoke_(bcx: block, llfn: ValueRef, llargs: [ValueRef],
fn get_landing_pad(bcx: block) -> BasicBlockRef {
fn in_lpad_scope_cx(bcx: block, f: fn(scope_info)) {
let bcx = bcx;
while true {
loop {
alt bcx.kind {
block_scope(info) {
if info.cleanups.len() > 0u || bcx.parent == parent_none {
@ -3493,7 +3493,7 @@ fn trans_break_cont(bcx: block, to_end: bool)
-> block {
// Locate closest loop block, outputting cleanup as we go.
let unwind = bcx, target = bcx;
while true {
loop {
alt unwind.kind {
block_scope({is_loop: some({cnt, brk}), _}) {
target = if to_end {
@ -3748,7 +3748,7 @@ fn trans_block_cleanups(bcx: block, cleanup_cx: block) ->
fn cleanup_and_leave(bcx: block, upto: option<BasicBlockRef>,
leave: option<BasicBlockRef>) {
let cur = bcx, bcx = bcx;
while true {
loop {
alt cur.kind {
block_scope(info) if info.cleanups.len() > 0u {
for exists in info.cleanup_paths {

View File

@ -398,7 +398,7 @@ fn struct_elt(llstructty: TypeRef, n: uint) -> TypeRef unsafe {
fn in_scope_cx(cx: block, f: fn(scope_info)) {
let cur = cx;
while true {
loop {
alt cur.kind {
block_scope(info) { f(info); ret; }
_ {}

View File

@ -171,7 +171,7 @@ fn scan_exponent(rdr: reader) -> option<str> {
fn scan_digits(rdr: reader, radix: uint) -> str {
let rslt = "";
while true {
loop {
let c = rdr.curr;
if c == '_' { rdr.bump(); cont; }
alt char::to_digit(c, radix) {
@ -179,10 +179,9 @@ fn scan_digits(rdr: reader, radix: uint) -> str {
str::push_char(rslt, c);
rdr.bump();
}
_ { break; }
_ { ret rslt; }
}
}
ret rslt;
};
}
fn scan_number(c: char, rdr: reader) -> token::token {
@ -711,7 +710,7 @@ fn gather_comments_and_literals(cm: codemap::codemap,
let literals: [lit] = [];
let first_read: bool = true;
while !rdr.is_eof() {
while true {
loop {
let code_to_the_left = !first_read;
consume_non_eol_whitespace(rdr);
if rdr.curr == '\n' {

View File

@ -367,12 +367,11 @@ fn parse_constrs<T: copy>(pser: fn(parser) -> @ast::constr_general<T>,
p: parser) ->
[@ast::constr_general<T>] {
let constrs: [@ast::constr_general<T>] = [];
while true {
loop {
let constr = pser(p);
constrs += [constr];
if p.token == token::COMMA { p.bump(); } else { break; }
}
constrs
if p.token == token::COMMA { p.bump(); } else { ret constrs; }
};
}
fn parse_type_constraints(p: parser) -> [@ast::ty_constr] {
@ -1030,7 +1029,7 @@ fn parse_dot_or_call_expr_with(p: parser, e0: pexpr) -> pexpr {
let e = e0;
let lo = e.span.lo;
let hi = e.span.hi;
while true {
loop {
// expr.f
if eat(p, token::DOT) {
alt p.token {
@ -1310,7 +1309,7 @@ fn parse_capture_clause(p: parser) -> @ast::capture_clause {
fn eat_ident_list(p: parser) -> [@ast::capture_item] {
let res = [];
while true {
loop {
alt p.token {
token::IDENT(_, _) {
let id = p.get_id();
@ -1324,8 +1323,7 @@ fn parse_capture_clause(p: parser) -> @ast::capture_clause {
_ { ret res; }
}
}
core::unreachable();
};
}
let copies = [];
@ -1471,11 +1469,10 @@ fn parse_initializer(p: parser) -> option<ast::initializer> {
fn parse_pats(p: parser) -> [@ast::pat] {
let pats = [];
while true {
loop {
pats += [parse_pat(p)];
if p.token == token::BINOP(token::OR) { p.bump(); } else { break; }
}
ret pats;
if p.token == token::BINOP(token::OR) { p.bump(); } else { ret pats; }
};
}
fn parse_pat(p: parser) -> @ast::pat {

View File

@ -1538,7 +1538,7 @@ fn print_remaining_comments(s: ps) {
// If there aren't any remaining comments, then we need to manually
// make sure there is a line break at the end.
if option::is_none(next_comment(s)) { hardbreak(s.s); }
while true {
loop {
alt next_comment(s) {
some(cmnt) { print_comment(s, cmnt); s.cur_cmnt += 1u; }
_ { break; }
@ -1610,7 +1610,7 @@ fn next_lit(s: ps, pos: uint) -> option<lexer::lit> {
}
fn maybe_print_comment(s: ps, pos: uint) {
while true {
loop {
alt next_comment(s) {
some(cmnt) {
if cmnt.pos < pos {