]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
codegen: Drop inner casts before converting between generics and integers
authorRico Tzschichholz <ricotz@ubuntu.com>
Wed, 10 Mar 2021 11:12:06 +0000 (12:12 +0100)
committerRico Tzschichholz <ricotz@ubuntu.com>
Wed, 10 Mar 2021 11:12:06 +0000 (12:12 +0100)
Found by -Wpointer-to-int-cast

codegen/valaccodebasemodule.vala
tests/Makefile.am
tests/generics/integer-member-access.vala [new file with mode: 0644]

index ae6fddbf3c0bd44ca815e788b6d117f327e6bf56..308aa034e6de6bf242ccf984e2dd5f8a9f24de79 100644 (file)
@@ -6003,8 +6003,16 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
                        generate_type_declaration (actual_type, cfile);
                        result = new CCodeCastExpression (cexpr, get_ccode_name (actual_type));
                } else if (analyzer.is_signed_integer_type_argument (actual_type)) {
+                       // FIXME this should not happen
+                       while (cexpr is CCodeCastExpression) {
+                               cexpr = ((CCodeCastExpression) cexpr).inner;
+                       }
                        result = new CCodeCastExpression (new CCodeCastExpression (cexpr, "gintptr"), get_ccode_name (actual_type));
                } else if (analyzer.is_unsigned_integer_type_argument (actual_type)) {
+                       // FIXME this should not happen
+                       while (cexpr is CCodeCastExpression) {
+                               cexpr = ((CCodeCastExpression) cexpr).inner;
+                       }
                        result = new CCodeCastExpression (new CCodeCastExpression (cexpr, "guintptr"), get_ccode_name (actual_type));
                }
                return result;
@@ -6014,8 +6022,16 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
                unowned SemanticAnalyzer analyzer = context.analyzer;
                var result = cexpr;
                if (analyzer.is_signed_integer_type_argument (actual_type)) {
+                       // FIXME this should not happen
+                       while (cexpr is CCodeCastExpression) {
+                               cexpr = ((CCodeCastExpression) cexpr).inner;
+                       }
                        result = new CCodeCastExpression (new CCodeCastExpression (cexpr, "gintptr"), get_ccode_name (pointer_type));
                } else if (analyzer.is_unsigned_integer_type_argument (actual_type)) {
+                       // FIXME this should not happen
+                       while (cexpr is CCodeCastExpression) {
+                               cexpr = ((CCodeCastExpression) cexpr).inner;
+                       }
                        result = new CCodeCastExpression (new CCodeCastExpression (cexpr, "guintptr"), get_ccode_name (pointer_type));
                }
                return result;
index b22e19768fa045a20d4b3ea464df06170d35e9b8..6e7bd28b800b0367806e72f02c8b9e3deb363a6a 100644 (file)
@@ -675,6 +675,7 @@ TESTS = \
        generics/gvariant-serialization.test \
        generics/inference-argument-may-fail.vala \
        generics/inference-static-function.vala \
+       generics/integer-member-access.vala \
        generics/integer-type-cast.vala \
        generics/integer-type-cast-return.vala \
        generics/parameter-invalid-initializer.test \
diff --git a/tests/generics/integer-member-access.vala b/tests/generics/integer-member-access.vala
new file mode 100644 (file)
index 0000000..47dc31a
--- /dev/null
@@ -0,0 +1,18 @@
+class Foo<K,V> {
+       public K k;
+       public V v;
+}
+
+void main () {
+       var foo = new Foo<int,uint> ();
+       foo.k = int.MIN;
+       foo.v = uint.MAX;
+       assert (foo.k == int.MIN);
+       assert (foo.v == uint.MAX);
+
+       var bar = new Foo<int,uint> ();
+       bar.k = foo.k;
+       bar.v = foo.v;
+       assert (bar.k == int.MIN);
+       assert (bar.v == uint.MAX);
+}