diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 4875fbe1161470..46bc9334cabd1e 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -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 } } } diff --git a/vlib/v/tests/const_from_comptime_if_expr_test.v b/vlib/v/tests/const_from_comptime_if_expr_test.v new file mode 100644 index 00000000000000..c3612837cb7f1e --- /dev/null +++ b/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 +}