v icon indicating copy to clipboard operation
v copied to clipboard

cgen, generics: C gen error when using optional element in array

Open enghitalo opened this issue 9 months ago • 4 comments

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.

enghitalo avatar May 12 '25 12:05 enghitalo

Connected to Huly®: V_0.6-22836

huly-for-github[bot] avatar May 12 '25 12:05 huly-for-github[bot]

Offtopic, maybe this will help? https://modules.vlang.io/encoding.binary.html#decode_binary

koplenov avatar May 12 '25 21:05 koplenov

#24504 fix for:

fn test_main() {
	mut a := []?int{}
	a << none
	assert a == [?int(none)]
}

enghitalo avatar May 18 '25 16:05 enghitalo

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

enghitalo avatar May 18 '25 16:05 enghitalo