From: Rico Tzschichholz Date: Thu, 16 Jul 2020 13:25:05 +0000 (+0200) Subject: vala: Tranform instance member-access to a static one if possible X-Git-Tag: 0.46.12~21 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=d28aa61fd778e94292d32a4567dacb01bb8be766;p=thirdparty%2Fvala.git vala: Tranform instance member-access to a static one if possible There is a warning issued already and this cleans up the AST to prevent unwanted behaviour in the code-generator, which resulted in the invocation of "CCodeBaseModule.emit_temp_var()" and criticals like: vala_ccode_function_add_declaration: assertion 'self != NULL' failed vala_ccode_function_add_assignment: assertion 'self != NULL' failed Fixes https://gitlab.gnome.org/GNOME/vala/issues/270 --- diff --git a/tests/Makefile.am b/tests/Makefile.am index 261bda9a8..301ec6a23 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -823,6 +823,7 @@ TESTS = \ semantic/member-access-capture-out.test \ semantic/member-access-protected-invalid.test \ semantic/member-access-undefined.test \ + semantic/member-access-static-with-instance.vala \ semantic/method-abstract.test \ semantic/method-abstract-body.test \ semantic/method-async-ref-parameter.test \ diff --git a/tests/semantic/member-access-static-with-instance.vala b/tests/semantic/member-access-static-with-instance.vala new file mode 100644 index 000000000..d8bb23206 --- /dev/null +++ b/tests/semantic/member-access-static-with-instance.vala @@ -0,0 +1,12 @@ +struct Foo { + public const int FOO = 23; + + public static Foo static_field; + public int i; +} + +const int BAR = Foo.static_field.FOO; + +void main () { + assert (BAR == 23); +} diff --git a/vala/valamemberaccess.vala b/vala/valamemberaccess.vala index 5f8f46fef..89e0a795f 100644 --- a/vala/valamemberaccess.vala +++ b/vala/valamemberaccess.vala @@ -846,6 +846,16 @@ public class Vala.MemberAccess : Expression { // do not warn when calling .begin or .end on static async method } else { Report.warning (source_reference, "Access to static member `%s' with an instance reference".printf (symbol_reference.get_full_name ())); + + // Transform to static member access + unowned Symbol? inner_sym = symbol_reference.parent_symbol; + unowned MemberAccess? inner_ma = this; + while (inner_sym != null && inner_sym.name != null) { + inner_ma.inner = new MemberAccess (null, inner_sym.name, source_reference); + inner_ma = (MemberAccess) inner_ma.inner; + inner_sym = inner_sym.parent_symbol; + } + inner.check (context); } }