From 6b5551e37c36bba37a42b9f5a8cb475dcb057cff Mon Sep 17 00:00:00 2001 From: Jean Privat Date: Thu, 11 Jun 2009 22:41:48 -0400 Subject: [PATCH] syntax: Update check_conform_multiexpr with nullables Add also some tests. Signed-off-by: Jean Privat --- src/syntax/syntax_base.nit | 7 +++++++ tests/sav/test_array_comb_alt1.sav | 1 + tests/sav/test_array_comb_alt2.sav | 1 + tests/test_array_comb.nit | 24 ++++++++++++++++++++++++ 4 files changed, 33 insertions(+) create mode 100644 tests/sav/test_array_comb.sav create mode 100644 tests/sav/test_array_comb_alt1.sav create mode 100644 tests/sav/test_array_comb_alt2.sav create mode 100644 tests/test_array_comb.nit diff --git a/src/syntax/syntax_base.nit b/src/syntax/syntax_base.nit index e5b492d..ab1ced0 100644 --- a/src/syntax/syntax_base.nit +++ b/src/syntax/syntax_base.nit @@ -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 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 index 0000000..7e94e0f --- /dev/null +++ b/tests/sav/test_array_comb_alt1.sav @@ -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 index 0000000..d7042b5 --- /dev/null +++ b/tests/sav/test_array_comb_alt2.sav @@ -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 index 0000000..0f6b132 --- /dev/null +++ b/tests/test_array_comb.nit @@ -0,0 +1,24 @@ +# This file is part of NIT ( http://www.nitlanguage.org ). +# +# Copyright 2009 Jean Privat +# +# 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"] -- 1.7.9.5