Skip to content

cgen: fix interface eq method with option and ref(fix #19441) - #20201

Merged
spytheman merged 3 commits into
vlang:masterfrom
shove70:patch-2
Dec 18, 2023
Merged

spytheman merged 3 commits into
vlang:masterfrom
shove70:patch-2

Conversation

@shove70

@shove70 shove70 commented Dec 17, 2023 •

Copy link
Copy Markdown
Contributor
  1. Fixed Option field causes C error when struct embeds multiple other structs #19441
  2. Add tests.
pub interface NodeInterface {
mut:
	node_name string
}

pub interface Element {
	NodeInterface
mut:
	tag_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
}

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

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

outputs:

true

@shove70
shove70 marked this pull request as draft December 17, 2023 09:38
@shove70
shove70 marked this pull request as ready for review December 18, 2023 04:28
@shove70

shove70 commented Dec 18, 2023 •

Copy link
Copy Markdown
Contributor Author

By the way, the problem of repeatedly generating the same eq_method is solved.

interface Any{}

struct Foo {
	field &Any
}

fn main() {
	foo1 := &Any(Foo {
		&Any(1)
	})
	foo2 := &Any(Foo {
		&Any(1)
	})
	
	println(foo1 == foo2)
}

outputs:

Running code...
Can't run code. The server returned an error:
/tmp/v_60000/code.377993553552140351.tmp.c:2181: error: redefinition of 'main__Any_interface_eq'
builder error: 
==================
C error. This should never happen.

@shove70

shove70 commented Dec 18, 2023

Copy link
Copy Markdown
Contributor Author

@spytheman spy,

This needs to be merged, for the green CI:
vlang/ved#173

thank you! :)

@spytheman

Copy link
Copy Markdown
Contributor

This needs to be merged, for the green CI: vlang/ved#173

Done. Thanks for fixing it.

Comment thread vlib/v/tests/interface_eq_methods_with_option_and_ref_test.v
@spytheman
spytheman merged commit 17d540e into vlang:master Dec 18, 2023
@shove70
shove70 deleted the patch-2 branch December 18, 2023 22:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Option field causes C error when struct embeds multiple other structs

2 participants