cgen: fix `in` empty array

pull/5382/head
yuyi 2020-06-14 16:54:10 +08:00 committed by GitHub
parent 471c931ada
commit 7e0197c1b8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 7 deletions

View File

@ -1861,6 +1861,7 @@ fn (mut g Gen) infix_expr(node ast.InfixExpr) {
if right_sym.kind == .array { if right_sym.kind == .array {
match node.right { match node.right {
ast.ArrayInit { ast.ArrayInit {
if it.exprs.len > 0 {
// `a in [1,2,3]` optimization => `a == 1 || a == 2 || a == 3` // `a in [1,2,3]` optimization => `a == 1 || a == 2 || a == 3`
// avoids an allocation // avoids an allocation
// g.write('/*in opt*/') // g.write('/*in opt*/')
@ -1869,6 +1870,7 @@ fn (mut g Gen) infix_expr(node ast.InfixExpr) {
g.write(')') g.write(')')
return return
} }
}
else {} else {}
} }
styp := g.typ(g.table.mktyp(left_type)) styp := g.typ(g.table.mktyp(left_type))

View File

@ -214,3 +214,7 @@ fn test_optimized_in_expression_with_string() {
a = 'ab' in ['ab', 'bc'] && false a = 'ab' in ['ab', 'bc'] && false
assert a == false assert a == false
} }
fn test_in_array_init() {
assert 1 !in []int{}
}