From c82a669707e9a44f5777b59096ea218d8adecd96 Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Wed, 17 Jan 2024 15:10:47 -0300 Subject: [PATCH 1/3] fix --- vlib/v/checker/checker.v | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 0800af2e18ba6f..47116cb2926b19 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -3883,7 +3883,7 @@ fn (mut c Checker) smartcast(mut expr ast.Expr, cur_type ast.Type, to_type_ ast. orig_type = expr.obj.typ } is_inherited = expr.obj.is_inherited - ct_type_var = if is_comptime && expr.obj.ct_type_var != .no_comptime { + ct_type_var = if is_comptime { .smartcast } else { .no_comptime From 73a4807b72991bbed6ddf5dd6ba8928d1cb8092d Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Fri, 19 Jan 2024 22:15:12 -0300 Subject: [PATCH 2/3] add test --- vlib/v/tests/comptime_smartcast_var_test.v | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 vlib/v/tests/comptime_smartcast_var_test.v diff --git a/vlib/v/tests/comptime_smartcast_var_test.v b/vlib/v/tests/comptime_smartcast_var_test.v new file mode 100644 index 00000000000000..44553dc8bd57c3 --- /dev/null +++ b/vlib/v/tests/comptime_smartcast_var_test.v @@ -0,0 +1,22 @@ +type TestSum = bool | f64 | int | string + +fn test_main() { + a := TestSum(true) + $for v in TestSum.variants { + if a is v { + $if a is bool { + assert a == true + } + $if a is string { + assert a == '' + } + $if a is f64 { + assert a == 0 + } + $if a is int { + assert a == 0 + } + } + } + assert true +} From 135bdab3677df66241fdcfd87e6275a60dcc2e89 Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Sat, 20 Jan 2024 08:38:46 -0300 Subject: [PATCH 3/3] fix comptime var handling --- vlib/v/debug/tests/comptime_variant.expect | 12 ++++++++---- vlib/v/debug/tests/comptime_variant.vv | 12 ++++++------ vlib/v/gen/c/cgen.v | 8 +++++++- 3 files changed, 21 insertions(+), 11 deletions(-) diff --git a/vlib/v/debug/tests/comptime_variant.expect b/vlib/v/debug/tests/comptime_variant.expect index 22a841157880e3..22ca6d21eeef43 100644 --- a/vlib/v/debug/tests/comptime_variant.expect +++ b/vlib/v/debug/tests/comptime_variant.expect @@ -1,9 +1,13 @@ #!/usr/bin/env expect source "common.tcl" -expect "Break on * comptime_variant_int in ${test_file}:7" -expect "${test_file}:7 vdbg> " -send "p a\n" -expect "a = 0 (int)" +expect "Break on * comptime_variant in ${test_file}:6" +expect "${test_file}:6 vdbg> " +send "p arg\n" +expect "arg = 0 (int)" +send "c\n" +expect "${test_file}:6 vdbg> " +send "p arg\n" +expect "arg = foo (string)" send "q\n" expect eof diff --git a/vlib/v/debug/tests/comptime_variant.vv b/vlib/v/debug/tests/comptime_variant.vv index a45c04a93305eb..b1be7fd8767c18 100644 --- a/vlib/v/debug/tests/comptime_variant.vv +++ b/vlib/v/debug/tests/comptime_variant.vv @@ -1,15 +1,15 @@ type MySum = int | string -fn comptime_variant_int() { - a := MySum(int(0)) - $for v in MySum.variants { - if a is v { +fn comptime_variant(arg MySum) { + $for v in arg.variants { + if arg is v { $dbg; - dump(a) + dump(arg) } } } fn main() { - comptime_variant_int() + comptime_variant(MySum(int(0))) + comptime_variant(MySum('foo')) } diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index 174023e59e4dcb..4d4758544de62c 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -3937,7 +3937,13 @@ fn (mut g Gen) debugger_stmt(node ast.DebuggerStmt) { if obj.name !in vars { if obj is ast.Var && obj.pos.pos < node.pos.pos { keys.write_string('_SLIT("${obj.name}")') - var_typ := if obj.smartcasts.len > 0 { obj.smartcasts.last() } else { obj.typ } + var_typ := if obj.ct_type_var != .no_comptime { + g.comptime.get_comptime_var_type(ast.Ident{ obj: obj }) + } else if obj.smartcasts.len > 0 { + obj.smartcasts.last() + } else { + obj.typ + } values.write_string('{.typ=_SLIT("${g.table.type_to_str(g.unwrap_generic(var_typ))}"),.value=') obj_sym := g.table.sym(obj.typ) cast_sym := g.table.sym(var_typ)