Skip to content

Commit aced4ce

Browse files
brsongraydon
authored andcommitted
Handle structural types as the result of if expressions
1 parent a5a319f commit aced4ce

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

src/comp/middle/trans.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3049,6 +3049,9 @@ fn trans_if(@block_ctxt cx, @ast.expr cond,
30493049
// FIXME: Handle dynamic type sizes
30503050
auto expr_ty = ty.expr_ty(elexpr);
30513051
expr_llty = type_of(else_res.bcx.fcx.ccx, expr_ty);
3052+
if (ty.type_is_structural(expr_ty)) {
3053+
expr_llty = T_ptr(expr_llty);
3054+
}
30523055
}
30533056
case (_) {
30543057
else_res = res(else_cx, C_nil());

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// xfail-boot
2+
// -*- rust -*-
3+
4+
// Tests for if as expressions returning structural types
5+
6+
fn test_rec() {
7+
auto res = if (true) { rec(i = 100) } else { rec(i = 101) };
8+
check (res == rec(i = 100));
9+
}
10+
11+
fn test_tag() {
12+
tag mood {
13+
happy;
14+
sad;
15+
}
16+
17+
auto res = if (true) { happy } else { sad };
18+
check (res == happy);
19+
}
20+
21+
fn main() {
22+
test_rec();
23+
test_tag();
24+
}

0 commit comments

Comments
 (0)