Skip to content

Commit 35400e1

Browse files
committed
Use loop instead of while(true) in libraries and compiler itself
And remove spurious fails/unreachable() calls.
1 parent 98260a2 commit 35400e1

File tree

19 files changed

+81
-104
lines changed

19 files changed

+81
-104
lines changed

src/libcore/int.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,16 +64,15 @@ fn parse_buf(buf: [u8], radix: uint) -> option<int> {
6464
start = 1u;
6565
}
6666
let mut n = 0;
67-
while true {
67+
loop {
6868
alt char::to_digit(buf[i] as char, radix) {
6969
some(d) { n += (d as int) * power; }
7070
none { ret none; }
7171
}
7272
power *= radix as int;
7373
if i <= start { ret some(n); }
7474
i -= 1u;
75-
}
76-
fail;
75+
};
7776
}
7877

7978
#[doc = "Parse a string to an int"]

src/libcore/u64.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,16 +70,15 @@ fn from_str(buf: str, radix: u64) -> option<u64> {
7070
if str::len(buf) == 0u { ret none; }
7171
let mut i = str::len(buf) - 1u;
7272
let mut power = 1u64, n = 0u64;
73-
while true {
73+
loop {
7474
alt char::to_digit(buf[i] as char, radix as uint) {
7575
some(d) { n += d as u64 * power; }
7676
none { ret none; }
7777
}
7878
power *= radix;
7979
if i == 0u { ret some(n); }
8080
i -= 1u;
81-
}
82-
fail;
81+
};
8382
}
8483

8584
#[doc = "Computes the bitwise complement"]

src/libcore/uint.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,16 +133,15 @@ fn parse_buf(buf: [u8], radix: uint) -> option<uint> {
133133
let mut i = vec::len(buf) - 1u;
134134
let mut power = 1u;
135135
let mut n = 0u;
136-
while true {
136+
loop {
137137
alt char::to_digit(buf[i] as char, radix) {
138138
some(d) { n += d * power; }
139139
none { ret none; }
140140
}
141141
power *= radix;
142142
if i == 0u { ret some(n); }
143143
i -= 1u;
144-
}
145-
fail;
144+
};
146145
}
147146

148147
#[doc = "Parse a string to an int"]

src/libstd/generic_os.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ fn setenv(n: str, v: str) {
5959
#[cfg(target_os = "win32")]
6060
fn getenv(n: str) -> option<str> {
6161
let nsize = 256u;
62-
while true {
62+
loop {
6363
let v: [u8] = [];
6464
vec::reserve(v, nsize);
6565
let res =
@@ -80,7 +80,6 @@ fn getenv(n: str) -> option<str> {
8080
ret option::some(str::from_bytes(v)); // UTF-8 or fail
8181
} else { nsize = res; }
8282
}
83-
core::unreachable();
8483
}
8584

8685
#[cfg(target_os = "win32")]

src/libstd/io.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ impl reader_util for reader {
104104

105105
fn read_line() -> str {
106106
let buf: [u8] = [];
107-
while true {
107+
loop {
108108
let ch = self.read_byte();
109109
if ch == -1 || ch == 10 { break; }
110110
buf += [ch as u8];
@@ -114,7 +114,7 @@ impl reader_util for reader {
114114

115115
fn read_c_str() -> str {
116116
let buf: [u8] = [];
117-
while true {
117+
loop {
118118
let ch = self.read_byte();
119119
if ch < 1 { break; } else { buf += [ch as u8]; }
120120
}

src/libstd/json.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -383,23 +383,23 @@ impl parser for parser {
383383
ret ok(list(values));
384384
}
385385

386-
while true {
386+
loop {
387387
alt self.parse_value() {
388388
ok(v) { vec::push(values, v); }
389389
e { ret e; }
390390
}
391391

392392
self.parse_whitespace();
393-
if self.eof() { break; }
393+
if self.eof() {
394+
ret self.error("EOF while parsing list");
395+
}
394396

395397
alt self.ch {
396398
',' { self.bump(); }
397399
']' { self.bump(); ret ok(list(values)); }
398400
_ { ret self.error("expecting ',' or ']'"); }
399401
}
400-
}
401-
402-
ret self.error("EOF while parsing list");
402+
};
403403
}
404404

405405
fn parse_object() -> result::t<json, error> {

src/libstd/list.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,27 +43,25 @@ is returned. If `f` matches no elements then none is returned.
4343
fn find<T: copy, U: copy>(ls: list<T>, f: fn(T) -> option<U>)
4444
-> option<U> {
4545
let ls = ls;
46-
while true {
46+
loop {
4747
alt ls {
4848
cons(hd, tl) {
4949
alt f(hd) { none { ls = *tl; } some(rs) { ret some(rs); } }
5050
}
51-
nil { break; }
51+
nil { ret none; }
5252
}
53-
}
54-
ret none;
53+
};
5554
}
5655

5756
#[doc = "Returns true if a list contains an element with the given value"]
5857
fn has<T: copy>(ls: list<T>, elt: T) -> bool {
5958
let ls = ls;
60-
while true {
59+
loop {
6160
alt ls {
6261
cons(hd, tl) { if elt == hd { ret true; } else { ls = *tl; } }
63-
nil { break; }
62+
nil { ret false; }
6463
}
65-
}
66-
ret false;
64+
};
6765
}
6866

6967
#[doc = "Returns true if the list is empty"]
@@ -113,7 +111,7 @@ fn iter<T>(l: list<T>, f: fn(T)) {
113111
cons(hd, tl) {
114112
f(hd);
115113
let cur = tl;
116-
while true {
114+
loop {
117115
alt *cur {
118116
cons(hd, tl) {
119117
f(hd);

src/libstd/map.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ mod chained {
9696
e_root: @entry<K,V>) -> search_result<K,V> {
9797
let e0 = e_root;
9898
let comp = 1u; // for logging
99-
while true {
99+
loop {
100100
alt e0.next {
101101
absent {
102102
#debug("search_tbl: absent, comp %u, hash %u, idx %u",
@@ -115,8 +115,7 @@ mod chained {
115115
}
116116
}
117117
}
118-
}
119-
core::unreachable();
118+
};
120119
}
121120

122121
fn search_tbl<K: copy, V: copy>(
@@ -209,7 +208,7 @@ mod chained {
209208
fn foreach_entry<K: copy, V: copy>(chain0: chain<K,V>,
210209
blk: fn(@entry<K,V>)) {
211210
let chain = chain0;
212-
while true {
211+
loop {
213212
alt chain {
214213
absent { ret; }
215214
present(entry) {

src/libstd/rope.rs

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -800,7 +800,7 @@ mod node {
800800
let buf = vec::to_mut(vec::init_elt(byte_len(node), 0u8));
801801
let offset = 0u;//Current position in the buffer
802802
let it = leaf_iterator::start(node);
803-
while true {
803+
loop {
804804
alt(leaf_iterator::next(it)) {
805805
option::none { break; }
806806
option::some(x) {
@@ -862,7 +862,7 @@ mod node {
862862
//1. Gather all leaves as a forest
863863
let forest = [mutable];
864864
let it = leaf_iterator::start(node);
865-
while true {
865+
loop {
866866
alt (leaf_iterator::next(it)) {
867867
option::none { break; }
868868
option::some(x) { forest += [mutable @leaf(x)]; }
@@ -896,7 +896,7 @@ mod node {
896896
fn sub_bytes(node: @node, byte_offset: uint, byte_len: uint) -> @node {
897897
let node = node;
898898
let byte_offset = byte_offset;
899-
while true {
899+
loop {
900900
if byte_offset == 0u && byte_len == node::byte_len(node) {
901901
ret node;
902902
}
@@ -932,8 +932,7 @@ mod node {
932932
}
933933
}
934934
}
935-
}
936-
core::unreachable();
935+
};
937936
}
938937

939938
#[doc ="
@@ -958,7 +957,7 @@ mod node {
958957
fn sub_chars(node: @node, char_offset: uint, char_len: uint) -> @node {
959958
let node = node;
960959
let char_offset = char_offset;
961-
while true {
960+
loop {
962961
alt(*node) {
963962
node::leaf(x) {
964963
if char_offset == 0u && char_len == x.char_len {
@@ -997,8 +996,7 @@ mod node {
997996
}
998997
}
999998
}
1000-
}
1001-
core::unreachable();
999+
};
10021000
}
10031001

10041002
fn concat2(left: @node, right: @node) -> @node {
@@ -1066,7 +1064,7 @@ mod node {
10661064
"]
10671065
fn loop_leaves(node: @node, it: fn(leaf) -> bool) -> bool{
10681066
let current = node;
1069-
while true {
1067+
loop {
10701068
alt(*current) {
10711069
leaf(x) {
10721070
ret it(x);
@@ -1079,8 +1077,7 @@ mod node {
10791077
}
10801078
}
10811079
}
1082-
}
1083-
core::unreachable();
1080+
};
10841081
}
10851082

10861083
#[doc ="
@@ -1103,7 +1100,7 @@ mod node {
11031100
fn char_at(node: @node, pos: uint) -> char {
11041101
let node = node;
11051102
let pos = pos;
1106-
while true {
1103+
loop {
11071104
alt *node {
11081105
leaf(x) {
11091106
ret str::char_at(*x.content, pos);
@@ -1114,8 +1111,7 @@ mod node {
11141111
else { pos -= left_len; right };
11151112
}
11161113
}
1117-
}
1118-
core::unreachable();
1114+
};
11191115
}
11201116

11211117
mod leaf_iterator {
@@ -1139,7 +1135,7 @@ mod node {
11391135

11401136
fn next(it: t) -> option<leaf> {
11411137
if it.stackpos < 0 { ret option::none; }
1142-
while true {
1138+
loop {
11431139
let current = it.stack[it.stackpos];
11441140
it.stackpos -= 1;
11451141
alt(*current) {
@@ -1153,8 +1149,7 @@ mod node {
11531149
ret option::some(x);
11541150
}
11551151
}
1156-
}
1157-
core::unreachable();
1152+
};
11581153
}
11591154
}
11601155

@@ -1182,7 +1177,7 @@ mod node {
11821177
}
11831178

11841179
fn next(it: t) -> option<char> {
1185-
while true {
1180+
loop {
11861181
alt(get_current_or_next_leaf(it)) {
11871182
option::none { ret option::none; }
11881183
option::some(_) {
@@ -1197,8 +1192,7 @@ mod node {
11971192
}
11981193
}
11991194
}
1200-
}
1201-
core::unreachable();
1195+
};
12021196
}
12031197

12041198
fn get_current_or_next_leaf(it: t) -> option<leaf> {
@@ -1326,7 +1320,7 @@ mod tests {
13261320

13271321
let len = 0u;
13281322
let it = iterator::char::start(r);
1329-
while true {
1323+
loop {
13301324
alt(node::char_iterator::next(it)) {
13311325
option::none { break; }
13321326
option::some(_) { len += 1u; }

src/libstd/sort.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ fn qsort3<T: copy>(compare_func_lt: le<T>, compare_func_eq: le<T>,
100100
let j: int = right;
101101
let p: int = i;
102102
let q: int = j;
103-
while true {
103+
loop {
104104
i += 1;
105105
while compare_func_lt(copy arr[i], v) { i += 1; }
106106
j -= 1;

0 commit comments

Comments
 (0)