]> 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>
Thu, 16 Jul 2020 14:30:19 +0000 (16:30 +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 6d9e32ebe4878bd16487509415a4614394ec8c9e..d80ef2ed0217cde92e04fe7b4b8941aa807c7902 100644 (file)
@@ -866,6 +866,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 02d3b50ad7f2613509c4d1e3b707f866463e190e..3aa9d21fa600b1fbade8496c5e2965f8532b6729 100644 (file)
@@ -901,6 +901,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);
                                }
                        }