Skip to content

Commit a2775a5

Browse files
jdmbrson
authored andcommitted
Make non-str fail expression a type checking failure instead of a translation one.
1 parent b110bbf commit a2775a5

File tree

3 files changed

+24
-4
lines changed

3 files changed

+24
-4
lines changed

src/comp/middle/trans.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6388,9 +6388,9 @@ fn trans_fail_expr(&@block_ctxt cx, &option::t[common::span] sp_opt,
63886388
[C_int(0), C_int(abi::vec_elt_data)]);
63896389
ret trans_fail_value(bcx, sp_opt, elt);
63906390
} else {
6391-
bcx.fcx.lcx.ccx.sess.span_fatal(expr.span,
6392-
"fail called with unsupported \
6393-
type " + ty_to_str(tcx, e_ty));
6391+
cx.fcx.lcx.ccx.sess.span_bug(expr.span,
6392+
"fail called with unsupported \
6393+
type " + ty_to_str(tcx, e_ty));
63946394
}
63956395
}
63966396
case (_) {

src/comp/middle/typeck.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,11 @@ fn type_is_scalar(&@fn_ctxt fcx, &span sp, ty::t typ) -> bool {
229229
ret ty::type_is_scalar(fcx.ccx.tcx, typ_s);
230230
}
231231

232+
fn type_is_str(&@fn_ctxt fcx, &span sp, ty::t typ) -> bool {
233+
auto typ_s = structurally_resolved_type(fcx, sp, typ);
234+
ret ty::type_is_str(fcx.ccx.tcx, typ_s);
235+
}
236+
232237

233238
// Parses the programmer's textual representation of a type into our internal
234239
// notion of a type. `getter` is a function that returns the type
@@ -1658,7 +1663,17 @@ fn check_expr(&@fn_ctxt fcx, &@ast::expr expr) {
16581663
case (ast::expr_fail(?expr_opt)) {
16591664
alt (expr_opt) {
16601665
case (none) { /* do nothing */ }
1661-
case (some(?e)) { check_expr(fcx, e); }
1666+
case (some(?e)) {
1667+
check_expr(fcx, e);
1668+
auto tcx = fcx.ccx.tcx;
1669+
auto ety = expr_ty(tcx, e);
1670+
if (!type_is_str(fcx, e.span, ety)) {
1671+
tcx.sess.span_fatal(e.span,
1672+
#fmt("mismatched types: expected \
1673+
str, found %s",
1674+
ty_to_str(tcx, ety)));
1675+
}
1676+
}
16621677
}
16631678
write::bot_ty(fcx.ccx.tcx, id);
16641679
}

src/test/compile-fail/fail-expr.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// error-pattern:mismatched types
2+
3+
fn main() {
4+
fail 5;
5+
}

0 commit comments

Comments
 (0)