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

generic function that is called with different generic types cannot infer correct generic type in sub call #19427

Closed
Casper64 opened this issue Sep 23, 2023 · 1 comment
Labels
Bug This tag is applied to issues which reports bugs. Generics[T] Bugs/feature requests, that are related to the V generics. Unit: cgen Bugs/feature requests, that are related to the default C generating backend.

Comments

@Casper64
Copy link
Member

Casper64 commented Sep 23, 2023

Describe the bug

In the following code V cannot determine what generic type should be passed to double_array(mut typ) and always uses the last used type.

The functions are generated, but the wrong generic function is used.

module main

pub fn generic_caller[T]() !T {
	mut typ := T{}
	$if T is $array {
		double_array(mut typ)
		return typ
	} $else {
		return error('not an array!')
	}
}

fn double_array[T](mut typ []T) {
	println('setting array of type ${T.typ}')
	for _ in 0 .. 2 {
		el := T{}
		typ << el
	}
}

pub struct User {
	name string
	age  int
}

pub struct Employee {
	id  int
	job string
}

fn main() {
	println('idx of User ${typeof[User]().idx}')

	users := generic_caller[[]User]()!
	println('users: ${users}')

	println('idx of Employee ${typeof[Employee]().idx}')
	// works if you comment out the following line
	employees := generic_caller[[]Employee]()!
	println('emplyees: ${employees}')
}

Reproduction Steps

>  v -cg run infer_array.v 

Expected Behavior

no errors

Current Behavior

> v -cg run infer_array.v 
idx of User 97
setting array of type 98
7ff826e9b649 : at ???: RUNTIME ERROR: invalid memory access
/tmp/v_1000/infer_array.13578371958434614598.tmp.c:9757: by string_clone
/tmp/v_1000/infer_array.13578371958434614598.tmp.c:11823: by StrIntpData_process_str_intp_data
/tmp/v_1000/infer_array.13578371958434614598.tmp.c:12256: by str_intp
/tmp/v_1000/infer_array.13578371958434614598.tmp.c:2145: by indent_main__User_str
/tmp/v_1000/infer_array.13578371958434614598.tmp.c:2104: by indent_Array_main__User_str
/tmp/v_1000/infer_array.13578371958434614598.tmp.c:2098: by Array_main__User_str
/tmp/v_1000/infer_array.13578371958434614598.tmp.c:12607: by main__main
/tmp/v_1000/infer_array.13578371958434614598.tmp.c:12653: by main

Possible Solution

I tried to fix it and found that node.concrete_types is correctly inferred for double_array in the checker stage, but this is not present in cgen.

Additional Information/Context

C code:

Array_main__User main__generic_caller_T_Array_main__User(void) {
	Array_main__User typ = __new_array_with_default(0, 0, sizeof(main__User), 0);
	#if 0
	{
	}
	#elif 1
	{
		main__double_array_T_main__Employee(&/*arr*/typ);
	}
	#endif
	return typ;
}
Array_main__Employee main__generic_caller_T_Array_main__Employee(void) {
	Array_main__Employee typ = __new_array_with_default(0, 0, sizeof(main__Employee), 0);
	#if 0
	{
	}
	#elif 1
	{
		main__double_array_T_main__Employee(&/*arr*/typ);
	}
	#endif
	return typ;
}

V version

V 0.4.1 V 0.4.1 a685088

Environment details (OS name and version, etc.)

V full version: V 0.4.1 a685088
OS: linux (WSL 2)

Important

You can vote for this issue using the 👍 reaction. More votes increase the issue's priority
for developers.

Take into account that only the 👍 reaction counts as a vote.
Only reactions to the issue itself will be counted as votes, not comments.

@Casper64 Casper64 added the Bug This tag is applied to issues which reports bugs. label Sep 23, 2023
@ArtemkaKun ArtemkaKun added Generics[T] Bugs/feature requests, that are related to the V generics. Unit: cgen Bugs/feature requests, that are related to the default C generating backend. labels Sep 24, 2023
@felipensp
Copy link
Member

Already fixed.

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. Generics[T] Bugs/feature requests, that are related to the V generics. Unit: cgen Bugs/feature requests, that are related to the default C generating backend.
Projects
None yet
Development

No branches or pull requests

3 participants