cgen: fix `in` multi_array

pull/5389/head
yuyi 2020-06-15 19:21:06 +08:00 committed by GitHub
parent e73ed56231
commit b0138e021e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 2 deletions

View File

@ -3358,18 +3358,29 @@ fn (mut g Gen) type_of_call_expr(node ast.Expr) string {
// `a in [1,2,3]` => `a == 1 || a == 2 || a == 3` // `a in [1,2,3]` => `a == 1 || a == 2 || a == 3`
fn (mut g Gen) in_optimization(left ast.Expr, right ast.ArrayInit) { fn (mut g Gen) in_optimization(left ast.Expr, right ast.ArrayInit) {
is_str := right.elem_type == table.string_type is_str := right.elem_type == table.string_type
elem_sym := g.table.get_type_symbol(right.elem_type)
is_array := elem_sym.kind == .array
for i, array_expr in right.exprs { for i, array_expr in right.exprs {
if is_str { if is_str {
g.write('string_eq(') g.write('string_eq(')
} else if is_array {
styp := g.table.value_type(right.elem_type)
ptr_typ := g.typ(right.elem_type).split('_')[1]
if ptr_typ !in g.array_fn_definitions {
sym := g.table.get_type_symbol(elem_sym.array_info().elem_type)
g.generate_array_equality_fn(ptr_typ, styp, sym)
} }
g.write('${ptr_typ}_arr_eq(')
}
g.expr(left) g.expr(left)
if is_str { if is_str || is_array {
g.write(', ') g.write(', ')
} else { } else {
g.write(' == ') g.write(' == ')
} }
g.expr(array_expr) g.expr(array_expr)
if is_str { if is_str || is_array {
g.write(')') g.write(')')
} }
if i < right.exprs.len - 1 { if i < right.exprs.len - 1 {

View File

@ -217,4 +217,5 @@ fn test_optimized_in_expression_with_string() {
fn test_in_array_init() { fn test_in_array_init() {
assert 1 !in []int{} assert 1 !in []int{}
assert [1] in [[1]]
} }