]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
vala: Generics value holding struct pointer requires casting on access
authorRico Tzschichholz <ricotz@ubuntu.com>
Fri, 19 Feb 2021 13:22:20 +0000 (14:22 +0100)
committerRico Tzschichholz <ricotz@ubuntu.com>
Sat, 27 Feb 2021 19:26:43 +0000 (20:26 +0100)
Fixes https://gitlab.gnome.org/GNOME/vala/issues/347

tests/Makefile.am
tests/generics/value-pointer-type-access.vala [new file with mode: 0644]
vala/valamemberaccess.vala

index 797db328c474db239fc295accf01be04915c6955..dcac3682f020d6041fbbcb5c0581c158f1897521 100644 (file)
@@ -663,6 +663,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 (file)
index 0000000..d98ca43
--- /dev/null
@@ -0,0 +1,22 @@
+class Foo<G> {
+       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<Bar*> ();
+       foo.set_g (&bar);
+
+       assert (foo.get_g ()->i == 42);
+       assert (((Bar*) foo.get_g ())->i == 42);
+}
index 5d10e3009a9d1dacf1a0a66c250aacc18a0e6039..e3e1853ac67ccad38ae4710f938d42483331d2fc 100644 (file)
@@ -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);