Skip to content

Commit

Permalink
cgen: fix const expr using optional or result (#15850)
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyi98 committed Sep 23, 2022
1 parent 1f26e3f commit 41fd024
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 9 deletions.
13 changes: 7 additions & 6 deletions vlib/v/gen/c/cgen.v
Expand Up @@ -128,7 +128,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_const_opt_or_res bool
inside_lambda bool
loop_depth int
ternary_names map[string]string
Expand Down Expand Up @@ -4570,14 +4570,15 @@ 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)
if field.expr.return_type.has_flag(.optional)
|| field.expr.return_type.has_flag(.result) {
g.inside_const_opt_or_res = true
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 {
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 {
// Note: -usecache uses prebuilt modules, each compiled with:
Expand Down
2 changes: 1 addition & 1 deletion vlib/v/gen/c/fn.v
Expand Up @@ -671,7 +671,7 @@ fn (mut g Gen) call_expr(node ast.CallExpr) {
if unwrapped_typ == ast.void_type {
g.write('\n $cur_line')
} else {
if !g.inside_const_optional {
if !g.inside_const_opt_or_res {
g.write('\n $cur_line (*($unwrapped_styp*)${tmp_opt}.data)')
} else {
g.write('\n $cur_line $tmp_opt')
Expand Down
@@ -1,13 +1,13 @@
const (
aaa = iopt()?
bbb = sopt()?
bbb = sopt()!
)

fn iopt() ?int {
return 1234
}

fn sopt() ?string {
fn sopt() !string {
return 'xyz'
}

Expand Down

0 comments on commit 41fd024

Please sign in to comment.