[vala/0.40] vala: Transform cast from integer-type to boxed-type



commit 13de4e2e970b19066d3e5d3384d55e9fe9d927ee
Author: Rico Tzschichholz <ricotz ubuntu com>
Date:   Sun May 10 12:54:15 2020 +0200

    vala: Transform cast from integer-type to boxed-type
    
    Don't generate faulty c-code with results in segmentation faults.
    
    Fixes https://gitlab.gnome.org/GNOME/vala/issues/992

 tests/Makefile.am                          |  2 ++
 tests/basic-types/integers-boxed-cast.vala | 13 +++++++++++++
 tests/generics/integer-type-cast.vala      |  9 +++++++++
 vala/valacastexpression.vala               |  3 +--
 4 files changed, 25 insertions(+), 2 deletions(-)
---
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 7cb21aad2..f4badf3a7 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -16,6 +16,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 \
@@ -490,6 +491,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
index 000000000..b200c4aca
--- /dev/null
+++ b/tests/basic-types/integers-boxed-cast.vala
@@ -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
index 000000000..4d7ea8ba6
--- /dev/null
+++ b/tests/generics/integer-type-cast.vala
@@ -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);
+}
diff --git a/vala/valacastexpression.vala b/vala/valacastexpression.vala
index 911ae8d67..5ef749f46 100644
--- a/vala/valacastexpression.vala
+++ b/vala/valacastexpression.vala
@@ -171,8 +171,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);
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]