Skip to content

Cgen: Failed to compile when passing a function option as a closure variable #26088

@Yv-vY

Description

@Yv-vY
V version: V 0.4.12 5d6f1a9, press to see full `v doctor` output
V full version V 0.4.12 caf7d41.5d6f1a9
OS linux, Linux version 6.17.12-300.fc43.x86_64 (mockbuild@008798550d8d49b8909ac02baf9be783) (gcc (GCC) 15.2.1 20251111 (Red Hat 15.2.1-4), GNU ld version 2.45.1-1.fc43) #1 SMP PREEMPT_DYNAMIC Sat Dec 13 05:06:24 UTC 2025
Processor 16 cpus, 64bit, little endian, AMD Ryzen 7 7800X3D 8-Core Processor
Memory 14.62GB/30.94GB
V executable /home/yann/Apps/v/v
V last modified time 2025-12-22 19:43:43
V home dir OK, value: /home/yann/Apps/v
VMODULES OK, value: /home/yann/.vmodules
VTMP OK, value: /tmp/v_1000
Current working dir OK, value: /home/yann/devel/perso/v/bug_function_opt_closure
Git version git version 2.52.0
V git status weekly.2025.43-377-g5d6f1a99
.git/config present true
cc version cc (GCC) 15.2.1 20251211 (Red Hat 15.2.1-5)
gcc version gcc (GCC) 15.2.1 20251211 (Red Hat 15.2.1-5)
clang version clang version 21.1.7 (Fedora 21.1.7-1.fc43)
tcc version tcc version 0.9.28rc 2025-02-13 HEAD@f8bd136d (x86_64 Linux)
tcc git status thirdparty-linux-amd64 696c1d84
emcc version N/A
glibc version ldd (GNU libc) 2.42

What did you do?
./v -g -o vdbg cmd/v && ./vdbg main.v && main

module main

fn f_a() {
	println('a')
}

type Fun = fn()

fn main() {
	opt := ?Fun(f_a)

	handler := fn [opt] () {
		if g := opt {
			g()
		}
	}

	handler()
}

What did you see?

================== C compilation error (from tcc): ==============
cc: /tmp/v_1000/main.01KD3WRW1GFPC36S7XHG2PDXDZ.tmp.c:850: error: cannot convert 'void (*)()' to 'struct _option_anon_fn_'
=================================================================
Try passing `-g` when compiling, to see a .v file:line information, that correlates more with the C error.
(Alternatively, pass `-show-c-output`, to print the full C error message).
builder error: 
==================
C error found. It should never happen, when compiling pure V code.
This is a V compiler bug, please report it using `v bug file.v`,
or goto https://github.com/vlang/v/issues/new/choose .
You can also use #help on Discord: https://discord.gg/vlang .

What did you expect to see?

Proper compilation of the program.

Investigation

The generated closure context struct is:

struct _V_anon_fn_aa4c3c73e99943ac__113_Ctx {
	void (*opt) ();
};

Instead the proper type of the opt struct member should be:

struct _V_anon_fn_aa4c3c73e99943ac__113_Ctx {
	_option_anon_fn_ opt;
};

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

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