-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
3 changed files
with
70 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
-- Error: tests/neg-macros/i19842/Test.scala:9:50 ---------------------------------------------------------------------- | ||
9 |@main def Test = summon[Serializer[ValidationCls]] // error | ||
| ^ | ||
|Malformed tree was found while expanding macro with -Xcheck-macros. | ||
|The tree does not conform to the compiler's tree invariants. | ||
| | ||
|Macro was: | ||
|scala.quoted.runtime.Expr.splice[Serializer[ValidationCls]](((contextual$2: scala.quoted.Quotes) ?=> Macros.makeSerializer[ValidationCls](scala.quoted.Type.of[ValidationCls](contextual$2), contextual$2))) | ||
| | ||
|The macro returned: | ||
|{ | ||
| object objectSerializer$macro$1 extends Serializer[ValidationCls] { this: objectSerializer$macro$1.type => | ||
| | ||
| } | ||
| objectSerializer$macro$1 | ||
|} | ||
| | ||
|Error: | ||
|assertion failed: Parents of class symbol differs from the parents in the tree for object objectSerializer$macro$1 | ||
| | ||
|Parents in symbol: [class Object, trait Serializer] | ||
|Parents in tree: [trait Serializer] | ||
| | ||
| | ||
|stacktrace available when compiling with `-Ydebug` | ||
|--------------------------------------------------------------------------------------------------------------------- | ||
|Inline stack trace | ||
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | ||
|This location contains code that was inlined from Test.scala:5 | ||
5 | implicit inline def implicitMakeSerializer[T]: Serializer[T] = ${ Macros.makeSerializer[T] } | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
--------------------------------------------------------------------------------------------------------------------- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
|
||
import scala.annotation.{experimental, targetName} | ||
import scala.quoted.* | ||
import scala.util.Try | ||
|
||
object Macros { | ||
def makeSerializer[T: Type](using Quotes): Expr[Serializer[T]] = { | ||
import quotes.reflect.* | ||
|
||
val tpe: TypeRepr = TypeRepr.of[T] | ||
val name: String = Symbol.freshName("objectSerializer") | ||
|
||
val modSym: Symbol = Symbol.newModule( | ||
Symbol.spliceOwner, | ||
name, | ||
Flags.Implicit, | ||
Flags.EmptyFlags, | ||
// Without TypeRep.of[Object] it would fail with java.lang.AssertionError: assertion failed: First parent must be a class | ||
List(TypeRepr.of[Object], TypeRepr.of[Serializer[T]]), | ||
_ => Nil, | ||
Symbol.noSymbol | ||
) | ||
|
||
val (modValDef: ValDef, modClassDef: ClassDef) = | ||
ClassDef.module(modSym, List(TypeTree.of[Serializer[T]]), Nil) | ||
|
||
Block(List(modValDef, modClassDef), Ref(modSym)).asExprOf[Serializer[T]] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
|
||
trait Serializer[@specialized T] | ||
|
||
object Serializer: | ||
implicit inline def implicitMakeSerializer[T]: Serializer[T] = ${ Macros.makeSerializer[T] } | ||
|
||
case class ValidationCls(string: String) | ||
|
||
@main def Test = summon[Serializer[ValidationCls]] // error |