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

Option field causes C error when struct embeds multiple other structs #19441

Closed
islonely opened this issue Sep 25, 2023 · 3 comments · Fixed by #20201
Closed

Option field causes C error when struct embeds multiple other structs #19441

islonely opened this issue Sep 25, 2023 · 3 comments · Fixed by #20201
Labels
Bug This tag is applied to issues which reports bugs. Build V build error on any OS/CPU architecture. Option Type Bugs/feature requests, that are related to `?Type`.

Comments

@islonely
Copy link
Contributor

islonely commented Sep 25, 2023

Describe the bug

When struct A embeds struct B and struct C (which has an option field) and struct A is cast to interface A, then comparing the interface A with another interface A will cause a C error.

Reproduction Steps

fn main() {
	mut arr := []&NodeInterface{}
	arr << &HTMLElement{}
	arr << &HTMLElement{}

	// removing this comparison lets the code compile
	println(arr[0] == arr[1])
}

pub interface Element {
	NodeInterface
mut:
	tag_name string
}

pub interface NodeInterface {
mut:
	node_name string
}

pub struct Node {
pub mut:
	node_name string
}

pub struct HTMLElement {
	Node
pub mut:
	tag_name string
}

pub struct HTMLInputElement {
	// removing either one of these embedded structs lets the code compile
	HTMLElement
	PopoverInvokerElement
pub mut:
	value string
}

pub struct PopoverInvokerElement {
pub mut:
	// changing this field to `&Element` lets the code compile
	popover_target_element ?&Element
	popover_target_action  string
}

Expected Behavior

Expected code to compile and print true or false

Current Behavior

C:/Users/imado/AppData/Local/Temp/v_0/test.10654267466622163022.tmp.c:510: warning: WINVER redefined
C:/Users/imado/AppData/Local/Temp/v_0/test.10654267466622163022.tmp.c:2319: error: expected struct or union but not 'struct main__Element *'
builder error: 
==================
C error. This should never happen.

Possible Solution

No response

Additional Information/Context

No response

V version

V full version: V 0.4.1 68cbf27.20bce37

Environment details (OS name and version, etc.)

V full version: V 0.4.1 68cbf27.20bce37
OS: windows, Microsoft Windows 11 Pro v22621 64-bit
Processor: 16 cpus, 64bit, little endian, 

getwd: C:\Users\imado\Documents\cyberian_tiger
vexe: C:\Users\imado\v\v.exe
vexe mtime: 2023-09-25 15:32:08

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

Git version: git version 2.33.1.windows.1
Git vroot status: weekly.2023.38-38-g20bce37d
.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 e90c2620

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.

@islonely islonely added the Bug This tag is applied to issues which reports bugs. label Sep 25, 2023
@ArtemkaKun ArtemkaKun added Option Type Bugs/feature requests, that are related to `?Type`. Build V build error on any OS/CPU architecture. labels Sep 25, 2023
@yuyi98
Copy link
Member

yuyi98 commented Sep 27, 2023

[]&NodeInterface{}
Why do interfaces need to use references? Just []NodeInterface{} is ok.

fn main() {
	mut arr := []NodeInterface{}
	arr << &HTMLElement{}
	arr << &HTMLElement{}

	// removing this comparison lets the code compile
	println(arr[0] == arr[1])
}

pub interface Element {
	NodeInterface
mut:
	tag_name string
}

pub interface NodeInterface {
mut:
	node_name string
}

pub struct Node {
pub mut:
	node_name string
}

pub struct HTMLElement {
	Node
pub mut:
	tag_name string
}

pub struct HTMLInputElement {
	// removing either one of these embedded structs lets the code compile
	HTMLElement
	PopoverInvokerElement
pub mut:
	value string
}

pub struct PopoverInvokerElement {
pub mut:
	// changing this field to `&Element` lets the code compile
	popover_target_element ?Element
	popover_target_action  string
}

PS D:\Test\v\tt1> v run .
false

@islonely
Copy link
Contributor Author

Because I wanted to make sure the underlying type is passed by reference and not value. Sorry if that's not right. I have a background of primarily Java and JavaScript where everything is references.

@islonely
Copy link
Contributor Author

The way you have it I can't cast back to the original type. Changing your main fn, this println is not allowed,

fn main() {
	mut arr := []NodeInterface{}
	arr << &HTMLElement{}
	arr << &HTMLElement{}

	println(HTMLElement(arr[0]))
}

But this is.

fn main() {
	mut arr := []&NodeInterface{}
	arr << &HTMLElement{}
	arr << &HTMLElement{}

	println(&HTMLElement(arr[0]))
}

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. Build V build error on any OS/CPU architecture. Option Type Bugs/feature requests, that are related to `?Type`.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants