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

ClassCastException while executing macro expansion on Scala3 #836

Closed
hiroshi-cl opened this issue Dec 25, 2022 · 5 comments · Fixed by #956
Closed

ClassCastException while executing macro expansion on Scala3 #836

hiroshi-cl opened this issue Dec 25, 2022 · 5 comments · Fixed by #956

Comments

@hiroshi-cl
Copy link

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 added a commit to mkurz/play-json-836 that referenced this issue Jul 21, 2023
Just run "sbt compile"
mkurz added a commit to mkurz/play-json-836 that referenced this issue Jul 21, 2023
Just run "sbt compile"
@mkurz
Copy link
Member

mkurz commented Jul 21, 2023

@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!

@pschichtel
Copy link

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.

@cchantep
Copy link
Member

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

cchantep added a commit to cchantep/play-json that referenced this issue Dec 23, 2023
mkurz pushed a commit to cchantep/play-json that referenced this issue Jan 16, 2024
mkurz added a commit that referenced this issue Jan 16, 2024
mergify bot pushed a commit that referenced this issue Jan 16, 2024
mergify bot added a commit that referenced this issue Jan 16, 2024
[2.10.x] Fix #836 - Scala3 macro nesting class (backport #956) by @cchantep
@mkurz
Copy link
Member

mkurz commented Jan 16, 2024

Fixed in 2.10.4 and 3.0.2

@hiroshi-cl
Copy link
Author

Thank you for your fixing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants