Skip to content

Commit 27f0201

Browse files
hamzaremmalWojciechMazur
authored andcommitted
Add test for #19842
[Cherry-picked 96e1d4e]
1 parent f87d4cb commit 27f0201

File tree

3 files changed

+70
-0
lines changed

3 files changed

+70
-0
lines changed

tests/neg-macros/i19842.check

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
-- Error: tests/neg-macros/i19842/Test.scala:9:50 ----------------------------------------------------------------------
2+
9 |@main def Test = summon[Serializer[ValidationCls]] // error
3+
| ^
4+
|Malformed tree was found while expanding macro with -Xcheck-macros.
5+
|The tree does not conform to the compiler's tree invariants.
6+
|
7+
|Macro was:
8+
|scala.quoted.runtime.Expr.splice[Serializer[ValidationCls]](((contextual$2: scala.quoted.Quotes) ?=> Macros.makeSerializer[ValidationCls](scala.quoted.Type.of[ValidationCls](contextual$2), contextual$2)))
9+
|
10+
|The macro returned:
11+
|{
12+
| object objectSerializer$macro$1 extends Serializer[ValidationCls] { this: objectSerializer$macro$1.type =>
13+
|
14+
| }
15+
| objectSerializer$macro$1
16+
|}
17+
|
18+
|Error:
19+
|assertion failed: Parents of class symbol differs from the parents in the tree for object objectSerializer$macro$1
20+
|
21+
|Parents in symbol: [class Object, trait Serializer]
22+
|Parents in tree: [trait Serializer]
23+
|
24+
|
25+
|stacktrace available when compiling with `-Ydebug`
26+
|---------------------------------------------------------------------------------------------------------------------
27+
|Inline stack trace
28+
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
29+
|This location contains code that was inlined from Test.scala:5
30+
5 | implicit inline def implicitMakeSerializer[T]: Serializer[T] = ${ Macros.makeSerializer[T] }
31+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
32+
---------------------------------------------------------------------------------------------------------------------

tests/neg-macros/i19842/Macro.scala

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
2+
import scala.annotation.{experimental, targetName}
3+
import scala.quoted.*
4+
import scala.util.Try
5+
6+
object Macros {
7+
def makeSerializer[T: Type](using Quotes): Expr[Serializer[T]] = {
8+
import quotes.reflect.*
9+
10+
val tpe: TypeRepr = TypeRepr.of[T]
11+
val name: String = Symbol.freshName("objectSerializer")
12+
13+
val modSym: Symbol = Symbol.newModule(
14+
Symbol.spliceOwner,
15+
name,
16+
Flags.Implicit,
17+
Flags.EmptyFlags,
18+
// Without TypeRep.of[Object] it would fail with java.lang.AssertionError: assertion failed: First parent must be a class
19+
List(TypeRepr.of[Object], TypeRepr.of[Serializer[T]]),
20+
_ => Nil,
21+
Symbol.noSymbol
22+
)
23+
24+
val (modValDef: ValDef, modClassDef: ClassDef) =
25+
ClassDef.module(modSym, List(TypeTree.of[Serializer[T]]), Nil)
26+
27+
Block(List(modValDef, modClassDef), Ref(modSym)).asExprOf[Serializer[T]]
28+
}
29+
}

tests/neg-macros/i19842/Test.scala

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2+
trait Serializer[@specialized T]
3+
4+
object Serializer:
5+
implicit inline def implicitMakeSerializer[T]: Serializer[T] = ${ Macros.makeSerializer[T] }
6+
7+
case class ValidationCls(string: String)
8+
9+
@main def Test = summon[Serializer[ValidationCls]] // error

0 commit comments

Comments
 (0)