1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

cgen: fix const expr using optional or result (#15850)

This commit is contained in:
yuyi 2022-09-24 03:36:56 +08:00 committed by GitHub
parent 1f26e3fb1b
commit 41fd02496a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 10 additions and 9 deletions

View File

@ -128,7 +128,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_const_opt_or_res bool
inside_lambda bool inside_lambda bool
loop_depth int loop_depth int
ternary_names map[string]string ternary_names map[string]string
@ -4570,14 +4570,15 @@ 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 || field.expr.return_type.has_flag(.result) {
unwrap_option := field.expr.or_block.kind != .absent g.inside_const_opt_or_res = true
g.const_decl_init_later(field.mod, name, field.expr, field.typ, unwrap_option) unwrap_opt_res := field.expr.or_block.kind != .absent
g.const_decl_init_later(field.mod, name, field.expr, field.typ, unwrap_opt_res)
} 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 g.inside_const_opt_or_res = false
} }
else { else {
// Note: -usecache uses prebuilt modules, each compiled with: // Note: -usecache uses prebuilt modules, each compiled with:

View File

@ -671,7 +671,7 @@ fn (mut g Gen) call_expr(node ast.CallExpr) {
if unwrapped_typ == ast.void_type { if unwrapped_typ == ast.void_type {
g.write('\n $cur_line') g.write('\n $cur_line')
} else { } else {
if !g.inside_const_optional { if !g.inside_const_opt_or_res {
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')

View File

@ -1,13 +1,13 @@
const ( const (
aaa = iopt()? aaa = iopt()?
bbb = sopt()? bbb = sopt()!
) )
fn iopt() ?int { fn iopt() ?int {
return 1234 return 1234
} }
fn sopt() ?string { fn sopt() !string {
return 'xyz' return 'xyz'
} }