Skip to content

Commit d1fdcfb

Browse files
authored
cgen: fix comptime variant string interpolation (fix #22366) (#22368)
1 parent fc72044 commit d1fdcfb

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

vlib/v/gen/c/str_intp.v

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,9 @@ fn (mut g Gen) str_val(node ast.StringInterLiteral, i int, fmts []u8) {
191191
} else if fmt == `s` || typ.has_flag(.variadic) {
192192
mut exp_typ := typ
193193
if expr is ast.Ident {
194-
if expr.obj is ast.Var {
194+
if g.comptime.get_ct_type_var(expr) == .smartcast {
195+
exp_typ = g.comptime.get_comptime_var_type(expr)
196+
} else if expr.obj is ast.Var {
195197
if expr.obj.smartcasts.len > 0 {
196198
exp_typ = g.unwrap_generic(expr.obj.smartcasts.last())
197199
cast_sym := g.table.sym(exp_typ)
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
module main
2+
3+
struct Foo {
4+
a int
5+
}
6+
7+
type SumType = Foo | int | string
8+
9+
fn (v SumType) cast[T](val T) string {
10+
mut res := ''
11+
$for variant_value in v.variants {
12+
if v is variant_value {
13+
println('v: ${v}')
14+
res = 'v: ${v}'
15+
println(res)
16+
}
17+
}
18+
return res
19+
}
20+
21+
fn test_main() {
22+
assert SumType(1).cast[int](1) == 'v: 1'
23+
}

0 commit comments

Comments
 (0)