Skip to content

Commit c11a1bc

Browse files
Merge pull request #12314 from dotty-staging/add-quotes-reflect-defdef-docs
Add regression test and docs for `reflect.DefDef.apply`
2 parents 1440699 + e120390 commit c11a1bc

File tree

3 files changed

+44
-0
lines changed

3 files changed

+44
-0
lines changed

library/src/scala/quoted/Quotes.scala

+16
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,14 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
497497

498498
/** Methods of the module object `val DefDef` */
499499
trait DefDefModule { this: DefDef.type =>
500+
/** Create a method definition `def f[..](...)` with the signature defined in the symbol.
501+
*
502+
* The `rhsFn` is a function that receives references to its parameters and should return
503+
* `Some` containing the implementation of the method. Returns `None` the method has no implementation.
504+
* Any definition directly inside the implementation should have `symbol` as owner.
505+
*
506+
* See also: `Tree.changeOwner`
507+
*/
500508
def apply(symbol: Symbol, rhsFn: List[List[Tree]] => Option[Term]): DefDef
501509
def copy(original: Tree)(name: String, paramss: List[ParamClause], tpt: TypeTree, rhs: Option[Term]): DefDef
502510
def unapply(ddef: DefDef): (String, List[ParamClause], TypeTree, Option[Term])
@@ -558,6 +566,14 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
558566

559567
/** Methods of the module object `val ValDef` */
560568
trait ValDefModule { this: ValDef.type =>
569+
/** Create a value definition `val x`, `var x` or `lazy val x` with the signature defined in the symbol.
570+
*
571+
* The `rhs` should return be `Some` containing the implementation of the method.
572+
* Returns `None` the method has no implementation.
573+
* Any definition directly inside the implementation should have `symbol` as owner.
574+
*
575+
* See also: `Tree.changeOwner`
576+
*/
561577
def apply(symbol: Symbol, rhs: Option[Term]): ValDef
562578
def copy(original: Tree)(name: String, tpt: TypeTree, rhs: Option[Term]): ValDef
563579
def unapply(vdef: ValDef): (String, TypeTree, Option[Term])

tests/pos-macros/i12309/Macro_1.scala

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import scala.quoted.*
2+
3+
object TestMacro {
4+
def use(f: () => String): Unit = ()
5+
6+
inline def test: Unit = ${testImpl}
7+
8+
def testImpl(using Quotes): Expr[Unit] = {
9+
import quotes.reflect.*
10+
11+
def resultDefBody(): Term = '{
12+
val result: String = "xxx"
13+
result
14+
}.asTerm
15+
val resultDefSymbol = Symbol.newMethod(Symbol.spliceOwner, "getResult", MethodType(Nil)(_ => Nil, _ => TypeRepr.of[String]))
16+
val resultDef = DefDef(resultDefSymbol, { case _ => Some(resultDefBody().changeOwner(resultDefSymbol)) })
17+
val resultExpr = Block(List(resultDef), Closure(Ref(resultDefSymbol), None)).asExprOf[() => String]
18+
19+
//
20+
21+
val r = '{ TestMacro.use($resultExpr) }
22+
// println(r.asTerm.show(using Printer.TreeShortCode))
23+
r
24+
}
25+
}

tests/pos-macros/i12309/Test_2.scala

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
object Test extends App {
2+
TestMacro.test
3+
}

0 commit comments

Comments
 (0)