Add optional message to fail.

This commit is contained in:
Josh Matthews 2011-06-08 03:58:52 -04:00
parent b5c73605ea
commit 2235fb73ef
10 changed files with 41 additions and 13 deletions

View File

@ -272,7 +272,7 @@ tag expr_ {
expr_index(@expr, @expr, ann);
expr_path(path, ann);
expr_ext(path, vec[@expr], option::t[str], @expr, ann);
expr_fail(ann);
expr_fail(ann, option::t[str]);
expr_break(ann);
expr_cont(ann);
expr_ret(option::t[@expr], ann);

View File

@ -256,7 +256,6 @@ fn parse_value_ident(&parser p) -> ast::ident {
ret parse_ident(p);
}
/* FIXME: gross hack copied from rustboot to make certain configuration-based
* decisions work at build-time. We should probably change it to use a
* lexical sytnax-extension or something similar. For now we just imitate
@ -934,7 +933,17 @@ fn parse_bottom_expr(&parser p) -> @ast::expr {
ex = expand_syntax_ext(p, ext_span, pth, es.node,
none[str]);
} else if (eat_word(p, "fail")) {
ex = ast::expr_fail(p.get_ann());
auto msg;
alt (p.peek()) {
case (token::LIT_STR(?s)) {
msg = some[str](p.get_str(s));
p.bump();
}
case (_) {
msg = none[str];
}
}
ex = ast::expr_fail(p.get_ann(), msg);
} else if (eat_word(p, "log")) {
auto e = parse_expr(p);
auto hi = e.span.hi;
@ -1643,7 +1652,7 @@ fn stmt_ends_with_semi(&ast::stmt stmt) -> bool {
case (ast::expr_field(_,_,_)) { ret true; }
case (ast::expr_index(_,_,_)) { ret true; }
case (ast::expr_path(_,_)) { ret true; }
case (ast::expr_fail(_)) { ret true; }
case (ast::expr_fail(_,_)) { ret true; }
case (ast::expr_break(_)) { ret true; }
case (ast::expr_cont(_)) { ret true; }
case (ast::expr_ret(_,_)) { ret true; }

View File

@ -5628,8 +5628,17 @@ fn trans_expr_out(&@block_ctxt cx, &@ast::expr e, out_method output)
ret trans_expr(cx, expanded);
}
case (ast::expr_fail(_)) {
ret trans_fail(cx, some(e.span), "explicit failure");
case (ast::expr_fail(_, ?str)) {
auto failmsg;
alt (str) {
case (some(?msg)) {
failmsg = msg;
}
case (_) {
failmsg = "explicit failure";
}
}
ret trans_fail(cx, some(e.span), failmsg);
}
case (ast::expr_log(?lvl, ?a, _)) {

View File

@ -459,7 +459,7 @@ fn find_pre_post_expr(&fn_ctxt fcx, @expr e) -> () {
find_pre_post_expr(fcx, operator);
copy_pre_post(fcx.ccx, a, operator);
}
case (expr_fail(?a)) {
case (expr_fail(?a, _)) {
set_pre_and_post(fcx.ccx, a,
/* if execution continues after fail,
then everything is true! */

View File

@ -508,7 +508,7 @@ fn find_pre_post_state_expr(&fn_ctxt fcx, &prestate pres, @expr e) -> bool {
expr_poststate(fcx.ccx, operand)) || changed;
ret changed;
}
case (expr_fail(?a)) {
case (expr_fail(?a, _)) {
changed = extend_prestate_ann(fcx.ccx, a, pres) || changed;
/* if execution continues after fail, then everything is true! woo! */
changed = set_poststate_ann(fcx.ccx, a,

View File

@ -1657,7 +1657,7 @@ fn expr_ann(&@ast::expr e) -> ast::ann {
case (ast::expr_index(_,_,?a)) { ret a; }
case (ast::expr_path(_,?a)) { ret a; }
case (ast::expr_ext(_,_,_,_,?a)) { ret a; }
case (ast::expr_fail(?a)) { ret a; }
case (ast::expr_fail(?a,_)) { ret a; }
case (ast::expr_ret(_,?a)) { ret a; }
case (ast::expr_put(_,?a)) { ret a; }
case (ast::expr_be(_,?a)) { ret a; }

View File

@ -1276,7 +1276,7 @@ mod pushdown {
write::ty_only_fixup(fcx, ann.id, t);
}
/* FIXME: should this check the type annotations? */
case (ast::expr_fail(_)) { /* no-op */ }
case (ast::expr_fail(_,_)) { /* no-op */ }
case (ast::expr_log(_,_,_)) { /* no-op */ }
case (ast::expr_break(_)) { /* no-op */ }
case (ast::expr_cont(_)) { /* no-op */ }
@ -1972,7 +1972,7 @@ fn check_expr(&@fn_ctxt fcx, &@ast::expr expr) {
write::ty_only_fixup(fcx, a.id, t);
}
case (ast::expr_fail(?a)) {
case (ast::expr_fail(?a, _)) {
write::bot_ty(fcx.ccx.tcx, a.id);
}

View File

@ -431,7 +431,7 @@ fn walk_expr(&ast_visitor v, @ast::expr e) {
// Only walk expansion, not args/body.
walk_expr(v, expansion);
}
case (ast::expr_fail(_)) { }
case (ast::expr_fail(_, _)) { }
case (ast::expr_break(_)) { }
case (ast::expr_cont(_)) { }
case (ast::expr_ret(?eo, _)) {

View File

@ -825,8 +825,13 @@ fn print_expr(&ps s, &@ast::expr expr) {
case (ast::expr_path(?path,_)) {
print_path(s, path);
}
case (ast::expr_fail(_)) {
case (ast::expr_fail(_, ?str)) {
word(s.s, "fail");
alt (str) {
case (some(?msg)) {
word(s.s, #fmt("\"%s\"", msg));
}
}
}
case (ast::expr_break(_)) {
word(s.s, "break");

View File

@ -0,0 +1,5 @@
// error-pattern:woooo
fn main() {
fail "woooo";
}