]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
vala: Transform cast from integer-type to boxed-type
authorRico Tzschichholz <ricotz@ubuntu.com>
Sun, 10 May 2020 10:54:15 +0000 (12:54 +0200)
committerRico Tzschichholz <ricotz@ubuntu.com>
Sun, 10 May 2020 11:02:03 +0000 (13:02 +0200)
Don't generate faulty c-code with results in segmentation faults.

Fixes https://gitlab.gnome.org/GNOME/vala/issues/992

tests/Makefile.am
tests/basic-types/integers-boxed-cast.vala [new file with mode: 0644]
tests/generics/integer-type-cast.vala [new file with mode: 0644]
vala/valacastexpression.vala

index 160d77169cbe69b9faf978b12dd826d505a36984..de2a0e032c0570a314c775ea3821f1f9b2d87cbc 100644 (file)
@@ -22,6 +22,7 @@ TESTS_ENVIRONMENT = EXEEXT=$(EXEEXT) CFLAGS='$(CFLAGS)' CPPFLAGS='$(CPPFLAGS)' L
 
 TESTS = \
        basic-types/integers.vala \
+       basic-types/integers-boxed-cast.vala \
        basic-types/escape-chars.vala \
        basic-types/floats.vala \
        basic-types/floats-boxed-cast.vala \
@@ -574,6 +575,7 @@ TESTS = \
        generics/floating-type-cast.vala \
        generics/inference-argument-may-fail.vala \
        generics/inference-static-function.vala \
+       generics/integer-type-cast.vala \
        generics/parameter-sizeof-initializer.vala \
        generics/member-dup-destroy.vala \
        generics/type-parameter-properties.vala \
diff --git a/tests/basic-types/integers-boxed-cast.vala b/tests/basic-types/integers-boxed-cast.vala
new file mode 100644 (file)
index 0000000..b200c4a
--- /dev/null
@@ -0,0 +1,13 @@
+void main () {
+       var i = (int?) int.MIN;
+       assert (i == int.MIN);
+
+       var u = (uint?) uint.MAX;
+       assert (u == uint.MAX);
+
+       var i64 = (int64?) int64.MIN;
+       assert (i64 == int64.MIN);
+
+       var u64 = (uint64?) uint64.MAX;
+       assert (u64 == uint64.MAX);
+}
diff --git a/tests/generics/integer-type-cast.vala b/tests/generics/integer-type-cast.vala
new file mode 100644 (file)
index 0000000..4d7ea8b
--- /dev/null
@@ -0,0 +1,9 @@
+void foo<G,T> (G g, T t) {
+       assert ((int64?) g == int64.MIN);
+       assert ((uint64?) t == uint64.MAX);
+}
+
+void main () {
+       foo ((int64?) int64.MIN, (uint64?) uint64.MAX);
+       foo<int64?,uint64?> ((int64?) int64.MIN, (uint64?) uint64.MAX);
+}
index 5dc60c7260c24f8f1c949dde5a3c0f264f237cb8..f86aad157dfa662d6ad7db4c2b37552105fa2abf 100644 (file)
@@ -175,8 +175,7 @@ public class Vala.CastExpression : Expression {
                // Implicit transformation of stack-allocated value to heap-allocated boxed-type
                if (!(is_silent_cast || is_non_null_cast)
                    && (type_reference is ValueType && type_reference.nullable)
-                   && !inner.value_type.nullable
-                   && inner.value_type is FloatingType) {
+                   && inner.value_type.is_non_null_simple_type ()) {
                        var local = new LocalVariable (type_reference, get_temp_name (), null, inner.source_reference);
                        var decl = new DeclarationStatement (local, source_reference);