Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
1ethanhansen opened this issue Feb 22, 2023 · 0 comments · Fixed by #22198
Closed

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

1ethanhansen opened this issue Feb 22, 2023 · 0 comments · Fixed by #22198
Labels
Bug This tag is applied to issues which reports bugs. Status: Confirmed This bug has been confirmed to be valid by a contributor. Unit: cgen Bugs/feature requests, that are related to the default C generating backend.

Comments

@1ethanhansen
Copy link
Contributor

1ethanhansen commented Feb 22, 2023

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 #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 1ethanhansen added the Bug This tag is applied to issues which reports bugs. label Feb 22, 2023
@spytheman spytheman added Unit: cgen Bugs/feature requests, that are related to the default C generating backend. Status: Confirmed This bug has been confirmed to be valid by a contributor. labels Sep 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug This tag is applied to issues which reports bugs. Status: Confirmed This bug has been confirmed to be valid by a contributor. Unit: cgen Bugs/feature requests, that are related to the default C generating backend.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants