[vala/wip/issue/991] vala: Forbid cast of floating-type literal to pointer
- From: Rico Tzschichholz <ricotz src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [vala/wip/issue/991] vala: Forbid cast of floating-type literal to pointer
- Date: Sat, 9 May 2020 16:23:50 +0000 (UTC)
commit a82ad5fd26a0da0b1a95c7470341e068523b5951
Author: Rico Tzschichholz <ricotz ubuntu com>
Date: Sat May 9 13:35:07 2020 +0200
vala: Forbid cast of floating-type literal to pointer
Fixes https://gitlab.gnome.org/GNOME/vala/issues/991
tests/Makefile.am | 2 ++
tests/basic-types/floats-boxed-cast.vala | 7 +++++++
tests/generics/floating-type-cast.vala | 9 +++++++++
vala/valacastexpression.vala | 21 +++++++++++++++++++++
4 files changed, 39 insertions(+)
---
diff --git a/tests/Makefile.am b/tests/Makefile.am
index d5038fd9d..2bbd07efe 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -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 \
@@ -569,6 +570,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
index 000000000..86e9f52cf
--- /dev/null
+++ b/tests/basic-types/floats-boxed-cast.vala
@@ -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
index 000000000..9475a4219
--- /dev/null
+++ b/tests/generics/floating-type-cast.vala
@@ -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);
+}
diff --git a/vala/valacastexpression.vala b/vala/valacastexpression.vala
index d51b4e205..45d5d1e83 100644
--- a/vala/valacastexpression.vala
+++ b/vala/valacastexpression.vala
@@ -172,6 +172,27 @@ public class Vala.CastExpression : Expression {
}
}
+ // Implicit transformation of stack-allocated floating-type to boxed-type
+ if (!(is_silent_cast || is_non_null_cast) && inner.value_type is FloatingType &&
!inner.value_type.nullable) {
+ if (type_reference is PointerType || (type_reference is ValueType &&
type_reference.nullable)) {
+ 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;
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]