Skip to content

Commit 48477fa

Browse files
yuyi98pull[bot]
authored andcommitted
checker: fix const from comptime if expr (#19179)
1 parent 33fd4b2 commit 48477fa

2 files changed

Lines changed: 27 additions & 0 deletions

File tree

vlib/v/checker/checker.v

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1648,6 +1648,13 @@ fn (mut c Checker) const_decl(mut node ast.ConstDecl) {
16481648
field.expr.is_expr = true
16491649
field.expr.typ = (branch.stmts.last() as ast.ExprStmt).typ
16501650
field.typ = field.expr.typ
1651+
// update ConstField object's type in table
1652+
if mut obj := c.file.global_scope.find(field.name) {
1653+
if mut obj is ast.ConstField {
1654+
obj.typ = field.typ
1655+
}
1656+
}
1657+
break
16511658
}
16521659
}
16531660
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import term
2+
3+
const (
4+
color = $if windows {
5+
term.bright_cyan('WINDOWS')
6+
} $else {
7+
term.bright_green('UNIX')
8+
}
9+
)
10+
11+
fn test_const_from_comptime_if_expr() {
12+
salutation := 'hello'
13+
val := match salutation {
14+
'hello' { color + ' some text' }
15+
'goodbyte' { color + ' some other text' }
16+
else { 'invalid' }
17+
}
18+
println(val)
19+
assert true
20+
}

0 commit comments

Comments
 (0)