Skip to content
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

Fix TreeTypeMap to correctly substitute type parameters #14537

Merged
merged 5 commits into from
Feb 23, 2022
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
14 changes: 13 additions & 1 deletion compiler/src/dotty/tools/dotc/ast/TreeTypeMap.scala
Original file line number Diff line number Diff line change
Expand Up @@ -204,11 +204,23 @@ class TreeTypeMap(
lazy val origCls = mapped.zip(syms).filter(_._1.isClass).toMap
mapped.filter(_.isClass).foldLeft(substMap) { (tmap, cls) =>
val origDcls = cls.info.decls.toList.filterNot(_.is(TypeParam))
val mappedDcls = mapSymbols(origDcls, tmap, mapAlways = true)
val tmap0 = tmap.withSubstitution(origCls(cls).typeParams, cls.typeParams)
val mappedDcls = mapSymbols(origDcls, tmap0, mapAlways = true)
val tmap1 = tmap.withMappedSyms(
origCls(cls).typeParams ::: origDcls,
cls.typeParams ::: mappedDcls)
origDcls.lazyZip(mappedDcls).foreach(cls.asClass.replace)
tmap1
}

override def toString =
def showSyms(syms: List[Symbol]) =
syms.map(sym => s"$sym#${sym.id}").mkString(", ")
s"""TreeTypeMap(
|typeMap = $typeMap
|treeMap = $treeMap
|oldOwners = ${showSyms(oldOwners)}
|newOwners = ${showSyms(newOwners)}
|substFrom = ${showSyms(substFrom)}
|substTo = ${showSyms(substTo)}""".stripMargin
}
10 changes: 9 additions & 1 deletion compiler/src/dotty/tools/dotc/core/Symbols.scala
Original file line number Diff line number Diff line change
Expand Up @@ -826,11 +826,19 @@ object Symbols {
copy.info = completer
copy.denot match
case cd: ClassDenotation =>
cd.registeredCompanion = cd.unforcedRegisteredCompanion.subst(originals, copies)
cd.registeredCompanion = original.registeredCompanion.subst(originals, copies)
case _ =>
}

copies.foreach(_.ensureCompleted()) // avoid memory leak

// Update Child annotations of classes encountered previously to new values
// if some child is among the mapped symbols
for orig <- ttmap1.substFrom do
if orig.is(Sealed) && orig.children.exists(originals.contains) then
val sealedCopy = orig.subst(ttmap1.substFrom, ttmap1.substTo)
sealedCopy.annotations = sealedCopy.annotations.mapConserve(ttmap1.apply)

copies
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1040,7 +1040,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
else if (sym.is(ModuleClass) && sym.isPackageObject && sym.name.stripModuleClassSuffix == tpnme.PACKAGE)
nameString(sym.owner.name)
else if (sym.is(ModuleClass))
nameString(sym.name.stripModuleClassSuffix)
nameString(sym.name.stripModuleClassSuffix) + idString(sym)
else if (hasMeaninglessName(sym))
simpleNameString(sym.owner) + idString(sym)
else
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/reporting/trace.scala
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ trait TraceSyntax:
else
// Avoid evaluating question multiple time, since each evaluation
// may cause some extra logging output.
val q = question.replace('\n', ' ')
val q = question
val leading = s"==> $q?"
val trailing = (res: T) => s"<== $q = ${showOp(res)}"
var finalized = false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ class SyntheticMembers(thisPhase: DenotTransformer) {
val parentEnum = vdef.owner.companionClass
val children = parentEnum.children.zipWithIndex
val candidate: Option[Int] = children.collectFirst { case (child, idx) if child == vdef => idx }
assert(candidate.isDefined, i"could not find child for $vdef")
assert(candidate.isDefined, i"could not find child for $vdef in ${parentEnum.children}%, % of $parentEnum")
Literal(Constant(candidate.get))

def toStringBody(vrefss: List[List[Tree]]): Tree =
Expand Down
3 changes: 3 additions & 0 deletions compiler/src/dotty/tools/dotc/typer/ReTyper.scala
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ class ReTyper(nestingLevel: Int = 0) extends Typer(nestingLevel) with ReChecking
override def typedSuper(tree: untpd.Super, pt: Type)(using Context): Tree =
promote(tree)

override def typedImport(tree: untpd.Import, sym: Symbol)(using Context): Tree =
promote(tree)

override def typedTyped(tree: untpd.Typed, pt: Type)(using Context): Tree = {
assertTyped(tree)

Expand Down
17 changes: 17 additions & 0 deletions tests/pos/i12508.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Test {
inline def test(fun: Any): Any = ???
test {
class Foo[X]:
def x: X = ???
def foo: Unit = this.x.toString
}
}
class Test2 {
inline def test(fun: => Any): Any = fun
test {
case class Pair[X, Y](
x: X,
y: Y,
)
}
}
14 changes: 14 additions & 0 deletions tests/pos/i12508a.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
def fun(a: Any, b: Any = 2): Any = ???
def test =
fun(
b = println(1),
a = {
class Foo[X]:
def x: X = ???
def foo: Unit = this.x.toString
locally {
def x: X = ???
println(x.toString)
}
}
)
13 changes: 13 additions & 0 deletions tests/pos/i12508b.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
def fun(a: Any, b: Any = 2): Any = ???
def test =
fun(
b = println(1),
a = {
sealed class Foo[X]:
def x: X = ???
def foo: Unit = this.x.toString
class Bar extends Foo[String]
class Baz[Y] extends Foo[Y]
if ??? then Bar() else Baz[Int]
}
)
12 changes: 12 additions & 0 deletions tests/pos/i12508c.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
def fun(a: Any, b: Any = 2): Any = ???

def test =
fun(
b = println(1),
a = {
enum Option[+X]:
case Some(x: X)
case None
if ??? then Option.Some(1) else Option.None
}
)