commit 2adf5ca288595d9fdd6111ff90293d362b0d92ac
parent a73765fe2e9b3497637af42b5fcfa73083557d2f
Author: Eyal Sawady <ecs@d2evs.net>
Date: Fri, 5 Feb 2021 13:24:31 -0500
check: use type hint in if statements
Also simplify malloc and segmalloc based on these changes
Diffstat:
3 files changed, 8 insertions(+), 8 deletions(-)
diff --git a/rt/+linux/segmalloc.ha b/rt/+linux/segmalloc.ha
@@ -3,10 +3,7 @@ fn segmalloc(n: size) nullable *void = {
let p: *void = mmap(null, n,
PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANON, -1, 0z);
- // TODO: remove the cast to nullable *void
- return
- if (p: uintptr: int == -ENOMEM) null: nullable *void
- else p: nullable *void;
+ return if (p: uintptr: int == -ENOMEM) null else p;
};
// Frees a segment allocated with segmalloc.
diff --git a/rt/malloc.ha b/rt/malloc.ha
@@ -83,15 +83,15 @@ fn malloc_small(n: size) nullable *void = {
return if (p != null: nullable *void) {
let q = *(p: **void);
bins[b] = q;
- p: nullable *void;
- } else null: nullable *void;
+ p;
+ } else null;
};
fn fill_bin(b: size) nullable *void = {
const s = bin2size(b);
let p = segmalloc(BIGBLOCK);
- return if (p == null: nullable *void) null: nullable *void
- else list_from_block(s, p: uintptr);
+ return if (p == null: nullable *void) null
+ else list_from_block(s, p: uintptr);
};
fn list_from_block(s: size, p: uintptr) nullable *void = {
diff --git a/src/check.c b/src/check.c
@@ -941,6 +941,9 @@ check_expr_if(struct context *ctx,
expr->result = false_branch->result;
} else if (false_branch->terminates) {
expr->result = true_branch->result;
+ } else if (hint && type_is_assignable(hint, true_branch->result)
+ && type_is_assignable(hint, false_branch->result)) {
+ expr->result = hint;
} else if (true_branch->result == false_branch->result) {
expr->result = true_branch->result;
} else {