Skip to content

Fix #5257: Support auto generic-tupling of parameters #5259

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

Merged
merged 3 commits into from
Nov 2, 2018
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
15 changes: 13 additions & 2 deletions compiler/src/dotty/tools/dotc/ast/Desugar.scala
Original file line number Diff line number Diff line change
Expand Up @@ -870,10 +870,21 @@ object desugar {
* def pn = x$1._n
* body
* }
*
* or if `isGenericTuple`
*
* x$1 => {
* def p1 = x$1.apply(0)
* ...
* def pn = x$1.apply(n-1)
* body
* }
*/
def makeTupledFunction(params: List[ValDef], body: Tree)(implicit ctx: Context): Tree = {
def makeTupledFunction(params: List[ValDef], body: Tree, isGenericTuple: Boolean)(implicit ctx: Context): Tree = {
val param = makeSyntheticParameter()
def selector(n: Int) = Select(refOfDef(param), nme.selectorName(n))
def selector(n: Int) =
if (isGenericTuple) Apply(Select(refOfDef(param), nme.apply), Literal(Constant(n)))
else Select(refOfDef(param), nme.selectorName(n))
val vdefs =
params.zipWithIndex.map{
case (param, idx) =>
Expand Down
15 changes: 13 additions & 2 deletions compiler/src/dotty/tools/dotc/typer/Applications.scala
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,19 @@ object Applications {
extractorMemberType(tp, nme.get, errorPos).exists

def productSelectorTypes(tp: Type, errorPos: Position = NoPosition)(implicit ctx: Context): List[Type] = {
val sels = for (n <- Iterator.from(0)) yield extractorMemberType(tp, nme.selectorName(n), errorPos)
sels.takeWhile(_.exists).toList
def tupleSelectors(n: Int, tp: Type): List[Type] = {
val sel = extractorMemberType(tp, nme.selectorName(n), errorPos)
// extractorMemberType will return NoType if this is the tail of tuple with an unknown tail
// such as `Int *: T` where `T <: Tuple`.
if (sel.exists) sel :: tupleSelectors(n + 1, tp) else Nil
}
def genTupleSelectors(n: Int, tp: Type): List[Type] = tp match {
case tp: AppliedType if !tp.derivesFrom(defn.ProductClass) && tp.derivesFrom(defn.PairClass) =>
val List(head, tail) = tp.args
head :: genTupleSelectors(n, tail)
case _ => tupleSelectors(n, tp)
}
genTupleSelectors(0, tp)
}

def productArity(tp: Type)(implicit ctx: Context): Int =
Expand Down
5 changes: 3 additions & 2 deletions compiler/src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -906,7 +906,7 @@ class Typer extends Namer
/** Is `formal` a product type which is elementwise compatible with `params`? */
def ptIsCorrectProduct(formal: Type) = {
isFullyDefined(formal, ForceDegree.noBottom) &&
defn.isProductSubType(formal) &&
(defn.isProductSubType(formal) || formal.derivesFrom(defn.PairClass)) &&
Applications.productSelectorTypes(formal).corresponds(params) {
(argType, param) =>
param.tpt.isEmpty || argType <:< typedAheadType(param.tpt).tpe
Expand All @@ -915,7 +915,8 @@ class Typer extends Namer

val desugared =
if (protoFormals.length == 1 && params.length != 1 && ptIsCorrectProduct(protoFormals.head)) {
desugar.makeTupledFunction(params, fnBody)
val isGenericTuple = !protoFormals.head.derivesFrom(defn.ProductClass)
desugar.makeTupledFunction(params, fnBody, isGenericTuple)
}
else {
val inferredParams: List[untpd.ValDef] =
Expand Down
1 change: 1 addition & 0 deletions compiler/test/dotc/run-test-pickling.blacklist
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ i4947b
i5119
i5119b
i5188a
i5257.scala
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you know what's failing?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I remember correctly there was a match type that was not explicitly expanded in some part of the tree.

inline-varargs-1
implicitShortcut
inline-case-objects
Expand Down
1 change: 1 addition & 0 deletions compiler/test/dotty/tools/dotc/CompilationTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ class CompilationTests extends ParallelTesting {
implicit val testGroup: TestGroup = TestGroup("runAll")
compileFilesInDir("tests/run-custom-args/Yretain-trees", defaultOptions and "-Yretain-trees") +
compileFile("tests/run-custom-args/tuple-cons.scala", allowDeepSubtypes) +
compileFile("tests/run-custom-args/i5256.scala", allowDeepSubtypes) +
compileFilesInDir("tests/run", defaultOptions)
}.checkRuns()

Expand Down
1 change: 1 addition & 0 deletions tests/run-custom-args/i5256.check
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
276
11 changes: 11 additions & 0 deletions tests/run-custom-args/i5256.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
object Test {

def main(args: Array[String]): Unit = {
val f23: ((Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int)) => Int = {
(x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15, x16, x17, x18, x19, x20, x21, x22, x23) =>
x1 + x2 + x3 + x4 + x5 + x6 + x7 + x8 + x9 + x10 + x11 + x12 + x13 + x14 + x15 + x16 + x17 + x18 + x19 + x20 + x21 + x22 + x23
}

println(f23((1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23)))
}
}
2 changes: 2 additions & 0 deletions tests/run/i5257.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
3
5
11 changes: 11 additions & 0 deletions tests/run/i5257.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
object Test {

def main(args: Array[String]): Unit = {
val f: Int *: Int *: Unit => Int = (x, y) => x + y
val g: Int *: Tuple1[Int] => Int = (x, y) => x + y

println(f((1, 2)))
println(g((2, 3)))
}

}