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

checker: fix const init with comptime (#17528)

This commit is contained in:
Felipe Pena 2023-03-07 05:15:46 -03:00 committed by GitHub
parent 784592af83
commit 1e416de627
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 0 deletions

View File

@ -388,6 +388,27 @@ fn (mut c Checker) eval_comptime_const_expr(expr ast.Expr, nlevel int) ?ast.Comp
} }
} }
} }
ast.IfExpr {
if !expr.is_comptime {
return none
}
for i in 0 .. expr.branches.len {
branch := expr.branches[i]
if !expr.has_else || i < expr.branches.len - 1 {
if c.comptime_if_branch(branch.cond, branch.pos) == .eval {
last_stmt := branch.stmts.last()
if last_stmt is ast.ExprStmt {
return c.eval_comptime_const_expr(last_stmt.expr, nlevel + 1)
}
}
} else {
last_stmt := branch.stmts.last()
if last_stmt is ast.ExprStmt {
return c.eval_comptime_const_expr(last_stmt.expr, nlevel + 1)
}
}
}
}
// ast.ArrayInit {} // ast.ArrayInit {}
// ast.PrefixExpr { // ast.PrefixExpr {
// c.note('prefixexpr: $expr', expr.pos) // c.note('prefixexpr: $expr', expr.pos)

View File

@ -0,0 +1,12 @@
const (
buf_size = $if true {
2
} $else {
1
}
)
fn test_main() {
mut buf := [buf_size]u8{}
assert buf.len == 2
}