From f26626117db2c27c67a7df392446a94a94953426 Mon Sep 17 00:00:00 2001 From: yuyi Date: Fri, 11 Jun 2021 16:58:25 +0800 Subject: [PATCH] cgen: fix match struct type cond (#10421) --- vlib/v/gen/c/cgen.v | 7 +++++++ ...p_cond_test.v => match_compound_type_cond_test.v} | 12 ++++++++++++ 2 files changed, 19 insertions(+) rename vlib/v/tests/{match_array_or_map_cond_test.v => match_compound_type_cond_test.v} (76%) diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index 34aebd912f..9f731c8342 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -4254,6 +4254,13 @@ fn (mut g Gen) match_expr_classic(node ast.MatchExpr, is_expr bool, cond_var str g.write(', ') g.expr(expr) g.write(')') + } else if type_sym.kind == .struct_ { + ptr_typ := g.gen_struct_equality_fn(node.cond_type) + g.write('${ptr_typ}_struct_eq(') + g.write(cond_var) + g.write(', ') + g.expr(expr) + g.write(')') } else if expr is ast.RangeExpr { // if type is unsigned and low is 0, check is unneeded mut skip_low := false diff --git a/vlib/v/tests/match_array_or_map_cond_test.v b/vlib/v/tests/match_compound_type_cond_test.v similarity index 76% rename from vlib/v/tests/match_array_or_map_cond_test.v rename to vlib/v/tests/match_compound_type_cond_test.v index 4274bd592f..b38f907e85 100644 --- a/vlib/v/tests/match_array_or_map_cond_test.v +++ b/vlib/v/tests/match_compound_type_cond_test.v @@ -1,3 +1,5 @@ +struct Foo {} + fn test_match_array_or_map_cond() { // array y1 := []byte{} @@ -34,4 +36,14 @@ fn test_match_array_or_map_cond() { } println('ret3 is $ret3') assert ret3 + + // struct + y4 := Foo{} + x4 := Foo{} + ret4 := match x4 { + y4 { true } + else { false } + } + println('ret4 is $ret4') + assert ret4 }