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

cgen: fix C struct sumtype support #21129

Merged
merged 6 commits into from
Mar 30, 2024
Merged

Conversation

felipensp
Copy link
Member

@felipensp felipensp commented Mar 28, 2024

Fix #21122

#include "@VMODROOT/foo.h"

struct C.Foo {
	a int
}

struct C.Bar {
	a int
}

type Baz = C.Foo | C.Bar

fn main() {
	mut a := Baz{}
	a = C.Foo{a: 1000}
	dump(a)	
}

@felipensp felipensp marked this pull request as ready for review March 28, 2024 14:45
@felipensp felipensp marked this pull request as draft March 28, 2024 14:45
@trufae
Copy link
Contributor

trufae commented Mar 28, 2024

ive tested this pr with the uing code and it builds but crashes. the resulting C code looks like this:

  uing__Window_set_child(w, HEAP(uing__Control, uing__Box_to_sumtype_uing__Control/*KEK*/(box)));

so it seems there's an extra abstraction for V sumtypes but it should just do a plain cast. Because i'm using a V struct as the public interface for those C.uiX structs which makes the public interface V-friendly while keeping the C structs internal to the module. So not sure if that's intended to be used that way

@felipensp
Copy link
Member Author

ive tested this pr with the uing code and it builds but crashes. the resulting C code looks like this:

  uing__Window_set_child(w, HEAP(uing__Control, uing__Box_to_sumtype_uing__Control/*KEK*/(box)));

so it seems there's an extra abstraction for V sumtypes but it should just do a plain cast. Because i'm using a V struct as the public interface for those C.uiX structs which makes the public interface V-friendly while keeping the C structs internal to the module. So not sure if that's intended to be used that way

Yes, thanks for testing. I'll need to change a bit the sumtype implementation after the major PR about sumtype get merged.

@felipensp felipensp marked this pull request as ready for review March 29, 2024 18:09
@trufae
Copy link
Contributor

trufae commented Mar 29, 2024

still seems to not make my code to work, sumtypes contain the data and the type, while i think for those cases it should just pass the data :? but at least its getting closer. also another issue is that in my case i want Control to be a sumtype for Box, Label, Button.. but at the same time all those Box Label Button types should be castable to C.uiControl. so there's an extra level of indirection in my case..

Copy link
Member

@spytheman spytheman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very good work.

@spytheman spytheman merged commit d49fd0f into vlang:master Mar 30, 2024
54 checks passed
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.

Sumtypes used as C union types not working
3 participants