aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2025-05-03 14:28:19 -0400
committerBruce Hill <bruce@bruce-hill.com>2025-05-03 14:28:19 -0400
commit0df984c4b2ec7cdc683b452ffea068c220470405 (patch)
tree1f81ce8b2028c4e8ad789bab65b0b7f03c02b62b
parent51a5bf63a2a9dd73e5b20b0b9aab2f87c6400eb0 (diff)
Bugfix for promotions in comparisons
-rw-r--r--src/compile.c24
-rw-r--r--src/typecheck.c8
-rw-r--r--src/types.c4
3 files changed, 15 insertions, 21 deletions
diff --git a/src/compile.c b/src/compile.c
index 0eab15bc..8494766a 100644
--- a/src/compile.c
+++ b/src/compile.c
@@ -2805,26 +2805,16 @@ CORD compile(env_t *env, ast_t *ast)
type_t *lhs_t = get_type(env, binop.lhs);
type_t *rhs_t = get_type(env, binop.rhs);
type_t *operand_t;
- if (is_numeric_type(lhs_t) && binop.rhs->tag == Int) {
+ if (binop.lhs->tag == Int && is_numeric_type(rhs_t)) {
+ operand_t = rhs_t;
+ } else if (binop.rhs->tag == Int && is_numeric_type(lhs_t)) {
+ operand_t = lhs_t;
+ } else if (can_compile_to_type(env, binop.rhs, lhs_t)) {
operand_t = lhs_t;
- } else if (is_numeric_type(rhs_t) && binop.lhs->tag == Int) {
+ } else if (can_compile_to_type(env, binop.lhs, rhs_t)) {
operand_t = rhs_t;
} else {
- switch (compare_precision(lhs_t, rhs_t)) {
- case NUM_PRECISION_LESS: operand_t = rhs_t; break;
- case NUM_PRECISION_MORE: operand_t = lhs_t; break;
- case NUM_PRECISION_EQUAL: operand_t = lhs_t; break;
- default: {
- if (can_compile_to_type(env, binop.rhs, lhs_t)) {
- operand_t = lhs_t;
- } else if (can_compile_to_type(env, binop.lhs, rhs_t)) {
- operand_t = rhs_t;
- } else {
- code_err(ast, "I can't do comparisons between ", type_to_str(lhs_t), " and ", type_to_str(rhs_t));
- }
- break;
- }
- }
+ code_err(ast, "I can't do comparisons between ", type_to_str(lhs_t), " and ", type_to_str(rhs_t));
}
CORD lhs, rhs;
diff --git a/src/typecheck.c b/src/typecheck.c
index 04c6897f..f0326913 100644
--- a/src/typecheck.c
+++ b/src/typecheck.c
@@ -1689,6 +1689,10 @@ PUREFUNC bool can_compile_to_type(env_t *env, ast_t *ast, type_t *needed)
if (needed->tag == OptionalType && ast->tag == None)
return true;
+ type_t *actual = get_type(env, ast);
+ if (actual->tag == OptionalType && needed->tag == OptionalType)
+ return can_promote(actual, needed);
+
needed = non_optional(needed);
if (needed->tag == ListType && ast->tag == List) {
type_t *item_type = Match(needed, ListType)->item_type;
@@ -1722,9 +1726,9 @@ PUREFUNC bool can_compile_to_type(env_t *env, ast_t *ast, type_t *needed)
else if (ast->tag == StackReference)
return ptr->is_stack && can_compile_to_type(env, Match(ast, StackReference)->value, ptr->pointed);
else
- return can_promote(needed, get_type(env, ast));
+ return can_promote(actual, needed);
} else {
- return can_promote(needed, get_type(env, ast));
+ return can_promote(actual, needed);
}
}
diff --git a/src/types.c b/src/types.c
index e1ada1f8..91848bef 100644
--- a/src/types.c
+++ b/src/types.c
@@ -384,10 +384,10 @@ PUREFUNC bool can_promote(type_t *actual, type_t *needed)
return true;
}
- if (needed->tag == ClosureType && actual->tag == FunctionType)
+ if (actual->tag == FunctionType && needed->tag == ClosureType)
return can_promote(actual, Match(needed, ClosureType)->fn);
- if (needed->tag == ClosureType && actual->tag == ClosureType)
+ if (actual->tag == ClosureType && needed->tag == ClosureType)
return can_promote(Match(actual, ClosureType)->fn, Match(needed, ClosureType)->fn);
if (actual->tag == FunctionType && needed->tag == FunctionType) {