From d87011ab7864504ce73f241fb82e04d82bb14e25 Mon Sep 17 00:00:00 2001 From: Swastik Baranwal Date: Sun, 27 Dec 2020 23:37:53 +0530 Subject: [PATCH] cgen: fix generic contains method (#7618) --- vlib/builtin/array_test.v | 21 +++++++++++++++++++++ vlib/v/gen/auto_eq_methods.v | 2 +- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/vlib/builtin/array_test.v b/vlib/builtin/array_test.v index 7b992f7ec4..3a2b70d22d 100644 --- a/vlib/builtin/array_test.v +++ b/vlib/builtin/array_test.v @@ -1251,3 +1251,24 @@ fn test_struct_array_of_multi_type_index() { println(people.index(ivan)) assert people.index(ivan) == 0 } + +struct Coord { + x int + y int + z int +} + +fn test__array_struct_contains() { + mut coords := []Coord{} + coord_1 := Coord{ + x: 1 + y: 2 + z: -1 + } + coords << coord_1 + exists := coord_1 in coords + not_exists := coord_1 !in coords + println('`exists`: $exists and `not exists`: $not_exists') + assert exists == true + assert not_exists == false +} diff --git a/vlib/v/gen/auto_eq_methods.v b/vlib/v/gen/auto_eq_methods.v index 656b441c23..8282dabad1 100644 --- a/vlib/v/gen/auto_eq_methods.v +++ b/vlib/v/gen/auto_eq_methods.v @@ -38,7 +38,7 @@ fn (mut g Gen) gen_struct_equality_fn(left table.Type) string { fn_builder.writeln('\tif (*((voidptr*)(a.$field.name)) != *((voidptr*)(b.$field.name))) {') } else { - fn_builder.writeln('\tif (a.$field.name != b.$field.name)) {') + fn_builder.writeln('\tif (a.$field.name != b.$field.name) {') } } fn_builder.writeln('\t\treturn false;')