v
v copied to clipboard
cgen, generics: C gen error when using optional element in array
Describe the bug
[]T unwrap option elements, which will not work in real world
Reproduction Steps
struct Decoder { }
pub fn decode[T](val string) !T {
mut decoder := Decoder{}
mut result := T{}
decoder.decode_value(mut result)!
return result
}
fn (mut decoder Decoder) decode_value[T](mut val T) ! {
$if T.unaliased_typ is $array {
decoder.decode_array(mut val)!
return
} $else $if T is $option {
println('passed here 2')
val = none
}
}
fn (mut decoder Decoder) decode_array[T](mut val []T) ! {
$if T is $option {
println('passed here')
val << none
val << 1
} $else {
val << 1
}
mut array_element := T{}
decoder.decode_value(mut array_element)!
val << array_element
}
fn main() {
assert decode[[]int]('')! == [1]
assert decode[[]?int]('')! == [?int(none), 1, none]
}
Expected Behavior
assert pass
Current Behavior
/tmp/v_60000/code.01JV23N10FWK0SD8320EBNH30R.tmp.c:1816: error: invalid operand types for binary operation
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 .
Possible Solution
No response
Additional Information/Context
No response
V version
V 0.4.10 e93c344
Environment details (OS name and version, etc.)
Ubuntu
[!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.
Connected to Huly®: V_0.6-22836
Offtopic, maybe this will help? https://modules.vlang.io/encoding.binary.html#decode_binary
#24504 fix for:
fn test_main() {
mut a := []?int{}
a << none
assert a == [?int(none)]
}
Current Behavior in (V 0.4.10 2c42574)
a.v:35: FAIL: fn main.main: assert decode('')! == [1]
left value: decode('')! = [1, 0]
right value: [1]
V panic: Assertion failed...
v hash: 2c42574
/tmp/v_1000/a.01JVJ2874ESYZHTDMHGN0XJ4W8.tmp.c:4202: at _v_panic: Backtrace
/tmp/v_1000/a.01JVJ2874ESYZHTDMHGN0XJ4W8.tmp.c:6238: by main__main
/tmp/v_1000/a.01JVJ2874ESYZHTDMHGN0XJ4W8.tmp.c:6297: by main