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 #18484: Query every legal child of sealed class on children call #18561

Closed
wants to merge 1 commit into from
Closed
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
22 changes: 15 additions & 7 deletions compiler/src/dotty/tools/dotc/core/SymDenotations.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1688,7 +1688,7 @@ object SymDenotations {
*/
def children(using Context): List[Symbol] =

def completeChildrenIn(owner: Symbol)(using Context) =
def completeChildrenIn(owner: Symbol, recursively: Boolean)(using Context): Unit =
// Possible children are: classes extending Scala classes and
// Scala or Java enum values that are defined in owner.
// If owner is a package, we complete only
Expand All @@ -1698,20 +1698,28 @@ object SymDenotations {
&& (!owner.is(Package)
|| sym.originDenotation.infoOrCompleter.match
case _: SymbolLoaders.SecondCompleter => sym.associatedFile == this.symbol.associatedFile
case _ => false)
case _: ClassInfo if recursively && sym.isValidInCurrentRun =>
sym.is(Package) || sym.associatedFile == this.symbol.associatedFile
case other => false)

if owner.isClass then
for c <- owner.info.decls.toList if maybeChild(c) do
c.ensureCompleted()
if recursively then completeChildrenIn(c, recursively)
end completeChildrenIn

if is(Sealed) || isAllOf(JavaEnumTrait) then
if !is(ChildrenQueried) then
// Make sure all visible children are completed, so that
// they show up in Child annotations. A possible child is visible if it
// is defined in the same scope as `cls` or in the companion object of `cls`.
completeChildrenIn(owner)
completeChildrenIn(companionClass)
if !is(Enum) then // non-enum sealed class
// Make sure all children are completed, so that
// they show up in Child annotations.
completeChildrenIn(defn.RootClass, recursively = true)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I spent some time trying to find something smarter than just starting at the <root> package - but neither Symbol.topLevelClass nor Symbol.enclosingPackageClass really helped. If some kind of method that returns packages/classes for a source file there already exists in the compiler, it is probably more performant than this (but I could not find anything like that).

else
// Make sure all visible children are completed, so that
// they show up in Child annotations. A possible child is visible if it
// is defined in the same scope as `cls` or in the companion object of `cls`.
completeChildrenIn(owner, recursively = false)
completeChildrenIn(companionClass, recursively = false)
setFlag(ChildrenQueried)

annotations.collect { case Annotation.Child(child) => child }.reverse
Expand Down
11 changes: 11 additions & 0 deletions tests/pos/i18484/Macro_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
object Macro {
import scala.quoted.*

def subtypesImpl[A: Type](using quotes: Quotes): Expr[String] = {
import quotes.reflect.*
val a = TypeRepr.of[A].typeSymbol.children
'{""}
}

inline def subtypes[A]: String = ${ subtypesImpl[A] }
}
21 changes: 21 additions & 0 deletions tests/pos/i18484/Test_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class Test {
def test(): Unit = {
scala.compiletime.testing.typeCheckErrors("Macro.subtypes[top.inner.Inner.Shape]")
}
}

package top {
package inner {
object Inner:
sealed trait Shape
case class Circle(center: Int, rad: Double) extends Shape
object Inner2:
case class Circle2(center: Int, rad: Double) extends Shape
}
case class Circle3(center: Int, rad: Double) extends inner.Inner.Shape
}
case class Circle4(center: Int, rad: Double) extends top.inner.Inner.Shape

package top2 {
case class Circle5(center: Int, rad: Double) extends top.inner.Inner.Shape
}