]> 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>
Tue, 19 May 2020 14:54:17 +0000 (16:54 +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 cdcea3d1e69929018dc9479bf42a76124cb8cfd9..8fe220f6988bdc4e03ecfe633f3dbec558a9387e 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 \
@@ -549,6 +550,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/type-parameter-properties.vala \
        generics/bug640330.test \
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 b5080dc5c65b5395d58d70f970515ad40fb82c7e..ccd52e92e691a2234ffe95f173e7d2ce897abefa 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);