mirror of
https://github.com/vlang/v.git
synced 2025-09-14 09:56:16 -04:00
cgen: fix cast interface value in match expr (#23068)
This commit is contained in:
parent
af4ae51b8a
commit
b6b96543e5
@ -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 {
|
||||
|
24
vlib/v/tests/casts/cast_interface_value_in_match_test.v
Normal file
24
vlib/v/tests/casts/cast_interface_value_in_match_test.v
Normal file
@ -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))
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user