Skip to content

Conversation

@kbkpbot
Copy link
Contributor

@kbkpbot kbkpbot commented Nov 20, 2025

Fix issue #25807
force anon struct/union in C.struct keep the same language of outside.

To fix issue #25788 in another PR due to bootstrap problem.

@kbkpbot kbkpbot linked an issue Nov 20, 2025 that may be closed by this pull request
@kbkpbot kbkpbot changed the title parser: fix embed struct language parser: fix C struct nested anon struct language Nov 23, 2025
@kbkpbot kbkpbot linked an issue Nov 23, 2025 that may be closed by this pull request
Comment on lines +1191 to +1195
typed_obj := if lang == .c {
'(${sym.cname}*)&(${obj})'
} else {
'*(${sym.cname}*)&(${obj})'
}
Copy link
Member

Choose a reason for hiding this comment

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

Why is this different for a C one?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The V compiler handles C structs differently than V structs.

// V auto str functions:
static string SYSTEM_INFO_2_str(SYSTEM_INFO_2* it) { return indent_SYSTEM_INFO_2_str(it, 0);}
static string C___VAnonStruct1_str(C___VAnonStruct1* it) { return indent_C___VAnonStruct1_str(it, 0);}
static string C___VAnonStruct2_str(C___VAnonStruct2* it) { return indent_C___VAnonStruct2_str(it, 0);}
// V auto str functions:
static string main__DeepStruct_str(main__DeepStruct it) { return indent_main__DeepStruct_str(it, 0);}
static string main___VAnonStruct1_str(main___VAnonStruct1 it) { return indent_main___VAnonStruct1_str(it, 0);}
static string main___VAnonStruct2_str(main___VAnonStruct2 it) { return indent_main___VAnonStruct2_str(it, 0);}

As shown above, when the compiler encounters a C struct, it accesses the value via a pointer. In contrast, for a V struct, it passes the struct by value (making a copy).

To ensure auto_str works correctly with a V anonymous struct, a corresponding C___VAnonStruct1 instance must be created for it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We can't direct access a C anon struct

Copy link
Member

Choose a reason for hiding this comment

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

It may be better in the long term, to pass the V struct for the generated auto str methods by a reference too, to keep things consistent 🤔.

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.

Excellent work.
Thanks @kbkpbot .

@spytheman spytheman merged commit e861b73 into vlang:master Nov 23, 2025
85 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.

Does not support deeply nested anonymous C structs

3 participants