From a37ec9b2ee7d3d3decd74398639217796462dc7b Mon Sep 17 00:00:00 2001 From: Josh Matthews Date: Tue, 14 Jun 2011 23:26:08 -0400 Subject: [PATCH 1/4] Test. --- src/test/compile-fail/crazy-type-error.rs | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 src/test/compile-fail/crazy-type-error.rs diff --git a/src/test/compile-fail/crazy-type-error.rs b/src/test/compile-fail/crazy-type-error.rs new file mode 100644 index 0000000000000..2712a636e1081 --- /dev/null +++ b/src/test/compile-fail/crazy-type-error.rs @@ -0,0 +1,9 @@ +// error-pattern: mismatched types + +tag t { a; } + +fn f(int a) {} + +fn main() { + f(a); +} \ No newline at end of file From e4011525aca5694e3ea7aef3d2f6c1c071f01f40 Mon Sep 17 00:00:00 2001 From: Josh Matthews Date: Fri, 1 Jul 2011 14:33:15 -0400 Subject: [PATCH 2/4] Allow any string expression to be used with fail. --- src/comp/front/ast.rs | 2 +- src/comp/front/parser.rs | 11 +++-- src/comp/middle/trans.rs | 46 +++++++++++++++---- src/comp/middle/tstate/pre_post_conditions.rs | 12 ++++- src/comp/middle/tstate/states.rs | 10 +++- src/comp/middle/typeck.rs | 8 +++- src/comp/middle/walk.rs | 2 +- src/comp/pretty/pprust.rs | 6 +-- src/test/run-fail/explicit-fail-msg.rs | 11 +++-- 9 files changed, 82 insertions(+), 26 deletions(-) diff --git a/src/comp/front/ast.rs b/src/comp/front/ast.rs index c120535d1d014..428ba6a0e8326 100644 --- a/src/comp/front/ast.rs +++ b/src/comp/front/ast.rs @@ -281,7 +281,7 @@ tag expr_ { expr_index(@expr, @expr); expr_path(path); expr_ext(path, vec[@expr], option::t[str], @expr); - expr_fail(option::t[str]); + expr_fail(option::t[@expr]); expr_break; expr_cont; expr_ret(option::t[@expr]); diff --git a/src/comp/front/parser.rs b/src/comp/front/parser.rs index a3d20d23b6b32..20363d2fa2ee3 100644 --- a/src/comp/front/parser.rs +++ b/src/comp/front/parser.rs @@ -855,12 +855,15 @@ fn parse_bottom_expr(&parser p) -> @ast::expr { lo = ex_ext.span.lo; ex = ex_ext.node; } else if (eat_word(p, "fail")) { - auto msg; alt (p.peek()) { - case (token::LIT_STR(?s)) { msg = some(p.get_str(s)); p.bump(); } - case (_) { msg = none; } + case (token::SEMI) { ex = ast::expr_fail(none) } + case (token::RBRACE) { ex = ast::expr_fail(none) } + case (_) { + auto e = parse_expr(p); + hi = e.span.hi; + ex = ast::expr_fail(some(e)); + } } - ex = ast::expr_fail(msg); } else if (eat_word(p, "log")) { auto e = parse_expr(p); ex = ast::expr_log(1, e); diff --git a/src/comp/middle/trans.rs b/src/comp/middle/trans.rs index cb20eb1f22460..1ac0237bf5689 100644 --- a/src/comp/middle/trans.rs +++ b/src/comp/middle/trans.rs @@ -6086,13 +6086,8 @@ fn trans_expr_out(&@block_ctxt cx, &@ast::expr e, out_method output) -> case (ast::expr_ext(_, _, _, ?expanded)) { ret trans_expr(cx, expanded); } - 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_fail(?expr)) { + ret trans_fail_expr(cx, some(e.span), expr); } case (ast::expr_log(?lvl, ?a)) { ret trans_log(lvl, cx, a); } case (ast::expr_assert(?a)) { @@ -6295,9 +6290,42 @@ fn trans_check_expr(&@block_ctxt cx, &@ast::expr e, &str s) -> result { ret rslt(next_cx, C_nil()); } +fn trans_fail_expr(&@block_ctxt cx, &option::t[common::span] sp_opt, + &option::t[@ast::expr] fail_expr) + -> result { + alt (fail_expr) { + case (some(?expr)) { + //auto log_cx = new_scope_block_ctxt(cx, "log"); + + auto tcx = cx.fcx.lcx.ccx.tcx; + auto expr_res = trans_expr(cx, expr); + auto e_ty = ty::expr_ty(tcx, expr); + + if (ty::type_is_str(tcx, e_ty)) { + auto elt = cx.build.GEP(expr_res.val, + [C_int(0), C_int(abi::vec_elt_data)]); + ret trans_fail_value(cx, sp_opt, elt); + } else { + cx.fcx.lcx.ccx.sess.span_fatal(expr.span, + "fail called with unsupported \ + type " + ty_to_str(tcx, e_ty)); + } + } + case (_) { + ret trans_fail(cx, sp_opt, "explicit failure"); + } + } +} + fn trans_fail(&@block_ctxt cx, &option::t[common::span] sp_opt, &str fail_str) -> result { auto V_fail_str = C_cstr(cx.fcx.lcx.ccx, fail_str); + ret trans_fail_value(cx, sp_opt, V_fail_str); +} + +fn trans_fail_value(&@block_ctxt cx, &option::t[common::span] sp_opt, + &ValueRef V_fail_str) + -> result { auto V_filename; auto V_line; alt (sp_opt) { @@ -6311,9 +6339,9 @@ fn trans_fail(&@block_ctxt cx, &option::t[common::span] sp_opt, &str fail_str) V_line = 0; } } - V_fail_str = cx.build.PointerCast(V_fail_str, T_ptr(T_i8())); + auto V_str = cx.build.PointerCast(V_fail_str, T_ptr(T_i8())); V_filename = cx.build.PointerCast(V_filename, T_ptr(T_i8())); - auto args = [cx.fcx.lltaskptr, V_fail_str, V_filename, C_int(V_line)]; + auto args = [cx.fcx.lltaskptr, V_str, V_filename, C_int(V_line)]; cx.build.Call(cx.fcx.lcx.ccx.upcalls._fail, args); cx.build.Unreachable(); ret rslt(cx, C_nil()); diff --git a/src/comp/middle/tstate/pre_post_conditions.rs b/src/comp/middle/tstate/pre_post_conditions.rs index ee056f8ddb63f..25d6fb98a92b4 100644 --- a/src/comp/middle/tstate/pre_post_conditions.rs +++ b/src/comp/middle/tstate/pre_post_conditions.rs @@ -529,11 +529,19 @@ fn find_pre_post_expr(&fn_ctxt fcx, @expr e) { find_pre_post_expr(fcx, operator); copy_pre_post(fcx.ccx, e.id, operator); } - case (expr_fail(_)) { + case (expr_fail(?maybe_val)) { + auto prestate; + alt (maybe_val) { + case (none) { prestate = empty_prestate(num_local_vars); } + case (some(?fail_val)) { + find_pre_post_expr(fcx, fail_val); + prestate = expr_precond(fcx.ccx, fail_val); + } + } set_pre_and_post(fcx.ccx, e.id, /* if execution continues after fail, then everything is true! */ - empty_prestate(num_local_vars), + prestate, false_postcond(num_local_vars)); } case (expr_assert(?p)) { diff --git a/src/comp/middle/tstate/states.rs b/src/comp/middle/tstate/states.rs index dc64f64e6dfdc..4e4c3a961d7d5 100644 --- a/src/comp/middle/tstate/states.rs +++ b/src/comp/middle/tstate/states.rs @@ -543,12 +543,18 @@ fn find_pre_post_state_expr(&fn_ctxt fcx, &prestate pres, @expr e) -> bool { case (expr_cast(?operand, _)) { ret find_pre_post_state_sub(fcx, pres, operand, e.id, none); } - case (expr_fail(_)) { + case (expr_fail(?maybe_fail_val)) { ret set_prestate_ann(fcx.ccx, e.id, pres) | /* if execution continues after fail, then everything is true! woo! */ set_poststate_ann(fcx.ccx, e.id, - false_postcond(num_constrs)); + false_postcond(num_constrs)) | + alt(maybe_fail_val) { + case (none) { false } + case (some(?fail_val)) { + find_pre_post_state_expr(fcx, pres, fail_val) + } + } } case (expr_assert(?p)) { ret find_pre_post_state_sub(fcx, pres, p, e.id, none); diff --git a/src/comp/middle/typeck.rs b/src/comp/middle/typeck.rs index 524c4262cf2a0..e0d7e1df7eac5 100644 --- a/src/comp/middle/typeck.rs +++ b/src/comp/middle/typeck.rs @@ -1575,7 +1575,13 @@ fn check_expr(&@fn_ctxt fcx, &@ast::expr expr) { auto t = expr_ty(fcx.ccx.tcx, expanded); write::ty_only_fixup(fcx, id, t); } - case (ast::expr_fail(_)) { write::bot_ty(fcx.ccx.tcx, id); } + case (ast::expr_fail(?expr_opt)) { + alt (expr_opt) { + case (none) { /* do nothing */ } + case (some(?e)) { check_expr(fcx, e); } + } + write::bot_ty(fcx.ccx.tcx, id); + } case (ast::expr_break) { write::bot_ty(fcx.ccx.tcx, id); } case (ast::expr_cont) { write::bot_ty(fcx.ccx.tcx, id); } case (ast::expr_ret(?expr_opt)) { diff --git a/src/comp/middle/walk.rs b/src/comp/middle/walk.rs index 8821db4914ac9..2224c8f475ccb 100644 --- a/src/comp/middle/walk.rs +++ b/src/comp/middle/walk.rs @@ -375,7 +375,7 @@ fn walk_expr(&ast_visitor v, @ast::expr e) { walk_expr(v, expansion); } - case (ast::expr_fail(_)) { } + case (ast::expr_fail(?eo)) { walk_expr_opt(v, eo); } case (ast::expr_break) { } case (ast::expr_cont) { } case (ast::expr_ret(?eo)) { walk_expr_opt(v, eo); } diff --git a/src/comp/pretty/pprust.rs b/src/comp/pretty/pprust.rs index 433a7234dd421..f275e7758c2b5 100644 --- a/src/comp/pretty/pprust.rs +++ b/src/comp/pretty/pprust.rs @@ -825,10 +825,10 @@ fn print_expr(&ps s, &@ast::expr expr) { pclose(s); } case (ast::expr_path(?path)) { print_path(s, path); } - case (ast::expr_fail(?str)) { + case (ast::expr_fail(?maybe_fail_val)) { word(s.s, "fail"); - alt (str) { - case (some(?msg)) { word(s.s, #fmt("\"%s\"", msg)); } + alt (maybe_fail_val) { + case (some(?expr)) { word(s.s, " "); print_expr(s, expr); } case (_) { } } } diff --git a/src/test/run-fail/explicit-fail-msg.rs b/src/test/run-fail/explicit-fail-msg.rs index 123b1087d9b6e..793260210b088 100644 --- a/src/test/run-fail/explicit-fail-msg.rs +++ b/src/test/run-fail/explicit-fail-msg.rs @@ -1,5 +1,10 @@ - -// error-pattern:woooo -fn main() { fail"woooo"; } \ No newline at end of file +// error-pattern:wooooo +fn main() { + auto a = 1; + if (1 == 1) { + a = 2; + } + fail "woooo" + "o"; +} \ No newline at end of file From 5d0a8e42d51ad3a8f313b13c6c89a31a65e96b04 Mon Sep 17 00:00:00 2001 From: Josh Matthews Date: Sat, 2 Jul 2011 00:37:17 -0400 Subject: [PATCH 3/4] Revert "Test." This reverts commit a37ec9b2ee7d3d3decd74398639217796462dc7b. --- src/test/compile-fail/crazy-type-error.rs | 9 --------- 1 file changed, 9 deletions(-) delete mode 100644 src/test/compile-fail/crazy-type-error.rs diff --git a/src/test/compile-fail/crazy-type-error.rs b/src/test/compile-fail/crazy-type-error.rs deleted file mode 100644 index 2712a636e1081..0000000000000 --- a/src/test/compile-fail/crazy-type-error.rs +++ /dev/null @@ -1,9 +0,0 @@ -// error-pattern: mismatched types - -tag t { a; } - -fn f(int a) {} - -fn main() { - f(a); -} \ No newline at end of file From 76b2f51cf9a09ead3eef5d5e14542a00563d442e Mon Sep 17 00:00:00 2001 From: Josh Matthews Date: Sat, 2 Jul 2011 01:04:58 -0400 Subject: [PATCH 4/4] Remove unneeded old code remnant. --- src/comp/middle/trans.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/comp/middle/trans.rs b/src/comp/middle/trans.rs index 1ac0237bf5689..0464fc6ae7beb 100644 --- a/src/comp/middle/trans.rs +++ b/src/comp/middle/trans.rs @@ -6295,8 +6295,6 @@ fn trans_fail_expr(&@block_ctxt cx, &option::t[common::span] sp_opt, -> result { alt (fail_expr) { case (some(?expr)) { - //auto log_cx = new_scope_block_ctxt(cx, "log"); - auto tcx = cx.fcx.lcx.ccx.tcx; auto expr_res = trans_expr(cx, expr); auto e_ty = ty::expr_ty(tcx, expr);