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

Make format generation idempotent #1047

Merged
merged 1 commit into from
Jun 17, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package play.api.libs.json

import scala.annotation.tailrec
import scala.collection.mutable
import scala.reflect.macros.blackbox

/**
Expand Down Expand Up @@ -555,25 +556,26 @@ class JsMacroImpl(val c: blackbox.Context) {
val tpeSym = atag.tpe.typeSymbol.asClass

@tailrec
def allSubclasses(path: List[Symbol], subclasses: Set[Type]): Set[Type] = path match {
case (cls: ClassSymbol) :: tail if tpeSym != cls && cls.selfType.baseClasses.contains(tpeSym) => {
if (cls.typeParams.nonEmpty)
c.warning(c.enclosingPosition, s"cannot handle class ${cls.fullName}: type parameter not supported")
val newSub = if (cls.typeParams.isEmpty) Set(cls.selfType) else Set.empty
allSubclasses(tail, subclasses ++ newSub)
}
def allSubclasses(path: List[Symbol], subclasses: mutable.LinkedHashSet[Type]): mutable.LinkedHashSet[Type] =
path match {
case (cls: ClassSymbol) :: tail if tpeSym != cls && cls.selfType.baseClasses.contains(tpeSym) => {
if (cls.typeParams.nonEmpty)
c.warning(c.enclosingPosition, s"cannot handle class ${cls.fullName}: type parameter not supported")
val newSub = if (cls.typeParams.isEmpty) Set(cls.selfType) else Set.empty
allSubclasses(tail, subclasses ++ newSub)
}

case (o: ModuleSymbol) :: tail
if o.companion == NoSymbol // not a companion object
&& o.typeSignature.baseClasses.contains(tpeSym) =>
allSubclasses(tail, subclasses ++ Set(o.typeSignature))
case (o: ModuleSymbol) :: tail
if o.companion == NoSymbol // not a companion object
&& o.typeSignature.baseClasses.contains(tpeSym) =>
allSubclasses(tail, subclasses ++ Set(o.typeSignature))

case _ :: tail => allSubclasses(tail, subclasses)
case _ => subclasses
}
case _ :: tail => allSubclasses(tail, subclasses)
case _ => subclasses
}

if (tpeSym.isSealed && tpeSym.isAbstract) {
Some(allSubclasses(tpeSym.owner.typeSignature.decls.sorted, Set.empty).toList)
Some(allSubclasses(tpeSym.owner.typeSignature.decls.sorted, mutable.LinkedHashSet[Type]()).toList)
} else None
}

Expand Down
Loading