cgen: fix error for const using nested optionals (#13939)
parent
0bd8fbc9a8
commit
f6b8e1e13f
|
@ -121,6 +121,7 @@ mut:
|
||||||
inside_comptime_for_field bool
|
inside_comptime_for_field bool
|
||||||
inside_cast_in_heap int // inside cast to interface type in heap (resolve recursive calls)
|
inside_cast_in_heap int // inside cast to interface type in heap (resolve recursive calls)
|
||||||
inside_const bool
|
inside_const bool
|
||||||
|
inside_const_optional bool
|
||||||
inside_lambda bool
|
inside_lambda bool
|
||||||
loop_depth int
|
loop_depth int
|
||||||
ternary_names map[string]string
|
ternary_names map[string]string
|
||||||
|
@ -4174,11 +4175,13 @@ fn (mut g Gen) const_decl(node ast.ConstDecl) {
|
||||||
}
|
}
|
||||||
ast.CallExpr {
|
ast.CallExpr {
|
||||||
if field.expr.return_type.has_flag(.optional) {
|
if field.expr.return_type.has_flag(.optional) {
|
||||||
|
g.inside_const_optional = true
|
||||||
unwrap_option := field.expr.or_block.kind != .absent
|
unwrap_option := field.expr.or_block.kind != .absent
|
||||||
g.const_decl_init_later(field.mod, name, field.expr, field.typ, unwrap_option)
|
g.const_decl_init_later(field.mod, name, field.expr, field.typ, unwrap_option)
|
||||||
} else {
|
} else {
|
||||||
g.const_decl_init_later(field.mod, name, field.expr, field.typ, false)
|
g.const_decl_init_later(field.mod, name, field.expr, field.typ, false)
|
||||||
}
|
}
|
||||||
|
g.inside_const_optional = false
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
// Note: -usecache uses prebuilt modules, each compiled with:
|
// 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 {
|
} else if g.table.sym(node.return_type).kind == .multi_return {
|
||||||
g.write('\n $cur_line $tmp_opt /*U*/')
|
g.write('\n $cur_line $tmp_opt /*U*/')
|
||||||
} else {
|
} else {
|
||||||
if !g.inside_const {
|
if !g.inside_const || !g.inside_const_optional {
|
||||||
g.write('\n $cur_line (*($unwrapped_styp*)${tmp_opt}.data)')
|
g.write('\n $cur_line (*($unwrapped_styp*)${tmp_opt}.data)')
|
||||||
} else {
|
} else {
|
||||||
g.write('\n $cur_line $tmp_opt')
|
g.write('\n $cur_line $tmp_opt')
|
||||||
|
|
|
@ -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