mirror of
https://github.com/vlang/v.git
synced 2025-09-12 08:57:09 -04:00
cgen: fix premature variable release by autofree (#20731)
This commit is contained in:
parent
4b4646169b
commit
e48e28de1e
@ -2970,7 +2970,9 @@ fn (mut g Gen) autofree_scope_vars2(scope &ast.Scope, start_pos int, end_pos int
|
||||
// continue
|
||||
// }
|
||||
// if v.pos.pos > end_pos {
|
||||
if obj.pos.pos > end_pos || (obj.pos.pos < start_pos && obj.pos.line_nr == line_nr) {
|
||||
if obj.pos.pos > end_pos
|
||||
|| (obj.pos.pos < start_pos && obj.pos.line_nr == line_nr)
|
||||
|| (end_pos < scope.end_pos && obj.expr is ast.IfExpr) {
|
||||
// Do not free vars that were declared after this scope
|
||||
continue
|
||||
}
|
||||
|
19
vlib/v/slow_tests/valgrind/if_expr_autofree_optional.v
Normal file
19
vlib/v/slow_tests/valgrind/if_expr_autofree_optional.v
Normal file
@ -0,0 +1,19 @@
|
||||
fn main() {
|
||||
out := if temp := f('') {
|
||||
temp
|
||||
} else if temp := f('something') {
|
||||
temp
|
||||
} else {
|
||||
return
|
||||
}
|
||||
|
||||
assert out[0] == 104
|
||||
assert out[1] == 105
|
||||
}
|
||||
|
||||
fn f(s string) ?[]u8 {
|
||||
if s == '' {
|
||||
return none
|
||||
}
|
||||
return [u8(104), 105]
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user