nitc: do not autocast primitive types
authorJean Privat <jean@pryen.org>
Thu, 5 Apr 2018 18:05:46 +0000 (14:05 -0400)
committerJean Privat <jean@pryen.org>
Thu, 5 Apr 2018 18:05:46 +0000 (14:05 -0400)
Signed-off-by: Jean Privat <jean@pryen.org>

src/compiler/abstract_compiler.nit
tests/base_autocast_prim.nit [new file with mode: 0644]
tests/sav/base_autocast_prim.res [new file with mode: 0644]

index 11e4790..ac6edce 100644 (file)
@@ -1392,6 +1392,11 @@ abstract class AbstractCompilerVisitor
                mtype = self.anchor(mtype)
                var valmtype = value.mcasttype
 
+               # CPrimitive is the best you can do
+               if valmtype.is_c_primitive then
+                       return value
+               end
+
                # Do nothing if useless autocast
                if valmtype.is_subtype(self.compiler.mainmodule, null, mtype) then
                        return value
diff --git a/tests/base_autocast_prim.nit b/tests/base_autocast_prim.nit
new file mode 100644 (file)
index 0000000..4dd1e34
--- /dev/null
@@ -0,0 +1,24 @@
+# This file is part of NIT ( http://www.nitlanguage.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.
+
+import core::kernel
+
+var bool = true
+var int = 42
+
+assert int isa Bool
+
+if int == bool then
+       1.output
+end
diff --git a/tests/sav/base_autocast_prim.res b/tests/sav/base_autocast_prim.res
new file mode 100644 (file)
index 0000000..1a261b5
--- /dev/null
@@ -0,0 +1 @@
+Runtime error: Assert failed (base_autocast_prim.nit:20)