aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorBrian Anderson <[email protected]>2011-03-27 20:20:12 -0400
committerGraydon Hoare <[email protected]>2011-03-28 21:14:08 -0700
commit8f2a97a562916a882ec0818e6295d41971aad5f1 (patch)
treeffe8982b5f86bea44766186e68dbcca0a1482f99 /src
parentAdd an XFAILed test for if expressions resulting in boxes (diff)
downloadrust-8f2a97a562916a882ec0818e6295d41971aad5f1.tar.xz
rust-8f2a97a562916a882ec0818e6295d41971aad5f1.zip
Add partial support for if expressions resulting in generic types
Diffstat (limited to 'src')
-rw-r--r--src/comp/middle/trans.rs11
-rw-r--r--src/test/run-pass/expr-if-generic.rs53
2 files changed, 60 insertions, 4 deletions
diff --git a/src/comp/middle/trans.rs b/src/comp/middle/trans.rs
index 433a8012..3995e1de 100644
--- a/src/comp/middle/trans.rs
+++ b/src/comp/middle/trans.rs
@@ -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 (_) {
diff --git a/src/test/run-pass/expr-if-generic.rs b/src/test/run-pass/expr-if-generic.rs
new file mode 100644
index 00000000..f23ce23a
--- /dev/null
+++ b/src/test/run-pass/expr-if-generic.rs
@@ -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();
+} \ No newline at end of file