From: Rico Tzschichholz Date: Fri, 19 Feb 2021 13:22:20 +0000 (+0100) Subject: vala: Generics value holding struct pointer requires casting on access X-Git-Tag: 0.51.3~19 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=23feafb648a4bd1858d131eb98751e4f002d147e;p=thirdparty%2Fvala.git vala: Generics value holding struct pointer requires casting on access Fixes https://gitlab.gnome.org/GNOME/vala/issues/347 --- diff --git a/tests/Makefile.am b/tests/Makefile.am index 612808724..c2a9c7fbe 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -670,6 +670,7 @@ TESTS = \ generics/property-int-cast.vala \ generics/string-literal-comparison.vala \ generics/type-parameter-properties.vala \ + generics/value-pointer-type-access.vala \ generics/bug640330.test \ generics/bug640330.vala \ generics/bug694765-1.vala \ diff --git a/tests/generics/value-pointer-type-access.vala b/tests/generics/value-pointer-type-access.vala new file mode 100644 index 000000000..d98ca43f9 --- /dev/null +++ b/tests/generics/value-pointer-type-access.vala @@ -0,0 +1,22 @@ +class Foo { + G g; + public void set_g (G data) { + g = data; + } + public G get_g () { + return g; + } +} + +struct Bar { + public int i; +} + +void main () { + Bar bar = { 42 }; + var foo = new Foo (); + foo.set_g (&bar); + + assert (foo.get_g ()->i == 42); + assert (((Bar*) foo.get_g ())->i == 42); +} diff --git a/vala/valamemberaccess.vala b/vala/valamemberaccess.vala index 368f48c88..d56f7ba93 100644 --- a/vala/valamemberaccess.vala +++ b/vala/valamemberaccess.vala @@ -370,6 +370,9 @@ public class Vala.MemberAccess : Expression { if (inner.value_type is PointerType) { unowned PointerType? pointer_type = inner.value_type as PointerType; if (pointer_type != null && pointer_type.base_type is ValueType) { + if (inner.formal_value_type is GenericType) { + inner = new CastExpression (inner, pointer_type.copy (), source_reference); + } // transform foo->bar to (*foo).bar inner = new PointerIndirection (inner, source_reference); inner.check (context);