Skip to content

Commit 1efbe44

Browse files
committed
Fix #6988: add test
1 parent 13da159 commit 1efbe44

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package foo
2+
3+
case class FirstArg(value: Any, source: String)
4+
object FirstArg {
5+
inline given as FirstArg = ${Macros.argsImpl}
6+
}
7+
8+
object Macros {
9+
import language.experimental.macros
10+
import scala.quoted._
11+
import scala.tasty._
12+
13+
def argsImpl given (qctx: QuoteContext): Expr[FirstArg] = {
14+
import qctx.tasty._
15+
var enclosingCls: ClassDefSymbol = rootContext.owner.asInstanceOf[ClassDefSymbol]
16+
def enclosingParamList(owner: Symbol): Seq[Seq[Symbol]] =
17+
owner match {
18+
case IsClassDefSymbol(x) =>
19+
enclosingCls = x
20+
x.tree.constructor.paramss map { _ map {
21+
_.symbol
22+
}}
23+
case _ =>
24+
enclosingParamList(owner.owner)
25+
}
26+
def literal(value: String): Expr[String] =
27+
Literal(Constant(value)).seal.asInstanceOf[Expr[String]]
28+
val paramss = enclosingParamList(rootContext.owner)
29+
val firstArg = paramss.flatten.head
30+
val ref = Select.unique(This(enclosingCls), firstArg.name)
31+
'{ FirstArg(${ref.seal}, ${firstArg.name.toExpr}) }
32+
}
33+
}

tests/run-macros/i6988/Test_2.scala

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
object Test {
2+
var firstArgName = ""
3+
var firstArgValue: Any = ""
4+
def main(args: Array[String]): Unit = {
5+
val x = new Foo("something", 2L, false)
6+
assert("p1" == firstArgName)
7+
assert("something" == firstArgValue)
8+
}
9+
def debug given foo.FirstArg: Unit = {
10+
firstArgName = the[foo.FirstArg].source
11+
firstArgValue = the[foo.FirstArg].value
12+
}
13+
class Foo(p1: String, p2: Long, p3: Boolean) {
14+
debug
15+
}
16+
}

0 commit comments

Comments
 (0)