Skip to content

Commit

Permalink
cgen: fix comptime variant string interpolation (fix #22366) (#22368)
Browse files Browse the repository at this point in the history
  • Loading branch information
felipensp authored Oct 1, 2024
1 parent fc72044 commit d1fdcfb
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
4 changes: 3 additions & 1 deletion vlib/v/gen/c/str_intp.v
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,9 @@ fn (mut g Gen) str_val(node ast.StringInterLiteral, i int, fmts []u8) {
} else if fmt == `s` || typ.has_flag(.variadic) {
mut exp_typ := typ
if expr is ast.Ident {
if expr.obj is ast.Var {
if g.comptime.get_ct_type_var(expr) == .smartcast {
exp_typ = g.comptime.get_comptime_var_type(expr)
} else if expr.obj is ast.Var {
if expr.obj.smartcasts.len > 0 {
exp_typ = g.unwrap_generic(expr.obj.smartcasts.last())
cast_sym := g.table.sym(exp_typ)
Expand Down
23 changes: 23 additions & 0 deletions vlib/v/tests/comptime/comptime_variant_interp_test.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
module main

struct Foo {
a int
}

type SumType = Foo | int | string

fn (v SumType) cast[T](val T) string {
mut res := ''
$for variant_value in v.variants {
if v is variant_value {
println('v: ${v}')
res = 'v: ${v}'
println(res)
}
}
return res
}

fn test_main() {
assert SumType(1).cast[int](1) == 'v: 1'
}

0 comments on commit d1fdcfb

Please sign in to comment.