syntax: add PType::is_typed
authorJean Privat <jean@pryen.org>
Sat, 4 Jul 2009 02:19:31 +0000 (22:19 -0400)
committerJean Privat <jean@pryen.org>
Sat, 4 Jul 2009 02:19:31 +0000 (22:19 -0400)
Add tests to ensure that type errors do not crash the compiler.

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

src/syntax/typing.nit
tests/error_type_not_ok5.nit [new file with mode: 0644]
tests/sav/error_expr_not_ok_alt2.sav [new file with mode: 0644]
tests/sav/error_type_not_ok5.sav [new file with mode: 0644]

index a0b437f..fad4baa 100644 (file)
@@ -289,6 +289,7 @@ end
 
 redef class PType
        fun stype: MMType do return _stype.as(not null)
+       fun is_typed: Bool do return _stype != null
        var _stype: nullable MMType
 
        redef fun after_typing(v)
@@ -342,6 +343,7 @@ redef class AVardeclExpr
                if n_expr != null then v.variable_ctx.mark_is_set(va)
 
                if n_type != null then
+                       if not n_type.is_typed then return
                        va.stype = n_type.stype
                        if n_expr != null then
                                v.check_conform_expr(n_expr.as(not null), va.stype)
@@ -1248,7 +1250,7 @@ redef class ANewExpr
 special AAbsSendExpr
        redef fun after_typing(v)
        do
-               if n_type._stype == null then return
+               if not n_type.is_typed then return
                var t = n_type.stype
                if t.local_class.global.is_abstract then
                        v.error(self, "Error: try to instantiate abstract class {t.local_class}.")
@@ -1633,6 +1635,7 @@ special PExpr
        private fun check_expr_cast(v: TypingVisitor, n_expr: PExpr, n_type: PType)
        do
                if not v.check_expr(n_expr) then return
+               if not n_type.is_typed then return
                var etype = n_expr.stype
                var ttype = n_type.stype
                if etype == ttype then
@@ -1660,6 +1663,7 @@ special ATypeCheckExpr
        redef fun after_typing(v)
        do
                check_expr_cast(v, n_expr, n_type)
+               if not n_type.is_typed then return
                var variable = n_expr.its_variable
                if variable != null then
                        _if_true_variable_ctx = v.variable_ctx.sub_with(self, variable, n_type.stype)
@@ -1674,6 +1678,7 @@ special ATypeCheckExpr
        redef fun after_typing(v)
        do
                check_expr_cast(v, n_expr, n_type)
+               if not n_type.is_typed then return
                _stype = n_type.stype
                _is_typed = _stype != null
        end
diff --git a/tests/error_type_not_ok5.nit b/tests/error_type_not_ok5.nit
new file mode 100644 (file)
index 0000000..3be2a4a
--- /dev/null
@@ -0,0 +1,32 @@
+# 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.
+
+#alt1#class Fail[E]
+#alt1#end
+
+fun trash(g: Char) do end
+
+var ok: Int = 1
+var a: Fail = 5
+trash(a)
+var na: nullable Fail = null
+trash(na)
+
+trash(ok isa Fail)
+trash(new Fail)
+trash(new Fail.fail(5))
+trash(ok.as(Fail))
+
diff --git a/tests/sav/error_expr_not_ok_alt2.sav b/tests/sav/error_expr_not_ok_alt2.sav
new file mode 100644 (file)
index 0000000..bd116d2
--- /dev/null
@@ -0,0 +1,42 @@
+alt/error_expr_not_ok_alt2.nit:19,19--22: Error: Method or variable 'fail' unknown in A.
+alt/error_expr_not_ok_alt2.nit:21,11--14: Error: Method or variable 'fail' unknown in A.
+alt/error_expr_not_ok_alt2.nit:22,26--29: Error: Method or variable 'fail' unknown in A.
+alt/error_expr_not_ok_alt2.nit:24,8--11: Error: Method or variable 'fail' unknown in A.
+alt/error_expr_not_ok_alt2.nit:25,9--12: Error: Method or variable 'fail' unknown in A.
+alt/error_expr_not_ok_alt2.nit:33,11: Type error: class Fail not found in module error_expr_not_ok_alt2.
+alt/error_expr_not_ok_alt2.nit:33,22: Type error: class Fail not found in module error_expr_not_ok_alt2.
+alt/error_expr_not_ok_alt2.nit:40,1--7: Error: Method 'fail' doesn't exists in Int.
+alt/error_expr_not_ok_alt2.nit:42,7--9: Type error: expected A, got Int
+alt/error_expr_not_ok_alt2.nit:43,1--8: Error: Method 'fail' doesn't exists in Int.
+alt/error_expr_not_ok_alt2.nit:60,20--20: Type error: expected A, got Int
+alt/error_expr_not_ok_alt2.nit:62,7--27: Type error: expected A, got Int
+alt/error_expr_not_ok_alt2.nit:66,21--21: Type error: expected A, got Int
+alt/error_expr_not_ok_alt2.nit:73,7--18: Type error: expected A, got Bool
+alt/error_expr_not_ok_alt2.nit:74,7--18: Type error: expected A, got Bool
+alt/error_expr_not_ok_alt2.nit:75,7--18: Type error: expected A, got Bool
+alt/error_expr_not_ok_alt2.nit:76,7--19: Type error: expected A, got Bool
+alt/error_expr_not_ok_alt2.nit:77,7--19: Type error: expected A, got Bool
+alt/error_expr_not_ok_alt2.nit:78,7--19: Type error: expected A, got Bool
+alt/error_expr_not_ok_alt2.nit:79,7--14: Type error: expected A, got Bool
+alt/error_expr_not_ok_alt2.nit:81,7--15: Type error: expected A, got Bool
+alt/error_expr_not_ok_alt2.nit:84,7--15: Type error: expected A, got Bool
+alt/error_expr_not_ok_alt2.nit:86,7--15: Type error: expected A, got Bool
+alt/error_expr_not_ok_alt2.nit:87,7--15: Type error: expected A, got Bool
+alt/error_expr_not_ok_alt2.nit:88,7--18: Type error: expected A, got Bool
+alt/error_expr_not_ok_alt2.nit:90,7--14: Type error: expected A, got Bool
+alt/error_expr_not_ok_alt2.nit:93,7--14: Type error: expected A, got Bool
+alt/error_expr_not_ok_alt2.nit:96,7--15: Type error: expected A, got Bool
+alt/error_expr_not_ok_alt2.nit:99,7--15: Type error: expected A, got Bool
+alt/error_expr_not_ok_alt2.nit:102,7--16: Type error: expected A, got Int
+alt/error_expr_not_ok_alt2.nit:105,7--14: Type error: expected A, got Int
+alt/error_expr_not_ok_alt2.nit:108,7--14: Type error: expected A, got Int
+alt/error_expr_not_ok_alt2.nit:111,7--14: Type error: expected A, got Int
+alt/error_expr_not_ok_alt2.nit:114,7--14: Type error: expected A, got Int
+alt/error_expr_not_ok_alt2.nit:117,7--14: Type error: expected A, got Int
+alt/error_expr_not_ok_alt2.nit:122,7--15: Error: Attribute _fail doesn't exists in String.
+alt/error_expr_not_ok_alt2.nit:123,1--13: Error: Attribute _fail doesn't exists in String.
+alt/error_expr_not_ok_alt2.nit:124,1--14: Error: Attribute _fail doesn't exists in String.
+alt/error_expr_not_ok_alt2.nit:131,7--14: Type error: expected A, got Int
+alt/error_expr_not_ok_alt2.nit:149,7--24: Type error: expected A, got String
+alt/error_expr_not_ok_alt2.nit:150,7--17: Type error: expected A, got Int
+alt/error_expr_not_ok_alt2.nit:152,7--18: Type error: expected A, got Bool
diff --git a/tests/sav/error_type_not_ok5.sav b/tests/sav/error_type_not_ok5.sav
new file mode 100644 (file)
index 0000000..fc35a2e
--- /dev/null
@@ -0,0 +1,6 @@
+./error_type_not_ok5.nit:23,8: Type error: class Fail not found in module error_type_not_ok5.
+./error_type_not_ok5.nit:25,18: Type error: class Fail not found in module error_type_not_ok5.
+./error_type_not_ok5.nit:28,14: Type error: class Fail not found in module error_type_not_ok5.
+./error_type_not_ok5.nit:29,11: Type error: class Fail not found in module error_type_not_ok5.
+./error_type_not_ok5.nit:30,11: Type error: class Fail not found in module error_type_not_ok5.
+./error_type_not_ok5.nit:31,13: Type error: class Fail not found in module error_type_not_ok5.