checker: fix $if x { condition, using const x = $d('ident', false) (fix #21709) (#21713)

This commit is contained in:
Felipe Pena 2024-06-23 04:11:54 -03:00 committed by GitHub
parent d7bc216713
commit 53d7a55424
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 17 additions and 0 deletions

View File

@ -4215,6 +4215,13 @@ fn (mut c Checker) find_obj_definition(obj ast.ScopeObject) !ast.Expr {
if mut expr is ast.Ident {
return c.find_definition(expr)
}
if mut expr is ast.ComptimeCall && expr.is_compile_value {
if expr.result_type == ast.bool_type {
return ast.BoolLiteral{
val: expr.compile_value.bool()
}
}
}
if !expr.is_pure_literal() {
return error('definition of `${name}` is unknown at compile time')
}

View File

@ -0,0 +1,10 @@
const x = $d('x', true)
fn test_main() {
a := $if x {
1
} $else {
0
}
assert a == 1
}