playframework/play-json

ClassCastException while executing macro expansion on Scala3

hiroshi-cl opened this issue · 5 comments

Play JSON Version (2.5.x / etc)

2.10.0-RC7

API (Scala / Java / Neither / Both)

Scala 3 (3.2.1)

Operating System (Ubuntu 15.10 / MacOS 10.10 / Windows 10)

Mac OS 12.6.2

JDK (Oracle 1.8.0_72, OpenJDK 1.8.x, Azul Zing)

openjdk version "11.0.2" 2019-01-15
OpenJDK Runtime Environment 18.9 (build 11.0.2+9)
OpenJDK 64-Bit Server VM 18.9 (build 11.0.2+9, mixed mode)

Library Dependencies

Nothing

Expected Behavior

Possible to compile this code on Scala 3.

import play.api.libs.json.Format
import play.api.libs.json.Json

case class JsonModel(rawString: String) extends AnyVal

object JsonModel {
  implicit val format: Format[JsonModel] = Json.valueFormat[JsonModel]
}

import play.api.libs.json.JsSuccess
import play.api.libs.json.Json
import play.api.libs.json.Reads

class Spec {

  case class Test(underlying: Option[JsonModel])

  object Test {
    implicit val reads: Reads[Test] = Json.reads[Test]
  }
}

Actual Behavior

ClassCastException on Scala 3. (No problem on Scala 2)

[error] 19 |    implicit val reads: Reads[Test] = Json.reads[Test]
[error]    |                                      ^^^^^^^^^^^^^^^^
[error]    |Exception occurred while executing macro expansion.
[error]    |java.lang.ClassCastException: class dotty.tools.dotc.ast.Trees$This cannot be cast to class dotty.tools.dotc.ast.Trees$RefTree (dotty.tools.dotc.ast.Trees$This and dotty.tools.dotc.ast.Trees$RefTree are in unnamed module of loader sbt.internal.classpath.ClassLoaderCache$Key$CachedClassLoader @210722d7)
[error]    |    at scala.quoted.runtime.impl.QuotesImpl.scala$quoted$runtime$impl$QuotesImpl$reflect$Ref$$$_$apply$$anonfun$6(QuotesImpl.scala:436)
[error]    |    at scala.quoted.runtime.impl.QuotesImpl$reflect$.scala$quoted$runtime$impl$QuotesImpl$reflect$$$withDefaultPos(QuotesImpl.scala:2906)
[error]    |    at scala.quoted.runtime.impl.QuotesImpl$reflect$Ref$.apply(QuotesImpl.scala:436)
[error]    |    at scala.quoted.runtime.impl.QuotesImpl$reflect$Ref$.apply(QuotesImpl.scala:434)
[error]    |    at play.api.libs.json.JsMacroImpl$ReadsHelper.productReads(JsMacroImpl.scala:322)
[error]    |    at play.api.libs.json.JsMacroImpl$ReadsHelper.productReads$(JsMacroImpl.scala:198)
[error]    |    at play.api.libs.json.JsMacroImpl$$anon$1.productReads(JsMacroImpl.scala:127)
[error]    |    at play.api.libs.json.JsMacroImpl$.readsExpr(JsMacroImpl.scala:168)
[error]    |    at play.api.libs.json.JsMacroImpl$.$anonfun$1(JsMacroImpl.scala:105)
[error]    |    at play.api.libs.json.JsMacroImpl$.$anonfun$adapted$1(JsMacroImpl.scala:105)
[error]    |    at dotty.tools.dotc.quoted.PickledQuotes$$anon$1.transform(PickledQuotes.scala:109)
[error]    |    at dotty.tools.dotc.ast.Trees$Instance$TreeMap.transform(Trees.scala:1446)
[error]    |    at dotty.tools.dotc.quoted.PickledQuotes$$anon$1.transform(PickledQuotes.scala:134)
[error]    |    at dotty.tools.dotc.ast.Trees$Instance$TreeMap.transformBlock(Trees.scala:1505)
[error]    |    at dotty.tools.dotc.ast.Trees$Instance$TreeMap.transform(Trees.scala:1426)
[error]    |    at dotty.tools.dotc.quoted.PickledQuotes$$anon$1.transform(PickledQuotes.scala:134)
[error]    |    at dotty.tools.dotc.ast.Trees$Instance$TreeMap.transform(Trees.scala:1478)
[error]    |    at dotty.tools.dotc.quoted.PickledQuotes$$anon$1.transform(PickledQuotes.scala:134)
[error]    |    at dotty.tools.dotc.ast.Trees$Instance$TreeMap.transform$$anonfun$1(Trees.scala:1507)
[error]    |    at scala.collection.immutable.List.mapConserve(List.scala:472)
[error]    |    at dotty.tools.dotc.ast.Trees$Instance$TreeMap.transform(Trees.scala:1507)
[error]    |    at dotty.tools.dotc.ast.Trees$Instance$TreeMap.transformStats(Trees.scala:1503)
[error]    |    at dotty.tools.dotc.ast.Trees$Instance$TreeMap.transformBlock(Trees.scala:1505)
[error]    |    at dotty.tools.dotc.ast.Trees$Instance$TreeMap.transform(Trees.scala:1426)
[error]    |    at dotty.tools.dotc.quoted.PickledQuotes$$anon$1.transform(PickledQuotes.scala:134)
[error]    |    at dotty.tools.dotc.ast.Trees$Instance$TreeMap.transformBlock(Trees.scala:1505)
[error]    |    at dotty.tools.dotc.ast.Trees$Instance$TreeMap.transform(Trees.scala:1426)
[error]    |    at dotty.tools.dotc.quoted.PickledQuotes$$anon$1.transform(PickledQuotes.scala:134)
[error]    |    at dotty.tools.dotc.ast.Trees$Instance$TreeMap.transform$$anonfun$1(Trees.scala:1507)
[error]    |    at scala.collection.immutable.List.mapConserve(List.scala:472)
[error]    |    at dotty.tools.dotc.ast.Trees$Instance$TreeMap.transform(Trees.scala:1507)
[error]    |    at dotty.tools.dotc.ast.Trees$Instance$TreeMap.transform(Trees.scala:1412)
[error]    |    at dotty.tools.dotc.quoted.PickledQuotes$$anon$1.transform(PickledQuotes.scala:134)
[error]    |    at dotty.tools.dotc.ast.Trees$Instance$TreeMap.transformBlock(Trees.scala:1505)
[error]    |    at dotty.tools.dotc.ast.Trees$Instance$TreeMap.transform(Trees.scala:1426)
[error]    |    at dotty.tools.dotc.quoted.PickledQuotes$$anon$1.transform(PickledQuotes.scala:134)
[error]    |    at dotty.tools.dotc.quoted.PickledQuotes$.spliceTerms(PickledQuotes.scala:151)
[error]    |    at dotty.tools.dotc.quoted.PickledQuotes$.unpickleTerm(PickledQuotes.scala:87)
[error]    |    at scala.quoted.runtime.impl.QuotesImpl.unpickleExprV2(QuotesImpl.scala:3044)
[error]    |    at play.api.libs.json.JsMacroImpl$.configuredReads(JsMacroImpl.scala:105)
[error]    |    at play.api.libs.json.JsMacroImpl$.reads$$anonfun$1(JsMacroImpl.scala:51)
[error]    |    at play.api.libs.json.JsMacroImpl$.withSummonedConfig(JsMacroImpl.scala:79)
[error]    |    at play.api.libs.json.JsMacroImpl$.reads(JsMacroImpl.scala:51)
[error]    |
[error]    |----------------------------------------------------------------------------
[error]    |Inline stack trace
[error]    |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
[error]    |This location contains code that was inlined from JsMacros.scala:28
[error]     ----------------------------------------------------------------------------
[error] one error found

Reproducible Test Case

above

mkurz commented

@cchantep and/or @xuwei-k it looks like you worked most on the Scala 3 JsMacroImpl.scala (see log), would you mind taking a look here what's going on? Maybe you are faster then me 😉

I uploaded a quick reproducer, you just need to run sbt compile to see the error:

I can take a look myself also, but if you have time before me that would help, thanks!

I've just hit this one. I originally thought this might be a scala lib/compiler issue, so tested it with 3.3.2-RC and 3.4.0-RC. Still happening for both.

Do not nest Test in class Spec, or make it an object.
Probably a metaprogramming limitation.

Thank you for your fixing