@@ -167,37 +167,53 @@ object EtaExpansion extends LiftImpure {
167167 *
168168 * { val xs = es; expr }
169169 *
170- * If xarity matches the number of parameters in `mt`, the eta-expansion is
170+ * The result of the eta-expansion is either (1)
171171 *
172172 * { val xs = es; (x1, ..., xn) => expr(x1, ..., xn) }
173173 *
174- * Note that the function value's parameters are untyped, hence the type will
175- * be supplied by the environment (or if missing be supplied by the target
176- * method as a fallback). On the other hand, if `xarity` is different from
177- * the number of parameters in `mt`, then we cannot propagate parameter types
178- * from the expected type, and we fallback to using the method's original
179- * parameter types instead.
174+ * or (2)
180175 *
181- * In either case, the result is an untyped tree, with `es` and `expr` as typed splices.
176+ * { val xs = es; (x1: T1, ..., xn: Tn) => expr(x1, ..., xn) }
177+ *
178+ * or (3)
179+ *
180+ * { val xs = es; (x1: T1, ..., xn: Tn) => expr(x1, ..., xn) _ }
181+ *
182+ * where `T1, ..., Tn` are the paremeter types of the expanded method.
183+ *
184+ * Case (3) applies if the method is curried, i.e. its result type is again a method
185+ * type. Case (2) applies if the expected arity of the function type `xarity` differs
186+ * from the number of parameters in `mt`. Case (1) applies if `mt` is uncurried
187+ * and its number of parameters equals `xarity`. In this case we can always infer
188+ * the parameter types later from the callee even if parameter types could not be
189+ * inferred from the expected type. Hence, we lose nothing by omitting parameter types
190+ * in the eta expansion. On the other hand omitting these parameters keeps the possibility
191+ * open that different parameters are inferred from the expected type, so we keep
192+ * more options open.
193+ *
194+ * In each case, the result is an untyped tree, with `es` and `expr` as typed splices.
195+ *
196+ * F[V](x) ==> (x => F[X])
182197 */
183198 def etaExpand (tree : Tree , mt : MethodType , xarity : Int )(implicit ctx : Context ): untpd.Tree = {
184199 import untpd ._
185200 assert(! ctx.isAfterTyper)
186201 val defs = new mutable.ListBuffer [tpd.Tree ]
187202 val lifted : Tree = TypedSplice (liftApp(defs, tree))
203+ val isLastApplication = mt.resultType match {
204+ case rt : MethodType => rt.isImplicitMethod
205+ case _ => true
206+ }
188207 val paramTypes : List [Tree ] =
189- if (mt.paramInfos.length == xarity) mt.paramInfos map (_ => TypeTree ())
208+ if (isLastApplication && mt.paramInfos.length == xarity) mt.paramInfos map (_ => TypeTree ())
190209 else mt.paramInfos map TypeTree
191210 val params = (mt.paramNames, paramTypes).zipped.map((name, tpe) =>
192211 ValDef (name, tpe, EmptyTree ).withFlags(Synthetic | Param ).withPos(tree.pos.startPos))
193212 var ids : List [Tree ] = mt.paramNames map (name => Ident (name).withPos(tree.pos.startPos))
194213 if (mt.paramInfos.nonEmpty && mt.paramInfos.last.isRepeatedParam)
195214 ids = ids.init :+ repeated(ids.last)
196215 var body : Tree = Apply (lifted, ids)
197- mt.resultType match {
198- case rt : MethodType if ! rt.isImplicitMethod => body = PostfixOp (body, Ident (nme.WILDCARD ))
199- case _ =>
200- }
216+ if (! isLastApplication) body = PostfixOp (body, Ident (nme.WILDCARD ))
201217 val fn = untpd.Function (params, body)
202218 if (defs.nonEmpty) untpd.Block (defs.toList map (untpd.TypedSplice (_)), fn) else fn
203219 }
0 commit comments