Skip to content

runtime/compile time reflection doesn't see all knownDirectSubclasses #7046

Closed

Description

@scabug

Unlike, say, annotations or flags, knownDirectSubclasses doesn't get auto-populated and requires a symbol to be pre-initialized to work correctly.

import scala.reflect.runtime.universe._
import scala.reflect.runtime.{currentMirror => cm}

sealed class C
class D extends C
class E extends C

object Test extends App {
  val c = cm.staticClass("C")
  println(c.knownDirectSubclasses)
  c.typeSignature
  println(c.knownDirectSubclasses)
}
19:43 ~/Projects/Kepler_7046/sandbox (ticket/7046)$ scalac Test.scala && scala Test
Set()
Set(class D, class E)

Activity

  1. scabug commented on Jan 30, 2013

    @scabug
    Author

    Imported From: https://issues.scala-lang.org/browse/SI-7046?orig=1
    Reporter: @xeno-by
    Affected Versions: 2.10.0, 2.11.0
    Other Milestones: 2.12.1

  2. scabug commented on Jan 31, 2013

    @scabug
    Author

    @xeno-by said:
    Probably should a very easy fix. Will try to get it once we're discussing synchronization.

  3. scabug commented on Jan 31, 2013

    @scabug
    Author
  4. scabug commented on Feb 4, 2013

    @scabug
    Author
  5. scabug commented on Apr 11, 2013

    @scabug
    Author

    @adriaanm said:
    Actually, this only works when you query knownDirectSubclasses after those trees have been typed, so macros that occur before the class C in your example has been type checked won't see its subclasses.

  6. scabug commented on May 20, 2013

    @scabug
    Author

    @JamesIry said:
    2.10.2 is about to be cut. Kicking down the road and un-assigning to foster work stealing.

  7. scabug commented on Jun 19, 2013

    @scabug
    Author

    @retronym said:
    Test case for compile time reflection in #7588

  8. scabug commented on Dec 10, 2013

    @scabug
    Author

    Valentin Churavy (wallnuss) said (edited on Dec 10, 2013 6:46:40 AM UTC):
    I still run into this on 2.10.3 and the work around with calling c.typeSignature before c.knownDirectSubclasses doesn't work for the reason Adriaan noticed. Forcing a recompile on only the parts which use the macro solve this error, but that is of course not really satisfactory.

  9. scabug commented on Dec 10, 2013

    @scabug
    Author

    @adriaanm said:
    Sorry, this is not something we can fix due the way the typechecker is implemented. The "fix" would be to remove this method from the reflection API.

  10. scabug commented on Dec 10, 2013

    @scabug
    Author

    Valentin Churavy (wallnuss) said:
    Would it be possible to reschedule the classes where the macro is used for compilation after the TypeCheck has occurred?

  11. scabug commented on Dec 10, 2013

    @scabug
    Author
  12. scabug commented on Apr 18, 2014

    @scabug
    Author

    @xeno-by said:
    Actually, I think we can fix this for an important subset of macros: https://groups.google.com/forum/#!topic/scala-user/oJP9aqs88rM

  13. scabug commented on Aug 3, 2014

    @scabug
    Author

    @xeno-by said:
    See another test case at #8776

  14. scabug commented on Jun 6, 2016

    @scabug
    Author

    @milessabin said:
    @adriaanm can you elaborate on "this is not something we can fix due the way the typechecker is implemented" ... are there any specific obstacles you have in mind?

  15. scabug commented on Jul 11, 2016

    @scabug
    Author

    @Blaisorblade said:

    1. Since this is a design issue, do scala.meta and/or Dotty change things here? I wonder if we need a label like redesign for such issues.
    2. @milessabin the design issue is mentioned in the discussion links, in particular in https://groups.google.com/d/msg/scala-internals/LRfKffsPxVA/db_jmsa7FO4J:

    knownDirectSubclasses relies on a full top-down pass having been completed before you call it.

    Technically, we could provide a onTypecheckCompilationUnit that you could use, but this isn't currently planned for the macro api (well, I'll let Eugene comment on that, but I'm not aware of any plans here -- in any case, 2.11 is in ramp down)

  16. scabug commented on Jul 12, 2016

    @scabug
    Author

    @xeno-by said:
    Scala.meta relies on the underlying platform typechecker, so it won't change anything here unless the implementation of scalac/dotc changes.

    One way to fix this issue without changing the typechecker is to move macro expansion to after typer instead of during typer. We'll have to experiment with this, because there are some technical difficulties with implementing that (e.g. implicit inference not working well after typer, etc).

  17. scabug commented on Jul 15, 2016

    @scabug
    Author

    @milessabin said:
    WIP PR here: scala/scala#5284

  18. scabug commented on Aug 11, 2016

    @scabug
    Author

    @cchantep said:
    Could the following be used as workaround in some case (seems to work in my case as far as I can see).

    val cls: ClassSymbol = sub.asClass
    
    println(s"knownDirectSubclasses = ${cls.knownDirectSubclasses}")
    // print "knownDirectSubclasses = Set()"
    
    val subsub = cls.owner.typeSignature.decls.filter {
      case c: ClassSymbol =>
        cls != c && c.selfType.baseClasses.contains(cls)
    
      case _ => false
    }
    
    println(s"subsub = $subsub")
    // print the actual sub classes
  19. scabug commented on Aug 26, 2016

    @scabug
    Author

    @milessabin said:
    Fix backported to 2.11.8 is available here.

  20. scabug commented on Oct 18, 2016

    @scabug
    Author
  21. scabug commented on Jan 9, 2017

    @scabug
    Author

    @adriaanm said:
    To track future refinements, please open a new issue. Closing this one to mark our progress.

  22. added this to the milestone on Apr 7, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions