syntax: Update check_conform_multiexpr with nullables
authorJean Privat <jean@pryen.org>
Fri, 12 Jun 2009 02:41:48 +0000 (22:41 -0400)
committerJean Privat <jean@pryen.org>
Wed, 24 Jun 2009 20:01:53 +0000 (16:01 -0400)
Add also some tests.

Signed-off-by: Jean Privat <jean@pryen.org>

src/syntax/syntax_base.nit
tests/sav/test_array_comb.sav [new file with mode: 0644]
tests/sav/test_array_comb_alt1.sav [new file with mode: 0644]
tests/sav/test_array_comb_alt2.sav [new file with mode: 0644]
tests/test_array_comb.nit [new file with mode: 0644]

index e5b492d..ab1ced0 100644 (file)
@@ -401,16 +401,23 @@ special Visitor
        # Conformance is granted if among them there is a most general type
        # Return the most general type if a conformance is found
        # Display an error and return null if no conformance is found
+       # The only allowed combinaison is with the nullable marker
        # @param stype is a possible additional type (without node)
        # Examples:
        #   Int, Int, Object => return Object
        #   Int, Float => display error, return null
+       #   nullable Int, Object => return nullable Object
        meth check_conform_multiexpr(stype: MMType, nodes: Collection[PExpr]): MMType
        do
                var node: PExpr = null # candidate node
                for n in nodes do
                        if not check_expr(n) then return null
                        var ntype = n.stype
+                       if stype != null and stype.is_nullable != ntype.is_nullable then
+                               # nullable combinaison: if one of them is nulable, considers that both are
+                               stype = stype.as_nullable
+                               ntype = ntype.as_nullable
+                       end
                        if stype == null or (ntype != null and stype < ntype) then
                                stype = ntype
                                node = n
diff --git a/tests/sav/test_array_comb.sav b/tests/sav/test_array_comb.sav
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/tests/sav/test_array_comb_alt1.sav b/tests/sav/test_array_comb_alt1.sav
new file mode 100644 (file)
index 0000000..7e94e0f
--- /dev/null
@@ -0,0 +1 @@
+alt/test_array_comb_alt1.nit:23,19--21: Type error: no most general type. Got String and nullable Int at alt/test_array_comb_alt1.nit:23,16--16.
diff --git a/tests/sav/test_array_comb_alt2.sav b/tests/sav/test_array_comb_alt2.sav
new file mode 100644 (file)
index 0000000..d7042b5
--- /dev/null
@@ -0,0 +1 @@
+alt/test_array_comb_alt2.nit:24,19--21: Type error: no most general type. Got String and nullable Int at alt/test_array_comb_alt2.nit:24,16--16.
diff --git a/tests/test_array_comb.nit b/tests/test_array_comb.nit
new file mode 100644 (file)
index 0000000..0f6b132
--- /dev/null
@@ -0,0 +1,24 @@
+# This file is part of NIT ( http://www.nitlanguage.org ).
+#
+# Copyright 2009 Jean Privat <jean@pryen.org>
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+var a = [5, null]
+var b = [null, 5]
+var c: nullable Int = 5
+var d = [5, c]
+var e = [c, 5]
+var f = [c, null]
+#alt1#var g = [null, 5, "5"]
+#alt2#var h = [null, c, "5"]