v icon indicating copy to clipboard operation
v copied to clipboard

Generic function pointer returns wrong output

Open lv37 opened this issue 1 year ago • 2 comments

Describe the bug

module main

struct Wrapper {
	a int
}

fn test[T](a &T) Wrapper {
	$if T is int {
		return Wrapper{a}
	}
	return Wrapper{}
}

fn main() {
	a := 123
	println(test(a).a)
}

Reproduction Steps

Run the code above

Expected Behavior

123

Current Behavior

Prints a random number

-654598052

Possible Solution

No response

Additional Information/Context

No response

V version

V 0.4.5 58a8fc6

Environment details (OS name and version, etc.)

V full version: V 0.4.5 b347f54.58a8fc6
OS: linux, Linux version 6.8.9-300.fc40.x86_64 (mockbuild@5f5c14101bdf4cb8942130379360ff7d) (gcc (GCC) 14.0.1 20240411 (Red Hat 14.0.1-0), GNU ld version 2.41-34.fc40) #1 SMP PREEMPT_DYNAMIC Thu May  2 18:59:06 UTC 2024
Processor: 4 cpus, 64bit, little endian, Intel(R) Core(TM) i5-4310U CPU @ 2.00GHz

getwd: /home/user/Projects/learncpp
vexe: /home/user/.local/lib/v/v
vexe mtime: 2024-05-13 20:51:27

vroot: OK, value: /home/user/.local/lib/v
VMODULES: OK, value: /home/user/.vmodules
VTMP: OK, value: /tmp/v_1002

Git version: git version 2.45.0
Git vroot status: weekly.2024.14-312-g58a8fc6c
.git/config present: true

CC version: cc (GCC) 14.1.1 20240507 (Red Hat 14.1.1-1)
thirdparty/tcc status: thirdparty-linux-amd64 12f392c3

[!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.

lv37 avatar May 14 '24 13:05 lv37

Adding a dereference prints the expected value:

struct Wrapper {
	a int
}

fn test[T](a &T) Wrapper {
	$if T is int {
		return Wrapper{*a} // <- here
	}
	return Wrapper{}
}

fn main() {
	a := 123
	println(test(a).a)
}

StunxFS avatar May 14 '24 21:05 StunxFS

What do you think @medvednikov @spytheman ?

felipensp avatar May 19 '24 20:05 felipensp