cgen: fix comptime variant string interpolation (fix #22366) (#22368)

This commit is contained in:
Felipe Pena 2024-10-01 05:25:56 -03:00 committed by GitHub
parent fc72044b42
commit d1fdcfbab4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 26 additions and 1 deletions

View file

@ -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)

View file

@ -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'
}