From ee1251f37f844bdb4f4ea69177e8183ad74e7b3d Mon Sep 17 00:00:00 2001 From: Martin Odersky Date: Sun, 16 Mar 2014 17:30:04 +0100 Subject: Fix of t0438 - lambdas and eta expansion Two fixes were needed 1) When typing a function value (x1: T1, ..., xN: Tn) => e, don't unconditionally issue an error if the expected function type arity is different from N. Instead, issue an error only if one of the types T1, ..., Tn is absent. The idea is that only then we need to consult the expected type for the parameter type. This allows to fix the problem later by an implicit conversion applied to the function value. 2) When eta-expanding, do not automtically take the arity of the expected function value as the arity of the generated lambda. Instead, take the method's arity, and copy method parameters into the lambda in case the arities are different. --- src/dotty/tools/dotc/typer/Typer.scala | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) (limited to 'src/dotty/tools/dotc/typer/Typer.scala') diff --git a/src/dotty/tools/dotc/typer/Typer.scala b/src/dotty/tools/dotc/typer/Typer.scala index 67e5c5902..4fdee86a3 100644 --- a/src/dotty/tools/dotc/typer/Typer.scala +++ b/src/dotty/tools/dotc/typer/Typer.scala @@ -496,14 +496,15 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit errorType(s"missing parameter type for parameter ${param.name}$ofFun, expected = ${pt.show}", param.pos) } - if (protoFormals.length != params.length) - ctx.error(s"wrong number of parameters, expected: ${protoFormals.length}", tree.pos) + def protoFormal(i: Int): Type = + if (protoFormals.length == params.length) protoFormals(i) + else errorType(s"wrong number of parameters, expected: ${protoFormals.length}", tree.pos) val inferredParams: List[untpd.ValDef] = - for ((param, formal) <- params zip protoFormals) yield + for ((param, i) <- params.zipWithIndex) yield if (!param.tpt.isEmpty) param else { - val paramTpt = untpd.TypeTree(inferredParamType(param, formal)) + val paramTpt = untpd.TypeTree(inferredParamType(param, protoFormal(i))) cpy.ValDef(param, param.mods, param.name, paramTpt, param.rhs) } @@ -1135,7 +1136,7 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit else if (pt eq AnyFunctionProto) wtp.paramTypes.length else -1 if (arity >= 0 && !tree.symbol.isConstructor) - typed(etaExpand(tree, wtp.paramNames take arity), pt) + typed(etaExpand(tree, wtp, arity), pt) else if (wtp.paramTypes.isEmpty) adaptInterpolated(tpd.Apply(tree, Nil), pt) else -- cgit v1.2.3