You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A generic function that walks the fields of a generic struct containing ?T, ?[]T and ?[N]T fields (where T is the generic parameter) generates invalid C: the codegen emits _option_string and _option_Array_string initializers as if they were raw byte arrays, producing both cannot convert 'struct _option_string' to 'unsigned char *' and incompatible types when initializing 'u8 *' using '_option_Array_string' errors.
Compiles. The comptime walker visits each field; the per-field generic gets instantiated for ?string, ?[]string, ?[1]string respectively.
Current Behavior
================== C compilation error ==============
error: cannot convert 'struct _option_string' to 'unsigned char *'
builtin___option_ok(&(Array_fixed_string_1[]) {val }, (_option*)(&_t1), sizeof(Array_fixed_string_1));
^~~
error: incompatible types when initializing type'u8 *' using type'_option_Array_string'
builtin___option_ok(&(Array_fixed_string_1[]) {val }, (_option*)(&_t1), sizeof(Array_fixed_string_1));
^~~
=====================================================
builder error: C error found while compiling generated C code.
Possible Solution
When the comptime $for f in T.fields walker over a generic struct sees field.typ of ?T, ?[]T or ?[N]T, the option's payload-init codegen drops the array layer somewhere on the way from field.typ resolution to the _option_X C struct emission. The result is that _option_string gets initialized through the path that expects an [N]T payload, leading to Array_fixed_string_1[] being assigned a struct value.
The fix is in the comptime/cgen layer that monomorphizes generic structs with option-of-array fields: produce a payload struct whose layout matches the option's payload type, not the surrounding fixed-array shape.
Describe the bug
A generic function that walks the fields of a generic struct containing
?T,?[]Tand?[N]Tfields (whereTis the generic parameter) generates invalid C: the codegen emits_option_stringand_option_Array_stringinitializers as if they were raw byte arrays, producing bothcannot convert 'struct _option_string' to 'unsigned char *'andincompatible types when initializing 'u8 *' using '_option_Array_string'errors.Reproduction Steps
Expected Behavior
Compiles. The comptime walker visits each field; the per-field generic gets instantiated for
?string,?[]string,?[1]stringrespectively.Current Behavior
Possible Solution
When the comptime
$for f in T.fieldswalker over a generic struct seesfield.typof?T,?[]Tor?[N]T, the option's payload-init codegen drops the array layer somewhere on the way fromfield.typresolution to the_option_XC struct emission. The result is that_option_stringgets initialized through the path that expects an[N]Tpayload, leading toArray_fixed_string_1[]being assigned a struct value.The fix is in the comptime/cgen layer that monomorphizes generic structs with option-of-array fields: produce a payload struct whose layout matches the option's payload type, not the surrounding fixed-array shape.
V version
V 0.5.1 (40b3711)
Environment details (OS name and version, etc.)
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.