From 7e933328b6268cfc37d4d9734f03638193040c83 Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Mon, 6 Oct 2025 04:40:01 -0300 Subject: [PATCH] cgen: fix sumtype alias cast to same sumtype (fix #25431) (#25443) --- vlib/v/gen/c/cgen.v | 4 ++++ .../v/tests/sumtypes/sumtype_cast_alias_test.v | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+) create mode 100644 vlib/v/tests/sumtypes/sumtype_cast_alias_test.v diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index 76d4c3bea..e5b949a7a 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -5595,6 +5595,10 @@ fn (mut g Gen) cast_expr(node ast.CastExpr) { } node_typ_is_option := node.typ.has_flag(.option) if sym.kind in [.sum_type, .interface] { + if g.table.unaliased_type(expr_type) == node_typ { + g.expr(node.expr) + return + } if node_typ_is_option && node.expr is ast.None { g.gen_option_error(node.typ, node.expr) } else if node.expr is ast.Ident && g.comptime.is_comptime_variant_var(node.expr) { diff --git a/vlib/v/tests/sumtypes/sumtype_cast_alias_test.v b/vlib/v/tests/sumtypes/sumtype_cast_alias_test.v new file mode 100644 index 000000000..77503bf8f --- /dev/null +++ b/vlib/v/tests/sumtypes/sumtype_cast_alias_test.v @@ -0,0 +1,18 @@ +struct Number {} + +struct String {} + +struct Variable {} + +struct FunctionCall {} + +type Value = Number | String | Variable | FunctionCall +type Statement = Value + +fn test_main() { + s := Value(Statement(Value(Number{}))) + assert '${s}' == 'Value(Number{})' + + s2 := Statement(Value(Number{})) + assert '${s2}' == 'Statement(Value(Number{}))' +} -- 2.39.5