GNOME Bugzilla – Bug 562017
Vala is not properly calling methods from base structs
Last modified: 2010-01-13 05:26:05 UTC
// From this program public struct A { public void foo() { } public int dummy; } public struct B : A { public void moo() { base.foo(); // <-- Problematic line } } void main() { var b = B(); b.moo(); } /* The line base.foo() is expanded to: a_foo ((_tmp0 = A (self), &_tmp0)); But that does not compile. test.c: In function ‘b_moo’: test.c:18: error: expected expression before ‘A’ test.c: In function ‘_main’: The line should be expanded to something like: a_foo ((A*) (self)); */
Created attachment 126469 [details] [review] make fields in structs inheritable This patch provides vala with inheritable fields in structs and typecasts the structs address when calling methods of an inherited struct.
Created attachment 126470 [details] a testcase to the patch
Created attachment 126471 [details] The compiled result of given testcase
Created attachment 126499 [details] [review] uses get_fields () on the struct instead of searching the scope greatly simplifies the code by using get_fields () instead of searching the structs' scope, this also makes sure that the searched symbol is a field (which wasn't checked with the scope lookup) the new property "Struct Struct.base_struct" does depend on 567661 which introduced the "DataType Struct.base_type" property -- the compiled code of the testcase remains unchanged
Created attachment 126586 [details] [review] also set the correct parent_struct fields when using initializer fields depends on attachment 126499 [details] [review] this also fixes the case when initializing fields which where inherited. var st = St () { my_i = 1, base_a = 3 };
Thanks for the patches. As mentioned on IRC, I'm not sure whether we actually want to support this. With structs there is no difference between the dynamic and the static type of a variable - in contrast to classes and interfaces. I'm tending towards only supporting struct aliases. This would mean that structs with a base type are not allowed to have fields, and the compiler would generate just a typedef instead of a struct declaration for the subtype. In Sandino's example, this would be typedef A B;
Marking as resolved just to close the cycle, since Jürg exposed good reasons to consider this not a real issue.