Skip to content

C compiler error when using Option types with Generics #25093

Description

@raw-bin

Describe the bug

Hi.

Please consider the following example:

module main

@[heap]
struct FLQueue[T] {
mut:
	inner [8]?T
}

fn (mut f FLQueue[T]) put[T](element T) {
	for index, item in f.inner {
		if item == none {
			f.inner[index] = element
			return
		}
	}
	panic('put: No free slot')
}

fn (mut f FLQueue[T]) get[T]() ?T {
	for item in f.inner {
		if item != none {
			return item
		}
	}
	return none
}

@[heap]
struct Thread {
	stack [1024]u8
}

fn main() {
	mut q := FLQueue[Thread]{}
	t := Thread{}
	q.put(t)
	p := q.get()
	println(p)
}

This produces the following complaint:

❱ ~/Work/repos/vlang/v/v run .
================== C compilation error (from cc): ==============
cc: /tmp/v_501/queue0.01K2EJCPGGXFNCHJN7N5VGQ0VA.tmp.c:1002:43: error: array has incomplete element type '_option_T' (aka 'struct _option_T')
cc:  1002 | typedef _option_T Array_fixed__option_T_8 [8];
cc:       |                                           ^
cc: /tmp/v_501/queue0.01K2EJCPGGXFNCHJN7N5VGQ0VA.tmp.c:52:16: note: forward declaration of 'struct _option_T'
cc:    52 | typedef struct _option_T _option_T;
cc:       |                ^
cc: /tmp/v_501/queue0.01K2EJCPGGXFNCHJN7N5VGQ0VA.tmp.c:1013:30: warning: redefinition of typedef 'Array_fixed__option_main__Thread_8' is a C11 feature [-Wtypedef-redefinition]
cc:  1013 | typedef _option_main__Thread Array_fixed__option_main__Thread_8 [8];
cc:       |                              ^
cc: /tmp/v_501/queue0.01K2EJCPGGXFNCHJN7N5VGQ0VA.tmp.c:1012:30: note: previous definition is here
cc:  1012 | typedef _option_main__Thread Array_fixed__option_main__Thread_8 [8];
cc:       |                              ^
...
cc: 1 warning and 4 errors generated.
(note: the original output was 23 lines long; it was truncated to its first 12 lines + the last line)
================================================================
(You can pass `-cg`, or `-show-c-output` as well, to print all the C error messages).
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 .

I think that the usage of options and generics in the example is legal but I may be mistaken.

Any help appreciated.

Thanks

Reproduction Steps

Please see above.

Expected Behavior

Correct compilation and operation as per the example.

Current Behavior

Please see above.

Possible Solution

This appears to be a codegen issue but I am not sure.

Additional Information/Context

No response

V version

V 0.4.11 d5458b5

Environment details (OS name and version, etc.)

❱ ~/Work/repos/vlang/v/v doctor
|V full version      |V 0.4.11 963e3e8ae22c1aa270ed08a5e68855d3de5f1460.d5458b5
|:-------------------|:-------------------
|OS                  |macos, macOS, 15.6, 24G84
|Processor           |10 cpus, 64bit, little endian, Apple M4
|Memory              |0.37GB/32GB
|                    |
|V executable        |/Users/robin/Work/repos/vlang/v/v
|V last modified time|2025-08-12 07:26:31
|                    |
|V home dir          |OK, value: /Users/robin/Work/repos/vlang/v
|VMODULES            |OK, value: /Users/robin/.vmodules
|VTMP                |OK, value: /tmp/v_501
|Current working dir |OK, value: /Users/robin/Work/personal/languages/vlang/experiments/queue0
|                    |
|env LDFLAGS         |"-L/opt/homebrew/opt/zlib/lib -L/opt/homebrew/opt/binutils/lib -L/opt/homebrew/opt/zlib/lib -L/opt/homebrew/opt/binutils/lib"
|Git version         |git version 2.50.1
|V git status        |weekly.2025.31-82-gd5458b58
|.git/config present |true
|                    |
|cc version          |Apple clang version 17.0.0 (clang-1700.0.13.5)
|gcc version         |Apple clang version 17.0.0 (clang-1700.0.13.5)
|clang version       |Homebrew clang version 20.1.8
|tcc version         |tcc version 0.9.28rc 2024-02-05 HEAD@105d70f7 (AArch64 Darwin)
|tcc git status      |thirdparty-macos-arm64 e447816c
|emcc version        |N/A
|glibc version       |N/A
❱ neofetch
                    'c.          robin@feather.local
                 ,xNMM.          -------------------
               .OMMMMo           OS: macOS 15.6 24G84 arm64
               OMMM0,            Host: Mac16,12
     .;loddo:' loolloddol;.      Kernel: 24.6.0
   cKMMMMMMMMMMNWMMMMMMMMMM0:    Uptime: 9 days, 22 hours, 59 mins
 .KMMMMMMMMMMMMMMMMMMMMMMMWd.    Packages: 121 (brew)
 XMMMMMMMMMMMMMMMMMMMMMMMX.      Shell: fish 4.0.2
;MMMMMMMMMMMMMMMMMMMMMMMM:       Resolution: 1470x956
:MMMMMMMMMMMMMMMMMMMMMMMM:       DE: Aqua
.MMMMMMMMMMMMMMMMMMMMMMMMX.      WM: Rectangle
 kMMMMMMMMMMMMMMMMMMMMMMMMWd.    Terminal: tmux
 .XMMMMMMMMMMMMMMMMMMMMMMMMMMk   CPU: Apple M4
  .XMMMMMMMMMMMMMMMMMMMMMMMMK.   GPU: Apple M4
    kMMMMMMMMMMMMMMMMMMMMMMd     Memory: 4877MiB / 32768MiB
     ;KMMMMMMMWXXWMMMMMMMk.
       .cooc,.    .,coo:.

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.Generics[T]Bugs/feature requests, that are related to the V generics.Option TypeBugs/feature requests, that are related to `?Type`.

Type

No fields configured for Bug.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions