Skip to content

Commit 0167227

Browse files
Backport "used derived types to type arguments of dependent function type" to LTS (#20970)
Backports #19838 to the LTS branch. PR submitted by the release tooling. [skip ci]
2 parents 2f368f4 + f87d4cb commit 0167227

File tree

5 files changed

+45
-4
lines changed

5 files changed

+45
-4
lines changed

compiler/src/dotty/tools/dotc/core/tasty/TreeUnpickler.scala

+10-1
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,15 @@ class TreeUnpickler(reader: TastyReader,
6161
/** A map from addresses of definition entries to the symbols they define */
6262
private val symAtAddr = new mutable.HashMap[Addr, Symbol]
6363

64+
private def addrOfSymbol(sym: Symbol): Option[Addr] = symAtAddr.iterator.collectFirst {
65+
case (addr, s) if s == sym => addr
66+
}
67+
68+
private def locatedSymbol(sym: Symbol)(using Context): String =
69+
addrOfSymbol(sym) match
70+
case Some(addr) => i"local $sym @ ${addr.index}"
71+
case None => i"external $sym"
72+
6473
/** A temporary map from addresses of definition entries to the trees they define.
6574
* Used to remember trees of symbols that are created by a completion. Emptied
6675
* once the tree is inlined into a larger tree.
@@ -275,7 +284,7 @@ class TreeUnpickler(reader: TastyReader,
275284
/** The symbol defined by current definition */
276285
def symbolAtCurrent()(using Context): Symbol = symAtAddr.get(currentAddr) match {
277286
case Some(sym) =>
278-
assert(ctx.owner == sym.owner, i"owner discrepancy for $sym, expected: ${ctx.owner}, found: ${sym.owner}")
287+
assert(ctx.owner == sym.owner, i"owner discrepancy for ${locatedSymbol(sym)}, expected: ${locatedSymbol(ctx.owner)}, found: ${locatedSymbol(sym.owner)}")
279288
sym
280289
case None =>
281290
createSymbol()

compiler/src/dotty/tools/dotc/typer/Typer.scala

+3-3
Original file line numberDiff line numberDiff line change
@@ -1439,14 +1439,14 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
14391439
if isErasedClass then arg.withAddedFlags(Erased) else arg
14401440
}
14411441
return typedDependent(newParams)
1442-
val resTpt = TypeTree(mt.nonDependentResultApprox).withSpan(body.span)
1443-
val typeArgs = appDef.termParamss.head.map(_.tpt) :+ resTpt
14441442
val core =
14451443
if mt.hasErasedParams then TypeTree(defn.ErasedFunctionClass.typeRef)
14461444
else
1445+
val resTpt = TypeTree(mt.nonDependentResultApprox).withSpan(body.span)
1446+
val paramTpts = appDef.termParamss.head.map(p => TypeTree(p.tpt.tpe).withSpan(p.tpt.span))
14471447
val funSym = defn.FunctionSymbol(numArgs, isContextual, isImpure)
14481448
val tycon = TypeTree(funSym.typeRef)
1449-
AppliedTypeTree(tycon, typeArgs)
1449+
AppliedTypeTree(tycon, paramTpts :+ resTpt)
14501450
RefinedTypeTree(core, List(appDef), ctx.owner.asClass)
14511451
end typedDependent
14521452

tests/pos/i19629.scala

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
trait CP[A,B]
2+
trait TypeEqK[F[_], G[_]]
3+
4+
trait Knit[CP[_, _], F[_]] {
5+
type Res
6+
7+
def visit[R](
8+
caseInFst: [F1[_], Y] => (k: Knit[CP, F1]) => (ev: TypeEqK[F, [x] =>> CP[F1[x], Y]]) => R
9+
): R
10+
}

tests/run/i19629/Test_2.scala

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
2+
class Container[Y1, G[_]]:
3+
lazy val outer: Knit[CP, G] = new:
4+
type Res = Y1
5+
def visit[R](caseInFst: [F1[_], Y] => (k: Knit[CP, F1]) => (ev: TypeEqK[G, [x] =>> CP[F1[x], Y]]) => R): R =
6+
caseInFst[G, Res](outer)(new TypeEqK[G, [x] =>> CP[G[x], Res]] {})
7+
8+
@main def Test =
9+
val knit = new Container[Unit, Option].outer
10+
val res = knit.visit:
11+
[F1[_], Y] => (k: Knit[CP, F1]) => (ev: TypeEqK[Option, [x] =>> CP[F1[x], Y]]) => 42
12+
assert(res == 42)

tests/run/i19629/lib_1.scala

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
trait CP[A,B]
2+
trait TypeEqK[F[_], G[_]]
3+
4+
trait Knit[CP[_, _], F[_]] {
5+
type Res
6+
7+
def visit[R](
8+
caseInFst: [F1[_], Y] => (k: Knit[CP, F1]) => (ev: TypeEqK[F, [x] =>> CP[F1[x], Y]]) => R
9+
): R
10+
}

0 commit comments

Comments
 (0)