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

cgen: fix anon optional fn (fix #10640) (#10644)

This commit is contained in:
yuyi 2021-07-02 22:07:05 +08:00 committed by GitHub
parent 8628b19a3a
commit c0f855ace7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 47 additions and 4 deletions

View File

@ -198,7 +198,8 @@ pub fn (t &Table) fn_type_signature(f &Fn) string {
} }
if f.return_type != 0 && f.return_type != void_type { if f.return_type != 0 && f.return_type != void_type {
sym := t.get_type_symbol(f.return_type) sym := t.get_type_symbol(f.return_type)
sig += '__$sym.kind' opt := if f.return_type.has_flag(.optional) { 'option_' } else { '' }
sig += '__$opt$sym.kind'
} }
return sig return sig
} }
@ -225,7 +226,11 @@ pub fn (t &Table) fn_type_source_signature(f &Fn) string {
sig += ' ?' sig += ' ?'
} else if f.return_type != void_type { } else if f.return_type != void_type {
return_type_sym := t.get_type_symbol(f.return_type) return_type_sym := t.get_type_symbol(f.return_type)
sig += ' $return_type_sym.name' if f.return_type.has_flag(.optional) {
sig += ' ?$return_type_sym.name'
} else {
sig += ' $return_type_sym.name'
}
} }
return sig return sig
} }

View File

@ -433,9 +433,15 @@ fn (mut g Gen) fn_decl_str(info ast.FnType) string {
fn_str += util.strip_main_name(g.table.get_type_name(g.unwrap_generic(arg.typ))) fn_str += util.strip_main_name(g.table.get_type_name(g.unwrap_generic(arg.typ)))
} }
fn_str += ')' fn_str += ')'
if info.func.return_type != ast.void_type { if info.func.return_type == ast.ovoid_type {
fn_str += ' ?'
} else if info.func.return_type != ast.void_type {
x := util.strip_main_name(g.table.get_type_name(g.unwrap_generic(info.func.return_type))) x := util.strip_main_name(g.table.get_type_name(g.unwrap_generic(info.func.return_type)))
fn_str += ' $x' if info.func.return_type.has_flag(.optional) {
fn_str += ' ?$x'
} else {
fn_str += ' $x'
}
} }
return fn_str return fn_str
} }

View File

@ -0,0 +1,32 @@
struct Response {
ret int
}
pub struct Route {
handler fn (mut App) Response
middlewares []fn (mut app App) ?Response
}
struct App {
routes []Route
}
pub fn check_auth(mut app App) ?Response {
return none
}
fn test_anon_fn_with_optional() {
app := App{
routes: [
Route{
middlewares: [
check_auth,
]
},
]
}
println(app)
app_str := '$app'
assert app_str.contains('handler: fn (mut App) Response')
assert app_str.contains('middlewares: [fn (mut App) ?Response]')
}