Skip to content

Commit 6824f11

Browse files
committed
rustc: Allow alt expressions to fail
1 parent 418b4c4 commit 6824f11

File tree

3 files changed

+52
-3
lines changed

3 files changed

+52
-3
lines changed

src/comp/middle/typeck.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1457,7 +1457,10 @@ mod Pushdown {
14571457
for (ast::arm arm_0 in arms_0) {
14581458
pushdown_block(scx, expected, arm_0.block);
14591459
auto bty = block_ty(scx.fcx.ccx.tcx, arm_0.block);
1460-
t = Demand::simple(scx, e.span, t, bty);
1460+
// Failing alt arms don't need to have a matching type
1461+
if (!ty::type_is_bot(scx.fcx.ccx.tcx, bty)) {
1462+
t = Demand::simple(scx, e.span, t, bty);
1463+
}
14611464
}
14621465
write::ty_only_fixup(scx, ann.id, t);
14631466
}
@@ -2209,8 +2212,11 @@ fn check_expr(&@stmt_ctxt scx, &@ast::expr expr) {
22092212
check_block(scx, arm.block);
22102213

22112214
auto bty = block_ty(scx.fcx.ccx.tcx, arm.block);
2212-
result_ty = Demand::simple(scx, arm.block.span, result_ty,
2213-
bty);
2215+
// Failing alt arms don't need to have a matching type
2216+
if (!ty::type_is_bot(scx.fcx.ccx.tcx, bty)) {
2217+
result_ty = Demand::simple(scx, arm.block.span,
2218+
result_ty, bty);
2219+
}
22142220
}
22152221

22162222
auto i = 0u;

src/test/run-fail/expr-alt-fail.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// xfail-stage0
2+
// error-pattern:explicit failure
3+
4+
fn main() {
5+
auto x = alt(true) {
6+
case (false) {
7+
0
8+
}
9+
case (true) {
10+
fail
11+
}
12+
};
13+
}

src/test/run-pass/expr-alt-fail.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// xfail-stage0
2+
3+
fn test_simple() {
4+
auto r = alt (true) {
5+
case (true) {
6+
true
7+
}
8+
case (false) {
9+
fail
10+
}
11+
};
12+
assert (r == true);
13+
}
14+
15+
fn test_box() {
16+
auto r = alt (true) {
17+
case (true) {
18+
[10]
19+
}
20+
case (false) {
21+
fail
22+
}
23+
};
24+
assert (r.(0) == 10);
25+
}
26+
27+
fn main() {
28+
test_simple();
29+
test_box();
30+
}

0 commit comments

Comments
 (0)