Skip to content

Inliner: don't confuse method parameters and nested lambda parameters #13658

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 1 commit into from
Oct 5, 2021
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
13 changes: 12 additions & 1 deletion compiler/src/dotty/tools/dotc/typer/Inliner.scala
Original file line number Diff line number Diff line change
Expand Up @@ -764,7 +764,13 @@ class Inliner(call: tpd.Tree, rhsToInline: tpd.Tree)(using Context) {
for (param <- tpe.cls.typeParams)
paramProxy(param.typeRef) = adaptToPrefix(param.typeRef)
case tpe: NamedType
if tpe.symbol.is(Param) && tpe.symbol.owner == inlinedMethod && !paramProxy.contains(tpe) =>
if tpe.symbol.is(Param)
&& tpe.symbol.owner == inlinedMethod
&& (tpe.symbol.isTerm || inlinedMethod.paramSymss.exists(_.contains(tpe.symbol)))
// this test is needed to rule out nested LambdaTypeTree parameters
// with the same name as the method's parameters. Note that the nested
// LambdaTypeTree parameters also have the inlineMethod as owner. C.f. i13460.scala.
&& !paramProxy.contains(tpe) =>
paramBinding.get(tpe.name) match
case Some(bound) => paramProxy(tpe) = bound
case _ => // can happen for params bound by type-lambda trees.
Expand Down Expand Up @@ -960,6 +966,11 @@ class Inliner(call: tpd.Tree, rhsToInline: tpd.Tree)(using Context) {
substTo = Nil
)(using inlineCtx)

inlining.println(
i"""inliner transform with
|thisProxy = ${thisProxy.toList.map(_._1)}%, % --> ${thisProxy.toList.map(_._2)}%, %
|paramProxy = ${paramProxy.toList.map(_._1.typeSymbol.showLocated)}%, % --> ${paramProxy.toList.map(_._2)}%, %""")

// Apply inliner to `rhsToInline`, split off any implicit bindings from result, and
// make them part of `bindingsBuf`. The expansion is then the tree that remains.
val expansion = inliner.transform(rhsToInline)
Expand Down
55 changes: 55 additions & 0 deletions tests/pos/i13460.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import scala.compiletime.*
import scala.deriving.Mirror

class Lazy[A](obj: => A) {
lazy val value: A = obj
}
object Lazy {
given [A](using obj: => A ): Lazy[A] = new Lazy(obj)
}

trait MyTypeClass[A] {
def makeString(a: A): String
}
object MyTypeClass {

given IntTypeClass: MyTypeClass[Int] with
def makeString(a: Int): String = a.toString

inline given derived[A](using m: Mirror.Of[A]): MyTypeClass[A] =
inline m match
case p: Mirror.ProductOf[A] => productConverter(p)


private inline def summonElementTypeClasses[A](m: Mirror.Of[A]): IArray[Object] =
// this doesn't work
summonAll[Tuple.Map[m.MirroredElemTypes, [A] =>> Lazy[MyTypeClass[A]]]].toIArray
// but this does
// summonAll[Tuple.Map[Tuple.Map[m.MirroredElemTypes, MyTypeClass], Lazy]].toIArray

private inline def productConverter[A](m: Mirror.ProductOf[A]): MyTypeClass[A] = {
val elementTypeClasses = summonElementTypeClasses(m)
new MyTypeClass[A] {
def makeString(a: A): String = {
val product = a.asInstanceOf[Product]
elementTypeClasses
.view
.zipWithIndex
.map((obj, i) => {
val tc = obj.asInstanceOf[Lazy[MyTypeClass[Any]]].value
tc.makeString(product.productElement(i))
})
.mkString("[", ", ", "]")
}
}
}
}

case class Example(a: Int, b: Int) derives MyTypeClass

object Main {
def main(args: Array[String]): Unit = {
println("hello world")
println(summon[MyTypeClass[Example]].makeString(Example(1,2)))
}
}