[vala/0.48] vala: Tranform instance member-access to a static one if possible



commit 0586a58054c2637bb0dfc4caa1e17bcd103a7c0a
Author: Rico Tzschichholz <ricotz ubuntu com>
Date:   Thu Jul 16 15:25:05 2020 +0200

    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

 tests/Makefile.am                                      |  1 +
 tests/semantic/member-access-static-with-instance.vala | 12 ++++++++++++
 vala/valamemberaccess.vala                             | 10 ++++++++++
 3 files changed, 23 insertions(+)
---
diff --git a/tests/Makefile.am b/tests/Makefile.am
index d20cb1862..d40ee5ede 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -853,6 +853,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 5a013dfcc..c68e8e3f5 100644
--- a/vala/valamemberaccess.vala
+++ b/vala/valamemberaccess.vala
@@ -877,6 +877,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);
                                }
                        }
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]