-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Description
Describe the bug
Calling a function pointer obtained from a ?&Fn (optional pointer to function) generates invalid C code when unwrapped via if guards or iterator loops for x in iter.
The C generator creates a double pointer void (**)(void) but attempts a direct call p(), causing a function pointer expected error. But unwrapping via or { return } works correctly.
Reproduction Steps
The if guard version compared to or { return } version:
type MyFn = fn()
fn hello() {}
fn get_ptr() ?&MyFn {
return &hello
}
fn main() {
if p := get_ptr() {
p() // error: function pointer expected
}
p2 := get_ptr() or { return } // works
p2() // OK
}The iterator fails either:
type MyFn = fn()
struct Iter {
f MyFn = fn(){}
mut:
done bool
}
fn (mut it Iter) next() ?&MyFn {
if it.done { return none }
it.done = true
return &it.f
}
fn main() {
for p in Iter{} {
p() // error: function pointer expected
}
}Expected Behavior
The if guard and iterator versions should compile and run.
Current Behavior
C compilation fails: error: function pointer expected
Possible Solution
No response
Additional Information/Context
No response
V version
V 0.4.12 ecd0018
Environment details (OS name and version, etc.)
V 0.4.12 ecd0018
Note
You can use the 👍 reaction to increase the issue's priority for developers.
Please note that only the 👍 reaction to the issue itself counts as a vote.
Other reactions and those to comments will not be taken into account.