GNOME Bugzilla – Bug 734295
struct inheritance between multiple files does not compile in C.
Last modified: 2018-05-22 15:14:36 UTC
When a struct inherits from other struct in other file, it does compile at vala level (generation of C Code), but it does not compile at C level with a "Unknown Type" error. This can be reproduced with any struct inheritance, which one of more of struct definitions are separated in other file(s), and here is one of example. which has two struct in each file. astruct.vala: (also attached) public struct AStruct { public int a_field; } bstruct.vala: (also attached) public struct BStruct: AStruct { } bar.vala: (also attached) public static int main (string[] args) { BStruct bstruct = new BStruct (); return 0; } [wsid@WSIDPC ~]$ valac --version Vala 0.25.1 [wsid@WSIDPC ~]$ valac astruct.vala bstruct.vala bar.vala bar.vala:2.20-2.33: warning: deprecated syntax, don't use `new' to initialize structs BStruct bstruct = new BStruct (); ^^^^^^^^^^^^^^ bar.vala:2.10-2.33: warning: local variable `bstruct' declared but never used BStruct bstruct = new BStruct (); ^^^^^^^^^^^^^^^^^^^^^^^^ /home/wsid/bstruct.vala.c:11:9: error: unknown type name ‘AStruct’ typedef AStruct BStruct; ^ /home/wsid/bar.vala.c:12:9: error: unknown type name ‘AStruct’ typedef AStruct BStruct; ^ error: cc exited with status 256 Compilation failed: 1 error(s), 2 warning(s) [wsid@WSIDPC ~]$ valac astruct.vala bstruct.vala bar.vala -C bar.vala:2.20-2.33: warning: deprecated syntax, don't use `new' to initialize structs BStruct bstruct = new BStruct (); ^^^^^^^^^^^^^^ bar.vala:2.10-2.33: warning: local variable `bstruct' declared but never used BStruct bstruct = new BStruct (); ^^^^^^^^^^^^^^^^^^^^^^^^ Compilation succeeded - 2 warning(s) The generated C files are attached, and bstruct.c and bar.c lacks of definition of AStruct.
Created attachment 282575 [details] Attached vala source
Created attachment 282577 [details] Attached vala source
Created attachment 282578 [details] Attached vala source
Created attachment 282579 [details] Attached generated C source
Created attachment 282580 [details] Attached generated C source
Created attachment 282581 [details] Attached generated C source
Created attachment 314139 [details] [review] Bug fix for 734295 Added a check to see whether the non-simple struct has a base struct, and if so, explicitly call for the base struct's declaration.
Review of attachment 314139 [details] [review]: Cosmetically: - patch follows Vala project coding style - git apply produces 'trailing whitespace' message - git am cannot detect format of patch - better to use git format-patch when creating patch? Functionally: - the code allows compilation by the C compiler, the C compiler no longer fails with "error: unknown type name ‘inherited_struct_name_in_other_file’" - this is tested on the example attached to the bug report and the example at https://github.com/GNOME/vala/pull/4 Elegance of solution: - there appears to be duplicate code - it looks as though the solution is to call generate_struct_declaration() whenever st.base_struct is not null, at present the code only does this when st is boolean, integer or float. so it would be better to move generate_struct_declaration() call out from and place it before the type check There has been no regression testing for this review
Created attachment 314252 [details] [review] Enable inheritance of structs across files This uses Kristján's solution, but re-factors the type decision for boolean, integer and floating type structs to improve readability. It makes it more explicit that Kristján's solution is to always call generate_struct_declaration() if there is a base_struct. Not sure if there are any side effects to this. What was happening before when the base struct was declared in the same file? 'make check' doesn't produce any errors.
Created attachment 314253 [details] [review] Enable inheritance of structs across files Correct whitespace error
-- GitLab Migration Automatic Message -- This bug has been migrated to GNOME's GitLab instance and has been closed from further activity. You can subscribe and participate further through the new bug through this link to our GitLab instance: https://gitlab.gnome.org/GNOME/vala/issues/464.