syntax: implements __debug__ type construct
authorJean Privat <jean@pryen.org>
Tue, 10 Apr 2012 16:18:47 +0000 (12:18 -0400)
committerJean Privat <jean@pryen.org>
Tue, 10 Apr 2012 18:30:30 +0000 (14:30 -0400)
Signed-off-by: Jean Privat <jean@pryen.org>

12 files changed:
src/syntax/icode_generation.nit
src/syntax/typing.nit
tests/base_types_formal_and_virtual.nit [new file with mode: 0644]
tests/base_types_formal_and_virtual2.nit [new file with mode: 0644]
tests/base_types_formal_and_virtual3.nit [new file with mode: 0644]
tests/base_types_nullable.nit [new file with mode: 0644]
tests/base_types_nullable_formal_and_virtual.nit [new file with mode: 0644]
tests/sav/base_types_formal_and_virtual.sav [new file with mode: 0644]
tests/sav/base_types_formal_and_virtual2.fail [new file with mode: 0644]
tests/sav/base_types_formal_and_virtual3.sav [new file with mode: 0644]
tests/sav/base_types_nullable.sav [new file with mode: 0644]
tests/sav/base_types_nullable_formal_and_virtual.sav [new file with mode: 0644]

index 2d67582..54874d1 100644 (file)
@@ -1361,3 +1361,11 @@ redef class AClosureCallExpr
                return r
        end
 end
+
+redef class ADebugTypeExpr
+       redef fun generate_icode(v)
+       do
+               # Do nothing.
+               return null
+       end
+end
index ffcdec1..d894ea7 100644 (file)
@@ -2141,3 +2141,15 @@ redef class AOnceExpr
        end
 end
 
+redef class ADebugTypeExpr
+       redef fun after_typing(v)
+       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
+                       v.warning(self, "Warning: Expression is a {etype}, expected {ttype}.")
+               end
+       end
+end
diff --git a/tests/base_types_formal_and_virtual.nit b/tests/base_types_formal_and_virtual.nit
new file mode 100644 (file)
index 0000000..4d352d1
--- /dev/null
@@ -0,0 +1,38 @@
+# 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 end
+
+class Object
+end
+
+class Int
+end
+
+class A[E: Object]
+       fun e: E is abstract
+
+       type V: E
+       fun v: V is abstract
+end
+
+class C[G: Int]
+       type U: A[G]
+
+       fun test(u: U)
+       do
+               __debug__ type G: u.e
+               __debug__ type G: u.v
+       end
+end
diff --git a/tests/base_types_formal_and_virtual2.nit b/tests/base_types_formal_and_virtual2.nit
new file mode 100644 (file)
index 0000000..005b436
--- /dev/null
@@ -0,0 +1,71 @@
+# 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 end
+
+class Object
+end
+
+class Int
+end
+
+class Bool
+end
+
+class Char
+end
+
+class A[E, F]
+       type U: F
+
+       fun e: E is abstract
+       fun f: F is abstract
+       fun u: U is abstract
+
+       fun test1(a: A[E, F], ax: A[E, A[U, Int]])
+       do
+               __debug__ type E: self.e # E -> Object
+               __debug__ type F: self.f # F -> Object
+               __debug__ type U: self.u # U -> Object
+
+               __debug__ type E: a.e # E -> Object
+               __debug__ type F: a.f # F -> Object
+               __debug__ type F: a.u # F -> Object
+
+               __debug__ type A[U, Int]: ax.u # A[U, Int] -> A[Object, Int]
+       end
+end
+
+class B[G]
+       super A[Int, G]
+       fun g: G is abstract
+end
+
+class C[H: Char]
+       super B[Bool]
+
+       type W: B[H]
+
+       fun test3(b1: B[H], b2: B[W], w: W)
+       do
+               __debug__ type Int: self.e
+               __debug__ type U: self.u
+
+               __debug__ type H: b1.u
+
+               __debug__ type W: b2.u
+
+               __debug__ type H: w.u
+       end
+end
diff --git a/tests/base_types_formal_and_virtual3.nit b/tests/base_types_formal_and_virtual3.nit
new file mode 100644 (file)
index 0000000..74d0366
--- /dev/null
@@ -0,0 +1,34 @@
+# 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 end
+
+class A
+end
+
+class G[T: A]
+       type U: A
+
+       var at: T
+       var au: U
+
+       fun test(t: T, u: U)
+       do
+               __debug__ type T: t
+               __debug__ type U: u
+
+               __debug__ type T: at
+               __debug__ type U: au
+       end
+end
diff --git a/tests/base_types_nullable.nit b/tests/base_types_nullable.nit
new file mode 100644 (file)
index 0000000..60bfe48
--- /dev/null
@@ -0,0 +1,27 @@
+# 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 end
+
+class A
+       var a: A = self
+       var na: nullable A = self
+end
+
+var a = new A
+__debug__ type A: a
+__debug__ type A: a.a
+__debug__ type nullable A: a.na
+__debug__ type nullable A: a.a.na
+__debug__ type A: a.na.a
diff --git a/tests/base_types_nullable_formal_and_virtual.nit b/tests/base_types_nullable_formal_and_virtual.nit
new file mode 100644 (file)
index 0000000..97942ee
--- /dev/null
@@ -0,0 +1,38 @@
+# 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 end
+
+class A
+       type T: A
+       var t: T = self
+       var nt: nullable T = self
+
+       type U: nullable A
+       var u: U = self
+       var nu: nullable U = self
+       fun test
+       do
+               __debug__ type T : self.t
+               __debug__ type nullable T : self.nt
+               __debug__ type U : self.u
+               __debug__ type nullable U : self.nu
+       end
+end
+
+var a = new A
+__debug__ type A : a.t
+__debug__ type nullable A : a.nt
+__debug__ type nullable A : a.u
+__debug__ type nullable A : a.nu
diff --git a/tests/sav/base_types_formal_and_virtual.sav b/tests/sav/base_types_formal_and_virtual.sav
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/tests/sav/base_types_formal_and_virtual2.fail b/tests/sav/base_types_formal_and_virtual2.fail
new file mode 100644 (file)
index 0000000..340f7f1
--- /dev/null
@@ -0,0 +1 @@
+base_types_formal_and_virtual2.nit:67,3--18: Warning: Expression is a B[H], expected W.
diff --git a/tests/sav/base_types_formal_and_virtual3.sav b/tests/sav/base_types_formal_and_virtual3.sav
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/tests/sav/base_types_nullable.sav b/tests/sav/base_types_nullable.sav
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/tests/sav/base_types_nullable_formal_and_virtual.sav b/tests/sav/base_types_nullable_formal_and_virtual.sav
new file mode 100644 (file)
index 0000000..e69de29