1
0
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:
yuyi 2022-04-05 10:58:11 +08:00 committed by GitHub
parent 0bd8fbc9a8
commit f6b8e1e13f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 14 additions and 1 deletions

View File

@ -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:

View File

@ -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')

View 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
}