Skip to content

Commit 42ac269

Browse files
committed
Merge pull request #455 from jdm/fail_str
Add optional message to fail.
2 parents 0857d22 + 2235fb7 commit 42ac269

File tree

10 files changed

+41
-13
lines changed

10 files changed

+41
-13
lines changed

src/comp/front/ast.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ tag expr_ {
272272
expr_index(@expr, @expr, ann);
273273
expr_path(path, ann);
274274
expr_ext(path, vec[@expr], option::t[str], @expr, ann);
275-
expr_fail(ann);
275+
expr_fail(ann, option::t[str]);
276276
expr_break(ann);
277277
expr_cont(ann);
278278
expr_ret(option::t[@expr], ann);

src/comp/front/parser.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,6 @@ fn parse_value_ident(&parser p) -> ast::ident {
256256
ret parse_ident(p);
257257
}
258258

259-
260259
/* FIXME: gross hack copied from rustboot to make certain configuration-based
261260
* decisions work at build-time. We should probably change it to use a
262261
* lexical sytnax-extension or something similar. For now we just imitate
@@ -934,7 +933,17 @@ fn parse_bottom_expr(&parser p) -> @ast::expr {
934933
ex = expand_syntax_ext(p, ext_span, pth, es.node,
935934
none[str]);
936935
} else if (eat_word(p, "fail")) {
937-
ex = ast::expr_fail(p.get_ann());
936+
auto msg;
937+
alt (p.peek()) {
938+
case (token::LIT_STR(?s)) {
939+
msg = some[str](p.get_str(s));
940+
p.bump();
941+
}
942+
case (_) {
943+
msg = none[str];
944+
}
945+
}
946+
ex = ast::expr_fail(p.get_ann(), msg);
938947
} else if (eat_word(p, "log")) {
939948
auto e = parse_expr(p);
940949
auto hi = e.span.hi;
@@ -1643,7 +1652,7 @@ fn stmt_ends_with_semi(&ast::stmt stmt) -> bool {
16431652
case (ast::expr_field(_,_,_)) { ret true; }
16441653
case (ast::expr_index(_,_,_)) { ret true; }
16451654
case (ast::expr_path(_,_)) { ret true; }
1646-
case (ast::expr_fail(_)) { ret true; }
1655+
case (ast::expr_fail(_,_)) { ret true; }
16471656
case (ast::expr_break(_)) { ret true; }
16481657
case (ast::expr_cont(_)) { ret true; }
16491658
case (ast::expr_ret(_,_)) { ret true; }

src/comp/middle/trans.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5628,8 +5628,17 @@ fn trans_expr_out(&@block_ctxt cx, &@ast::expr e, out_method output)
56285628
ret trans_expr(cx, expanded);
56295629
}
56305630

5631-
case (ast::expr_fail(_)) {
5632-
ret trans_fail(cx, some(e.span), "explicit failure");
5631+
case (ast::expr_fail(_, ?str)) {
5632+
auto failmsg;
5633+
alt (str) {
5634+
case (some(?msg)) {
5635+
failmsg = msg;
5636+
}
5637+
case (_) {
5638+
failmsg = "explicit failure";
5639+
}
5640+
}
5641+
ret trans_fail(cx, some(e.span), failmsg);
56335642
}
56345643

56355644
case (ast::expr_log(?lvl, ?a, _)) {

src/comp/middle/tstate/pre_post_conditions.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,7 @@ fn find_pre_post_expr(&fn_ctxt fcx, @expr e) -> () {
459459
find_pre_post_expr(fcx, operator);
460460
copy_pre_post(fcx.ccx, a, operator);
461461
}
462-
case (expr_fail(?a)) {
462+
case (expr_fail(?a, _)) {
463463
set_pre_and_post(fcx.ccx, a,
464464
/* if execution continues after fail,
465465
then everything is true! */

src/comp/middle/tstate/states.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -508,7 +508,7 @@ fn find_pre_post_state_expr(&fn_ctxt fcx, &prestate pres, @expr e) -> bool {
508508
expr_poststate(fcx.ccx, operand)) || changed;
509509
ret changed;
510510
}
511-
case (expr_fail(?a)) {
511+
case (expr_fail(?a, _)) {
512512
changed = extend_prestate_ann(fcx.ccx, a, pres) || changed;
513513
/* if execution continues after fail, then everything is true! woo! */
514514
changed = set_poststate_ann(fcx.ccx, a,

src/comp/middle/ty.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1657,7 +1657,7 @@ fn expr_ann(&@ast::expr e) -> ast::ann {
16571657
case (ast::expr_index(_,_,?a)) { ret a; }
16581658
case (ast::expr_path(_,?a)) { ret a; }
16591659
case (ast::expr_ext(_,_,_,_,?a)) { ret a; }
1660-
case (ast::expr_fail(?a)) { ret a; }
1660+
case (ast::expr_fail(?a,_)) { ret a; }
16611661
case (ast::expr_ret(_,?a)) { ret a; }
16621662
case (ast::expr_put(_,?a)) { ret a; }
16631663
case (ast::expr_be(_,?a)) { ret a; }

src/comp/middle/typeck.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1276,7 +1276,7 @@ mod pushdown {
12761276
write::ty_only_fixup(fcx, ann.id, t);
12771277
}
12781278
/* FIXME: should this check the type annotations? */
1279-
case (ast::expr_fail(_)) { /* no-op */ }
1279+
case (ast::expr_fail(_,_)) { /* no-op */ }
12801280
case (ast::expr_log(_,_,_)) { /* no-op */ }
12811281
case (ast::expr_break(_)) { /* no-op */ }
12821282
case (ast::expr_cont(_)) { /* no-op */ }
@@ -1972,7 +1972,7 @@ fn check_expr(&@fn_ctxt fcx, &@ast::expr expr) {
19721972
write::ty_only_fixup(fcx, a.id, t);
19731973
}
19741974

1975-
case (ast::expr_fail(?a)) {
1975+
case (ast::expr_fail(?a, _)) {
19761976
write::bot_ty(fcx.ccx.tcx, a.id);
19771977
}
19781978

src/comp/middle/walk.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,7 @@ fn walk_expr(&ast_visitor v, @ast::expr e) {
431431
// Only walk expansion, not args/body.
432432
walk_expr(v, expansion);
433433
}
434-
case (ast::expr_fail(_)) { }
434+
case (ast::expr_fail(_, _)) { }
435435
case (ast::expr_break(_)) { }
436436
case (ast::expr_cont(_)) { }
437437
case (ast::expr_ret(?eo, _)) {

src/comp/pretty/pprust.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -825,8 +825,13 @@ fn print_expr(&ps s, &@ast::expr expr) {
825825
case (ast::expr_path(?path,_)) {
826826
print_path(s, path);
827827
}
828-
case (ast::expr_fail(_)) {
828+
case (ast::expr_fail(_, ?str)) {
829829
word(s.s, "fail");
830+
alt (str) {
831+
case (some(?msg)) {
832+
word(s.s, #fmt("\"%s\"", msg));
833+
}
834+
}
830835
}
831836
case (ast::expr_break(_)) {
832837
word(s.s, "break");
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// error-pattern:woooo
2+
3+
fn main() {
4+
fail "woooo";
5+
}

0 commit comments

Comments
 (0)