From 30cb3213c0df43f70a43f697996d13fcddc6b0d2 Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Thu, 26 Mar 2026 00:12:47 +0300 Subject: [PATCH] cgen: fix interface with method str() returned as option (fixes #25931) --- vlib/v/gen/c/str_intp.v | 3 +- ...tion_interface_string_interpolation_test.v | 31 +++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 vlib/v/tests/printing/dump_option_interface_string_interpolation_test.v diff --git a/vlib/v/gen/c/str_intp.v b/vlib/v/gen/c/str_intp.v index cc8c872cb..0a8e5ac27 100644 --- a/vlib/v/gen/c/str_intp.v +++ b/vlib/v/gen/c/str_intp.v @@ -297,7 +297,8 @@ fn (mut g Gen) str_val(node ast.StringInterLiteral, i int, fmts []u8) { } g.expr(expr) } - } else if typ_sym.kind == .interface && (typ_sym.info as ast.Interface).defines_method('str') { + } else if !typ.has_option_or_result() && typ_sym.kind == .interface + && (typ_sym.info as ast.Interface).defines_method('str') { rec_type_name := util.no_dots(g.cc_type(typ, false)) g.write('${c_name(rec_type_name)}_name_table[') g.expr(expr) diff --git a/vlib/v/tests/printing/dump_option_interface_string_interpolation_test.v b/vlib/v/tests/printing/dump_option_interface_string_interpolation_test.v new file mode 100644 index 000000000..7fd35e582 --- /dev/null +++ b/vlib/v/tests/printing/dump_option_interface_string_interpolation_test.v @@ -0,0 +1,31 @@ +interface Nested { + str() string +} + +struct Leaf { + name string +} + +fn (l Leaf) str() string { + return l.name +} + +fn denest_none() ?Nested { + return none +} + +fn denest_value() ?Nested { + return Leaf{'leaf'} +} + +fn test_dump_string_interpolation_of_option_interface_none() { + n := denest_none() + s := dump('${n}') + assert s == 'Option(none)' +} + +fn test_dump_string_interpolation_of_option_interface_value() { + n := denest_value() + s := dump('${n}') + assert s == 'Option(Nested(leaf))' +} -- 2.39.5