v icon indicating copy to clipboard operation
v copied to clipboard

error: cannot convert 'struct array' to 'int'

Open 1ethanhansen opened this issue 3 years ago • 0 comments

Describe the bug

V throws an error when I try to access a certain function in an array of functions given as an argument.

Expected Behavior

Should compile properly and let me access the function I want

Current Behavior

==================
/tmp/v_1000/main.3539769618563317589.tmp.c:12291: error: cannot convert 'struct array' to 'int'
...
==================
(Use `v -cg` to print the entire error message)

builder error: 
==================
C error. This should never happen.

This is a compiler bug, please report it using `v bug file.v`.

Reproduction Steps

module main

fn max_speed(functions []fn (int) int, g fn (int) int, end_time int) int {
	mut s := [0]
	mut gears := [0]

	for t in 0..end_time {
		stay_in_gear_speed := if t-1 >= 0 {functions[gears[t-1]](s[t-1])} else {0}
		shift_gear_speed := if t-2 >= 0 {functions[gears[t-2]+1](g(s[t-2]))} else {0}
		if stay_in_gear_speed > shift_gear_speed {
			gears[t] = gears[t-1]
			s << stay_in_gear_speed
		} else {
			gears[t] = gears[t-2] + 1
			s << shift_gear_speed
		}
	}
	return s[end_time]
}

fn first_gear(speed int) int {
	return speed + 1
}

fn second_gear(speed int) int {
	return speed + 1
}

fn third_gear(speed int) int {
	return speed + 1
}

fn decelerate(speed int) int {
	return speed - 3
}

fn main() {
	funcs := [first_gear, second_gear, third_gear]
	println(max_speed(funcs, decelerate, 3))
}

Possible Solution

I tried commenting out the whole for loop and just hard-coding println(functions[0](s[0])) -- that's fine. But once I do println(functions[gears[0]](s[0])), it throws the same error.

Additional Information/Context

Potentially related to https://github.com/vlang/v/issues/15232 ? But in my case I am not passing a slice where a single value is expected.

V version

V 0.3.3 b1ed1d3

Environment details (OS name and version, etc.)

OS: linux, Ubuntu 22.04.2 LTS Processor: 16 cpus, 64bit, little endian, 12th Gen Intel(R) Core(TM) i5-1240P CC version: cc (Ubuntu 11.3.0-1ubuntu1~22.04) 11.3.0

getwd: /home/neniu/Documents/Gitlab/algos vmodules: /home/NAME/.vmodules vroot: /home/NAME/v vexe: /home/NAME/v/v vexe mtime: 2023-02-22 03:28:32 is vroot writable: true is vmodules writable: true V full version: V 0.3.3 58e150d.b1ed1d3

Git version: git version 2.34.1 Git vroot status: weekly.2023.08-8-gb1ed1d3b .git/config present: true thirdparty/tcc status: thirdparty-linux-amd64 12f392c3

1ethanhansen avatar Feb 22 '23 03:02 1ethanhansen