Skip to content

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

Closed
@WojciechMazur

Description

@WojciechMazur

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

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions