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,13 +1861,15 @@ fn (mut g Gen) infix_expr(node ast.InfixExpr) {
if right_sym.kind == .array {
match node.right {
ast.ArrayInit {
// `a in [1,2,3]` optimization => `a == 1 || a == 2 || a == 3`
// avoids an allocation
// g.write('/*in opt*/')
g.write('(')
g.in_optimization(node.left, it)
g.write(')')
return
if it.exprs.len > 0 {
// `a in [1,2,3]` optimization => `a == 1 || a == 2 || a == 3`
// avoids an allocation
// g.write('/*in opt*/')
g.write('(')
g.in_optimization(node.left, it)
g.write(')')
return
}
}
else {}
}

View File

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