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:
parent
784592af83
commit
1e416de627
@ -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.PrefixExpr {
|
||||
// c.note('prefixexpr: $expr', expr.pos)
|
||||
|
12
vlib/v/tests/comptime_const_def_test.v
Normal file
12
vlib/v/tests/comptime_const_def_test.v
Normal 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
|
||||
}
|
Loading…
Reference in New Issue
Block a user