mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
cgen: fix error for const using nested optionals (#13939)
This commit is contained in:
parent
0bd8fbc9a8
commit
f6b8e1e13f
@ -121,6 +121,7 @@ mut:
|
||||
inside_comptime_for_field bool
|
||||
inside_cast_in_heap int // inside cast to interface type in heap (resolve recursive calls)
|
||||
inside_const bool
|
||||
inside_const_optional bool
|
||||
inside_lambda bool
|
||||
loop_depth int
|
||||
ternary_names map[string]string
|
||||
@ -4174,11 +4175,13 @@ fn (mut g Gen) const_decl(node ast.ConstDecl) {
|
||||
}
|
||||
ast.CallExpr {
|
||||
if field.expr.return_type.has_flag(.optional) {
|
||||
g.inside_const_optional = true
|
||||
unwrap_option := field.expr.or_block.kind != .absent
|
||||
g.const_decl_init_later(field.mod, name, field.expr, field.typ, unwrap_option)
|
||||
} else {
|
||||
g.const_decl_init_later(field.mod, name, field.expr, field.typ, false)
|
||||
}
|
||||
g.inside_const_optional = false
|
||||
}
|
||||
else {
|
||||
// Note: -usecache uses prebuilt modules, each compiled with:
|
||||
|
@ -698,7 +698,7 @@ fn (mut g Gen) call_expr(node ast.CallExpr) {
|
||||
} else if g.table.sym(node.return_type).kind == .multi_return {
|
||||
g.write('\n $cur_line $tmp_opt /*U*/')
|
||||
} else {
|
||||
if !g.inside_const {
|
||||
if !g.inside_const || !g.inside_const_optional {
|
||||
g.write('\n $cur_line (*($unwrapped_styp*)${tmp_opt}.data)')
|
||||
} else {
|
||||
g.write('\n $cur_line $tmp_opt')
|
||||
|
10
vlib/v/tests/const_use_nested_optionals_test.v
Normal file
10
vlib/v/tests/const_use_nested_optionals_test.v
Normal file
@ -0,0 +1,10 @@
|
||||
module main
|
||||
|
||||
import os
|
||||
|
||||
const iterations = (os.getenv_opt('ITERATIONS') or { '5' }).int()
|
||||
|
||||
fn test_const_use_nested_optionals() {
|
||||
println('Number of iterations: $iterations')
|
||||
assert iterations == 5
|
||||
}
|
Loading…
Reference in New Issue
Block a user