提交 321fd802 编写于 作者: T Tim Chevalier

Add an infinite loop construct

Add a loop {} construct for infinite loops, and use it in test
cases. See #1906 for details.
上级 4ffcb959
......@@ -138,7 +138,7 @@ fn range(lo: uint, hi: uint, it: fn(uint)) {
}
/*
Function: loop
Function: iterate
Iterate over the range [`lo`..`hi`), or stop when requested
......@@ -153,7 +153,7 @@ fn range(lo: uint, hi: uint, it: fn(uint)) {
`true` If execution proceeded correctly, `false` if it was interrupted,
that is if `it` returned `false` at any point.
*/
fn loop(lo: uint, hi: uint, it: fn(uint) -> bool) -> bool {
fn iterate(lo: uint, hi: uint, it: fn(uint) -> bool) -> bool {
let mut i = lo;
while i < hi {
if (!it(i)) { ret false; }
......
......@@ -53,11 +53,11 @@ enum uv_loop {
#[nolink]
native mod rustrt {
fn rust_uv_loop_new() -> *ctypes::void;
fn rust_uv_loop_delete(loop: *ctypes::void);
fn rust_uv_loop_delete(lp: *ctypes::void);
fn rust_uv_loop_set_data(
loop: *ctypes::void,
lp: *ctypes::void,
data: *uv_loop_data);
fn rust_uv_bind_op_cb(loop: *ctypes::void, cb: *u8)
fn rust_uv_bind_op_cb(lp: *ctypes::void, cb: *u8)
-> *ctypes::void;
fn rust_uv_stop_op_cb(handle: *ctypes::void);
fn rust_uv_run(loop_handle: *ctypes::void);
......@@ -317,37 +317,37 @@ fn loop_new() -> uv_loop unsafe {
ret comm::recv(ret_recv_port);
}
fn loop_delete(loop: uv_loop) {
let loop_ptr = get_loop_ptr_from_uv_loop(loop);
fn loop_delete(lp: uv_loop) {
let loop_ptr = get_loop_ptr_from_uv_loop(lp);
rustrt::rust_uv_loop_delete(loop_ptr);
}
fn run(loop: uv_loop) {
fn run(lp: uv_loop) {
let end_port = comm::port::<bool>();
let end_chan = comm::chan::<bool>(end_port);
let loop_chan = get_loop_chan_from_uv_loop(loop);
let loop_chan = get_loop_chan_from_uv_loop(lp);
comm::send(loop_chan, msg_run(end_chan));
comm::recv(end_port);
}
fn run_in_bg(loop: uv_loop) {
let loop_chan = get_loop_chan_from_uv_loop(loop);
fn run_in_bg(lp: uv_loop) {
let loop_chan = get_loop_chan_from_uv_loop(lp);
comm::send(loop_chan, msg_run_in_bg);
}
fn async_init (
loop: uv_loop,
lp: uv_loop,
async_cb: fn~(uv_handle),
after_cb: fn~(uv_handle)) {
let msg = msg_async_init(async_cb, after_cb);
let loop_chan = get_loop_chan_from_uv_loop(loop);
let loop_chan = get_loop_chan_from_uv_loop(lp);
comm::send(loop_chan, msg);
}
fn async_send(async: uv_handle) {
alt async {
uv_async(id, loop) {
let loop_chan = get_loop_chan_from_uv_loop(loop);
uv_async(id, lp) {
let loop_chan = get_loop_chan_from_uv_loop(lp);
comm::send(loop_chan, msg_async_send(id));
}
_ {
......@@ -362,18 +362,18 @@ fn close(h: uv_handle, cb: fn~()) {
comm::send(loop_chan, msg_close(h, cb));
}
fn timer_init(loop: uv_loop, after_cb: fn~(uv_handle)) {
fn timer_init(lp: uv_loop, after_cb: fn~(uv_handle)) {
let msg = msg_timer_init(after_cb);
let loop_chan = get_loop_chan_from_uv_loop(loop);
let loop_chan = get_loop_chan_from_uv_loop(lp);
comm::send(loop_chan, msg);
}
fn timer_start(the_timer: uv_handle, timeout: u32, repeat:u32,
timer_cb: fn~(uv_handle)) {
alt the_timer {
uv_timer(id, loop) {
uv_timer(id, lp) {
let msg = msg_timer_start(id, timeout, repeat, timer_cb);
let loop_chan = get_loop_chan_from_uv_loop(loop);
let loop_chan = get_loop_chan_from_uv_loop(lp);
comm::send(loop_chan, msg);
}
_ {
......@@ -385,8 +385,8 @@ fn timer_start(the_timer: uv_handle, timeout: u32, repeat:u32,
fn timer_stop(the_timer: uv_handle, after_cb: fn~(uv_handle)) {
alt the_timer {
uv_timer(id, loop) {
let loop_chan = get_loop_chan_from_uv_loop(loop);
uv_timer(id, lp) {
let loop_chan = get_loop_chan_from_uv_loop(lp);
let msg = msg_timer_stop(id, after_cb);
comm::send(loop_chan, msg);
}
......@@ -423,8 +423,8 @@ fn get_loop_chan_from_data(data: *uv_loop_data)
fn get_loop_chan_from_handle(handle: uv_handle)
-> comm::chan<uv_msg> {
alt handle {
uv_async(id,loop) | uv_timer(id,loop) {
let loop_chan = get_loop_chan_from_uv_loop(loop);
uv_async(id,lp) | uv_timer(id,lp) {
let loop_chan = get_loop_chan_from_uv_loop(lp);
ret loop_chan;
}
_ {
......@@ -434,15 +434,15 @@ fn get_loop_chan_from_handle(handle: uv_handle)
}
}
fn get_loop_ptr_from_uv_loop(loop: uv_loop) -> *ctypes::void {
alt loop {
fn get_loop_ptr_from_uv_loop(lp: uv_loop) -> *ctypes::void {
alt lp {
uv_loop_new(loop_chan, loop_ptr) {
ret loop_ptr;
}
}
}
fn get_loop_chan_from_uv_loop(loop: uv_loop) -> comm::chan<uv_msg> {
alt loop {
fn get_loop_chan_from_uv_loop(lp: uv_loop) -> comm::chan<uv_msg> {
alt lp {
uv_loop_new(loop_chan, loop_ptr) {
ret loop_chan;
}
......@@ -451,7 +451,7 @@ fn get_loop_chan_from_uv_loop(loop: uv_loop) -> comm::chan<uv_msg> {
fn get_id_from_handle(handle: uv_handle) -> [u8] {
alt handle {
uv_async(id,loop) | uv_timer(id,loop) {
uv_async(id,lp) | uv_timer(id,lp) {
ret id;
}
_ {
......@@ -462,7 +462,7 @@ fn get_id_from_handle(handle: uv_handle) -> [u8] {
// crust
crust fn process_operation(
loop: *ctypes::void,
lp: *ctypes::void,
data: *uv_loop_data) unsafe {
let op_port = (*data).operation_port;
let loop_chan = get_loop_chan_from_data(data);
......@@ -472,7 +472,7 @@ fn get_id_from_handle(handle: uv_handle) -> [u8] {
op_async_init(id) {
let id_ptr = vec::unsafe::to_ptr(id);
let async_handle = rustrt::rust_uv_async_init(
loop,
lp,
process_async_send,
id_ptr);
comm::send(loop_chan, uv_async_init(
......@@ -485,7 +485,7 @@ fn get_id_from_handle(handle: uv_handle) -> [u8] {
op_timer_init(id) {
let id_ptr = vec::unsafe::to_ptr(id);
let timer_handle = rustrt::rust_uv_timer_init(
loop,
lp,
process_timer_call,
id_ptr);
comm::send(loop_chan, uv_timer_init(
......@@ -515,12 +515,12 @@ fn get_id_from_handle(handle: uv_handle) -> [u8] {
fn handle_op_close(handle: uv_handle, handle_ptr: *ctypes::void) {
// it's just like im doing C
alt handle {
uv_async(id, loop) {
uv_async(id, lp) {
let cb = process_close_async;
rustrt::rust_uv_close(
handle_ptr, cb);
}
uv_timer(id, loop) {
uv_timer(id, lp) {
let cb = process_close_timer;
rustrt::rust_uv_close(
handle_ptr, cb);
......
此差异已折叠。
......@@ -33,7 +33,7 @@ enum is_last_use {
type last_uses = std::map::hashmap<node_id, is_last_use>;
enum seen { unset, seen(node_id), }
enum block_type { func, loop, }
enum block_type { func, lp, }
enum use { var_use(node_id), close_over(node_id), }
type set = [{def: node_id, uses: list<use>}];
......@@ -97,13 +97,13 @@ fn visit_expr(ex: @expr, cx: ctx, v: visit::vt<ctx>) {
visit::visit_expr_opt(oexpr, cx, v);
leave_fn(cx);
}
expr_break { add_block_exit(cx, loop); }
expr_while(_, _) | expr_do_while(_, _) {
visit_block(loop, cx) {|| visit::visit_expr(ex, cx, v);}
expr_break { add_block_exit(cx, lp); }
expr_while(_, _) | expr_do_while(_, _) | expr_loop(_) {
visit_block(lp, cx) {|| visit::visit_expr(ex, cx, v);}
}
expr_for(_, coll, blk) {
v.visit_expr(coll, cx, v);
visit_block(loop, cx) {|| visit::visit_block(blk, cx, v);}
visit_block(lp, cx) {|| visit::visit_block(blk, cx, v);}
}
expr_alt(input, arms, _) {
v.visit_expr(input, cx, v);
......
......@@ -2029,6 +2029,17 @@ fn trans_do_while(cx: block, body: ast::blk, cond: @ast::expr) ->
ret next_cx;
}
fn trans_loop(cx:block, body: ast::blk) -> block {
let next_cx = sub_block(cx, "next");
let body_cx =
loop_scope_block(cx, cont_self, next_cx,
"infinite loop body", body.span);
let body_end = trans_block(body_cx, body, ignore);
cleanup_and_Br(body_end, body_cx, body_cx.llbb);
Br(cx, body_cx.llbb);
ret next_cx;
}
type generic_info = {item_type: ty::t,
static_tis: [option<@tydesc_info>],
tydescs: [ValueRef],
......@@ -3255,6 +3266,10 @@ fn trans_expr(bcx: block, e: @ast::expr, dest: dest) -> block {
assert dest == ignore;
ret trans_while(bcx, cond, body);
}
ast::expr_loop(body) {
assert dest == ignore;
ret trans_loop(bcx, body);
}
ast::expr_do_while(body, cond) {
assert dest == ignore;
ret trans_do_while(bcx, body, cond);
......@@ -3293,7 +3308,7 @@ fn trans_expr(bcx: block, e: @ast::expr, dest: dest) -> block {
assert dest == ignore;
ret trans_assign_op(bcx, e, op, dst, src);
}
_ { bcx.tcx().sess.span_bug(e.span, "trans_expr reached\
_ { bcx.tcx().sess.span_bug(e.span, "trans_expr reached \
fall-through case"); }
}
......
......@@ -446,6 +446,19 @@ fn find_pre_post_expr(fcx: fn_ctxt, e: @expr) {
expr_pp(fcx.ccx, test)]),
loop_postcond);
}
expr_loop(body) {
find_pre_post_block(fcx, body);
/* Infinite loop: if control passes it, everything is true. */
let loop_postcond = false_postcond(num_local_vars);
/* Conservative approximation: if the body has any nonlocal exits,
the poststate is blank since we don't know what parts of it
execute. */
if has_nonlocal_exits(body) {
loop_postcond = empty_poststate(num_local_vars);
}
set_pre_and_post(fcx.ccx, e.id, block_precond(fcx.ccx, body),
loop_postcond);
}
expr_for(d, index, body) {
find_pre_post_loop(fcx, d, index, body, e.id);
}
......
......@@ -354,7 +354,6 @@ fn find_pre_post_state_cap_clause(fcx: fn_ctxt, e_id: node_id,
fn find_pre_post_state_expr(fcx: fn_ctxt, pres: prestate, e: @expr) -> bool {
let num_constrs = num_constraints(fcx.enclosing);
alt e.node {
expr_vec(elts, _) {
ret find_pre_post_state_exprs(fcx, pres, e.id,
......@@ -489,8 +488,6 @@ fn find_pre_post_state_expr(fcx: fn_ctxt, pres: prestate, e: @expr) -> bool {
*/
let loop_pres =
intersect_states(block_poststate(fcx.ccx, body), pres);
// aux::log_tritv_err(fcx, loop_pres);
// #error("---------------");
let changed =
set_prestate_ann(fcx.ccx, e.id, loop_pres) |
......@@ -545,6 +542,21 @@ fn find_pre_post_state_expr(fcx: fn_ctxt, pres: prestate, e: @expr) -> bool {
}
ret changed;
}
expr_loop(body) {
let loop_pres =
intersect_states(block_poststate(fcx.ccx, body), pres);
let changed = set_prestate_ann(fcx.ccx, e.id, loop_pres)
| find_pre_post_state_block(fcx, loop_pres, body);
/* conservative approximation: if a loop contains a break
or cont, we assume nothing about the poststate */
/* which is still unsound -- see [Break-unsound] */
if has_nonlocal_exits(body) {
ret changed | set_poststate_ann(fcx.ccx, e.id, pres);
} else {
ret changed | set_poststate_ann(fcx.ccx, e.id,
block_poststate(fcx.ccx, body));
}
}
expr_for(d, index, body) {
ret find_pre_post_state_loop(fcx, pres, d, index, body, e.id);
}
......
......@@ -2323,6 +2323,10 @@ fn check_user_unop(fcx: @fn_ctxt, op_str: str, mname: str,
check_block_no_value(fcx, body);
write_ty(tcx, id, block_ty(tcx, body));
}
ast::expr_loop(body) {
check_block_no_value(fcx, body);
write_ty(tcx, id, ty::mk_nil(tcx));
}
ast::expr_alt(expr, arms, _) {
bot = check_expr(fcx, expr);
......@@ -2613,7 +2617,6 @@ fn get_node(f: spanned<field>) -> field { f.node }
}
}
}
_ { tcx.sess.unimpl("expr type in typeck::check_expr"); }
}
if bot { write_ty(tcx, expr.id, ty::mk_bot(tcx)); }
......
......@@ -229,6 +229,10 @@ enum expr_ {
expr_while(@expr, blk),
expr_for(@local, @expr, blk),
expr_do_while(blk, @expr),
/* Conditionless loop (can be exited with break, cont, ret, or fail)
Same semantics as while(true) { body }, but typestate knows that the
(implicit) condition is always true. */
expr_loop(blk),
expr_alt(@expr, [arm], alt_mode),
expr_fn(proto, fn_decl, blk, @capture_clause),
expr_fn_block(fn_decl, blk),
......
......@@ -380,7 +380,7 @@ fn fold_field_(field: field, fld: ast_fold) -> field {
let fold_mac = bind fold_mac_(_, fld);
ret alt e {
expr_vec(exprs, mutt) {
expr_vec(exprs, mutt) {
expr_vec(fld.map_exprs(fld.fold_expr, exprs), mutt)
}
expr_rec(fields, maybe_expr) {
......@@ -417,6 +417,9 @@ fn fold_field_(field: field, fld: ast_fold) -> field {
expr_do_while(blk, expr) {
expr_do_while(fld.fold_block(blk), fld.fold_expr(expr))
}
expr_loop(body) {
expr_loop(fld.fold_block(body))
}
expr_alt(expr, arms, mode) {
expr_alt(fld.fold_expr(expr), vec::map(arms, fld.fold_arm), mode)
}
......
......@@ -146,11 +146,11 @@ fn new_parser(sess: parse_sess, cfg: ast::crate_cfg, rdr: reader,
fn bad_expr_word_table() -> hashmap<str, ()> {
let words = new_str_hash();
for word in ["alt", "assert", "be", "break", "check", "claim",
"class", "const", "cont", "copy", "do", "else", "enum",
"export", "fail", "fn", "for", "if", "iface", "impl",
"import", "let", "log", "mod", "mutable", "native", "pure",
"resource", "ret", "trait", "type", "unchecked", "unsafe",
"while", "crust", "mut"] {
"class", "const", "cont", "copy", "crust", "do", "else",
"enum", "export", "fail", "fn", "for", "if", "iface",
"impl", "import", "let", "log", "loop", "mod", "mut",
"mutable", "native", "pure", "resource", "ret", "trait",
"type", "unchecked", "unsafe", "while"] {
words.insert(word, ());
}
words
......@@ -839,6 +839,8 @@ fn parse_bottom_expr(p: parser) -> pexpr {
ret pexpr(parse_while_expr(p));
} else if eat_word(p, "do") {
ret pexpr(parse_do_while_expr(p));
} else if eat_word(p, "loop") {
ret pexpr(parse_loop_expr(p));
} else if eat_word(p, "alt") {
ret pexpr(parse_alt_expr(p));
} else if eat_word(p, "fn") {
......@@ -1399,6 +1401,13 @@ fn parse_do_while_expr(p: parser) -> @ast::expr {
ret mk_expr(p, lo, hi, ast::expr_do_while(body, cond));
}
fn parse_loop_expr(p: parser) -> @ast::expr {
let lo = p.last_span.lo;
let body = parse_block_no_value(p);
let hi = body.span.hi;
ret mk_expr(p, lo, hi, ast::expr_loop(body));
}
fn parse_alt_expr(p: parser) -> @ast::expr {
let lo = p.last_span.lo;
let mode = if eat_word(p, "check") { ast::alt_check }
......@@ -1691,7 +1700,7 @@ fn expr_requires_semi_to_be_stmt(e: @ast::expr) -> bool {
ast::expr_if(_, _, _) | ast::expr_if_check(_, _, _)
| ast::expr_alt(_, _, _) | ast::expr_block(_)
| ast::expr_do_while(_, _) | ast::expr_while(_, _)
| ast::expr_for(_, _, _)
| ast::expr_loop(_) | ast::expr_for(_, _, _)
| ast::expr_call(_, _, true) {
false
}
......
......@@ -913,6 +913,11 @@ fn print_opt(s: ps, expr: option<@ast::expr>) {
space(s.s);
print_block(s, blk);
}
ast::expr_loop(blk) {
head(s, "loop");
space(s.s);
print_block(s, blk);
}
ast::expr_for(decl, expr, blk) {
head(s, "for");
print_for_decl(s, decl, expr);
......
......@@ -343,6 +343,7 @@ fn visit_expr<E>(ex: @expr, e: E, v: vt<E>) {
visit_expr_opt(eo, e, v);
}
expr_while(x, b) { v.visit_expr(x, e, v); v.visit_block(b, e, v); }
expr_loop(b) { v.visit_block(b, e, v); }
expr_for(dcl, x, b) {
v.visit_local(dcl, e, v);
v.visit_expr(x, e, v);
......
......@@ -289,7 +289,7 @@ fn future_writer() -> (writer, future::future<str>) {
};
let future = future::from_fn {||
let res = "";
while true {
loop {
alt comm::recv(port) {
write(s) { res += s }
done { break }
......
......@@ -43,7 +43,7 @@ fn run(
fn make_doc_from_pages(page_port: page_port) -> doc::doc {
let mut pages = [];
while true {
loop {
let val = comm::recv(page_port);
if option::is_some(val) {
pages += [option::unwrap(val)];
......
......@@ -60,7 +60,7 @@ fn parse_opts(argv: [str]) -> config {
fn stress_task(&&id: int) {
let i = 0;
while true {
loop {
let n = 15;
assert (fib(n) == fib(n));
i += 1;
......
......@@ -34,7 +34,7 @@
fn map(&&filename: [u8], emit: map_reduce::putter<[u8], int>) {
let f = io::file_reader(str::from_bytes(filename));
while true {
loop {
alt read_word(f) {
some(w) { emit(str::bytes(w), 1); }
none { break; }
......@@ -45,7 +45,7 @@ fn map(&&filename: [u8], emit: map_reduce::putter<[u8], int>) {
fn reduce(&&_word: [u8], get: map_reduce::getter<int>) {
let count = 0;
while true { alt get() { some(_) { count += 1; } none { break; } } }
loop { alt get() { some(_) { count += 1; } none { break; } } }
}
mod map_reduce {
......
......@@ -25,7 +25,7 @@ fn map(input: str, emit: map_reduce::putter) {
let f = io::str_reader(input);
while true {
loop {
alt read_word(f) { some(w) { emit(w, 1); } none { break; } }
}
}
......@@ -33,7 +33,7 @@ fn map(input: str, emit: map_reduce::putter) {
fn reduce(_word: str, get: map_reduce::getter) {
let count = 0;
while true { alt get() { some(_) { count += 1; } none { break; } } }
loop { alt get() { some(_) { count += 1; } none { break; } } }
}
mod map_reduce {
......
// error-pattern:mismatched types: expected `()` but found `bool`
fn main() {
while true {
loop {
true
}
}
\ No newline at end of file
// error-pattern: precondition constraint
fn f() -> int { let x: int; while true { x = 10; } ret x; }
fn f() -> int { let x: int; while 1 == 1 { x = 10; } ret x; }
fn main() { f(); }
......@@ -3,11 +3,11 @@ fn main() {
let y: int = 42;
let x: int;
while true {
loop {
log(debug, y);
while true {
while true {
while true { x <- y; }
loop {
loop {
loop { x <- y; }
}
}
}
......
......@@ -9,8 +9,8 @@ fn main() {
let y: int = 42;
let x: int = 1;
check (even(y));
while true {
loop {
print_even(y);
while true { while true { while true { y += x; } } }
loop { loop { loop { y += x; } } }
}
}
......@@ -15,7 +15,7 @@ fn f(c: comm::_chan<int>) {
comm::send(c, 1);
while true {
loop {
// spin waiting for the parent to kill us.
#debug("child waiting to die...");
......
fn int_id(x: int) -> int { ret x; }
fn main() { while true { int_id(break); } }
fn main() { loop { int_id(break); } }
......@@ -14,7 +14,7 @@ fn loop(n: int) {
if n > 0 { t1 = spawn loop(n - 1); t2 = spawn loop(n - 1); }
while true { }
loop { }
}
fn main() { let t: task = spawn loop(5); join(t); }
\ No newline at end of file
......@@ -18,7 +18,7 @@ fn producer(c: chan<[u8]>) {
fn packager(cb: chan<chan<[u8]>>, msg: chan<msg>) {
let p: port<[u8]> = port();
send(cb, chan(p));
while true {
loop {
#debug("waiting for bytes");
let data = recv(p);
#debug("got bytes");
......@@ -46,7 +46,7 @@ fn main() {
let source_chan: chan<[u8]> = recv(recv_reader);
let prod = task::spawn {|| producer(source_chan); };
while true {
loop {
let msg = recv(p);
alt msg {
closed { #debug("Got close message"); break; }
......
......@@ -17,7 +17,7 @@ fn main() {
// Check that no false positives are found in loops.
let q = ~40, p = 10;
while true {
loop {
let i = q;
p += *i;
if p > 100 { break; }
......
// Issue #1818
fn loop<T>(s: str, f: fn(str) -> T) -> T {
fn lp<T>(s: str, f: fn(str) -> T) -> T {
while false {
let r = f(s);
ret r;
......
......@@ -6,7 +6,7 @@ fn starve_main(alive: chan<int>) {
alive <| 1;
#debug("starving main");
let i: int = 0;
while true { i += 1; }
loop { i += 1; }
}
fn main() {
......
......@@ -12,7 +12,7 @@ fn iloop() {
task::spawn {|| die(); };
let p = comm::port::<()>();
let c = comm::chan(p);
while true {
loop {
// Sending and receiving here because these actions yield,
// at which point our child can kill us
comm::send(c, ());
......
......@@ -4,7 +4,7 @@
use std;
fn test_break() { while true { let x: @int = break; } }
fn test_break() { loop { let x: @int = break; } }
fn test_cont() { let i = 0; while i < 1 { i += 1; let x: @int = cont; } }
......
......@@ -23,7 +23,7 @@ fn call_id_4() { while id(break) { } }
fn log_ret() { log(error, ret); }
fn log_break() { while true { log(error, break); } }
fn log_break() { loop { log(error, break); } }
fn log_cont() { do { log(error, cont); } while false }
......
......@@ -55,7 +55,7 @@ fn canttouchthis() -> uint {
}
fn angrydome() {
while true { if break { } }
loop { if break { } }
let i = 0;
do { i += 1; if i == 1 { alt cont { _ { } } } } while false
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册