Skip to content

Commit

Permalink
Properly map children of mapped classes
Browse files Browse the repository at this point in the history
If sealed classes are mapped in TypeTreeMap or mapSymbols, their
children have to be mapped accordingly.
  • Loading branch information
odersky committed Feb 22, 2022
1 parent c86fb4f commit e0ff75d
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 3 deletions.
10 changes: 9 additions & 1 deletion compiler/src/dotty/tools/dotc/core/Symbols.scala
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
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
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
12 changes: 12 additions & 0 deletions tests/pos/i12508c.scala
@@ -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
}
)

0 comments on commit e0ff75d

Please sign in to comment.