diff --git a/library/src/scala/quoted/Quotes.scala b/library/src/scala/quoted/Quotes.scala index 9718d8a668e0..5f286f88354e 100644 --- a/library/src/scala/quoted/Quotes.scala +++ b/library/src/scala/quoted/Quotes.scala @@ -497,6 +497,14 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching => /** Methods of the module object `val DefDef` */ trait DefDefModule { this: DefDef.type => + /** Create a method definition `def f[..](...)` with the signature defined in the symbol. + * + * The `rhsFn` is a function that receives references to its parameters and should return + * `Some` containing the implementation of the method. Returns `None` the method has no implementation. + * Any definition directly inside the implementation should have `symbol` as owner. + * + * See also: `Tree.changeOwner` + */ def apply(symbol: Symbol, rhsFn: List[List[Tree]] => Option[Term]): DefDef def copy(original: Tree)(name: String, paramss: List[ParamClause], tpt: TypeTree, rhs: Option[Term]): DefDef def unapply(ddef: DefDef): (String, List[ParamClause], TypeTree, Option[Term]) @@ -558,6 +566,14 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching => /** Methods of the module object `val ValDef` */ trait ValDefModule { this: ValDef.type => + /** Create a value definition `val x`, `var x` or `lazy val x` with the signature defined in the symbol. + * + * The `rhs` should return be `Some` containing the implementation of the method. + * Returns `None` the method has no implementation. + * Any definition directly inside the implementation should have `symbol` as owner. + * + * See also: `Tree.changeOwner` + */ def apply(symbol: Symbol, rhs: Option[Term]): ValDef def copy(original: Tree)(name: String, tpt: TypeTree, rhs: Option[Term]): ValDef def unapply(vdef: ValDef): (String, TypeTree, Option[Term]) diff --git a/tests/pos-macros/i12309/Macro_1.scala b/tests/pos-macros/i12309/Macro_1.scala new file mode 100644 index 000000000000..91c0932682bb --- /dev/null +++ b/tests/pos-macros/i12309/Macro_1.scala @@ -0,0 +1,25 @@ +import scala.quoted.* + +object TestMacro { + def use(f: () => String): Unit = () + + inline def test: Unit = ${testImpl} + + def testImpl(using Quotes): Expr[Unit] = { + import quotes.reflect.* + + def resultDefBody(): Term = '{ + val result: String = "xxx" + result + }.asTerm + val resultDefSymbol = Symbol.newMethod(Symbol.spliceOwner, "getResult", MethodType(Nil)(_ => Nil, _ => TypeRepr.of[String])) + val resultDef = DefDef(resultDefSymbol, { case _ => Some(resultDefBody().changeOwner(resultDefSymbol)) }) + val resultExpr = Block(List(resultDef), Closure(Ref(resultDefSymbol), None)).asExprOf[() => String] + + // + + val r = '{ TestMacro.use($resultExpr) } + // println(r.asTerm.show(using Printer.TreeShortCode)) + r + } +} diff --git a/tests/pos-macros/i12309/Test_2.scala b/tests/pos-macros/i12309/Test_2.scala new file mode 100644 index 000000000000..b290481ebbd2 --- /dev/null +++ b/tests/pos-macros/i12309/Test_2.scala @@ -0,0 +1,3 @@ +object Test extends App { + TestMacro.test +}