typing: text formatting
[nit.git] / src / semantize / typing.nit
index 8258cbf..649d564 100644 (file)
@@ -76,6 +76,12 @@ private class TypeVisitor
                end
        end
 
+       # Display a warning on `node` if `not mpropdef.is_fictive`
+       fun display_warning(node: ANode, tag: String, message: String)
+       do
+               if not mpropdef.is_fictive then self.modelbuilder.warning(node, tag, message)
+       end
+
        fun anchor_to(mtype: MType): MType
        do
                return mtype.anchor_to(mmodule, anchor)
@@ -190,9 +196,9 @@ private class TypeVisitor
                if sup == null then return null # Forward error
 
                if sup == sub then
-                       self.modelbuilder.warning(node, "useless-type-test", "Warning: expression is already a `{sup}`.")
+                       display_warning(node, "useless-type-test", "Warning: expression is already a `{sup}`.")
                else if self.is_subtype(sub, sup) then
-                       self.modelbuilder.warning(node, "useless-type-test", "Warning: expression is already a `{sup}` since it is a `{sub}`.")
+                       display_warning(node, "useless-type-test", "Warning: expression is already a `{sup}` since it is a `{sub}`.")
                end
                return sup
        end
@@ -215,16 +221,16 @@ private class TypeVisitor
        fun check_can_be_null(anode: ANode, mtype: MType): Bool
        do
                if mtype isa MNullType then
-                       modelbuilder.warning(anode, "useless-null-test", "Warning: expression is always `null`.")
+                       display_warning(anode, "useless-null-test", "Warning: expression is always `null`.")
                        return true
                end
                if can_be_null(mtype) then return true
 
                if mtype isa MFormalType then
                        var res = anchor_to(mtype)
-                       modelbuilder.warning(anode, "useless-null-test", "Warning: expression is not null, since it is a `{mtype}: {res}`.")
+                       display_warning(anode, "useless-null-test", "Warning: expression is not null, since it is a `{mtype}: {res}`.")
                else
-                       modelbuilder.warning(anode, "useless-null-test", "Warning: expression is not null, since it is a `{mtype}`.")
+                       display_warning(anode, "useless-null-test", "Warning: expression is not null, since it is a `{mtype}`.")
                end
                return false
        end
@@ -315,7 +321,7 @@ private class TypeVisitor
 
                var mproperty = self.try_get_mproperty_by_name2(node, unsafe_type, name)
                if name == "new" and mproperty == null then
-                       name = "autoinit"
+                       name = "defaultinit"
                        mproperty = self.try_get_mproperty_by_name2(node, unsafe_type, name)
                        if mproperty == null then
                                name = "init"
@@ -325,7 +331,12 @@ private class TypeVisitor
 
                if mproperty == null then
                        if recv_is_self then
-                               self.modelbuilder.error(node, "Error: method or variable `{name}` unknown in `{recvtype}`.")
+                               # FIXME This test was added to display a more explicit error when a potential duplication of root object class.
+                               if name == "init" then
+                                       self.modelbuilder.error(node, "Possible duplication of the root class `Object`")
+                               else
+                                       self.modelbuilder.error(node, "Error: method or variable `{name}` unknown in `{recvtype}`.")
+                               end
                        else if recvtype.need_anchor then
                                self.modelbuilder.error(node, "Error: method `{name}` does not exists in `{recvtype}: {unsafe_type}`.")
                        else
@@ -374,9 +385,9 @@ private class TypeVisitor
                if info != null and self.mpropdef.mproperty.deprecation == null then
                        var mdoc = info.mdoc
                        if mdoc != null then
-                               self.modelbuilder.warning(node, "deprecated-method", "Deprecation Warning: method `{mproperty.name}` is deprecated: {mdoc.content.first}")
+                               display_warning(node, "deprecated-method", "Deprecation Warning: method `{mproperty.name}` is deprecated: {mdoc.content.first}")
                        else
-                               self.modelbuilder.warning(node, "deprecated-method", "Deprecation Warning: method `{mproperty.name}` is deprecated.")
+                               display_warning(node, "deprecated-method", "Deprecation Warning: method `{mproperty.name}` is deprecated.")
                        end
                end
 
@@ -389,7 +400,7 @@ private class TypeVisitor
                else if propdefs.length == 1 then
                        mpropdef = propdefs.first
                else
-                       self.modelbuilder.warning(node, "property-conflict", "Warning: conflicting property definitions for property `{mproperty.name}` in `{unsafe_type}`: {propdefs.join(" ")}")
+                       display_warning(node, "property-conflict", "Warning: conflicting property definitions for property `{mproperty.name}` in `{unsafe_type}`: {propdefs.join(" ")}")
                        mpropdef = mproperty.intro
                end
 
@@ -1734,7 +1745,7 @@ redef class AArrayExpr
                end
                if useless then
                        assert ntype != null
-                       v.modelbuilder.warning(ntype, "useless-type", "Warning: useless type declaration `{mtype}` in literal Array since it can be inferred from the elements type.")
+                       v.display_warning(ntype, "useless-type", "Warning: useless type declaration `{mtype}` in literal Array since it can be inferred from the elements type.")
                end
 
                self.element_mtype = mtype
@@ -1778,7 +1789,7 @@ redef class ARangeExpr
                # get the constructor
                var callsite
                if self isa ACrangeExpr then
-                       callsite = v.build_callsite_by_name(self, mtype, "autoinit", false)
+                       callsite = v.build_callsite_by_name(self, mtype, "defaultinit", false)
                else if self isa AOrangeExpr then
                        callsite = v.build_callsite_by_name(self, mtype, "without_last", false)
                else
@@ -1988,9 +1999,7 @@ redef class ASendExpr
 
                var args = compute_raw_arguments
 
-                if not self isa ACallrefExpr then
-                       callsite.check_signature(v, node, args)
-                end
+               if not self isa ACallrefExpr then callsite.check_signature(v, node, args)
 
                if callsite.mproperty.is_init then
                        var vmpropdef = v.mpropdef
@@ -2059,7 +2068,7 @@ redef class AEqFormExpr
                if mtype == null or mtype2 == null then return
 
                if mtype == v.type_bool(self) and (n_expr2 isa AFalseExpr or n_expr2 isa ATrueExpr) then
-                       v.modelbuilder.warning(self, "useless-truism", "Warning: useless comparison to a Bool literal.")
+                       v.display_warning(self, "useless-truism", "Warning: useless comparison to a Bool literal.")
                end
 
                if not mtype2 isa MNullType then return
@@ -2160,7 +2169,7 @@ redef class ABraReassignExpr
 end
 
 redef class AInitExpr
-       redef fun property_name do if n_args.n_exprs.is_empty then return "init" else return "autoinit"
+       redef fun property_name do if n_args.n_exprs.is_empty then return "init" else return "defaultinit"
        redef fun property_node do return n_kwinit
        redef fun compute_raw_arguments do return n_args.to_a
 end
@@ -2576,7 +2585,7 @@ redef class ASafeExpr
                self.mtype = mtype.as_notnull
 
                if not v.can_be_null(mtype) then
-                       v.modelbuilder.warning(self, "useless-safe", "Warning: useless safe operator `?` on non-nullable value.")
+                       v.display_warning(self, "useless-safe", "Warning: useless safe operator `?` on non-nullable value.")
                        return
                end
        end
@@ -2604,7 +2613,7 @@ redef class ADebugTypeExpr
                var mtype = v.resolve_mtype(ntype)
                if mtype != null and mtype != expr then
                        var umtype = v.anchor_to(mtype)
-                       v.modelbuilder.warning(self, "debug", "Found type {expr} (-> {unsafe}), expected {mtype} (-> {umtype})")
+                       v.display_warning(self, "debug", "Found type {expr} (-> {unsafe}), expected {mtype} (-> {umtype})")
                end
                self.is_typed = true
        end