From a85467eb0f711a7b06dd5ad60327904ab2ddc664 Mon Sep 17 00:00:00 2001 From: crthpl <56052645+crthpl@users.noreply.github.com> Date: Fri, 27 Aug 2021 01:48:32 -0700 Subject: [PATCH] cgen: fix `expr in shared_array` (#11319) --- vlib/v/gen/c/array.v | 10 ++++++++-- vlib/v/gen/c/infix_expr.v | 5 ++++- vlib/v/tests/shared_in_test.v | 8 ++++++++ 3 files changed, 20 insertions(+), 3 deletions(-) create mode 100644 vlib/v/tests/shared_in_test.v diff --git a/vlib/v/gen/c/array.v b/vlib/v/gen/c/array.v index cd8d0a420f..74dc9aac3a 100644 --- a/vlib/v/gen/c/array.v +++ b/vlib/v/gen/c/array.v @@ -447,7 +447,10 @@ fn (mut g Gen) gen_array_prepend(node ast.CallExpr) { } fn (mut g Gen) gen_array_contains_method(left_type ast.Type) string { - unwrap_left_type := g.unwrap_generic(left_type) + mut unwrap_left_type := g.unwrap_generic(left_type) + if unwrap_left_type.share() == .shared_t { + unwrap_left_type = unwrap_left_type.clear_flag(.shared_f) + } mut left_sym := g.table.get_type_symbol(unwrap_left_type) left_final_sym := g.table.get_final_type_symbol(unwrap_left_type) mut left_type_str := g.typ(unwrap_left_type).replace('*', '') @@ -502,10 +505,13 @@ fn (mut g Gen) gen_array_contains_method(left_type ast.Type) string { fn (mut g Gen) gen_array_contains(node ast.CallExpr) { fn_name := g.gen_array_contains_method(node.left_type) g.write('${fn_name}(') - if node.left_type.is_ptr() { + if node.left_type.is_ptr() && node.left_type.share() != .shared_t { g.write('*') } g.expr(node.left) + if node.left_type.share() == .shared_t { + g.write('->val') + } g.write(', ') g.expr(node.args[0].expr) g.write(')') diff --git a/vlib/v/gen/c/infix_expr.v b/vlib/v/gen/c/infix_expr.v index b015e392f2..dc842c9730 100644 --- a/vlib/v/gen/c/infix_expr.v +++ b/vlib/v/gen/c/infix_expr.v @@ -328,10 +328,13 @@ fn (mut g Gen) infix_expr_in_op(node ast.InfixExpr) { } fn_name := g.gen_array_contains_method(node.right_type) g.write('(${fn_name}(') - if right.typ.is_ptr() { + if right.typ.is_ptr() && right.typ.share() != .shared_t { g.write('*') } g.expr(node.right) + if right.typ.share() == .shared_t { + g.write('->val') + } g.write(', ') g.expr(node.left) g.write('))') diff --git a/vlib/v/tests/shared_in_test.v b/vlib/v/tests/shared_in_test.v new file mode 100644 index 0000000000..1fbe2c0975 --- /dev/null +++ b/vlib/v/tests/shared_in_test.v @@ -0,0 +1,8 @@ +fn test_shared_in() { + shared a := [1, 3, 7, 3] + assert 1 in a + assert 0 !in a + assert 7 in a + assert 3 in a + assert 1238941 !in a +}