syntax: error on local variable masking
authorJean Privat <jean@pryen.org>
Thu, 6 Aug 2009 14:54:49 +0000 (10:54 -0400)
committerJean Privat <jean@pryen.org>
Thu, 6 Aug 2009 15:29:30 +0000 (11:29 -0400)
Signed-off-by: Jean Privat <jean@pryen.org>

22 files changed:
src/syntax/control_flow.nit
tests/base_var2.nit [new file with mode: 0644]
tests/base_var_closure.nit [new file with mode: 0644]
tests/sav/base_var2.sav [new file with mode: 0644]
tests/sav/base_var2_alt1.sav [new file with mode: 0644]
tests/sav/base_var2_alt2.sav [new file with mode: 0644]
tests/sav/base_var2_alt3.sav [new file with mode: 0644]
tests/sav/base_var2_alt4.sav [new file with mode: 0644]
tests/sav/base_var2_alt5.sav [new file with mode: 0644]
tests/sav/base_var2_alt6.sav [new file with mode: 0644]
tests/sav/base_var2_alt7.sav [new file with mode: 0644]
tests/sav/base_var2_alt8.sav [new file with mode: 0644]
tests/sav/base_var2_alt9.sav [new file with mode: 0644]
tests/sav/base_var_closure.sav [new file with mode: 0644]
tests/sav/base_var_closure_alt1.sav [new file with mode: 0644]
tests/sav/base_var_closure_alt2.sav [new file with mode: 0644]
tests/sav/base_var_closure_alt3.sav [new file with mode: 0644]
tests/sav/base_var_closure_alt4.sav [new file with mode: 0644]
tests/sav/base_var_closure_alt5.sav [new file with mode: 0644]
tests/sav/base_var_closure_alt6.sav [new file with mode: 0644]
tests/sav/base_var_closure_alt7.sav [new file with mode: 0644]
tests/sav/base_var_closure_alt8.sav [new file with mode: 0644]

index 4cd879b..4b8d0fc 100644 (file)
@@ -36,6 +36,10 @@ abstract class VariableContext
        # Register a new variable with its name
        fun add(v: Variable)
        do
+               var old_var = self[v.name]
+               if old_var != null then
+                       _visitor.error(v.decl, "Error: '{v}' already defined at {old_var.decl.location.relative_to(v.decl.location)}.")
+               end
                _dico[v.name] = v
                _all_variables.add(v)
        end
diff --git a/tests/base_var2.nit b/tests/base_var2.nit
new file mode 100644 (file)
index 0000000..b04ea58
--- /dev/null
@@ -0,0 +1,52 @@
+# 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.
+
+import array
+
+fun foo(p1: Int)
+do
+       var a1: Int = p1 + 1
+       #alt1#var p1: Int = a1 + 2
+       p1.output
+       a1.output
+end
+
+#alt9#fun bar(p1: Int, p1: Int) do p1.output
+
+foo(0)
+var a1: Int = 10
+do
+       var a2: Int = 11
+       #alt2#var a1: Int = 12
+       a1.output
+       a2.output
+end
+#alt3#var a1: Int = 13
+a1.output
+for a3 in [30, 31] do
+       var a2: Int = 20
+       #alt4#var a1: Int = 1
+       #alt5#var a3: Int = 1
+       a1.output
+       a2.output
+       a3.output
+       #alt6#for a2 in [400, 500] do a2.output
+end
+var a2: Int = 40
+a1.output
+a2.output
+#alt7#a3.output
+#alt8#p1.output
diff --git a/tests/base_var_closure.nit b/tests/base_var_closure.nit
new file mode 100644 (file)
index 0000000..a075d7f
--- /dev/null
@@ -0,0 +1,41 @@
+# 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.
+
+import kernel
+
+fun foo(p1: Int)
+       !bar(p2: Int) do (p1+p2).output
+       #alt1#!bar(p2: Int) do (p1*p2).output
+       #alt2#!p1 do abort
+do
+       bar(p1*10)
+       #alt3#var bar: Int = p1
+       #alt4#p2.output
+end
+
+foo(0)
+var a1: Int = 1
+foo(2) !bar(a2) do
+       #alt5#var a1: Int = 3
+       #alt6#var a2: Int = 3
+       #alt7#foo(3) !bar(a1) do a1.output
+       #alt8#foo(4) !bar(a2) do a1.output
+       a1.output
+       a2.output
+end
+var a2: Int = 5
+a1.output
+a2.output
diff --git a/tests/sav/base_var2.sav b/tests/sav/base_var2.sav
new file mode 100644 (file)
index 0000000..b6dc27f
--- /dev/null
@@ -0,0 +1,13 @@
+0
+1
+10
+11
+10
+10
+20
+30
+10
+20
+31
+10
+40
diff --git a/tests/sav/base_var2_alt1.sav b/tests/sav/base_var2_alt1.sav
new file mode 100644 (file)
index 0000000..9347b7e
--- /dev/null
@@ -0,0 +1 @@
+alt/base_var2_alt1.nit:22,6--7: Error: 'p1' already defined at 19,9--10.
diff --git a/tests/sav/base_var2_alt2.sav b/tests/sav/base_var2_alt2.sav
new file mode 100644 (file)
index 0000000..14fb585
--- /dev/null
@@ -0,0 +1 @@
+alt/base_var2_alt2.nit:33,6--7: Error: 'a1' already defined at 30,5--6.
diff --git a/tests/sav/base_var2_alt3.sav b/tests/sav/base_var2_alt3.sav
new file mode 100644 (file)
index 0000000..6e4f5ef
--- /dev/null
@@ -0,0 +1 @@
+alt/base_var2_alt3.nit:37,5--6: Error: 'a1' already defined at 30,5--6.
diff --git a/tests/sav/base_var2_alt4.sav b/tests/sav/base_var2_alt4.sav
new file mode 100644 (file)
index 0000000..5a63a89
--- /dev/null
@@ -0,0 +1 @@
+alt/base_var2_alt4.nit:41,6--7: Error: 'a1' already defined at 30,5--6.
diff --git a/tests/sav/base_var2_alt5.sav b/tests/sav/base_var2_alt5.sav
new file mode 100644 (file)
index 0000000..f1b08e3
--- /dev/null
@@ -0,0 +1 @@
+alt/base_var2_alt5.nit:42,6--7: Error: 'a3' already defined at 39,5--6.
diff --git a/tests/sav/base_var2_alt6.sav b/tests/sav/base_var2_alt6.sav
new file mode 100644 (file)
index 0000000..7025146
--- /dev/null
@@ -0,0 +1 @@
+alt/base_var2_alt6.nit:46,6--7: Error: 'a2' already defined at 40,6--7.
diff --git a/tests/sav/base_var2_alt7.sav b/tests/sav/base_var2_alt7.sav
new file mode 100644 (file)
index 0000000..740d76a
--- /dev/null
@@ -0,0 +1 @@
+alt/base_var2_alt7.nit:51,1--2: Error: Method or variable 'a3' unknown in Sys.
diff --git a/tests/sav/base_var2_alt8.sav b/tests/sav/base_var2_alt8.sav
new file mode 100644 (file)
index 0000000..1d6e83d
--- /dev/null
@@ -0,0 +1 @@
+alt/base_var2_alt8.nit:52,1--2: Error: Method or variable 'p1' unknown in Sys.
diff --git a/tests/sav/base_var2_alt9.sav b/tests/sav/base_var2_alt9.sav
new file mode 100644 (file)
index 0000000..af676c4
--- /dev/null
@@ -0,0 +1 @@
+alt/base_var2_alt9.nit:27,18--19: Error: 'p1' already defined at 27,9--10.
diff --git a/tests/sav/base_var_closure.sav b/tests/sav/base_var_closure.sav
new file mode 100644 (file)
index 0000000..874295b
--- /dev/null
@@ -0,0 +1,5 @@
+0
+1
+20
+1
+5
diff --git a/tests/sav/base_var_closure_alt1.sav b/tests/sav/base_var_closure_alt1.sav
new file mode 100644 (file)
index 0000000..7e375ca
--- /dev/null
@@ -0,0 +1 @@
+alt/base_var_closure_alt1.nit:21,3--5: A closure '!bar' already defined at 20,3--5.
diff --git a/tests/sav/base_var_closure_alt2.sav b/tests/sav/base_var_closure_alt2.sav
new file mode 100644 (file)
index 0000000..57162df
--- /dev/null
@@ -0,0 +1,2 @@
+alt/base_var_closure_alt2.nit:22,3--4: Error: 'p1' already defined at 19,9--10.
+alt/base_var_closure_alt2.nit:24,6--7: Type error: expected expression.
diff --git a/tests/sav/base_var_closure_alt3.sav b/tests/sav/base_var_closure_alt3.sav
new file mode 100644 (file)
index 0000000..b3524b4
--- /dev/null
@@ -0,0 +1 @@
+alt/base_var_closure_alt3.nit:25,6--8: Error: 'bar' already defined at 20,3--5.
diff --git a/tests/sav/base_var_closure_alt4.sav b/tests/sav/base_var_closure_alt4.sav
new file mode 100644 (file)
index 0000000..3a55540
--- /dev/null
@@ -0,0 +1 @@
+alt/base_var_closure_alt4.nit:26,2--3: Error: Method or variable 'p2' unknown in Object.
diff --git a/tests/sav/base_var_closure_alt5.sav b/tests/sav/base_var_closure_alt5.sav
new file mode 100644 (file)
index 0000000..e20bb39
--- /dev/null
@@ -0,0 +1 @@
+alt/base_var_closure_alt5.nit:32,6--7: Error: 'a1' already defined at 30,5--6.
diff --git a/tests/sav/base_var_closure_alt6.sav b/tests/sav/base_var_closure_alt6.sav
new file mode 100644 (file)
index 0000000..f4c1c54
--- /dev/null
@@ -0,0 +1 @@
+alt/base_var_closure_alt6.nit:33,6--7: Error: 'a2' already defined at 31,13--14.
diff --git a/tests/sav/base_var_closure_alt7.sav b/tests/sav/base_var_closure_alt7.sav
new file mode 100644 (file)
index 0000000..74a3885
--- /dev/null
@@ -0,0 +1 @@
+alt/base_var_closure_alt7.nit:34,14--15: Error: 'a1' already defined at 30,5--6.
diff --git a/tests/sav/base_var_closure_alt8.sav b/tests/sav/base_var_closure_alt8.sav
new file mode 100644 (file)
index 0000000..f84b921
--- /dev/null
@@ -0,0 +1 @@
+alt/base_var_closure_alt8.nit:35,14--15: Error: 'a2' already defined at 31,13--14.