Skip to content

Commit

Permalink
checker: fix const from comptime if expr (#19179)
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyi98 committed Aug 19, 2023
1 parent 5f595a6 commit 8b96175
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
7 changes: 7 additions & 0 deletions vlib/v/checker/checker.v
Expand Up @@ -1648,6 +1648,13 @@ fn (mut c Checker) const_decl(mut node ast.ConstDecl) {
field.expr.is_expr = true
field.expr.typ = (branch.stmts.last() as ast.ExprStmt).typ
field.typ = field.expr.typ
// update ConstField object's type in table
if mut obj := c.file.global_scope.find(field.name) {
if mut obj is ast.ConstField {
obj.typ = field.typ
}
}
break
}
}
}
Expand Down
20 changes: 20 additions & 0 deletions vlib/v/tests/const_from_comptime_if_expr_test.v
@@ -0,0 +1,20 @@
import term

const (
color = $if windows {
term.bright_cyan('WINDOWS')
} $else {
term.bright_green('UNIX')
}
)

fn test_const_from_comptime_if_expr() {
salutation := 'hello'
val := match salutation {
'hello' { color + ' some text' }
'goodbyte' { color + ' some other text' }
else { 'invalid' }
}
println(val)
assert true
}

0 comments on commit 8b96175

Please sign in to comment.