Skip to content

Commit 79d3c88

Browse files
committed
Add extra regression test for #12221
1 parent 2d2038e commit 79d3c88

File tree

2 files changed

+140
-0
lines changed

2 files changed

+140
-0
lines changed

tests/pos-macros/i12221/Macro_1.scala

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
import scala.quoted.*
2+
import scala.reflect.*
3+
4+
object Macros {
5+
6+
inline def show[A,B](inline expr: A => B): Unit =
7+
${printExpr('expr)}
8+
9+
def printExpr[A,B](expr: Expr[A=>B])(using Quotes) = '{
10+
println(${showExpr3(expr)})
11+
}
12+
13+
def showExpr3[A,B](expr: Expr[A=>B])(using Quotes): Expr[String] =
14+
import quotes.reflect.*
15+
16+
val sb = new StringBuilder
17+
18+
// Makes us only print the body of thr function
19+
def printDefFun(tree: Tree): Unit ={
20+
val acc = new TreeAccumulator[Unit]{
21+
def foldTree(s: Unit, tree: Tree)(owner: Symbol): Unit =
22+
tree match
23+
case deff : DefDef =>
24+
treePrint(deff.rhs.get, 0)
25+
sb.append("++++++++++++++++\n")
26+
sb.append(deff.rhs.get.show(using Printer.TreeStructure)).append('\n')
27+
case _ =>
28+
foldOverTree(s, tree)(owner)
29+
}
30+
acc.foldTree(List(), tree)(tree.symbol)
31+
}
32+
33+
def treePrint(tree: Tree, level: Int): Unit = {
34+
val pre = " " * level
35+
tree match {
36+
case body : Term => {
37+
body match {
38+
// Normal typed
39+
case typed: Typed =>
40+
sb.append(pre + typed.getClass()).append('\n')
41+
sb.append(pre + s"Typed with ${typed.tpt}:\n")
42+
treePrint(typed.expr , level + 1)
43+
case Block(statements, expr) =>
44+
sb.append(pre + "Block:{").append('\n')
45+
statements.map(stat => stat match{
46+
case term: Term => treePrint(term, level + 1)
47+
case deff: Definition =>
48+
sb.append(pre + "Definition statement\n")
49+
treePrint(deff, level + 1)
50+
case _ =>
51+
sb.append(pre + "Non-term statement\n")
52+
sb.append(stat.show(using Printer.TreeStructure)).append('\n')
53+
})
54+
treePrint(expr, level + 1)
55+
sb.append(pre + "}\n")
56+
57+
case Match(scrutinee, cases) =>
58+
sb.append(pre + "Match:\n")
59+
treePrint(scrutinee, level + 1)
60+
sb.append(pre + "with\n")
61+
cases.map(treePrint(_, level +1))
62+
63+
case Ident(name) =>
64+
sb.append(pre + s"Identifier(${name})\n")
65+
66+
case Apply(fun, args) =>
67+
sb.append(pre + "Apply\n")
68+
treePrint(fun, level + 1)
69+
if !args.isEmpty then
70+
sb.append(pre + "with arguments\n")
71+
args.zipWithIndex.map(
72+
(arg, index) =>
73+
treePrint(arg, level +1)
74+
if args.size > 1 && index < args.size -1 then
75+
// Used to seperate list of parameters
76+
sb.append(pre + ",\n")
77+
)
78+
case _ =>
79+
sb.append("Term\n")
80+
sb.append(tree.getClass()).append('\n')
81+
sb.append(tree.show(using Printer.TreeStructure)).append('\n')
82+
}
83+
}
84+
85+
case CaseDef(pattern, guard, rhs) =>
86+
sb.append(pre + "caseDef:\n" )
87+
treePrint(pattern, level + 1)
88+
treePrint(rhs, level + 1)
89+
90+
//Adding this unappy makes the typed get swallowed
91+
/*
92+
case Unapply(fun, implicits, pattern) =>
93+
sb.append(pre + "Unapply with function").append('\n')
94+
treePrint(fun , level + 1)
95+
sb.append(pre + "with patterns").append('\n')
96+
pattern.map(treePrint(_ , level + 1))
97+
*/
98+
case b: Bind => sb.append(pre + "Bind with stuff").append('\n')
99+
100+
case typed : Typed =>
101+
//sb.append(pre + typed.getClass()).append('\n')
102+
sb.append(pre + tree.getClass()).append('\n')
103+
sb.append(pre + s"Typed2 with ${typed.tpt}:").append('\n')
104+
treePrint(typed.expr , level + 1)
105+
106+
case Unapply(_,_,_) => sb.append(pre + "Unapply with stuff").append('\n')
107+
case _ =>
108+
tree match
109+
case t: Term => sb.append("Term").append('\n')
110+
case _ => ()
111+
sb.append(tree.getClass()).append('\n')
112+
sb.append(tree.show(using Printer.TreeStructure)).append('\n')
113+
}
114+
}
115+
116+
val tree: Term = expr.asTerm
117+
printDefFun(tree)
118+
sb.append("Finished").append('\n')
119+
Expr(sb.result())
120+
}

tests/pos-macros/i12221/Test_2.scala

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
@main
2+
def hello: Unit = {
3+
abstract class A
4+
case class Foo(a: Int) extends A
5+
6+
//Will print the body of the given function, then its print (using Printer.TreeStructure)
7+
Macros.show((x: A)=>{
8+
x match {
9+
case Foo(a) => a
10+
}:Int
11+
})
12+
/*
13+
val x = Foo(3)
14+
Macros.show(
15+
x match {
16+
case Foo(1) => 3
17+
}
18+
)*/
19+
20+
}

0 commit comments

Comments
 (0)