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

checker: fix const from multi branchs of if expr (#18951)

This commit is contained in:
yuyi 2023-07-23 19:09:44 +08:00 committed by GitHub
parent e1758bc0c5
commit 78681bf852
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 11 deletions

View File

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

View File

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