Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adapt function arguments to n-ary prototype #14651

Merged
merged 1 commit into from
Mar 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions compiler/src/dotty/tools/dotc/ast/Desugar.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1431,6 +1431,20 @@ object desugar {
Function(param :: Nil, Block(vdefs, body))
}

/** Convert a tuple pattern with given `elems` to a sequence of `ValDefs`,
* skipping elements that are not convertible.
*/
def patternsToParams(elems: List[Tree])(using Context): List[ValDef] =
def toParam(elem: Tree, tpt: Tree): Tree =
elem match
case Annotated(elem1, _) => toParam(elem1, tpt)
case Typed(elem1, tpt1) => toParam(elem1, tpt1)
case Ident(id: TermName) => ValDef(id, tpt, EmptyTree).withFlags(Param)
case _ => EmptyTree
elems.map(param => toParam(param, TypeTree()).withSpan(param.span)).collect {
case vd: ValDef => vd
}

def makeContextualFunction(formals: List[Tree], body: Tree, isErased: Boolean)(using Context): Function = {
val mods = if (isErased) Given | Erased else Given
val params = makeImplicitParameters(formals, mods)
Expand Down
68 changes: 42 additions & 26 deletions compiler/src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1424,33 +1424,49 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
param.tpt.isEmpty || argType.widenExpr <:< typedAheadType(param.tpt).tpe
}

val desugared =
if (protoFormals.length == 1 && params.length != 1 && ptIsCorrectProduct(protoFormals.head)) {
val isGenericTuple =
protoFormals.head.derivesFrom(defn.TupleClass)
&& !defn.isTupleClass(protoFormals.head.typeSymbol)
desugar.makeTupledFunction(params, fnBody, isGenericTuple)
}
else {
val inferredParams: List[untpd.ValDef] =
for ((param, i) <- params.zipWithIndex) yield
if (!param.tpt.isEmpty) param
else
val formal = protoFormal(i)
val knownFormal = isFullyDefined(formal, ForceDegree.failBottom)
val paramType =
if knownFormal then formal
else inferredFromTarget(param, formal, calleeType, paramIndex)
.orElse(errorType(AnonymousFunctionMissingParamType(param, tree, formal), param.srcPos))
val paramTpt = untpd.TypedSplice(
(if knownFormal then InferredTypeTree() else untpd.TypeTree())
.withType(paramType.translateFromRepeated(toArray = false))
.withSpan(param.span.endPos)
)
cpy.ValDef(param)(tpt = paramTpt)
desugar.makeClosure(inferredParams, fnBody, resultTpt, isContextual, tree.span)
}
var desugared: untpd.Tree = EmptyTree
if protoFormals.length == 1 && params.length != 1 && ptIsCorrectProduct(protoFormals.head) then
val isGenericTuple =
protoFormals.head.derivesFrom(defn.TupleClass)
&& !defn.isTupleClass(protoFormals.head.typeSymbol)
desugared = desugar.makeTupledFunction(params, fnBody, isGenericTuple)
else if protoFormals.length > 1 && params.length == 1 then
def isParamRef(scrut: untpd.Tree): Boolean = scrut match
case untpd.Annotated(scrut1, _) => isParamRef(scrut1)
case untpd.Ident(id) => id == params.head.name
fnBody match
case untpd.Match(scrut, untpd.CaseDef(untpd.Tuple(elems), untpd.EmptyTree, rhs) :: Nil)
if scrut.span.isSynthetic && isParamRef(scrut) && elems.hasSameLengthAs(protoFormals) =>
// If `pt` is N-ary function type, convert synthetic lambda
// x$1 => x$1 match case (a1, ..., aN) => e
// to
// (a1, ..., aN) => e
val params1 = desugar.patternsToParams(elems)
if params1.hasSameLengthAs(elems) then
desugared = cpy.Function(tree)(params1, rhs)
case _ =>

if desugared.isEmpty then
val inferredParams: List[untpd.ValDef] =
for ((param, i) <- params.zipWithIndex) yield
if (!param.tpt.isEmpty) param
else
val formal = protoFormal(i)
val knownFormal = isFullyDefined(formal, ForceDegree.failBottom)
val paramType =
if knownFormal then formal
else inferredFromTarget(param, formal, calleeType, paramIndex)
.orElse(errorType(AnonymousFunctionMissingParamType(param, tree, formal), param.srcPos))
val paramTpt = untpd.TypedSplice(
(if knownFormal then InferredTypeTree() else untpd.TypeTree())
.withType(paramType.translateFromRepeated(toArray = false))
.withSpan(param.span.endPos)
)
cpy.ValDef(param)(tpt = paramTpt)
desugared = desugar.makeClosure(inferredParams, fnBody, resultTpt, isContextual, tree.span)

typed(desugared, pt)
.showing(i"desugared fun $tree --> $desugared with pt = $pt", typr)
}

def typedClosure(tree: untpd.Closure, pt: Type)(using Context): Tree = {
Expand Down
3 changes: 3 additions & 0 deletions tests/pos/i14626.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import language.`future`
def Test =
for (a, b) <- List("a","b","c").lazyZip(List(1,2,3)) do println(s"$a$b")