Merge: Hint to bad class names
authorJean Privat <jean@pryen.org>
Sat, 23 Apr 2016 08:31:28 +0000 (04:31 -0400)
committerJean Privat <jean@pryen.org>
Sat, 23 Apr 2016 08:31:28 +0000 (04:31 -0400)
Add hints when a class name cause an error.

Basically 3 cases are considered: spellign, bad visibility and not imported. e.g.

~~~
Error: class `Fail` not found in module `error_unk_class`.
Error: class `Boolean` not found in module `error_unk_class`. Did you mean `Bool`?
Error: class `core::list::ListNode` not visible in module `error_unk_class`.
Error: class `POSet` not found in module `error_unk_class`. Maybe import `poset`?
~~~

Pull-Request: #2034
Reviewed-by: Alexandre Terrasa <alexandre@moz-code.org>
Reviewed-by: Alexis Laferrière <alexis.laf@xymus.net>

src/modelbuilder_base.nit
src/modelize/modelize_property.nit
tests/error_unk_class.nit
tests/error_unk_class2.nit [new file with mode: 0644]
tests/sav/error_unk_class.res
tests/sav/error_unk_class2.res [new file with mode: 0644]

index 06f2396..5c64d23 100644 (file)
@@ -301,7 +301,51 @@ class ModelBuilder
                        end
                end
 
-               # If everything fail, then give up :(
+               # If everything fail, then give up with class by proposing things.
+               #
+               # TODO Give hints on formal types (param and virtual)
+               # TODO How to move this in a libified autonomous code?
+
+               var all_classes = model.get_mclasses_by_name(name)
+
+               # Look for imported but invisible classes.
+               if all_classes != null then for c in all_classes do
+                       if not mmodule.in_importation <= c.intro_mmodule then continue
+                       error(ntype, "Error: class `{c.full_name}` not visible in module `{mmodule}`.")
+                       return null
+               end
+
+               # Look for not imported but known classes from importable modules
+               var hints = new Array[String]
+               if all_classes != null then for c in all_classes do
+                       if c.intro_mmodule.in_importation <= mmodule then continue
+                       if c.visibility <= private_visibility then continue
+                       hints.add "`{c.intro_mmodule.full_name}`"
+               end
+               if hints.not_empty then
+                       error(ntype, "Error: class `{name}` not found in module `{mmodule}`. Maybe import {hints.join(",", " or ")}?")
+                       return null
+               end
+
+               # Look for classes with an approximative name.
+               var bestd = name.length / 2 # limit up to 50% name change
+               for c in model.mclasses do
+                       if not mmodule.in_importation <= c.intro_mmodule then continue
+                       if not mmodule.is_visible(c.intro_mmodule, c.visibility) then continue
+                       var d = name.levenshtein_distance(c.name)
+                       if d <= bestd then
+                               if d < bestd then
+                                       hints.clear
+                                       bestd = d
+                               end
+                               hints.add "`{c.name}`"
+                       end
+               end
+               if hints.not_empty then
+                       error(ntype, "Error: class `{name}` not found in module `{mmodule}`. Did you mean {hints.join(",", " or ")}?")
+                       return null
+               end
+
                error(ntype, "Error: class `{name}` not found in module `{mmodule}`.")
                return null
        end
index df8f8bd..141ed81 100644 (file)
@@ -1463,7 +1463,7 @@ redef class AAttrPropdef
 
                var mlazypropdef = self.mlazypropdef
                if mlazypropdef != null then
-                       mlazypropdef.static_mtype = modelbuilder.model.get_mclasses_by_name("Bool").first.mclass_type
+                       mlazypropdef.static_mtype = mmodule.bool_type
                end
                check_repeated_types(modelbuilder)
        end
index 4d9517f..76ec29d 100644 (file)
@@ -1,7 +1,5 @@
 # This file is part of NIT ( http://www.nitlanguage.org ).
 #
-# Copyright 2004-2008 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
@@ -14,5 +12,9 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-var i = new Canard
+var i
+i = new Fail
+i = new Boolean
+i = new ListNode
+i = new POSet
 i.output
diff --git a/tests/error_unk_class2.nit b/tests/error_unk_class2.nit
new file mode 100644 (file)
index 0000000..c452bda
--- /dev/null
@@ -0,0 +1,16 @@
+# 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 poset
+import error_unk_class
index 7ae3800..13326c8 100644 (file)
@@ -1 +1,4 @@
-error_unk_class.nit:17,13--18: Error: class `Canard` not found in module `error_unk_class`.
+error_unk_class.nit:16,9--12: Error: class `Fail` not found in module `error_unk_class`.
+error_unk_class.nit:17,9--15: Error: class `Boolean` not found in module `error_unk_class`. Did you mean `Bool`?
+error_unk_class.nit:18,9--16: Error: class `core::list::ListNode` not visible in module `error_unk_class`.
+error_unk_class.nit:19,9--13: Error: class `POSet` not found in module `error_unk_class`. Did you mean `Set`?
diff --git a/tests/sav/error_unk_class2.res b/tests/sav/error_unk_class2.res
new file mode 100644 (file)
index 0000000..6b89188
--- /dev/null
@@ -0,0 +1,4 @@
+error_unk_class.nit:16,9--12: Error: class `Fail` not found in module `error_unk_class`.
+error_unk_class.nit:17,9--15: Error: class `Boolean` not found in module `error_unk_class`. Did you mean `Bool`?
+error_unk_class.nit:18,9--16: Error: class `core::list::ListNode` not visible in module `error_unk_class`.
+error_unk_class.nit:19,9--13: Error: class `POSet` not found in module `error_unk_class`. Maybe import `poset`?