X-Git-Url: http://nitlanguage.org diff --git a/src/semantize/typing.nit b/src/semantize/typing.nit index d558504..c26ca0f 100644 --- a/src/semantize/typing.nit +++ b/src/semantize/typing.nit @@ -427,9 +427,39 @@ private class TypeVisitor 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 @@ -438,6 +468,9 @@ private class TypeVisitor 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 @@ -450,15 +483,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