From 135e2f1cc6dd281dba85dd34b99c22d60bd4270e Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Sun, 21 Jan 2024 02:53:30 -0300 Subject: [PATCH] debug: fix variable dereferencing (#20594) --- vlib/v/debug/tests/mut_sumtype.expect | 13 ++++++++++ vlib/v/debug/tests/mut_sumtype.vv | 14 +++++++++++ vlib/v/debug/tests/option.expect | 35 +++++++++++++++++++++++++++ vlib/v/debug/tests/option.vv | 28 +++++++++++++++++++++ vlib/v/gen/c/cgen.v | 15 +++++++++--- 5 files changed, 101 insertions(+), 4 deletions(-) create mode 100644 vlib/v/debug/tests/mut_sumtype.expect create mode 100644 vlib/v/debug/tests/mut_sumtype.vv create mode 100644 vlib/v/debug/tests/option.expect create mode 100644 vlib/v/debug/tests/option.vv diff --git a/vlib/v/debug/tests/mut_sumtype.expect b/vlib/v/debug/tests/mut_sumtype.expect new file mode 100644 index 00000000000000..eba184d25e0f73 --- /dev/null +++ b/vlib/v/debug/tests/mut_sumtype.expect @@ -0,0 +1,13 @@ +#!/usr/bin/env expect +source "common.tcl" + +expect "Break on *main* test in ${test_file}:8\r\n" +expect "${test_file}:8 vdbg> " +send "p f\n" +expect "f = Test(Test2{}) (main.Test)" +expect "${test_file}:8 vdbg> " +send "p t\n" +expect "t = Test(Test2{}) (&main.Test)" +expect "${test_file}:8 vdbg> " +send "q\n" +expect eof diff --git a/vlib/v/debug/tests/mut_sumtype.vv b/vlib/v/debug/tests/mut_sumtype.vv new file mode 100644 index 00000000000000..6448e8eb859070 --- /dev/null +++ b/vlib/v/debug/tests/mut_sumtype.vv @@ -0,0 +1,14 @@ +struct Test2 {} + +struct Test3 {} + +type Test = Test2 | Test3 + +fn (f Test) test(mut t Test) { + $dbg; +} + +fn main() { + mut a := Test(Test2{}) + a.test(mut a) +} diff --git a/vlib/v/debug/tests/option.expect b/vlib/v/debug/tests/option.expect new file mode 100644 index 00000000000000..d6068cf29b51bb --- /dev/null +++ b/vlib/v/debug/tests/option.expect @@ -0,0 +1,35 @@ +#!/usr/bin/env expect +source "common.tcl" + +expect "Break on *main* test in ${test_file}:8\r\n" +expect "${test_file}:8 vdbg> " +send "p f\n" +expect "f = Test(Test2{}) (main.Test)" +expect "${test_file}:8 vdbg> " +send "p t\n" +expect "t = Option(Test(Test2{})) (?main.Test)" +expect "${test_file}:8 vdbg> " +send "c\n" + +expect "${test_file}:16 vdbg> " +send "p a\n" +expect "a = Option(none) (?int)" +expect "${test_file}:16 vdbg> " +send "c\n" + +expect "${test_file}:16 vdbg> " +send "p a\n" +expect "a = Option(1) (?int)" +expect "${test_file}:16 vdbg> " +send "c\n" + +expect "${test_file}:12 vdbg> " +send "p f\n" +expect "f = Test(Test2{}) (main.Test)" +expect "${test_file}:12 vdbg> " +send "p t\n" +expect "t = Option(none) (?&main.Test)" + +expect "${test_file}:12 vdbg> " +send "q\n" +expect eof diff --git a/vlib/v/debug/tests/option.vv b/vlib/v/debug/tests/option.vv new file mode 100644 index 00000000000000..15ac3e12eb919c --- /dev/null +++ b/vlib/v/debug/tests/option.vv @@ -0,0 +1,28 @@ +struct Test2 {} + +struct Test3 {} + +type Test = Test2 | Test3 + +fn (f Test) test(t ?Test) { + $dbg; +} + +fn (f Test) test2(t ?&Test) { + $dbg; +} + +fn test_int(a ?int) { + $dbg; +} + +fn main() { + mut a := ?Test(Test2{}) + a?.test(a) + + test_int(?int(none)) + test_int(?int(1)) + + b := ?&Test(none) + a?.test2(b) +} diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index 4d4758544de62c..6fc8bef4518289 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -4012,14 +4012,21 @@ fn (mut g Gen) debugger_stmt(node ast.DebuggerStmt) { values.write_string('${func}(*(${base_typ}*)${obj.name}.data)}') } else { _, str_method_expects_ptr, _ := cast_sym.str_method_info() - deref := if str_method_expects_ptr && !obj.typ.is_ptr() { + + // eprintln(">> ${obj.name} | str expects ptr? ${str_method_expects_ptr} | ptr? ${var_typ.is_ptr()} || auto heap? ${obj.is_auto_heap} | auto deref? ${obj.is_auto_deref}") + deref := if var_typ.has_flag(.option) { + '' + } else if str_method_expects_ptr && !obj.typ.is_ptr() { '&' + } else if obj.is_auto_heap && var_typ.is_ptr() && str_method_expects_ptr { + '*' + } else if !obj.is_auto_heap && var_typ.is_ptr() && str_method_expects_ptr { + '' + } else if obj.is_auto_heap && var_typ.is_ptr() { + '*' } else if obj.typ.is_ptr() && !obj.is_auto_deref { '&' } else if obj.typ.is_ptr() && obj.is_auto_deref { - '' - } else if obj.is_auto_heap - || (!var_typ.has_flag(.option) && var_typ.is_ptr()) { '*' } else { ''