Skip to content

Call to unwrapped ?&Fn fails in if guard and iterator loops #25911

@wenxuanjun

Description

@wenxuanjun

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.

Metadata

Metadata

Assignees

Labels

BugThis tag is applied to issues which reports bugs.Status: ConfirmedThis bug has been confirmed to be valid by a contributor.Unit: cgenBugs/feature requests, that are related to the default C generating backend.

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions