Skip to content

Commit

Permalink
checker: fix const from multi branchs of if expr (#18951)
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyi98 committed Jul 23, 2023
1 parent e1758bc commit 78681bf
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
15 changes: 4 additions & 11 deletions vlib/v/checker/checker.v
Expand Up @@ -1626,18 +1626,11 @@ fn (mut c Checker) const_decl(mut node ast.ConstDecl) {
}
node.fields[i].typ = ast.mktyp(typ)
if mut field.expr is ast.IfExpr {
if field.expr.branches.len == 2 {
first_stmts := field.expr.branches[0].stmts
second_stmts := field.expr.branches[1].stmts
if first_stmts.len > 0 && first_stmts.last() is ast.ExprStmt
&& first_stmts.last().typ != ast.void_type {
for branch in field.expr.branches {
if branch.stmts.len > 0 && branch.stmts.last() is ast.ExprStmt
&& branch.stmts.last().typ != ast.void_type {
field.expr.is_expr = true
field.expr.typ = (first_stmts.last() as ast.ExprStmt).typ
field.typ = field.expr.typ
} else if second_stmts.len > 0 && second_stmts.last() is ast.ExprStmt
&& second_stmts.last().typ != ast.void_type {
field.expr.is_expr = true
field.expr.typ = (second_stmts.last() as ast.ExprStmt).typ
field.expr.typ = (branch.stmts.last() as ast.ExprStmt).typ
field.typ = field.expr.typ
}
}
Expand Down
14 changes: 14 additions & 0 deletions vlib/v/tests/const_from_multi_branchs_of_if_expr_test.v
@@ -0,0 +1,14 @@
import os

const bin = $if linux {
os.join_path(@VMODROOT, 'bin_linux_64')
} $else $if macos {
os.join_path(@VMODROOT, 'bin_macos')
} $else {
''
}

fn test_const_from_mutli_branchs_of_if_expr() {
println('Hello')
assert true
}

0 comments on commit 78681bf

Please sign in to comment.