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

cgen: fix code generated to option fn (fix #17604) (#17614)

This commit is contained in:
Felipe Pena 2023-03-13 06:56:13 -03:00 committed by GitHub
parent 4ba7ad9446
commit d00237f02c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 48 additions and 1 deletions

View File

@ -571,7 +571,7 @@ fn (mut g Gen) fn_decl_params(params []ast.Param, scope &ast.Scope, is_variadic
}
param_type_sym := g.table.sym(typ)
mut param_type_name := g.typ(typ) // util.no_dots(param_type_sym.name)
if param_type_sym.kind == .function {
if param_type_sym.kind == .function && !typ.has_flag(.option) {
info := param_type_sym.info as ast.FnType
func := info.func
g.write('${g.typ(func.return_type)} (*${caname})(')

View File

@ -0,0 +1,47 @@
type HandlerFn = fn ()
fn new_static_router(route_not_found_handler ?fn ()) fn () {
return route_not_found_handler or {
fn () {}
}
}
fn new_static_router2(route_not_found_handler ?HandlerFn) fn () {
return route_not_found_handler or {
fn () {}
}
}
fn test_option_fn_alias_decl_with_none() {
b := new_static_router2(none)
$if b is $Function {
assert true
} $else {
assert false
}
}
fn test_option_fn_decl_with_none() {
a := new_static_router(none)
$if a is $Function {
assert true
} $else {
assert false
}
}
fn test_option_fn_passing_normal() {
anon_1 := fn () {
println(1)
}
c := new_static_router(anon_1)
assert c == anon_1
}
fn test_option_fn_passing_to_alias() {
anon_2 := fn () {
println(2)
}
d := new_static_router(anon_2)
assert d == anon_2
}