-
Notifications
You must be signed in to change notification settings - Fork 152
Mix scala 2 and 3 macros #395
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
base: master
Are you sure you want to change the base?
Changes from all commits
2ef62be
b69c89a
02b74d3
b33b462
650fd7c
3632171
18c59d8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -108,11 +108,14 @@ lazy val macros = crossProject(JSPlatform, JVMPlatform, NativePlatform) | |
| name := "enumeratum-macros", | ||
| version := Versions.Macros.head, | ||
| crossScalaVersions := scalaVersionsAll, // eventually move this to aggregateProject once more 2.13 libs are out | ||
| libraryDependencies += { | ||
| libraryDependencies ++= { | ||
| if (scalaBinaryVersion.value == "3") { | ||
| "org.scala-lang" %% "scala3-compiler" % scalaVersion.value % Provided | ||
| Seq( | ||
| "org.scala-lang" %% "scala3-compiler" % scalaVersion.value % Provided, | ||
| "org.scala-lang" % "scala-reflect" % scala_2_13Version, | ||
| ) | ||
| } else { | ||
| "org.scala-lang" % "scala-reflect" % scalaVersion.value | ||
| Seq("org.scala-lang" % "scala-reflect" % scalaVersion.value) | ||
| } | ||
| }, | ||
| libraryDependencies += scalaXmlTest | ||
|
|
@@ -141,7 +144,7 @@ lazy val core = crossProject(JSPlatform, JVMPlatform, NativePlatform) | |
| if (useLocalVersion) { | ||
| Seq.empty | ||
| } else { | ||
| Seq("com.beachape" %%% "enumeratum-macros" % Versions.Macros.stable) | ||
| Seq("com.beachape" %%% "enumeratum-macros" % Versions.Macros.head) | ||
| } | ||
| }, | ||
| libraryDependencies += scalaXmlTest | ||
|
|
@@ -685,7 +688,7 @@ lazy val compilerSettings = Seq( | |
|
|
||
| val base = { | ||
| if (scalaBinaryVersion.value == "3") { | ||
| minimal :+ "-deprecation" | ||
| minimal :+ "-Wconf:cat=deprecation:s" | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Curious why this was needed?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. and a few more identical errors |
||
| } else { | ||
| minimal ++ Seq( | ||
| // "-Ywarn-adapted-args", | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| scala-2.13 |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -1,4 +1,5 @@ | ||||||
| package enumeratum | ||||||
| package compat | ||||||
|
|
||||||
| import ContextUtils.Context | ||||||
|
|
||||||
|
|
@@ -10,7 +11,7 @@ object EnumMacros { | |||||
|
|
||||||
| /** Finds any [A] in the current scope and returns an expression for a list of them | ||||||
| */ | ||||||
| def findValuesImpl[A: c.WeakTypeTag](c: Context): c.Expr[IndexedSeq[A]] = { | ||||||
| def findValuesImpl[A: c.WeakTypeTag](c: Context): c.Tree = { | ||||||
| import c.universe._ | ||||||
| val typeSymbol = weakTypeOf[A].typeSymbol | ||||||
| validateType(c)(typeSymbol) | ||||||
|
|
@@ -160,20 +161,22 @@ object EnumMacros { | |||||
| @SuppressWarnings(Array("org.wartremover.warts.AsInstanceOf")) | ||||||
| private[enumeratum] def buildSeqExpr[A: c.WeakTypeTag](c: Context)( | ||||||
| subclassSymbols: Seq[c.universe.Symbol] | ||||||
| ) = { | ||||||
| ): c.Tree = { | ||||||
| import c.universe._ | ||||||
| val resultType = weakTypeOf[A] | ||||||
| val indexedSeq = Ident(c.mirror.staticModule(classOf[IndexedSeq[A]].getName)) | ||||||
|
||||||
| val indexedSeq = Ident(c.mirror.staticModule(classOf[IndexedSeq[A]].getName)) | |
| val indexedSeq = Ident(c.mirror.staticModule("scala.collection.immutable.IndexedSeq")) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe this change was necessary because we no longer have a WeakTypeTag[IndexedSeq[A\] available.
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,4 +1,5 @@ | ||||||||||
| package enumeratum | ||||||||||
| package compat | ||||||||||
|
|
||||||||||
| import ContextUtils.Context | ||||||||||
|
|
||||||||||
|
|
@@ -15,7 +16,7 @@ object ValueEnumMacros { | |||||||||
| */ | ||||||||||
| def findIntValueEntriesImpl[ValueEntryType: c.WeakTypeTag]( | ||||||||||
| c: Context | ||||||||||
| ): c.Expr[IndexedSeq[ValueEntryType]] = { | ||||||||||
| ): c.Tree = { | ||||||||||
| findValueEntriesImpl[ValueEntryType, ContextUtils.CTInt, Int](c)(identity) | ||||||||||
| } | ||||||||||
|
|
||||||||||
|
|
@@ -25,7 +26,7 @@ object ValueEnumMacros { | |||||||||
| */ | ||||||||||
| def findLongValueEntriesImpl[ValueEntryType: c.WeakTypeTag]( | ||||||||||
| c: Context | ||||||||||
| ): c.Expr[IndexedSeq[ValueEntryType]] = { | ||||||||||
| ): c.Tree = { | ||||||||||
| findValueEntriesImpl[ValueEntryType, ContextUtils.CTLong, Long](c)(identity) | ||||||||||
| } | ||||||||||
|
|
||||||||||
|
|
@@ -38,7 +39,7 @@ object ValueEnumMacros { | |||||||||
| */ | ||||||||||
| def findShortValueEntriesImpl[ValueEntryType: c.WeakTypeTag]( | ||||||||||
| c: Context | ||||||||||
| ): c.Expr[IndexedSeq[ValueEntryType]] = { | ||||||||||
| ): c.Tree = { | ||||||||||
| findValueEntriesImpl[ValueEntryType, ContextUtils.CTInt, Short](c)( | ||||||||||
| _.toShort | ||||||||||
| ) // do a transform because there is no such thing as Short literals | ||||||||||
|
|
@@ -52,7 +53,7 @@ object ValueEnumMacros { | |||||||||
| */ | ||||||||||
| def findStringValueEntriesImpl[ValueEntryType: c.WeakTypeTag]( | ||||||||||
| c: Context | ||||||||||
| ): c.Expr[IndexedSeq[ValueEntryType]] = { | ||||||||||
| ): c.Tree = { | ||||||||||
| findValueEntriesImpl[ValueEntryType, String, String](c)(identity) | ||||||||||
| } | ||||||||||
|
|
||||||||||
|
|
@@ -64,7 +65,7 @@ object ValueEnumMacros { | |||||||||
| */ | ||||||||||
| def findByteValueEntriesImpl[ValueEntryType: c.WeakTypeTag]( | ||||||||||
| c: Context | ||||||||||
| ): c.Expr[IndexedSeq[ValueEntryType]] = { | ||||||||||
| ): c.Tree = { | ||||||||||
| findValueEntriesImpl[ValueEntryType, ContextUtils.CTInt, Byte](c)(_.toByte) | ||||||||||
| } | ||||||||||
|
|
||||||||||
|
|
@@ -76,7 +77,7 @@ object ValueEnumMacros { | |||||||||
| */ | ||||||||||
| def findCharValueEntriesImpl[ValueEntryType: c.WeakTypeTag]( | ||||||||||
| c: Context | ||||||||||
| ): c.Expr[IndexedSeq[ValueEntryType]] = { | ||||||||||
| ): c.Tree = { | ||||||||||
| findValueEntriesImpl[ValueEntryType, ContextUtils.CTChar, Char](c)(identity) | ||||||||||
| } | ||||||||||
|
|
||||||||||
|
|
@@ -88,7 +89,7 @@ object ValueEnumMacros { | |||||||||
| ProcessedValue | ||||||||||
| ](c: Context)( | ||||||||||
| processFoundValues: ValueType => ProcessedValue | ||||||||||
| ): c.Expr[IndexedSeq[ValueEntryType]] = { | ||||||||||
| ): c.Tree = { | ||||||||||
| import c.universe._ | ||||||||||
| val typeSymbol = weakTypeOf[ValueEntryType].typeSymbol | ||||||||||
| EnumMacros.validateType(c)(typeSymbol) | ||||||||||
|
|
@@ -104,7 +105,11 @@ object ValueEnumMacros { | |||||||||
| processFoundValues | ||||||||||
| ) | ||||||||||
|
|
||||||||||
| if (weakTypeOf[ValueEntryType] <:< c.typeOf[AllowAlias]) { | ||||||||||
| if ( | ||||||||||
| weakTypeOf[ValueEntryType].baseClasses.contains( | ||||||||||
| c.mirror.staticClass(classOf[AllowAlias].getName) | ||||||||||
| ) | ||||||||||
|
Comment on lines
+109
to
+111
|
||||||||||
| weakTypeOf[ValueEntryType].baseClasses.contains( | |
| c.mirror.staticClass(classOf[AllowAlias].getName) | |
| ) | |
| weakTypeOf[ValueEntryType] <:< weakTypeOf[AllowAlias] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's a similar problem here: the Scala 3 compiler is unable to summon WeakTypeTags
[error] -- [E172] Type Error: /home/jpeterson/lucid/enumeratum/macros/src/main/scala/enumeratum/compat/ValueEnumMacros.scala:108:61
[error] 108 | if (weakTypeOf[ValueEntryType] <:< weakTypeOf[AllowAlias]) {
[error] | ^
[error] | No WeakTypeTag available for enumeratum.values.AllowAlias
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Curious if there's a way to make this additional dependency
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately I think only the consumer of the library knows if the mixed mode macro is being used, from here, sbt only knows that it's a scala3 build.
I believe we could mark this as
Providedtoo, but then all consumers would need to explicitly provide it. Marking it provided here means I also need to add it as Provided incore. It would only need to be added as a normal/runtime dep if the project was using the mixed mode because I think this is only needed for the scala 2 portion of the build.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@lloydmeta let me know if you have a preference here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Still having a think about this - has there been any better solution than the ones presented here? Adding another runtime dependency is not great.
Personally I think it's fine if the experience is subpar for "mixed mode" but not for Scala 2 or 3 users.