Merge: typing: Add a mechanism to disable warnings
authorJean Privat <jean@pryen.org>
Sat, 25 Apr 2020 07:12:47 +0000 (03:12 -0400)
committerJean Privat <jean@pryen.org>
Sat, 25 Apr 2020 07:12:47 +0000 (03:12 -0400)
Add a mechanism to suppress warnings during the typing phase. I also took the opportunity to modify a bad indentation in the file :smile: .

Pull-Request: #2818
Reviewed-by: Jean Privat <jean@pryen.org>

1  2 
src/semantize/typing.nit

diff --combined src/semantize/typing.nit
@@@ -76,6 -76,12 +76,12 @@@ private class TypeVisito
                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)
                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
        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
                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
  
                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
  
@@@ -1032,18 -1038,6 +1038,18 @@@ redef class AExp
                end
                return res
        end
 +
 +      # Type the expression as if located in `visited_mpropdef`
 +      # `TypeVisitor` and `PostTypingVisitor` will be used to do the typing, see them for more information.
 +      #
 +      # `visited_mpropdef`: Correspond to the evaluation context in which the expression is located.
 +      fun do_typing(modelbuilder: ModelBuilder, visited_mpropdef: MPropDef)
 +      do
 +              var type_visitor = new TypeVisitor(modelbuilder, visited_mpropdef)
 +              type_visitor.visit_stmt(self)
 +              var post_visitor = new PostTypingVisitor(type_visitor)
 +              post_visitor.enter_visit(self)
 +      end
  end
  
  redef class ABlockExpr
@@@ -1751,7 -1745,7 +1757,7 @@@ redef class AArrayExp
                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
@@@ -2005,9 -1999,7 +2011,7 @@@ redef class ASendExp
  
                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
@@@ -2076,7 -2068,7 +2080,7 @@@ redef class AEqFormExp
                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
@@@ -2593,7 -2585,7 +2597,7 @@@ redef class ASafeExp
                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
@@@ -2621,7 -2613,7 +2625,7 @@@ redef class ADebugTypeExp
                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