Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

if expressions #297

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 19 additions & 4 deletions src/comp/middle/trans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3021,21 +3021,36 @@ fn trans_if(@block_ctxt cx, @ast.expr cond,
auto then_res = trans_block(then_cx, thn);

auto else_cx = new_scope_block_ctxt(cx, "else");
auto else_res = res(else_cx, C_nil());

auto else_res;
auto expr_llty;
alt (els) {
case (some[@ast.expr](?elexpr)) {
else_res = trans_expr(else_cx, elexpr);

// If we have an else expression, then the entire
// if expression can have a non-nil type.
auto expr_ty = ty.expr_ty(elexpr);
if (ty.type_has_dynamic_size(expr_ty)) {
expr_llty = T_typaram_ptr(cx.fcx.ccx.tn);
} else {
expr_llty = type_of(else_res.bcx.fcx.ccx, expr_ty);
if (ty.type_is_structural(expr_ty)) {
expr_llty = T_ptr(expr_llty);
}
}
}
case (_) {
else_res = res(else_cx, C_nil());
expr_llty = T_nil();
}
case (_) { /* fall through */ }
}

cond_res.bcx.build.CondBr(cond_res.val,
then_cx.llbb,
else_cx.llbb);

// FIXME: use inferred type when available.
ret join_results(cx, T_nil(),
ret join_results(cx, expr_llty,
vec(then_res, else_res));
}

Expand Down
14 changes: 14 additions & 0 deletions src/test/run-pass/expr-if-box.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// xfail-boot
// xfail-stage0
// -*- rust -*-

// Tests for if as expressions returning boxed types

fn test_box() {
auto res = if (true) { @100 } else { @101 };
check (*res == 100);
}

fn main() {
test_box();
}
53 changes: 53 additions & 0 deletions src/test/run-pass/expr-if-generic.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// xfail-boot
// xfail-stage0
// -*- rust -*-

// Tests for if as expressions with dynamic type sizes

type compare[T] = fn(&T t1, &T t2) -> bool;

fn test_generic[T](&T expected, &T not_expected, &compare[T] eq) {
let T actual = if (true) { expected } else { not_expected };
check (eq(expected, actual));
}

fn test_bool() {
fn compare_bool(&bool b1, &bool b2) -> bool {
ret b1 == b2;
}
auto eq = bind compare_bool(_, _);
test_generic[bool](true, false, eq);
}

fn test_tup() {
type t = tup(int, int);
fn compare_tup(&t t1, &t t2) -> bool {
ret t1 == t2;
}
auto eq = bind compare_tup(_, _);
test_generic[t](tup(1, 2), tup(2, 3), eq);
}

fn test_vec() {
fn compare_vec(&vec[int] v1, &vec[int] v2) -> bool {
ret v1 == v2;
}
auto eq = bind compare_vec(_, _);
test_generic[vec[int]](vec(1, 2), vec(2, 3), eq);
}

fn test_box() {
fn compare_box(&@bool b1, &@bool b2) -> bool {
ret *b1 == *b2;
}
auto eq = bind compare_box(_, _);
test_generic[@bool](@true, @false, eq);
}

fn main() {
test_bool();
test_tup();
// FIXME: These two don't pass yet
test_vec();
test_box();
}
24 changes: 24 additions & 0 deletions src/test/run-pass/expr-if-struct.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// xfail-boot
// -*- rust -*-

// Tests for if as expressions returning structural types

fn test_rec() {
auto res = if (true) { rec(i = 100) } else { rec(i = 101) };
check (res == rec(i = 100));
}

fn test_tag() {
tag mood {
happy;
sad;
}

auto res = if (true) { happy } else { sad };
check (res == happy);
}

fn main() {
test_rec();
test_tag();
}
98 changes: 98 additions & 0 deletions src/test/run-pass/expr-if.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
// xfail-boot
// -*- rust -*-

// Tests for if as expressions

fn test_if() {
let bool res = if (true) { true } else { false };
check (res);
}

fn test_else() {
let bool res = if (false) { false } else { true };
check (res);
}

fn test_elseif1() {
let bool res = if (true) {
true
} else if (true) {
false
} else {
false
};
check (res);
}

fn test_elseif2() {
let bool res = if (false) {
false
} else if (true) {
true
} else {
false
};
check (res);
}

fn test_elseif3() {
let bool res = if (false) {
false
} else if (false) {
false
} else {
true
};
check (res);
}

fn test_inferrence() {
auto res = if (true) { true } else { false };
check (res);
}

fn test_if_as_if_condition() {
auto res1 = if (if (false) { false } else { true }) {
true
} else {
false
};
check (res1);

auto res2 = if (if (true) { false } else { true }) {
false
} else {
true
};
check (res2);
}

fn test_if_as_block_result() {
auto res = if (true) {
if (false) {
false
} else {
true
}
} else {
false
};
check (res);
}

fn test_str() {
auto res = if (true) { "happy" } else { "sad" };
check (res == "happy");
}

fn main() {
test_if();
test_else();
test_elseif1();
test_elseif2();
test_elseif3();
test_inferrence();
test_if_as_if_condition();
test_if_as_block_result();
test_str();
}