cgen: fix `expr in shared_array` (#11319)

pull/11323/head
crthpl 2021-08-27 01:48:32 -07:00 committed by GitHub
parent 5f90d5702e
commit a85467eb0f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 20 additions and 3 deletions

View File

@ -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(')')

View File

@ -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('))')

View File

@ -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
}