From 74f9ce6b86acdaf20d4ed05f1365337eef72d3e8 Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Mon, 30 Sep 2024 13:32:36 -0300 Subject: [PATCH 1/2] fix --- vlib/v/gen/c/str_intp.v | 4 +++- .../comptime/comptime_variant_interp_test.v | 23 +++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 vlib/v/tests/comptime/comptime_variant_interp_test.v diff --git a/vlib/v/gen/c/str_intp.v b/vlib/v/gen/c/str_intp.v index 4f1e894cde33de..1c9285fdeb4f43 100644 --- a/vlib/v/gen/c/str_intp.v +++ b/vlib/v/gen/c/str_intp.v @@ -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.is_comptime_var(expr) { + 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) diff --git a/vlib/v/tests/comptime/comptime_variant_interp_test.v b/vlib/v/tests/comptime/comptime_variant_interp_test.v new file mode 100644 index 00000000000000..c2f32ab7130daa --- /dev/null +++ b/vlib/v/tests/comptime/comptime_variant_interp_test.v @@ -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' +} From 1701cc375d87be6411adad9757389808df50cd90 Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Mon, 30 Sep 2024 14:18:37 -0300 Subject: [PATCH 2/2] fix --- vlib/v/gen/c/str_intp.v | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vlib/v/gen/c/str_intp.v b/vlib/v/gen/c/str_intp.v index 1c9285fdeb4f43..94503ef2ff5d06 100644 --- a/vlib/v/gen/c/str_intp.v +++ b/vlib/v/gen/c/str_intp.v @@ -191,7 +191,7 @@ 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 g.comptime.is_comptime_var(expr) { + 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 {