Skip to content

Regression in rssh/dotty-cps-async - compiler produces illegal TypeApply #20309

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

Closed
WojciechMazur opened this issue May 1, 2024 · 0 comments · Fixed by #20387
Closed

Regression in rssh/dotty-cps-async - compiler produces illegal TypeApply #20309

WojciechMazur opened this issue May 1, 2024 · 0 comments · Fixed by #20387
Assignees
Labels
area:metaprogramming:reflection Issues related to the quotes reflection API area:typer itype:bug regression This worked in a previous version but doesn't anymore
Milestone

Comments

@WojciechMazur
Copy link
Contributor

Based on OpenCB failure in rssh/dotty-cps-async - build logs.
Project uses quotes.reflect.TreeMap to manipulate AST, however it's usage fails due to illegal TypeApply type argument of () literal produced by the compiler.

Compiler version

Last good release: 3.5.0-RC1-bin-20240404-a7f00e2-NIGHTLY
First bad release: 3.5.0-RC1-bin-20240405-85672a0-NIGHTLY
Bisect points to 85672a0

Minimized code

// 1_defns.scala
trait Context
object Scope:
  def spawn[A](f: Context ?=> A): A = ???

type Contextual[T] = Context ?=> T

transparent inline def inScope[T](inline expr: Context ?=> T): T =
  val fn = Macros.transformContextLambda[T](expr)
  fn(new Context {})

@main def Test = {
  inScope {
    Scope.spawn[Unit] { () }
  }
}
// 2_macros.scala
import scala.quoted.*
import scala.compiletime.*

object Macros {
  inline def transformContextLambda[T](inline expr: Context ?=> T): Context => T =
    ${ transformContextLambdaImpl[T]('expr) }

  def transformContextLambdaImpl[T: Type](
      cexpr: Expr[Context ?=> T]
  )(using Quotes): Expr[Context => T] = {
    import quotes.reflect.*
    val tree = asTerm(cexpr)
    val traverse = new TreeMap() {}
    println(tree.show)
    traverse.transformTree(tree)(tree.symbol)
    '{ _ => ??? }
  }
}

Output

((contextual$2: Context) ?=> Scope.spawn[()](((contextual$6: Context) ?=> ())))
-- Error: /Users/wmazur/projects/sandbox/cps.scala:12:10 -----------------------
12 |  inScope {
   |  ^
   |Exception occurred while executing macro expansion.
   |scala.MatchError: Literal(UnitConstant()) (of class java.lang.String)
   |    at scala.quoted.Quotes$reflectModule$TreeMap.transformTypeTree(Quotes.scala:5126)
   |    at scala.quoted.Quotes$reflectModule$TreeMap.transformTypeTree$(Quotes.scala:4981)
   |    at Macros$$anon$1.transformTypeTree(cps.macros.scala:13)
   |    at scala.quoted.Quotes$reflectModule$TreeMap.transformTypeTrees$$anonfun$1(Quotes.scala:5147)
   |    at scala.collection.immutable.List.mapConserve(List.scala:472)
   |    at scala.quoted.Quotes$reflectModule$TreeMap.transformTypeTrees(Quotes.scala:5147)
   |    at scala.quoted.Quotes$reflectModule$TreeMap.transformTypeTrees$(Quotes.scala:4981)
   |    at Macros$$anon$1.transformTypeTrees(cps.macros.scala:13)
   |    at scala.quoted.Quotes$reflectModule$TreeMap.transformTerm(Quotes.scala:5064)
   |    at scala.quoted.Quotes$reflectModule$TreeMap.transformTerm$(Quotes.scala:4981)
   |    at Macros$$anon$1.transformTerm(cps.macros.scala:13)
   |    at scala.quoted.Quotes$reflectModule$TreeMap.transformTerm(Quotes.scala:5062)
   |    at scala.quoted.Quotes$reflectModule$TreeMap.transformTerm$(Quotes.scala:4981)
   |    at Macros$$anon$1.transformTerm(cps.macros.scala:13)
   |    at scala.quoted.Quotes$reflectModule$TreeMap.transformTerm(Quotes.scala:5092)
   |    at scala.quoted.Quotes$reflectModule$TreeMap.transformTerm$(Quotes.scala:4981)
   |    at Macros$$anon$1.transformTerm(cps.macros.scala:13)
   |    at scala.quoted.Quotes$reflectModule$TreeMap.transformTerm(Quotes.scala:5092)
   |    at scala.quoted.Quotes$reflectModule$TreeMap.transformTerm$(Quotes.scala:4981)
   |    at Macros$$anon$1.transformTerm(cps.macros.scala:13)
   |    at scala.quoted.Quotes$reflectModule$TreeMap.transformStatement$$anonfun$1(Quotes.scala:5029)
   |    at scala.Option.map(Option.scala:242)
   |    at scala.quoted.Quotes$reflectModule$TreeMap.transformStatement(Quotes.scala:5029)
   |    at scala.quoted.Quotes$reflectModule$TreeMap.transformStatement$(Quotes.scala:4981)
   |    at Macros$$anon$1.transformStatement(cps.macros.scala:13)
   |    at scala.quoted.Quotes$reflectModule$TreeMap.transformStats$$anonfun$1(Quotes.scala:5138)
   |    at scala.collection.immutable.List.mapConserve(List.scala:472)
   |    at scala.quoted.Quotes$reflectModule$TreeMap.transformStats(Quotes.scala:5138)
   |    at scala.quoted.Quotes$reflectModule$TreeMap.transformStats$(Quotes.scala:4981)
   |    at Macros$$anon$1.transformStats(cps.macros.scala:13)
   |    at scala.quoted.Quotes$reflectModule$TreeMap.transformTerm(Quotes.scala:5076)
   |    at scala.quoted.Quotes$reflectModule$TreeMap.transformTerm$(Quotes.scala:4981)
   |    at Macros$$anon$1.transformTerm(cps.macros.scala:13)
   |    at scala.quoted.Quotes$reflectModule$TreeMap.transformTerm(Quotes.scala:5092)
   |    at scala.quoted.Quotes$reflectModule$TreeMap.transformTerm$(Quotes.scala:4981)
   |    at Macros$$anon$1.transformTerm(cps.macros.scala:13)
   |    at scala.quoted.Quotes$reflectModule$TreeMap.transformStatement(Quotes.scala:5017)
   |    at scala.quoted.Quotes$reflectModule$TreeMap.transformStatement$(Quotes.scala:4981)
   |    at Macros$$anon$1.transformStatement(cps.macros.scala:13)
   |    at scala.quoted.Quotes$reflectModule$TreeMap.transformTree(Quotes.scala:4992)
   |    at scala.quoted.Quotes$reflectModule$TreeMap.transformTree$(Quotes.scala:4981)
   |    at Macros$$anon$1.transformTree(cps.macros.scala:13)
   |    at Macros$.transformContextLambdaImpl(cps.macros.scala:15)
   |
13 |    Scope.spawn[Unit] { () }
14 |  }
   |----------------------------------------------------------------------------
   |Inline stack trace
   |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
   |This location contains code that was inlined from cps.macros.scala:6
 6 |    ${ transformContextLambdaImpl[T]('expr) }
   |    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
   |This location contains code that was inlined from cps.macros.scala:6
 8 |  val fn = Macros.transformContextLambda[T](expr)
   |           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    ----------------------------------------------------------------------------
1 error found

Expectation

Should compile

@WojciechMazur WojciechMazur added itype:bug area:typer regression This worked in a previous version but doesn't anymore area:metaprogramming:reflection Issues related to the quotes reflection API labels May 1, 2024
@Kordyjan Kordyjan added this to the 3.5.1 milestone Jul 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:metaprogramming:reflection Issues related to the quotes reflection API area:typer itype:bug regression This worked in a previous version but doesn't anymore
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants