mirror of
https://github.com/vlang/v.git
synced 2025-09-16 10:57:25 -04:00
cgen: fix code generation wrong, when '?foo.array or {}' as a 'for-in' condition (fix #20528) (#20542)
This commit is contained in:
parent
f5db8f710b
commit
d783cda671
@ -235,7 +235,7 @@ fn (mut g Gen) for_in_stmt(node_ ast.ForInStmt) {
|
||||
}
|
||||
mut cond_var := ''
|
||||
if (node.cond is ast.Ident && !node.cond_type.has_flag(.option))
|
||||
|| node.cond is ast.SelectorExpr {
|
||||
|| (node.cond is ast.SelectorExpr && node.cond.or_block.kind == .absent) {
|
||||
cond_var = g.expr_string(node.cond)
|
||||
} else {
|
||||
cond_var = g.new_tmp_var()
|
||||
|
@ -6,3 +6,23 @@ fn test_for_in_option() {
|
||||
}
|
||||
assert true
|
||||
}
|
||||
|
||||
// for issue 20528
|
||||
// phenomenon: when the cond expr is SelectorExpr and the type is an array, cgen fails.
|
||||
struct Foo {
|
||||
data ?[]int
|
||||
}
|
||||
|
||||
fn (f Foo) get_first() ?int {
|
||||
for d in f.data or { return none } {
|
||||
return d
|
||||
}
|
||||
return none
|
||||
}
|
||||
|
||||
fn test_cond_is_selector_array_and_with_or_block() {
|
||||
foo := Foo{
|
||||
data: [1, 2, 3]
|
||||
}
|
||||
assert foo.get_first()? == 1
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user