]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
vala: Switch context if with-variable is not owned by with-statement ifself
authorRico Tzschichholz <ricotz@ubuntu.com>
Thu, 6 Aug 2020 09:09:52 +0000 (11:09 +0200)
committerRico Tzschichholz <ricotz@ubuntu.com>
Thu, 6 Aug 2020 17:07:22 +0000 (19:07 +0200)
See https://gitlab.gnome.org/GNOME/vala/issues/1043

tests/Makefile.am
tests/objects/with-nested-method.vala [new file with mode: 0644]
vala/valamemberaccess.vala

index 984266210f2e312bd1efb993b6e6f6aa89646070..8c857cb817934cb9eaf5deccb69c755b6392aa58 100644 (file)
@@ -527,6 +527,7 @@ TESTS = \
        objects/with-expression.vala \
        objects/with-instance.vala \
        objects/with-nested.vala \
+       objects/with-nested-method.vala \
        errors/catch-error-code.vala \
        errors/catch-in-finally.vala \
        errors/default-gtype.vala \
diff --git a/tests/objects/with-nested-method.vala b/tests/objects/with-nested-method.vala
new file mode 100644 (file)
index 0000000..c3796ac
--- /dev/null
@@ -0,0 +1,24 @@
+class Foo {
+       public int foo () {
+               return 23;
+       }
+}
+
+class Bar {
+       public int foo () {
+               return 42;
+       }
+}
+
+void main () {
+       var foo = new Foo ();
+       var bar = new Bar ();
+
+       with (foo) {
+               assert (foo () == 23);
+
+               with (bar) {
+                       assert (foo () == 42);
+               }
+       }
+}
index 454d856b92f7b28dfadad4ae5aebb730500616ae..d61e2f3fe2a09965006e6b3a1736cb5cbab49617 100644 (file)
@@ -292,7 +292,14 @@ public class Vala.MemberAccess : Expression {
                                        symbol_reference = variable_type.get_member (member_name);
                                        if (symbol_reference != null) {
                                                inner = new MemberAccess (null, w.with_variable.name, source_reference);
-                                               inner.check (context);
+                                               if (w.with_variable.parent_symbol == w.body) {
+                                                       inner.check (context);
+                                               } else {
+                                                       var old_symbol = context.analyzer.current_symbol;
+                                                       context.analyzer.current_symbol = w.parent_symbol;
+                                                       inner.check (context);
+                                                       context.analyzer.current_symbol = old_symbol;
+                                               }
                                                may_access_instance_members = true;
                                        }
                                }