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

cgen: don't voidptr cast option/result functions (fix #17204) (#17207)

This commit is contained in:
Tim Marston 2023-02-03 06:50:20 +00:00 committed by GitHub
parent 6d63b27c26
commit 603469b856
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 52 additions and 3 deletions

View File

@ -2580,7 +2580,8 @@ fn (mut g Gen) expr_with_cast(expr ast.Expr, got_type_raw ast.Type, expected_typ
return
}
}
if exp_sym.kind == .function {
if exp_sym.kind == .function && !expected_type.has_flag(.option)
&& !expected_type.has_flag(.result) {
g.write('(voidptr)')
}
// no cast

View File

@ -37,11 +37,30 @@ pub fn bar2(i int) !int {
}
}
pub fn bar3(ok bool) ?fn () string {
return if ok {
fn () string {
return 'yes'
}
} else {
none
}
}
pub fn bar4(ok bool) !fn () string {
return if ok {
fn () string {
return 'yes'
}
} else {
error('no:error')
}
}
fn test_if_expr_nested_with_option_result() {
ret11 := bar1(0) or { 0 }
println(ret11)
assert ret11 == 2
ret12 := bar1(1) or { 0 }
println(ret12)
assert ret12 == 3
@ -49,8 +68,37 @@ fn test_if_expr_nested_with_option_result() {
ret21 := bar2(0) or { 0 }
println(ret21)
assert ret21 == 2
ret22 := bar2(1) or { 0 }
println(ret22)
assert ret22 == 3
ret31 := bar3(true) or {
fn () string {
return 'no:default'
}
}
println(ret31())
assert ret31() == 'yes'
ret32 := bar3(false) or {
fn () string {
return 'no:default'
}
}
println(ret32())
assert ret32() == 'no:default'
ret41 := bar4(true) or {
fn [err] () string {
return err.msg()
}
}
println(ret41())
assert ret41() == 'yes'
ret42 := bar4(false) or {
fn [err] () string {
return err.msg()
}
}
println(ret42())
assert ret42() == 'no:error'
}