Skip to content

Add regression test and docs for reflect.DefDef.apply #12314

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions library/src/scala/quoted/Quotes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down Expand Up @@ -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])
Expand Down
25 changes: 25 additions & 0 deletions tests/pos-macros/i12309/Macro_1.scala
Original file line number Diff line number Diff line change
@@ -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
}
}
3 changes: 3 additions & 0 deletions tests/pos-macros/i12309/Test_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
object Test extends App {
TestMacro.test
}