Add partial support for if expressions resulting in generic types

This commit is contained in:
Brian Anderson 2011-03-27 20:20:12 -04:00 committed by Graydon Hoare
parent 38d5453584
commit 8f2a97a562
2 changed files with 60 additions and 4 deletions

View File

@ -3046,11 +3046,14 @@ fn trans_if(@block_ctxt cx, @ast.expr cond,
// If we have an else expression, then the entire
// if expression can have a non-nil type.
// FIXME: Handle dynamic type sizes
auto expr_ty = ty.expr_ty(elexpr);
expr_llty = type_of(else_res.bcx.fcx.ccx, expr_ty);
if (ty.type_is_structural(expr_ty)) {
expr_llty = T_ptr(expr_llty);
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 (_) {

View File

@ -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();
}