Add cstring specific comparison procedures to fix comparisons like cstring("") != cstring(nil)

This commit is contained in:
gingerBill
2023-09-26 12:21:43 +01:00
parent ecde06e3a3
commit c08bf1204f
3 changed files with 77 additions and 4 deletions
+13 -3
View File
@@ -2462,8 +2462,9 @@ gb_internal void add_comparison_procedures_for_fields(CheckerContext *c, Type *t
add_package_dependency(c, "runtime", "quaternion256_ne");
break;
case Basic_cstring:
add_package_dependency(c, "runtime", "cstring_to_string");
/*fallthrough*/
add_package_dependency(c, "runtime", "cstring_eq");
add_package_dependency(c, "runtime", "cstring_ne");
break;
case Basic_string:
add_package_dependency(c, "runtime", "string_eq");
add_package_dependency(c, "runtime", "string_ne");
@@ -2621,7 +2622,16 @@ gb_internal void check_comparison(CheckerContext *c, Ast *node, Operand *x, Oper
if (!is_type_untyped(x->type)) size = gb_max(size, type_size_of(x->type));
if (!is_type_untyped(y->type)) size = gb_max(size, type_size_of(y->type));
if (is_type_string(x->type) || is_type_string(y->type)) {
if (is_type_cstring(x->type) && is_type_cstring(y->type)) {
switch (op) {
case Token_CmpEq: add_package_dependency(c, "runtime", "cstring_eq"); break;
case Token_NotEq: add_package_dependency(c, "runtime", "cstring_ne"); break;
case Token_Lt: add_package_dependency(c, "runtime", "cstring_lt"); break;
case Token_Gt: add_package_dependency(c, "runtime", "cstring_gt"); break;
case Token_LtEq: add_package_dependency(c, "runtime", "cstring_le"); break;
case Token_GtEq: add_package_dependency(c, "runtime", "cstring_gt"); break;
}
} else if (is_type_string(x->type) || is_type_string(y->type)) {
switch (op) {
case Token_CmpEq: add_package_dependency(c, "runtime", "string_eq"); break;
case Token_NotEq: add_package_dependency(c, "runtime", "string_ne"); break;
+22 -1
View File
@@ -2414,7 +2414,28 @@ gb_internal lbValue lb_emit_comp(lbProcedure *p, TokenKind op_kind, lbValue left
}
if (is_type_string(a)) {
if (is_type_cstring(a)) {
if (is_type_cstring(a) && is_type_cstring(b)) {
left = lb_emit_conv(p, left, t_cstring);
right = lb_emit_conv(p, right, t_cstring);
char const *runtime_procedure = nullptr;
switch (op_kind) {
case Token_CmpEq: runtime_procedure = "cstring_eq"; break;
case Token_NotEq: runtime_procedure = "cstring_ne"; break;
case Token_Lt: runtime_procedure = "cstring_lt"; break;
case Token_Gt: runtime_procedure = "cstring_gt"; break;
case Token_LtEq: runtime_procedure = "cstring_le"; break;
case Token_GtEq: runtime_procedure = "cstring_gt"; break;
}
GB_ASSERT(runtime_procedure != nullptr);
auto args = array_make<lbValue>(permanent_allocator(), 2);
args[0] = left;
args[1] = right;
return lb_emit_runtime_call(p, runtime_procedure, args);
}
if (is_type_cstring(a) ^ is_type_cstring(b)) {
left = lb_emit_conv(p, left, t_string);
right = lb_emit_conv(p, right, t_string);
}