Skip to content

Commit 603469b

Browse files
authored
cgen: don't voidptr cast option/result functions (fix #17204) (#17207)
1 parent 6d63b27 commit 603469b

File tree

2 files changed

+52
-3
lines changed

2 files changed

+52
-3
lines changed

vlib/v/gen/c/cgen.v

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2580,7 +2580,8 @@ fn (mut g Gen) expr_with_cast(expr ast.Expr, got_type_raw ast.Type, expected_typ
25802580
return
25812581
}
25822582
}
2583-
if exp_sym.kind == .function {
2583+
if exp_sym.kind == .function && !expected_type.has_flag(.option)
2584+
&& !expected_type.has_flag(.result) {
25842585
g.write('(voidptr)')
25852586
}
25862587
// no cast

vlib/v/tests/if_expr_nested_with_option_result_test.v

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,20 +37,68 @@ pub fn bar2(i int) !int {
3737
}
3838
}
3939

40+
pub fn bar3(ok bool) ?fn () string {
41+
return if ok {
42+
fn () string {
43+
return 'yes'
44+
}
45+
} else {
46+
none
47+
}
48+
}
49+
50+
pub fn bar4(ok bool) !fn () string {
51+
return if ok {
52+
fn () string {
53+
return 'yes'
54+
}
55+
} else {
56+
error('no:error')
57+
}
58+
}
59+
4060
fn test_if_expr_nested_with_option_result() {
4161
ret11 := bar1(0) or { 0 }
4262
println(ret11)
4363
assert ret11 == 2
44-
4564
ret12 := bar1(1) or { 0 }
4665
println(ret12)
4766
assert ret12 == 3
4867

4968
ret21 := bar2(0) or { 0 }
5069
println(ret21)
5170
assert ret21 == 2
52-
5371
ret22 := bar2(1) or { 0 }
5472
println(ret22)
5573
assert ret22 == 3
74+
75+
ret31 := bar3(true) or {
76+
fn () string {
77+
return 'no:default'
78+
}
79+
}
80+
println(ret31())
81+
assert ret31() == 'yes'
82+
ret32 := bar3(false) or {
83+
fn () string {
84+
return 'no:default'
85+
}
86+
}
87+
println(ret32())
88+
assert ret32() == 'no:default'
89+
90+
ret41 := bar4(true) or {
91+
fn [err] () string {
92+
return err.msg()
93+
}
94+
}
95+
println(ret41())
96+
assert ret41() == 'yes'
97+
ret42 := bar4(false) or {
98+
fn [err] () string {
99+
return err.msg()
100+
}
101+
}
102+
println(ret42())
103+
assert ret42() == 'no:error'
56104
}

0 commit comments

Comments
 (0)