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 implementation maybe keeps state from previous run, that it should have reset #22855

Closed
blackshirt opened this issue Nov 14, 2024 · 2 comments · Fixed by #22865
Closed
Assignees
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. Unit: Checker Bugs/feature requests, that are related to the type checker.

Comments

@blackshirt
Copy link
Contributor

blackshirt commented Nov 14, 2024

Describe the bug

Code: https://play.vlang.io/p/7e39789b29

struct Parser {
mut:
	data []u8
}

fn (mut p Parser) read_element[T]() !T {
	t := T.parse(mut p)!
	return t
}

struct TestA {
	data []u8
}

fn TestA.parse(mut p Parser) !TestA {
	return TestA{
		data: p.data
	}
}

struct TestB {
	st string
}

fn TestB.parse(mut p Parser) !TestB {
	return TestB{
		st: p.data.hex()
	}
}

fn main() {
	data := []u8{len: 5, init: u8(5)}
	mut p := Parser{
		data: data
	}

	ta := p.read_element[TestA]()!
	dump(ta)

	tb := p.read_element[TestB]()!
	dump(tb)
}

Reproduction Steps

Run the code on the above playground link or below

Expected Behavior

[code.v:38] ta: TestA{
    data: [5, 5, 5, 5, 5]
}
[code.v:41] tb: TestB{
    st: '0505050505'
}

Current Behavior

Output:

code.v:8:2: error: `TestA` doesn't implement method `msg` of interface `IError`
    6 | fn (mut p Parser) read_element[T]() !T {
    7 |     t := T.parse(mut p)!
    8 |     return t
      |     ~~~~~~~~
    9 | }
   10 |
code.v:8:2: error: `TestA` doesn't implement method `code` of interface `IError`
    6 | fn (mut p Parser) read_element[T]() !T {
    7 |     t := T.parse(mut p)!
    8 |     return t
      |     ~~~~~~~~
    9 | }
   10 |
code.v:8:9: error: cannot use `TestA` as type `!TestB` in return argument
    6 | fn (mut p Parser) read_element[T]() !T {
    7 |     t := T.parse(mut p)!
    8 |     return t
      |            ^
    9 | }
   10 |
Exited with error status 1

Possible Solution

No response

Additional Information/Context

No response

V version

V 0.4.8 897ec51.ee3a182

Environment details (OS name and version, etc.)

V full version: V 0.4.8 897ec51.ee3a182
OS: linux, Debian GNU/Linux 12 (bookworm) (VM)
Processor: 2 cpus, 64bit, little endian, Intel(R) Xeon(R) CPU E5-2680 v4 @ 2.40GHz

getwd: /home/admin/playground
vexe: /home/admin/v/v
vexe mtime: 2024-11-14 06:25:27

vroot: OK, value: /home/admin/v
VMODULES: OK, value: .vmodules
VTMP: OK, value: /tmp/v_0

Git version: git version 2.39.5
Git vroot status: Error: fatal: detected dubious ownership in repository at '/home/admin/v'
To add an exception for this directory, call:

	git config --global --add safe.directory /home/admin/v
.git/config present: true

CC version: cc (Debian 12.2.0-14) 12.2.0
emcc version: N/A
thirdparty/tcc status: Error: fatal: detected dubious ownership in repository at '/home/admin/v/thirdparty/tcc'
To add an exception for this directory, call:

	git config --global --add safe.directory /home/admin/v/thirdparty/tcc
 Error: fatal: detected dubious ownership in repository at '/home/admin/v/thirdparty/tcc'
To add an exception for this directory, call:

	git config --global --add safe.directory /home/admin/v/thirdparty/tcc

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.

Huly®: V_0.6-21300

@blackshirt blackshirt added the Bug This tag is applied to issues which reports bugs. label Nov 14, 2024
@jorgeluismireles
Copy link

I think you can use an interface without handling any error:

interface Parser {
mut:
	parse_data(data []u8)
}

struct ParserA implements Parser {
mut:
	data []u8
}	

fn (mut p ParserA) parse_data(data []u8) {
	p.data = data.clone()
}

struct ParserB implements Parser {
mut:
	data string	
}

fn (mut p ParserB) parse_data(data []u8) {
	p.data = data.hex()
}

fn main() {
	data := [u8(5), 5, 5, 5]
	mut pa := ParserA{}
	mut pb := ParserB{}
	pa.parse_data(data)
	pb.parse_data(data)
	assert pa.data == [u8(5),5,5,5]
	assert pb.data == '05050505'
}

https://play.vlang.io/p/055efba784

@blackshirt
Copy link
Contributor Author

Thanks...I think its different use case.
Your snippet works without generic, the problem maybe arises when its work with generic like above snippet.

@felipensp felipensp added Unit: Checker Bugs/feature requests, that are related to the type checker. Unit: cgen Bugs/feature requests, that are related to the default C generating backend. labels Nov 15, 2024
@felipensp felipensp self-assigned this Nov 15, 2024
@felipensp felipensp added the Status: Confirmed This bug has been confirmed to be valid by a contributor. label Nov 15, 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. Unit: Checker Bugs/feature requests, that are related to the type checker.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants