]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
vala: Tranform instance member-access to a static one if possible
authorRico Tzschichholz <ricotz@ubuntu.com>
Thu, 16 Jul 2020 13:25:05 +0000 (15:25 +0200)
committerRico Tzschichholz <ricotz@ubuntu.com>
Sun, 26 Jul 2020 08:46:59 +0000 (10:46 +0200)
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

tests/Makefile.am
tests/semantic/member-access-static-with-instance.vala [new file with mode: 0644]
vala/valamemberaccess.vala

index 261bda9a8f9a52ca4676b060fd7bfcd59fbe081a..301ec6a239055c5e5d6095d71426902bd2d39994 100644 (file)
@@ -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 (file)
index 0000000..d8bb232
--- /dev/null
@@ -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);
+}
index 5f8f46fef0554ecbe09713387b6539d45e0e1a45..89e0a795f7bb2ada72a1710220367e0e479de186 100644 (file)
@@ -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);
                                }
                        }