Skip to content

Commit

Permalink
cgen: fix dump of alias to option fn type (fix vlang#22670) (vlang#22676
Browse files Browse the repository at this point in the history
)
  • Loading branch information
felipensp authored Oct 28, 2024
1 parent a5d1229 commit 986ed33
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
14 changes: 10 additions & 4 deletions vlib/v/gen/c/dumpexpr.v
Original file line number Diff line number Diff line change
Expand Up @@ -106,20 +106,26 @@ fn (mut g Gen) dump_expr_definitions() {
is_ptr := typ.is_ptr()
deref, _ := deref_kind(str_method_expects_ptr, is_ptr, typ)
to_string_fn_name := g.get_str_fn(typ.clear_flags(.shared_f, .result))
is_option := typ.has_option_or_result()
mut ptr_asterisk := if is_ptr { '*'.repeat(typ.nr_muls()) } else { '' }
mut str_dumparg_type := ''
mut str_dumparg_ret_type := ''
if dump_sym.kind == .none {
str_dumparg_type = 'IError' + ptr_asterisk
} else if dump_sym.kind == .function {
if is_option {
ptr_asterisk = ptr_asterisk.replace('*', '_ptr')
}
str_dumparg_type += g.styp(typ).replace('*', '') + ptr_asterisk
} else {
if typ.has_flag(.option) {
if is_option {
str_dumparg_type += '_option_'
ptr_asterisk = ptr_asterisk.replace('*', '_ptr')
}
str_dumparg_type += g.cc_type(typ, true) + ptr_asterisk
}
mut is_fixed_arr_ret := false
if dump_sym.kind == .function {
if dump_sym.kind == .function && !is_option {
fninfo := dump_sym.info as ast.FnType
str_dumparg_type = 'DumpFNType_${name}'
tdef_pos := g.out.len
Expand All @@ -128,7 +134,7 @@ fn (mut g Gen) dump_expr_definitions() {
g.go_back(str_tdef.len)
dump_typedefs['typedef ${str_tdef};'] = true
str_dumparg_ret_type = str_dumparg_type
} else if !typ.has_option_or_result() && dump_sym.is_array_fixed() {
} else if !is_option && dump_sym.is_array_fixed() {
match dump_sym.kind {
.array_fixed {
if (dump_sym.info as ast.ArrayFixed).is_fn_ret {
Expand Down Expand Up @@ -172,7 +178,7 @@ fn (mut g Gen) dump_expr_definitions() {
}
mut surrounder := util.new_surrounder(3)
surrounder.add('\tstring sline = int_str(line);', '\tstring_free(&sline);')
if dump_sym.kind == .function {
if dump_sym.kind == .function && !is_option {
surrounder.add('\tstring value = ${to_string_fn_name}();', '\tstring_free(&value);')
} else if dump_sym.kind == .none {
surrounder.add('\tstring value = _SLIT("none");', '\tstring_free(&value);')
Expand Down
12 changes: 12 additions & 0 deletions vlib/v/tests/options/option_fn_alias_test.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
type Foo = fn (bar string)

fn test_main() {
mut func := ?Foo(none)
res := dump(func)
assert res == none

mut func2 := ?Foo(fn (bar string) {})
res2 := dump(func2)
assert res2 != none
assert res2?.str() == 'fn (string)'
}

0 comments on commit 986ed33

Please sign in to comment.