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

Incorrect children queried before class was discovered error when typeCheckErrors is used #18484

Closed
jchyb opened this issue Aug 30, 2023 · 1 comment
Assignees
Labels
area:metaprogramming:compiletime The scala.compiletime package area:metaprogramming:reflection Issues related to the quotes reflection API stat:wontfix

Comments

@jchyb
Copy link
Contributor

jchyb commented Aug 30, 2023

Compiler version

3.3.0, main (8c311a0)

Minimized code

macro.scala

object Macro {
  import scala.quoted.*

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

  inline def subtypes[A]: String = ${ subtypesImpl[A] }
}

test.scala

class Test {

  def test(): Unit = {
    scala.compiletime.testing.typeCheckErrors("Macro.subtypes[fixture.Shape]")
  }
}

package fixture {

  sealed trait Shape
  case class Circle(center: Int, rad: Double) extends Shape

  object Inner {
    case class Circle2(center: Int, rad: Double) extends Shape
  }
}

Output

-- Error: test.scala:14:15 ------------------------------------------------------
14 |    case class Circle2(center: Int, rad: Double) extends Shape
   |               ^
   |children of trait Shape were already queried before class Circle2 was discovered.
   |As a remedy, you could move class Circle2 on the same nesting level as trait Shape.

Expectation

Code should compile, as it does when either the code inside typeCheckErrors is extracted outside of that method.

Original minimization by @MateuszKubuszok with usage context: here

@jchyb jchyb added itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label area:metaprogramming:reflection Issues related to the quotes reflection API area:metaprogramming:compiletime The scala.compiletime package and removed stat:needs triage Every issue needs to have an "area" and "itype" label labels Aug 30, 2023
@jchyb jchyb self-assigned this Sep 14, 2023
jchyb added a commit to dotty-staging/dotty that referenced this issue Sep 15, 2023
…call

Sometimes, a macro or Mirror can be called/generated for a type that
may have not had its children registered by natural typechecking
procedures of the compiler. This meant that, in those cases, the Mirror
could not have been generated correctly, and macro could not obtain the
children of the class.

This was partially fixed in a previous PR by introducing a separate
procedure for querying children, outside of the regular typechecking
procedure. However, they was only queried in the owner symbol or in the
companion class, when non-enum sealed classes specifically, can
appear anywhere in the file. This commit aims to rectify this by
searching for any class that appears in the same file as the sealed
type.
jchyb added a commit to dotty-staging/dotty that referenced this issue Sep 18, 2023
…call

Sometimes, a macro or Mirror can be called/generated for a type that
may have not had its children registered by natural typechecking
procedures of the compiler. This meant that, in those cases, the Mirror
could not have been generated correctly, and macro could not obtain the
children of the class.

This was partially fixed in a previous PR by introducing a separate
procedure for querying children, outside of the regular typechecking
procedure. However, they was only queried in the owner symbol or in the
companion class, when non-enum sealed classes specifically, can
appear anywhere in the file. This commit aims to rectify this by
searching for any class that appears in the same file as the sealed
type.
@jchyb
Copy link
Contributor Author

jchyb commented Jul 4, 2024

This issue unfortunately will not be fixed. The limitation mentioned in the error message is clear and expected. Apparently these kinds of symbol loading issues were a frequent problem in scala 2, which is why this limitation was chosen for non-standard symbol loading contexts (like the macro entry being in the same file). An experimental fix was prepared with #18484, however in addition to it being not performant, it would force us to spend more time later on related issues (as the limitation would then be made inconsistent). A workaround fix here is to move the macro call outside of the enum definition.

@jchyb jchyb closed this as not planned Won't fix, can't repro, duplicate, stale Jul 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:metaprogramming:compiletime The scala.compiletime package area:metaprogramming:reflection Issues related to the quotes reflection API stat:wontfix
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant