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

cgen: fix fn call of sumtype with alias fntype variant (#16519)

This commit is contained in:
yuyi 2022-11-24 18:05:57 +08:00 committed by GitHub
parent 8b5c75c481
commit f0a23c8d3c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 33 additions and 27 deletions

View File

@ -2281,25 +2281,23 @@ fn (mut g Gen) write_sumtype_casting_fn(fun SumtypeCastingFn) {
mut sb := strings.new_builder(128)
mut is_anon_fn := false
if got_sym.info is ast.FnType {
if got_sym.info.is_anon || g.table.known_fn(got_sym.name) {
got_name := 'fn ${g.table.fn_type_source_signature(got_sym.info.func)}'
got_cname = 'anon_fn_${g.table.fn_type_signature(got_sym.info.func)}'
type_idx = g.table.type_idxs[got_name].str()
exp_info := exp_sym.info as ast.SumType
for variant in exp_info.variants {
variant_sym := g.table.sym(variant)
if variant_sym.info is ast.FnType {
if g.table.fn_type_source_signature(variant_sym.info.func) == g.table.fn_type_source_signature(got_sym.info.func) {
got_cname = variant_sym.cname
type_idx = variant.idx().str()
break
}
got_name := 'fn ${g.table.fn_type_source_signature(got_sym.info.func)}'
got_cname = 'anon_fn_${g.table.fn_type_signature(got_sym.info.func)}'
type_idx = g.table.type_idxs[got_name].str()
exp_info := exp_sym.info as ast.SumType
for variant in exp_info.variants {
variant_sym := g.table.sym(variant)
if variant_sym.info is ast.FnType {
if g.table.fn_type_source_signature(variant_sym.info.func) == g.table.fn_type_source_signature(got_sym.info.func) {
got_cname = variant_sym.cname
type_idx = variant.idx().str()
break
}
}
sb.writeln('static inline ${exp_cname} ${fun.fn_name}(${got_cname} x) {')
sb.writeln('\t${got_cname} ptr = x;')
is_anon_fn = true
}
sb.writeln('static inline ${exp_cname} ${fun.fn_name}(${got_cname} x) {')
sb.writeln('\t${got_cname} ptr = x;')
is_anon_fn = true
}
if !is_anon_fn {
sb.writeln('static inline ${exp_cname} ${fun.fn_name}(${got_cname}* x) {')
@ -2436,8 +2434,8 @@ fn (mut g Gen) expr_with_cast(expr ast.Expr, got_type_raw ast.Type, expected_typ
if got_sym.info is ast.FnType {
if got_sym.info.is_anon {
got_styp = 'anon_fn_${g.table.fn_type_signature(got_sym.info.func)}'
got_is_fn = true
}
got_is_fn = true
}
if expected_type != ast.void_type {
unwrapped_expected_type := g.unwrap_generic(expected_type)

View File

@ -1452,8 +1452,9 @@ fn (mut g Gen) fn_call(node ast.CallExpr) {
}
}
if obj.smartcasts.len > 0 && is_cast_needed {
for _ in obj.smartcasts {
g.write('(*')
for typ in obj.smartcasts {
sym := g.table.sym(typ)
g.write('(*(${sym.cname})(')
}
for i, typ in obj.smartcasts {
cast_sym := g.table.sym(g.unwrap_generic(typ))
@ -1471,7 +1472,7 @@ fn (mut g Gen) fn_call(node ast.CallExpr) {
} else {
g.write('${dot}_${cast_sym.cname}')
}
g.write(')')
g.write('))')
}
is_fn_var = true
} else if obj.is_inherited {

View File

@ -11,18 +11,25 @@ fn abc(i int) int {
return i
}
// create Myfn
fn myfnfact() Myfn {
return abc
}
// run fn if exists
fn run(mmff Maybefnfact) string {
match mmff {
Myfnfact { return 'yes fn' }
None { return 'None fn' }
Myfnfact {
r := mmff()
return 'yes fn: ${r}'
}
None {
return 'None fn'
}
}
}
fn test_sumtype_with_alias_fntype() {
// create Myfn
myfnfact := fn () Myfn {
return abc
}
r1 := main.myfnfact()(1)
println(r1)
assert r1 == 1
@ -33,5 +40,5 @@ fn test_sumtype_with_alias_fntype() {
r3 := run(myfnfact)
println(r3)
assert r3 == 'yes fn'
assert r3 == 'yes fn: fn (int) int'
}