typing: new PostTypingVisitor that is run after the visit
[nit.git] / src / semantize / typing.nit
index 97e200c..ffd68e7 100644 (file)
@@ -301,15 +301,9 @@ private class TypeVisitor
 
                #debug("recv: {recvtype} (aka {unsafe_type})")
                if recvtype isa MNullType then
-                       # `null` only accepts some methods of object.
-                       if name == "==" or name == "!=" or name == "is_same_instance" then
-                               var objclass = get_mclass(node, "Object")
-                               if objclass == null then return null # Forward error
-                               unsafe_type = objclass.mclass_type
-                       else
-                               self.error(node, "Error: method `{name}` called on `null`.")
-                               return null
-                       end
+                       var objclass = get_mclass(node, "Object")
+                       if objclass == null then return null # Forward error
+                       unsafe_type = objclass.mclass_type
                end
 
                var mproperty = self.try_get_mproperty_by_name2(node, unsafe_type, name)
@@ -331,6 +325,14 @@ private class TypeVisitor
 
                assert mproperty isa MMethod
 
+               # `null` only accepts some methods of object.
+               if recvtype isa MNullType and not mproperty.is_null_safe then
+                       self.error(node, "Error: method `{name}` called on `null`.")
+                       return null
+               else if unsafe_type isa MNullableType and not mproperty.is_null_safe then
+                       modelbuilder.advice(node, "call-on-nullable", "Warning: method call on a nullable receiver `{recvtype}`.")
+               end
+
                if is_toplevel_context and recv_is_self and not mproperty.is_toplevel then
                        error(node, "Error: `{name}` is not a top-level method, thus need a receiver.")
                end
@@ -407,8 +409,18 @@ private class TypeVisitor
                                return null
                        end
                else if args.length != msignature.arity then
-                       modelbuilder.error(node, "Error: expected {msignature.arity} argument(s) for `{mproperty}{msignature}`; got {args.length}. See introduction at `{mproperty.full_name}`.")
-                       return null
+                       if msignature.arity == msignature.min_arity then
+                               modelbuilder.error(node, "Error: expected {msignature.arity} argument(s) for `{mproperty}{msignature}`; got {args.length}. See introduction at `{mproperty.full_name}`.")
+                               return null
+                       end
+                       if args.length > msignature.arity then
+                               modelbuilder.error(node, "Error: expected at most {msignature.arity} argument(s) for `{mproperty}{msignature}`; got {args.length}. See introduction at `{mproperty.full_name}`.")
+                               return null
+                       end
+                       if args.length < msignature.min_arity then
+                               modelbuilder.error(node, "Error: expected at least {msignature.min_arity} argument(s) for `{mproperty}{msignature}`; got {args.length}. See introduction at `{mproperty.full_name}`.")
+                               return null
+                       end
                end
 
                #debug("CALL {unsafe_type}.{msignature}")
@@ -416,10 +428,51 @@ private class TypeVisitor
                # Associate each parameter to a position in the arguments
                var map = new SignatureMap
 
+               var setted = args.length - msignature.min_arity
+
+               # First, handle named arguments
+               for i in [0..args.length[ do
+                       var e = args[i]
+                       if not e isa ANamedargExpr then continue
+                       var name = e.n_id.text
+                       var param = msignature.mparameter_by_name(name)
+                       if param == null then
+                               modelbuilder.error(e.n_id, "Error: no parameter `{name}` for `{mproperty}{msignature}`.")
+                               return null
+                       end
+                       if not param.is_default then
+                               modelbuilder.error(e, "Error: parameter `{name}` is not optional for `{mproperty}{msignature}`.")
+                               return null
+                       end
+                       var idx = msignature.mparameters.index_of(param)
+                       var prev = map.map.get_or_null(idx)
+                       if prev != null then
+                               modelbuilder.error(e, "Error: parameter `{name}` already associated with argument #{prev} for `{mproperty}{msignature}`.")
+                               return null
+                       end
+                       map.map[idx] = i
+                       setted -= 1
+                       e.mtype = self.visit_expr_subtype(e.n_expr, param.mtype)
+               end
+
+               # Second, associate remaining parameters
                var vararg_decl = args.length - msignature.arity
                var j = 0
                for i in [0..msignature.arity[ do
+                       # Skip parameters associated by name
+                       if map.map.has_key(i) then continue
+
                        var param = msignature.mparameters[i]
+                       if param.is_default then
+                               if setted > 0 then
+                                       setted -= 1
+                               else
+                                       continue
+                               end
+                       end
+
+                       # Search the next free argument: skip named arguments since they are already associated
+                       while args[j] isa ANamedargExpr do j += 1
                        var arg = args[j]
                        map.map[i] = j
                        j += 1
@@ -432,15 +485,31 @@ private class TypeVisitor
                        var paramtype = param.mtype
                        self.visit_expr_subtype(arg, paramtype)
                end
+
+               # Third, check varargs
                if vararg_rank >= 0 then
                        var paramtype = msignature.mparameters[vararg_rank].mtype
                        var first = args[vararg_rank]
-                       if vararg_decl == 0 and first isa AVarargExpr then
+                       if vararg_decl == 0 then
                                var mclass = get_mclass(node, "Array")
                                if mclass == null then return null # Forward error
                                var array_mtype = mclass.get_mtype([paramtype])
-                               self.visit_expr_subtype(first.n_expr, array_mtype)
-                               first.mtype  = first.n_expr.mtype
+                               if first isa AVarargExpr then
+                                       self.visit_expr_subtype(first.n_expr, array_mtype)
+                                       first.mtype  = first.n_expr.mtype
+                               else
+                                       # only one vararg, maybe `...` was forgot, so be gentle!
+                                       var t = visit_expr(first)
+                                       if t == null then return null # Forward error
+                                       if not is_subtype(t, paramtype) and is_subtype(t, array_mtype) then
+                                               # Not acceptable but could be a `...`
+                                               error(first, "Type Error: expected `{paramtype}`, got `{t}`. Is an ellipsis `...` missing on the argument?")
+                                               return null
+                                       end
+                                       # Standard valid vararg, finish the job
+                                       map.vararg_decl = 1
+                                       self.visit_expr_subtype(first, paramtype)
+                               end
                        else
                                map.vararg_decl = vararg_decl + 1
                                for i in [vararg_rank..vararg_rank+vararg_decl] do
@@ -579,7 +648,7 @@ end
 
 redef class Variable
        # The declared type of the variable
-       var declared_type: nullable MType
+       var declared_type: nullable MType is writable
 
        # Was the variable type-adapted?
        # This is used to speedup type retrieval while it remains `false`
@@ -679,6 +748,9 @@ redef class AMethPropdef
                        if not v.has_loop or not v.dirty then break
                end
 
+               var post_visitor = new PostTypingVisitor(v)
+               post_visitor.enter_visit(self)
+
                if not nblock.after_flow_context.is_unreachable and msignature.return_mtype != null then
                        # We reach the end of the function without having a return, it is bad
                        v.error(self, "Error: reached end of function; expected `return` with a value.")
@@ -686,20 +758,33 @@ redef class AMethPropdef
        end
 end
 
+private class PostTypingVisitor
+       super Visitor
+       var type_visitor: TypeVisitor
+       redef fun visit(n) do
+               n.visit_all(self)
+               n.accept_post_typing(type_visitor)
+       end
+end
+
+redef class ANode
+       private fun accept_post_typing(v: TypeVisitor) do end
+end
+
 redef class AAttrPropdef
        redef fun do_typing(modelbuilder: ModelBuilder)
        do
                if not has_value then return
 
-               var mpropdef = self.mpropdef
-               if mpropdef == null then return # skip error
+               var mpropdef = self.mreadpropdef
+               if mpropdef == null or mpropdef.msignature == null then return # skip error
 
                var v = new TypeVisitor(modelbuilder, mpropdef.mclassdef.mmodule, mpropdef)
                self.selfvariable = v.selfvariable
 
                var nexpr = self.n_expr
                if nexpr != null then
-                       var mtype = self.mpropdef.static_mtype
+                       var mtype = self.mtype
                        v.visit_expr_subtype(nexpr, mtype)
                end
                var nblock = self.n_block
@@ -891,7 +976,7 @@ redef class AVarReassignExpr
 
                v.set_variable(self, variable, rettype)
 
-               self.is_typed = true
+               self.is_typed = rettype != null
        end
 end
 
@@ -937,9 +1022,11 @@ redef class AReturnExpr
                        else
                                v.visit_expr(nexpr)
                                v.error(nexpr, "Error: `return` with value in a procedure.")
+                               return
                        end
                else if ret_type != null then
                        v.error(self, "Error: `return` without value in a function.")
+                       return
                end
                self.is_typed = true
        end
@@ -1233,7 +1320,8 @@ redef class AOrElseExpr
                end
 
                if t1 isa MNullType then
-                       v.error(n_expr, "Type Error: `or else` on `null`.")
+                       self.mtype = t2
+                       return
                else if v.check_can_be_null(n_expr, t1) then
                        t1 = t1.as_notnull
                end
@@ -1275,6 +1363,15 @@ redef class AIntExpr
        end
 end
 
+redef class AByteExpr
+       redef fun accept_typing(v)
+       do
+               var mclass = v.get_mclass(self, "Byte")
+               if mclass == null then return # Forward error
+               self.mtype = mclass.mclass_type
+       end
+end
+
 redef class AFloatExpr
        redef fun accept_typing(v)
        do
@@ -1608,14 +1705,8 @@ redef class ABinopExpr
        redef fun property_name do return operator
        redef fun property_node do return n_op
 end
-redef class AEqExpr
-       redef fun accept_typing(v)
-       do
-               super
-               v.null_test(self)
-       end
-end
-redef class ANeExpr
+
+redef class AEqFormExpr
        redef fun accept_typing(v)
        do
                super
@@ -1623,13 +1714,8 @@ redef class ANeExpr
        end
 end
 
-redef class AUplusExpr
-       redef fun property_name do return "unary +"
-       redef fun compute_raw_arguments do return new Array[AExpr]
-end
-
-redef class AUminusExpr
-       redef fun property_name do return "unary -"
+redef class AUnaryopExpr
+       redef fun property_name do return "unary {operator}"
        redef fun compute_raw_arguments do return new Array[AExpr]
 end
 
@@ -1988,7 +2074,7 @@ redef class AAttrAssignExpr
                var mtype = self.attr_type
 
                v.visit_expr_subtype(self.n_value, mtype)
-               self.is_typed = true
+               self.is_typed = mtype != null
        end
 end
 
@@ -1999,9 +2085,9 @@ redef class AAttrReassignExpr
                var mtype = self.attr_type
                if mtype == null then return # Skip error
 
-               self.resolve_reassignment(v, mtype, mtype)
+               var rettype = self.resolve_reassignment(v, mtype, mtype)
 
-               self.is_typed = true
+               self.is_typed = rettype != null
        end
 end