]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
vala: Transform cast from floating-type to boxed-type
authorRico Tzschichholz <ricotz@ubuntu.com>
Sat, 9 May 2020 11:35:07 +0000 (13:35 +0200)
committerRico Tzschichholz <ricotz@ubuntu.com>
Sun, 10 May 2020 10:56:12 +0000 (12:56 +0200)
Don't generate invalid c-code leading to "cannot convert to a pointer type"

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

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

index 7a99b0af2464e57e3b292e1111410d8173794593..160d77169cbe69b9faf978b12dd826d505a36984 100644 (file)
@@ -24,6 +24,7 @@ TESTS = \
        basic-types/integers.vala \
        basic-types/escape-chars.vala \
        basic-types/floats.vala \
+       basic-types/floats-boxed-cast.vala \
        basic-types/boolean.vala \
        basic-types/custom-types.vala \
        basic-types/default-gtype.vala \
@@ -570,6 +571,7 @@ TESTS = \
        asynchronous/yield.vala \
        generics/arrays-not-supported.test \
        generics/constructor-chain-up.vala \
+       generics/floating-type-cast.vala \
        generics/inference-argument-may-fail.vala \
        generics/inference-static-function.vala \
        generics/parameter-sizeof-initializer.vala \
diff --git a/tests/basic-types/floats-boxed-cast.vala b/tests/basic-types/floats-boxed-cast.vala
new file mode 100644 (file)
index 0000000..86e9f52
--- /dev/null
@@ -0,0 +1,7 @@
+void main () {
+       var f = (float?) 23.0f;
+       assert (f == 23.0f);
+
+       var d = (double?) 42.0;
+       assert (d == 42.0);
+}
diff --git a/tests/generics/floating-type-cast.vala b/tests/generics/floating-type-cast.vala
new file mode 100644 (file)
index 0000000..9475a42
--- /dev/null
@@ -0,0 +1,9 @@
+void foo<G,T> (G g, T t) {
+       assert ((float?) g == 23.0f);
+       assert ((double?) t == 42.0);
+}
+
+void main () {
+       foo ((float?) 23.0f, (double?) 42.0);
+       foo<float?,double?> ((float?) 23.0f, (double?) 42.0);
+}
index d51b4e205b384131016d21e8b749f4e287a431ca..5dc60c7260c24f8f1c949dde5a3c0f264f237cb8 100644 (file)
@@ -172,6 +172,28 @@ 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) {
+                       var local = new LocalVariable (type_reference, get_temp_name (), null, inner.source_reference);
+                       var decl = new DeclarationStatement (local, source_reference);
+
+                       insert_statement (context.analyzer.insert_block, decl);
+
+                       var temp_access = SemanticAnalyzer.create_temp_access (local, target_type);
+                       temp_access.formal_target_type = formal_target_type;
+
+                       // don't set initializer earlier as this changes parent_node and parent_statement
+                       local.initializer = inner;
+                       decl.check (context);
+
+                       context.analyzer.replaced_nodes.add (this);
+                       parent_node.replace_expression (this, temp_access);
+                       return temp_access.check (context);
+               }
+
                value_type = type_reference;
                value_type.value_owned = inner.value_type.value_owned;
                value_type.floating_reference = inner.value_type.floating_reference;