Skip to content

Commit

Permalink
used derived types to type arguments of dependent function type (#19838)
Browse files Browse the repository at this point in the history
We don't need to repeat the tree twice, instead derive a type tree from
the original tree, and reuse its spans.

fixes #19629
  • Loading branch information
sjrd authored Mar 1, 2024
2 parents 18504b9 + 0d79122 commit 2cd5721
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 4 deletions.
11 changes: 10 additions & 1 deletion compiler/src/dotty/tools/dotc/core/tasty/TreeUnpickler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,15 @@ class TreeUnpickler(reader: TastyReader,
/** A map from addresses of definition entries to the symbols they define */
private val symAtAddr = new mutable.HashMap[Addr, Symbol]

private def addrOfSymbol(sym: Symbol): Option[Addr] = symAtAddr.iterator.collectFirst {
case (addr, s) if s == sym => addr
}

private def locatedSymbol(sym: Symbol)(using Context): String =
addrOfSymbol(sym) match
case Some(addr) => i"local $sym @ ${addr.index}"
case None => i"external $sym"

/** A temporary map from addresses of definition entries to the trees they define.
* Used to remember trees of symbols that are created by a completion. Emptied
* once the tree is inlined into a larger tree.
Expand Down Expand Up @@ -297,7 +306,7 @@ class TreeUnpickler(reader: TastyReader,
/** The symbol defined by current definition */
def symbolAtCurrent()(using Context): Symbol = symAtAddr.get(currentAddr) match {
case Some(sym) =>
assert(ctx.owner == sym.owner, i"owner discrepancy for $sym, expected: ${ctx.owner}, found: ${sym.owner}")
assert(ctx.owner == sym.owner, i"owner discrepancy for ${locatedSymbol(sym)}, expected: ${locatedSymbol(ctx.owner)}, found: ${locatedSymbol(sym.owner)}")
sym
case None =>
createSymbol()
Expand Down
6 changes: 3 additions & 3 deletions compiler/src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1463,14 +1463,14 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
if isErasedClass then arg.withAddedFlags(Erased) else arg
}
return typedDependent(newParams)
val resTpt = TypeTree(mt.nonDependentResultApprox).withSpan(body.span)
val typeArgs = appDef.termParamss.head.map(_.tpt) :+ resTpt
val core =
if mt.hasErasedParams then TypeTree(defn.PolyFunctionClass.typeRef)
else
val resTpt = TypeTree(mt.nonDependentResultApprox).withSpan(body.span)
val paramTpts = appDef.termParamss.head.map(p => TypeTree(p.tpt.tpe).withSpan(p.tpt.span))
val funSym = defn.FunctionSymbol(numArgs, isContextual, isImpure)
val tycon = TypeTree(funSym.typeRef)
AppliedTypeTree(tycon, typeArgs)
AppliedTypeTree(tycon, paramTpts :+ resTpt)
RefinedTypeTree(core, List(appDef), ctx.owner.asClass)
end typedDependent

Expand Down
10 changes: 10 additions & 0 deletions tests/pos/i19629.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
trait CP[A,B]
trait TypeEqK[F[_], G[_]]

trait Knit[CP[_, _], F[_]] {
type Res

def visit[R](
caseInFst: [F1[_], Y] => (k: Knit[CP, F1]) => (ev: TypeEqK[F, [x] =>> CP[F1[x], Y]]) => R
): R
}
12 changes: 12 additions & 0 deletions tests/run/i19629/Test_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

class Container[Y1, G[_]]:
lazy val outer: Knit[CP, G] = new:
type Res = Y1
def visit[R](caseInFst: [F1[_], Y] => (k: Knit[CP, F1]) => (ev: TypeEqK[G, [x] =>> CP[F1[x], Y]]) => R): R =
caseInFst[G, Res](outer)(new TypeEqK[G, [x] =>> CP[G[x], Res]] {})

@main def Test =
val knit = new Container[Unit, Option].outer
val res = knit.visit:
[F1[_], Y] => (k: Knit[CP, F1]) => (ev: TypeEqK[Option, [x] =>> CP[F1[x], Y]]) => 42
assert(res == 42)
10 changes: 10 additions & 0 deletions tests/run/i19629/lib_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
trait CP[A,B]
trait TypeEqK[F[_], G[_]]

trait Knit[CP[_, _], F[_]] {
type Res

def visit[R](
caseInFst: [F1[_], Y] => (k: Knit[CP, F1]) => (ev: TypeEqK[F, [x] =>> CP[F1[x], Y]]) => R
): R
}

0 comments on commit 2cd5721

Please sign in to comment.