modelbuilder: error when a module defines a class more than once
authorJean Privat <jean@pryen.org>
Thu, 8 Nov 2012 19:22:07 +0000 (14:22 -0500)
committerJean Privat <jean@pryen.org>
Thu, 8 Nov 2012 19:22:07 +0000 (14:22 -0500)
Signed-off-by: Jean Privat <jean@pryen.org>

15 files changed:
src/modelbuilder.nit
tests/error_redef3.nit [new file with mode: 0644]
tests/error_redef4.nit [new file with mode: 0644]
tests/error_redef4_base.nit [new file with mode: 0644]
tests/sav/error_cons_arity.res [deleted file]
tests/sav/error_cons_arity2.res [deleted file]
tests/sav/error_constraint.res [deleted file]
tests/sav/error_defs_alt6.res [deleted file]
tests/sav/error_defs_alt7.res [deleted file]
tests/sav/error_defs_alt8.res [deleted file]
tests/sav/error_redef3.sav [new file with mode: 0644]
tests/sav/error_redef4.sav [new file with mode: 0644]
tests/sav/test_multiconstraint.res [deleted file]
tests/sav/test_multiconstraint_ref.res [deleted file]
tests/sav/test_multiconstraint_ref2.res [deleted file]

index 4809149..4b2bac7 100644 (file)
@@ -458,6 +458,9 @@ class ModelBuilder
                if mclass == null then
                        mclass = new MClass(mmodule, name, arity, mkind, mvisibility)
                        #print "new class {mclass}"
+               else if nclassdef isa AStdClassdef and nmodule.mclass2nclassdef.has_key(mclass) then
+                       error(nclassdef, "Error: A class {name} is already defined at line {nmodule.mclass2nclassdef[mclass].location.line_start}.")
+                       return
                else if nclassdef isa AStdClassdef and nclassdef.n_kwredef == null then
                        error(nclassdef, "Redef error: {name} is an imported class. Add the redef keyword to refine it.")
                        return
@@ -470,6 +473,7 @@ class ModelBuilder
                        error(nvisibility, "Error: refinement changed the visibility from a {mclass.visibility} to a {mvisibility}")
                end
                nclassdef.mclass = mclass
+               nmodule.mclass2nclassdef[mclass] = nclassdef
        end
 
        # Visit the AST and create the MClassDef objects
@@ -850,6 +854,10 @@ redef class AModule
        var is_importation_done: Bool = false
        # Flag that indicate if the class and prop building is already completed
        var build_classes_is_done: Bool = false
+       # What is the AClassdef associated to a MClass?
+       # Used to check multiple definition of a class.
+       var mclass2nclassdef: Map[MClass, AClassdef] = new HashMap[MClass, AClassdef]
+
 end
 
 redef class MClass
diff --git a/tests/error_redef3.nit b/tests/error_redef3.nit
new file mode 100644 (file)
index 0000000..b7075d6
--- /dev/null
@@ -0,0 +1,17 @@
+# 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.
+
+class A end
+class A end
+
diff --git a/tests/error_redef4.nit b/tests/error_redef4.nit
new file mode 100644 (file)
index 0000000..cf2d3c9
--- /dev/null
@@ -0,0 +1,19 @@
+# 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 error_redef4_base
+
+redef class A end
+redef class A end
+
diff --git a/tests/error_redef4_base.nit b/tests/error_redef4_base.nit
new file mode 100644 (file)
index 0000000..2ececf3
--- /dev/null
@@ -0,0 +1,15 @@
+# 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.
+
+class A end
diff --git a/tests/sav/error_cons_arity.res b/tests/sav/error_cons_arity.res
deleted file mode 100644 (file)
index 96d4c0f..0000000
+++ /dev/null
@@ -1 +0,0 @@
-error_cons_arity.nit:19,7: Redef error: C is an imported class. Add the redef keyword to refine it.
diff --git a/tests/sav/error_cons_arity2.res b/tests/sav/error_cons_arity2.res
deleted file mode 100644 (file)
index ea3103f..0000000
+++ /dev/null
@@ -1 +0,0 @@
-error_cons_arity2.nit:20,7: Redef error: A is an imported class. Add the redef keyword to refine it.
diff --git a/tests/sav/error_constraint.res b/tests/sav/error_constraint.res
deleted file mode 100644 (file)
index 1fe2eda..0000000
+++ /dev/null
@@ -1 +0,0 @@
-error_constraint.nit:20,7: Redef error: A is an imported class. Add the redef keyword to refine it.
diff --git a/tests/sav/error_defs_alt6.res b/tests/sav/error_defs_alt6.res
deleted file mode 100644 (file)
index 1de695d..0000000
+++ /dev/null
@@ -1 +0,0 @@
-alt/error_defs_alt6.nit:35,7: Redef error: A is an imported class. Add the redef keyword to refine it.
diff --git a/tests/sav/error_defs_alt7.res b/tests/sav/error_defs_alt7.res
deleted file mode 100644 (file)
index 1c267af..0000000
+++ /dev/null
@@ -1 +0,0 @@
-alt/error_defs_alt7.nit:25,6--8: Redef error: A::foo is an inherited property. To redefine it, add the redef keyword.
diff --git a/tests/sav/error_defs_alt8.res b/tests/sav/error_defs_alt8.res
deleted file mode 100644 (file)
index 7f7cb25..0000000
+++ /dev/null
@@ -1,2 +0,0 @@
-alt/error_defs_alt8.nit:25,6--8: Redef error: A::foo is an inherited property. To redefine it, add the redef keyword.
-alt/error_defs_alt8.nit:42,11--13: Error: No property A::foo is inherited. Remove the redef keyword to define a new property.
diff --git a/tests/sav/error_redef3.sav b/tests/sav/error_redef3.sav
new file mode 100644 (file)
index 0000000..9a726e6
--- /dev/null
@@ -0,0 +1 @@
+error_redef3.nit:16,7: Error: A class A is already defined at line 15.
diff --git a/tests/sav/error_redef4.sav b/tests/sav/error_redef4.sav
new file mode 100644 (file)
index 0000000..944e2cf
--- /dev/null
@@ -0,0 +1 @@
+error_redef4.nit:18,13: Error: A class A is already defined at line 17.
diff --git a/tests/sav/test_multiconstraint.res b/tests/sav/test_multiconstraint.res
deleted file mode 100644 (file)
index bd70a22..0000000
+++ /dev/null
@@ -1,2 +0,0 @@
-test_multiconstraint.nit:64,7: Redef error: G is an imported class. Add the redef keyword to refine it.
-test_multiconstraint.nit:70,7: Redef error: G is an imported class. Add the redef keyword to refine it.
diff --git a/tests/sav/test_multiconstraint_ref.res b/tests/sav/test_multiconstraint_ref.res
deleted file mode 100644 (file)
index 38d3644..0000000
+++ /dev/null
@@ -1,8 +0,0 @@
-test_multiconstraint.nit:19,7: Redef error: A is an imported class. Add the redef keyword to refine it.
-test_multiconstraint.nit:28,7: Redef error: B is an imported class. Add the redef keyword to refine it.
-test_multiconstraint.nit:42,7: Redef error: C is an imported class. Add the redef keyword to refine it.
-test_multiconstraint.nit:56,7: Redef error: G is an imported class. Add the redef keyword to refine it.
-test_multiconstraint.nit:64,7: Redef error: G is an imported class. Add the redef keyword to refine it.
-test_multiconstraint.nit:64,7: Redef error: G is an imported class. Add the redef keyword to refine it.
-test_multiconstraint.nit:70,7: Redef error: G is an imported class. Add the redef keyword to refine it.
-test_multiconstraint.nit:70,7: Redef error: G is an imported class. Add the redef keyword to refine it.
diff --git a/tests/sav/test_multiconstraint_ref2.res b/tests/sav/test_multiconstraint_ref2.res
deleted file mode 100644 (file)
index 38d3644..0000000
+++ /dev/null
@@ -1,8 +0,0 @@
-test_multiconstraint.nit:19,7: Redef error: A is an imported class. Add the redef keyword to refine it.
-test_multiconstraint.nit:28,7: Redef error: B is an imported class. Add the redef keyword to refine it.
-test_multiconstraint.nit:42,7: Redef error: C is an imported class. Add the redef keyword to refine it.
-test_multiconstraint.nit:56,7: Redef error: G is an imported class. Add the redef keyword to refine it.
-test_multiconstraint.nit:64,7: Redef error: G is an imported class. Add the redef keyword to refine it.
-test_multiconstraint.nit:64,7: Redef error: G is an imported class. Add the redef keyword to refine it.
-test_multiconstraint.nit:70,7: Redef error: G is an imported class. Add the redef keyword to refine it.
-test_multiconstraint.nit:70,7: Redef error: G is an imported class. Add the redef keyword to refine it.