cgen: fix cast interface value in match expr (#23068)

This commit is contained in:
yuyi 2024-12-05 19:54:14 +08:00 committed by GitHub
parent af4ae51b8a
commit b6b96543e5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 30 additions and 0 deletions

View File

@ -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 {

View 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))
}