Skip to content

Commit 1ecdddc

Browse files
committed
Fix TreeTypeMap to correctly substitute parameters
Fix TreeTypeMap to correctly substitute parameters when copying local class members. Fixes #12508
1 parent d63e19b commit 1ecdddc

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed

compiler/src/dotty/tools/dotc/ast/TreeTypeMap.scala

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,11 +204,23 @@ class TreeTypeMap(
204204
lazy val origCls = mapped.zip(syms).filter(_._1.isClass).toMap
205205
mapped.filter(_.isClass).foldLeft(substMap) { (tmap, cls) =>
206206
val origDcls = cls.info.decls.toList.filterNot(_.is(TypeParam))
207-
val mappedDcls = mapSymbols(origDcls, tmap, mapAlways = true)
207+
val tmap0 = tmap.withSubstitution(origCls(cls).typeParams, cls.typeParams)
208+
val mappedDcls = mapSymbols(origDcls, tmap0, mapAlways = true)
208209
val tmap1 = tmap.withMappedSyms(
209210
origCls(cls).typeParams ::: origDcls,
210211
cls.typeParams ::: mappedDcls)
211212
origDcls.lazyZip(mappedDcls).foreach(cls.asClass.replace)
212213
tmap1
213214
}
215+
216+
override def toString =
217+
def showSyms(syms: List[Symbol]) =
218+
syms.map(sym => s"$sym#${sym.id}").mkString(", ")
219+
s"""TreeTypeMap(
220+
|typeMap = $typeMap
221+
|treeMap = $treeMap
222+
|oldOwners = ${showSyms(oldOwners)}
223+
|newOwners = ${showSyms(newOwners)}
224+
|substFrom = ${showSyms(substFrom)}
225+
|substTo = ${showSyms(substTo)}""".stripMargin
214226
}

tests/pos/i12508.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class Test {
2+
inline def test(fun: Any): Any = ???
3+
test {
4+
class Foo[X]:
5+
def x: X = ???
6+
def foo: Unit = this.x.toString
7+
}
8+
}

tests/pos/i12508a.scala

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
def fun(a: Any, b: Any = 2): Any = ???
2+
def test =
3+
fun(
4+
b = println(1),
5+
a = {
6+
class Foo[X]:
7+
def x: X = ???
8+
def foo: Unit = this.x.toString
9+
locally {
10+
def x: X = ???
11+
println(x.toString)
12+
}
13+
}
14+
)

0 commit comments

Comments
 (0)