1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

cgen: fix passing sumtype parameter in sumtype matching results (fix #15078) (#15767)

This commit is contained in:
yuyi 2022-09-16 15:16:40 +08:00 committed by GitHub
parent f922ed0941
commit 0e49ce427e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 43 additions and 1 deletions

View File

@ -2306,7 +2306,7 @@ fn (mut g Gen) expr_with_cast(expr ast.Expr, got_type_raw ast.Type, expected_typ
scope := g.file.scope.innermost(expr.pos().pos)
if expr is ast.Ident {
if v := scope.find_var(expr.name) {
if v.smartcasts.len > 0 {
if v.smartcasts.len > 0 && unwrapped_expected_type == v.orig_type {
is_already_sum_type = true
}
}

View File

@ -1594,6 +1594,23 @@ fn (mut g Gen) call_args(node ast.CallExpr) {
if is_variadic && i == expected_types.len - 1 {
break
}
if arg.expr is ast.Ident {
if arg.expr.obj is ast.Var {
if arg.expr.obj.smartcasts.len > 0 {
exp_sym := g.table.sym(expected_types[i])
orig_sym := g.table.sym(arg.expr.obj.orig_type)
if orig_sym.kind != .interface_ && (exp_sym.kind != .sum_type
|| (exp_sym.kind == .sum_type
&& expected_types[i] != arg.expr.obj.orig_type)) {
expected_types[i] = g.unwrap_generic(arg.expr.obj.smartcasts.last())
cast_sym := g.table.sym(expected_types[i])
if cast_sym.info is ast.Aggregate {
expected_types[i] = cast_sym.info.types[g.aggregate_type_idx]
}
}
}
}
}
use_tmp_var_autofree := g.is_autofree && arg.typ == ast.string_type && arg.is_tmp_autofree
&& !g.inside_const && !g.is_builtin_mod
// g.write('/* af=$arg.is_tmp_autofree */')

View File

@ -0,0 +1,25 @@
struct Foo {}
type Bar = Foo | int
type Baz = Foo | f32
fn foo(a Bar) int {
return 22
}
fn bar() Baz {
return Foo{}
}
fn test_passing_sumtype_parameter_in_sumtype_matching_results() {
a := bar()
mut ret := 0
match a {
Foo {
ret = foo(Bar(a))
}
f32 {}
}
println(ret)
assert ret == 22
}