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

Generics use incorrect type/path when more than one generic function exists for embedded structs #20848

Closed
jcweaver997 opened this issue Feb 16, 2024 · 0 comments · Fixed by #20900
Labels
Bug This tag is applied to issues which reports bugs.

Comments

@jcweaver997
Copy link

jcweaver997 commented Feb 16, 2024

Describe the bug

In cases where an embedded member is accessed from within a generic function on the generic type, the second generated function seems to change the path to the embedded member in the first generated function

Reproduction Steps

The following v code produces this issue when compiling:

module main

struct Base {
	i int
}

struct Base1 {
	Base
}

struct Base2 {
	Base
}

struct Struct1 {
	Base1
}

struct Struct1O {
	Struct1
}

struct Struct2 {
	Base2
}

struct Struct2O {
	Struct2
}

fn (b &Base) func[T]() T {
	t := T{
		i: b.i
	}
	if t.i == 0 {
	}
	return t
}

pub fn main() {
	s1 := Struct1{}
	s2 := Struct2{}
	s1.func[Struct1O]()
	s2.func[Struct2O]()
}

Expected Behavior

successful compile

Current Behavior

The following error is produced:

PS C:\Users\jcwea\Documents\git\vtest> v -cg run .    
C:/Users/jcwea/AppData/Local/Temp/v_0/vtest.01HPSQPC6R0YY9H4AG342XFR12.tmp.c:501: warning: WINVER redefined
C:/Users/jcwea/AppData/Local/Temp/v_0/vtest.01HPSQPC6R0YY9H4AG342XFR12.tmp.c:6898: warning: implicit declaration of function 'tcc_backtrace'
C:/Users/jcwea/AppData/Local/Temp/v_0/vtest.01HPSQPC6R0YY9H4AG342XFR12.tmp.c:13116: error: field not found: Struct2
builder error:
==================
C error. This should never happen.

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

https://github.com/vlang/v/issues/new/choose

You can also use #help on Discord: https://discord.gg/vlang

Looking at the generated C code, it uses the wrong path:

main__Struct1O main__Base_func_T_main__Struct1O(main__Base* b) {
	main__Struct1O t = ((main__Struct1O){.Struct1 = ((main__Struct1){.Base1 = ((main__Base1){.Base = ((main__Base){.i = b->i,}),}),}),});
	if (t.Struct2.Base2.Base.i == 0) {
	}
	return t;
}
main__Struct2O main__Base_func_T_main__Struct2O(main__Base* b) {
	main__Struct2O t = ((main__Struct2O){.Struct2 = ((main__Struct2){.Base2 = ((main__Base2){.Base = ((main__Base){.i = b->i,}),}),}),});
	if (t.Struct2.Base2.Base.i == 0) {
	}
	return t;
}

It uses t.Struct2.Base2.Base.i instead of the correct t.Struct1.Base1.Base.i
It seems like the second use of the function poisons the first use of the function

Possible Solution

No response

Additional Information/Context

No response

V version

V 0.4.4 1d3147e

Environment details (OS name and version, etc.)

V full version: V 0.4.4 afd74ad.1d3147e
OS: windows, Microsoft Windows 11 Pro v26058 64-bit
Processor: 24 cpus, 64bit, little endian,

getwd: C:\Users\jcwea\Documents\git\vtest
vexe: C:\Users\jcwea\Documents\git\v\v.exe
vexe mtime: 2024-02-16 19:26:43

vroot: OK, value: C:\Users\jcwea\Documents\git\v
VMODULES: OK, value: C:\Users\jcwea.vmodules
VTMP: OK, value: C:\Users\jcwea\AppData\Local\Temp\v_0

Git version: git version 2.40.0.windows.1
Git vroot status: weekly.2024.07-30-g1d3147e1
.git/config present: true

CC version: Error: 'cc' is not recognized as an internal or external command,
operable program or batch file.

thirdparty/tcc status: thirdparty-windows-amd64 b99a453d

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.

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.
Projects
None yet
1 participant