From b6b96543e578286206bb6cd5ef57d2c28463262a Mon Sep 17 00:00:00 2001 From: yuyi Date: Thu, 5 Dec 2024 19:54:14 +0800 Subject: [PATCH] cgen: fix cast interface value in match expr (#23068) --- vlib/v/gen/c/cgen.v | 6 +++++ .../cast_interface_value_in_match_test.v | 24 +++++++++++++++++++ 2 files changed, 30 insertions(+) create mode 100644 vlib/v/tests/casts/cast_interface_value_in_match_test.v diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index b286228f492f22..a9340bd0e064ed 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -5302,6 +5302,12 @@ fn (mut g Gen) cast_expr(node ast.CastExpr) { g.expr_with_opt(node.expr, expr_type, sym.info.parent_type) } else { g.write('(${cast_label}(') + if node.expr is ast.Ident { + if !node.typ.is_ptr() && node.expr_type.is_ptr() && node.expr.obj is ast.Var + && node.expr.obj.smartcasts.len > 0 { + g.write('*'.repeat(node.expr_type.nr_muls())) + } + } if sym.kind == .alias && g.table.final_sym(node.typ).kind == .string { ptr_cnt := node.typ.nr_muls() - expr_type.nr_muls() if ptr_cnt > 0 { diff --git a/vlib/v/tests/casts/cast_interface_value_in_match_test.v b/vlib/v/tests/casts/cast_interface_value_in_match_test.v new file mode 100644 index 00000000000000..bf5abe82f4832b --- /dev/null +++ b/vlib/v/tests/casts/cast_interface_value_in_match_test.v @@ -0,0 +1,24 @@ +interface Number2 {} + +fn t(v Number2) { + match v { + int, i32, isize, i64 { + x := isize(v) + println(x) + assert x == 42 + } + else {} + } + match v { + int { + x := isize(v) + println(x) + assert x == 42 + } + else {} + } +} + +fn test_cast_interface_value_in_match() { + t(int(42)) +}