Skip to content

Commit

Permalink
cgen: fix cast interface value in match expr (vlang#23068)
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyi98 authored Dec 5, 2024
1 parent af4ae51 commit b6b9654
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
6 changes: 6 additions & 0 deletions vlib/v/gen/c/cgen.v
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
24 changes: 24 additions & 0 deletions vlib/v/tests/casts/cast_interface_value_in_match_test.v
Original file line number Diff line number Diff line change
@@ -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))
}

0 comments on commit b6b9654

Please sign in to comment.