From 9e71e324eb487fc70806131ceebd8a1070035763 Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Sat, 14 Dec 2024 13:52:40 -0300 Subject: [PATCH] cgen: fix assert for alias to fixed array (fix #23149) (#23161) --- vlib/v/ast/types.v | 10 ++++++++++ vlib/v/gen/c/auto_eq_methods.v | 5 +++++ vlib/v/gen/c/infix.v | 17 +++++++++++++++++ vlib/v/gen/c/str.v | 4 ++++ vlib/v/tests/assert_alias_array_test.v | 18 ++++++++++++++++++ 5 files changed, 54 insertions(+) create mode 100644 vlib/v/tests/assert_alias_array_test.v diff --git a/vlib/v/ast/types.v b/vlib/v/ast/types.v index ee1e4f168e947e..832521cb1feef0 100644 --- a/vlib/v/ast/types.v +++ b/vlib/v/ast/types.v @@ -1006,6 +1006,16 @@ pub fn (t &Struct) is_unresolved_generic() bool { return t.generic_types.len > 0 && t.concrete_types.len == 0 } +pub fn (t &TypeSymbol) is_primitive_fixed_array() bool { + if t.info is ArrayFixed { + return global_table.final_sym(t.info.elem_type).is_primitive() + } else if t.info is Alias { + return global_table.final_sym(t.info.parent_type).is_primitive_fixed_array() + } else { + return false + } +} + pub fn (t &TypeSymbol) is_array_fixed() bool { if t.info is ArrayFixed { return true diff --git a/vlib/v/gen/c/auto_eq_methods.v b/vlib/v/gen/c/auto_eq_methods.v index 429319702aa11b..330fbb3b98bc78 100644 --- a/vlib/v/gen/c/auto_eq_methods.v +++ b/vlib/v/gen/c/auto_eq_methods.v @@ -437,6 +437,11 @@ fn (mut g Gen) gen_fixed_array_equality_fn(left_type ast.Type) string { mut fn_builder := strings.new_builder(512) fn_builder.writeln('inline bool ${ptr_styp}_arr_eq(${arg_styp} a, ${arg_styp} b) {') + if left_typ.sym.is_primitive_fixed_array() { + fn_builder.writeln('\tif (!memcmp(${left}, ${right}, sizeof(${arg_styp}))) {') + fn_builder.writeln('\t\treturn true;') + fn_builder.writeln('\t}') + } fn_builder.writeln('\tfor (int i = 0; i < ${size}; ++i) {') // compare every pair of elements of the two fixed arrays if elem.sym.kind == .string { diff --git a/vlib/v/gen/c/infix.v b/vlib/v/gen/c/infix.v index 9a50f45c6e1a39..c33b40a131d79b 100644 --- a/vlib/v/gen/c/infix.v +++ b/vlib/v/gen/c/infix.v @@ -216,8 +216,17 @@ fn (mut g Gen) infix_expr_eq_op(node ast.InfixExpr) { if left.typ.is_ptr() { g.write('*'.repeat(left.typ.nr_muls())) } + if node.left is ast.StructInit && left.unaliased_sym.is_primitive_fixed_array() { + s := g.styp(left.unaliased) + g.write('(${s})') + } g.expr(node.left) g.write(', ') + if node.right is ast.StructInit + && right.unaliased_sym.is_primitive_fixed_array() { + s := g.styp(right.unaliased) + g.write('(${s})') + } if right.typ.is_ptr() { g.write('*'.repeat(right.typ.nr_muls())) } @@ -266,6 +275,10 @@ fn (mut g Gen) infix_expr_eq_op(node ast.InfixExpr) { s := g.styp(left.unaliased) g.write('(${s})') } + } else if node.left is ast.StructInit + && left.unaliased_sym.is_primitive_fixed_array() { + s := g.styp(left.unaliased) + g.write('(${s})') } g.expr(node.left) g.write(', ') @@ -274,6 +287,10 @@ fn (mut g Gen) infix_expr_eq_op(node ast.InfixExpr) { s := g.styp(right.unaliased) g.write('(${s})') } + } else if node.right is ast.StructInit + && right.unaliased_sym.is_primitive_fixed_array() { + s := g.styp(right.unaliased) + g.write('(${s})') } g.expr(node.right) g.write(')') diff --git a/vlib/v/gen/c/str.v b/vlib/v/gen/c/str.v index b3b818fc983809..37c6bc80f8597b 100644 --- a/vlib/v/gen/c/str.v +++ b/vlib/v/gen/c/str.v @@ -234,6 +234,10 @@ fn (mut g Gen) gen_expr_to_string(expr ast.Expr, etype ast.Type) { if temp_var_needed { g.write(tmp_var) } else { + if expr is ast.StructInit && g.table.final_sym(expr.typ).is_primitive_fixed_array() { + s := g.styp(expr.typ) + g.write('(${s})') + } g.expr_with_cast(expr, typ, typ) } } else if typ.has_flag(.option) { diff --git a/vlib/v/tests/assert_alias_array_test.v b/vlib/v/tests/assert_alias_array_test.v new file mode 100644 index 00000000000000..c89aa00d7887ff --- /dev/null +++ b/vlib/v/tests/assert_alias_array_test.v @@ -0,0 +1,18 @@ +type Arr = [4]u8 +type Arr2 = []u8 +type ArrStr = [4]string + +fn test_main() { + a := Arr{} + b := Arr{} + + assert a == b + assert Arr{} == Arr{} + assert Arr{} == [4]u8{} + assert Arr{} == [u8(0), 0, 0, 0]! + + assert Arr2{} == Arr2{} + assert Arr2{} == []u8{} + + assert ArrStr{} == ArrStr{} +}