From 35400e13ada84aeb578e37262a6bf16c48d128d7 Mon Sep 17 00:00:00 2001 From: Tim Chevalier Date: Sat, 10 Mar 2012 20:34:17 -0800 Subject: [PATCH] Use loop instead of while(true) in libraries and compiler itself And remove spurious fails/unreachable() calls. --- src/libcore/int.rs | 5 ++--- src/libcore/u64.rs | 5 ++--- src/libcore/uint.rs | 5 ++--- src/libstd/generic_os.rs | 3 +-- src/libstd/io.rs | 4 ++-- src/libstd/json.rs | 10 ++++----- src/libstd/list.rs | 16 +++++++------- src/libstd/map.rs | 7 +++---- src/libstd/rope.rs | 36 +++++++++++++------------------- src/libstd/sort.rs | 2 +- src/rustc/metadata/tydecode.rs | 21 ++++++++----------- src/rustc/middle/alias.rs | 7 +++---- src/rustc/middle/mutbl.rs | 4 ++-- src/rustc/middle/resolve.rs | 16 +++++++------- src/rustc/middle/trans/base.rs | 8 +++---- src/rustc/middle/trans/common.rs | 2 +- src/rustc/syntax/parse/lexer.rs | 9 ++++---- src/rustc/syntax/parse/parser.rs | 21 ++++++++----------- src/rustc/syntax/print/pprust.rs | 4 ++-- 19 files changed, 81 insertions(+), 104 deletions(-) diff --git a/src/libcore/int.rs b/src/libcore/int.rs index b0e918fdd99..174d62926f0 100644 --- a/src/libcore/int.rs +++ b/src/libcore/int.rs @@ -64,7 +64,7 @@ fn parse_buf(buf: [u8], radix: uint) -> option { 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 { power *= radix as int; if i <= start { ret some(n); } i -= 1u; - } - fail; + }; } #[doc = "Parse a string to an int"] diff --git a/src/libcore/u64.rs b/src/libcore/u64.rs index 6907ad5fe03..98bb98a0daa 100644 --- a/src/libcore/u64.rs +++ b/src/libcore/u64.rs @@ -70,7 +70,7 @@ fn from_str(buf: str, radix: u64) -> option { 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 { power *= radix; if i == 0u { ret some(n); } i -= 1u; - } - fail; + }; } #[doc = "Computes the bitwise complement"] diff --git a/src/libcore/uint.rs b/src/libcore/uint.rs index 341a155e87b..88230bea094 100644 --- a/src/libcore/uint.rs +++ b/src/libcore/uint.rs @@ -133,7 +133,7 @@ fn parse_buf(buf: [u8], radix: uint) -> option { 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 { power *= radix; if i == 0u { ret some(n); } i -= 1u; - } - fail; + }; } #[doc = "Parse a string to an int"] diff --git a/src/libstd/generic_os.rs b/src/libstd/generic_os.rs index 0432d136c35..246c7ee1d45 100644 --- a/src/libstd/generic_os.rs +++ b/src/libstd/generic_os.rs @@ -59,7 +59,7 @@ fn setenv(n: str, v: str) { #[cfg(target_os = "win32")] fn getenv(n: str) -> option { let nsize = 256u; - while true { + loop { let v: [u8] = []; vec::reserve(v, nsize); let res = @@ -80,7 +80,6 @@ fn getenv(n: str) -> option { ret option::some(str::from_bytes(v)); // UTF-8 or fail } else { nsize = res; } } - core::unreachable(); } #[cfg(target_os = "win32")] diff --git a/src/libstd/io.rs b/src/libstd/io.rs index bab37b0ba80..516aa25f05b 100644 --- a/src/libstd/io.rs +++ b/src/libstd/io.rs @@ -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]; } } diff --git a/src/libstd/json.rs b/src/libstd/json.rs index 499e3f36061..ca3d9f8b4d7 100644 --- a/src/libstd/json.rs +++ b/src/libstd/json.rs @@ -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 { diff --git a/src/libstd/list.rs b/src/libstd/list.rs index 613a4790d36..9f4254f20c4 100644 --- a/src/libstd/list.rs +++ b/src/libstd/list.rs @@ -43,27 +43,25 @@ is returned. If `f` matches no elements then none is returned. fn find(ls: list, f: fn(T) -> option) -> option { 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(ls: list, 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(l: list, f: fn(T)) { cons(hd, tl) { f(hd); let cur = tl; - while true { + loop { alt *cur { cons(hd, tl) { f(hd); diff --git a/src/libstd/map.rs b/src/libstd/map.rs index 6eae6992bcb..ded9e1ee208 100644 --- a/src/libstd/map.rs +++ b/src/libstd/map.rs @@ -96,7 +96,7 @@ mod chained { e_root: @entry) -> search_result { 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( @@ -209,7 +208,7 @@ mod chained { fn foreach_entry(chain0: chain, blk: fn(@entry)) { let chain = chain0; - while true { + loop { alt chain { absent { ret; } present(entry) { diff --git a/src/libstd/rope.rs b/src/libstd/rope.rs index b9656df7e2f..9c6c1b29c24 100644 --- a/src/libstd/rope.rs +++ b/src/libstd/rope.rs @@ -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 { 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 { - 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 { @@ -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; } diff --git a/src/libstd/sort.rs b/src/libstd/sort.rs index 8a4d59d9cfa..6a78a23777e 100644 --- a/src/libstd/sort.rs +++ b/src/libstd/sort.rs @@ -100,7 +100,7 @@ fn qsort3(compare_func_lt: le, compare_func_eq: le, 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; diff --git a/src/rustc/metadata/tydecode.rs b/src/rustc/metadata/tydecode.rs index 284c38a6a3e..37bb1e66f27 100644 --- a/src/rustc/metadata/tydecode.rs +++ b/src/rustc/metadata/tydecode.rs @@ -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 } diff --git a/src/rustc/middle/alias.rs b/src/rustc/middle/alias.rs index 463ad79ffce..126f24caeff 100644 --- a/src/rustc/middle/alias.rs +++ b/src/rustc/middle/alias.rs @@ -661,16 +661,15 @@ fn unsafe_set(from: option) -> [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> { diff --git a/src/rustc/middle/mutbl.rs b/src/rustc/middle/mutbl.rs index 3b2999c38b2..821b4a73402 100644 --- a/src/rustc/middle/mutbl.rs +++ b/src/rustc/middle/mutbl.rs @@ -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)); diff --git a/src/rustc/middle/resolve.rs b/src/rustc/middle/resolve.rs index 7333369d77a..56d2cc973a0 100644 --- a/src/rustc/middle/resolve.rs +++ b/src/rustc/middle/resolve.rs @@ -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 { 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) { 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))) { diff --git a/src/rustc/middle/trans/base.rs b/src/rustc/middle/trans/base.rs index 0a8c0350b40..03749b86818 100644 --- a/src/rustc/middle/trans/base.rs +++ b/src/rustc/middle/trans/base.rs @@ -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, leave: option) { 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 { diff --git a/src/rustc/middle/trans/common.rs b/src/rustc/middle/trans/common.rs index 82f826d9ee8..748a27999a0 100644 --- a/src/rustc/middle/trans/common.rs +++ b/src/rustc/middle/trans/common.rs @@ -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; } _ {} diff --git a/src/rustc/syntax/parse/lexer.rs b/src/rustc/syntax/parse/lexer.rs index 3350d7a7c95..076706b396b 100644 --- a/src/rustc/syntax/parse/lexer.rs +++ b/src/rustc/syntax/parse/lexer.rs @@ -171,7 +171,7 @@ fn scan_exponent(rdr: reader) -> option { 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' { diff --git a/src/rustc/syntax/parse/parser.rs b/src/rustc/syntax/parse/parser.rs index 53f75d8f7c3..e1309fc2641 100644 --- a/src/rustc/syntax/parse/parser.rs +++ b/src/rustc/syntax/parse/parser.rs @@ -367,12 +367,11 @@ fn parse_constrs(pser: fn(parser) -> @ast::constr_general, p: parser) -> [@ast::constr_general] { let constrs: [@ast::constr_general] = []; - 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 { 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 { diff --git a/src/rustc/syntax/print/pprust.rs b/src/rustc/syntax/print/pprust.rs index ba9c76c748f..43f2c1b3db7 100644 --- a/src/rustc/syntax/print/pprust.rs +++ b/src/rustc/syntax/print/pprust.rs @@ -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 { } fn maybe_print_comment(s: ps, pos: uint) { - while true { + loop { alt next_comment(s) { some(cmnt) { if cmnt.pos < pos {