Skip to content

Commit 9929f32

Browse files
brsongraydon
authored andcommitted
---
yaml --- r: 1902 b: refs/heads/master c: a5a319f h: refs/heads/master v: v3
1 parent aee1013 commit 9929f32

File tree

3 files changed

+112
-5
lines changed

3 files changed

+112
-5
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
---
2-
refs/heads/master: 2b27d12ce1b3babf4adea41d669140019cb80b8a
2+
refs/heads/master: a5a319fa047b04b525066d560115ad93a18e4a4f

trunk/src/comp/middle/trans.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3037,21 +3037,30 @@ fn trans_if(@block_ctxt cx, @ast.expr cond,
30373037
auto then_res = trans_block(then_cx, thn);
30383038

30393039
auto else_cx = new_scope_block_ctxt(cx, "else");
3040-
auto else_res = res(else_cx, C_nil());
30413040

3041+
auto else_res;
3042+
auto expr_llty;
30423043
alt (els) {
30433044
case (some[@ast.expr](?elexpr)) {
30443045
else_res = trans_expr(else_cx, elexpr);
3046+
3047+
// If we have an else expression, then the entire
3048+
// if expression can have a non-nil type.
3049+
// FIXME: Handle dynamic type sizes
3050+
auto expr_ty = ty.expr_ty(elexpr);
3051+
expr_llty = type_of(else_res.bcx.fcx.ccx, expr_ty);
3052+
}
3053+
case (_) {
3054+
else_res = res(else_cx, C_nil());
3055+
expr_llty = T_nil();
30453056
}
3046-
case (_) { /* fall through */ }
30473057
}
30483058

30493059
cond_res.bcx.build.CondBr(cond_res.val,
30503060
then_cx.llbb,
30513061
else_cx.llbb);
30523062

3053-
// FIXME: use inferred type when available.
3054-
ret join_results(cx, T_nil(),
3063+
ret join_results(cx, expr_llty,
30553064
vec(then_res, else_res));
30563065
}
30573066

trunk/src/test/run-pass/expr-if.rs

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
// xfail-boot
2+
// -*- rust -*-
3+
4+
// Tests for if as expressions
5+
6+
fn test_if() {
7+
let bool res = if (true) { true } else { false };
8+
check (res);
9+
}
10+
11+
fn test_else() {
12+
let bool res = if (false) { false } else { true };
13+
check (res);
14+
}
15+
16+
fn test_elseif1() {
17+
let bool res = if (true) {
18+
true
19+
} else if (true) {
20+
false
21+
} else {
22+
false
23+
};
24+
check (res);
25+
}
26+
27+
fn test_elseif2() {
28+
let bool res = if (false) {
29+
false
30+
} else if (true) {
31+
true
32+
} else {
33+
false
34+
};
35+
check (res);
36+
}
37+
38+
fn test_elseif3() {
39+
let bool res = if (false) {
40+
false
41+
} else if (false) {
42+
false
43+
} else {
44+
true
45+
};
46+
check (res);
47+
}
48+
49+
fn test_inferrence() {
50+
auto res = if (true) { true } else { false };
51+
check (res);
52+
}
53+
54+
fn test_if_as_if_condition() {
55+
auto res1 = if (if (false) { false } else { true }) {
56+
true
57+
} else {
58+
false
59+
};
60+
check (res1);
61+
62+
auto res2 = if (if (true) { false } else { true }) {
63+
false
64+
} else {
65+
true
66+
};
67+
check (res2);
68+
}
69+
70+
fn test_if_as_block_result() {
71+
auto res = if (true) {
72+
if (false) {
73+
false
74+
} else {
75+
true
76+
}
77+
} else {
78+
false
79+
};
80+
check (res);
81+
}
82+
83+
fn test_str() {
84+
auto res = if (true) { "happy" } else { "sad" };
85+
check (res == "happy");
86+
}
87+
88+
fn main() {
89+
test_if();
90+
test_else();
91+
test_elseif1();
92+
test_elseif2();
93+
test_elseif3();
94+
test_inferrence();
95+
test_if_as_if_condition();
96+
test_if_as_block_result();
97+
test_str();
98+
}

0 commit comments

Comments
 (0)