From 9c4dfd3848089a2a003a2391ea4a78367c941d0f Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Mon, 22 Sep 2025 05:31:29 -0300 Subject: [PATCH] cgen: fix option unwrap with string interpolation (fix #25337) (#25367) --- vlib/v/gen/c/str_intp.v | 3 +++ vlib/v/markused/walker.v | 3 +++ .../v/tests/options/option_unwrap_none_test.v | 19 +++++++++++++++++++ 3 files changed, 25 insertions(+) create mode 100644 vlib/v/tests/options/option_unwrap_none_test.v diff --git a/vlib/v/gen/c/str_intp.v b/vlib/v/gen/c/str_intp.v index 609b5b9f4..4dfaa70a4 100644 --- a/vlib/v/gen/c/str_intp.v +++ b/vlib/v/gen/c/str_intp.v @@ -225,6 +225,9 @@ fn (mut g Gen) str_val(node ast.StringInterLiteral, i int, fmts []u8) { if cast_sym.info is ast.Aggregate { exp_typ = cast_sym.info.types[g.aggregate_type_idx] } + if exp_typ.has_flag(.option) && expr.obj.is_unwrapped { + exp_typ = exp_typ.clear_flag(.option) + } } } } diff --git a/vlib/v/markused/walker.v b/vlib/v/markused/walker.v index 6b7bfc48f..f45adbef9 100644 --- a/vlib/v/markused/walker.v +++ b/vlib/v/markused/walker.v @@ -716,6 +716,9 @@ fn (mut w Walker) expr(node_ ast.Expr) { } } } + if node.obj is ast.Var && node.obj.is_unwrapped { + w.used_option++ + } w.or_block(node.or_expr) } ast.LambdaExpr { diff --git a/vlib/v/tests/options/option_unwrap_none_test.v b/vlib/v/tests/options/option_unwrap_none_test.v new file mode 100644 index 000000000..fca85a62c --- /dev/null +++ b/vlib/v/tests/options/option_unwrap_none_test.v @@ -0,0 +1,19 @@ +fn fake_prompt[T](msg string, default ?T) string { + return build_prompt(msg, default) +} + +fn build_prompt[T](msg string, default ?T) string { + mut s := msg + if default != none { + s += ' [${default}]' + } + s += ': ' + return s +} + +fn test_main() { + p1 := fake_prompt[string]('hello', none) + p2 := fake_prompt[string]('hello', 'world') + assert p1 == 'hello: ' + assert p2 == 'hello [world]: ' +} -- 2.39.5