From: Rico Tzschichholz Date: Thu, 6 Aug 2020 09:09:52 +0000 (+0200) Subject: vala: Switch context if with-variable is not owned by with-statement ifself X-Git-Tag: 0.49.2~10 X-Git-Url: http://git.ipfire.org/gitweb/gitweb.cgi?a=commitdiff_plain;h=b2746b9c3a2edc17ae7d27b30123fe0aeec52f82;p=thirdparty%2Fvala.git vala: Switch context if with-variable is not owned by with-statement ifself See https://gitlab.gnome.org/GNOME/vala/issues/1043 --- diff --git a/tests/Makefile.am b/tests/Makefile.am index 984266210..8c857cb81 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -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 index 000000000..c3796ac4f --- /dev/null +++ b/tests/objects/with-nested-method.vala @@ -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); + } + } +} diff --git a/vala/valamemberaccess.vala b/vala/valamemberaccess.vala index 454d856b9..d61e2f3fe 100644 --- a/vala/valamemberaccess.vala +++ b/vala/valamemberaccess.vala @@ -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; } }