Skip to content
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

Register paramProxy and thisProxy in Quote type #17541

Merged
merged 1 commit into from
May 22, 2023
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
1 change: 1 addition & 0 deletions compiler/src/dotty/tools/dotc/inlines/Inliner.scala
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,7 @@ class Inliner(val call: tpd.Tree)(using Context):
/** Register type of leaf node */
private def registerLeaf(tree: Tree): Unit = tree match
case _: This | _: Ident | _: TypeTree => registerTypes.traverse(tree.typeOpt)
case tree: Quote => registerTypes.traverse(tree.bodyType)
case _ =>

/** Make `tree` part of inlined expansion. This means its owner has to be changed
Expand Down
8 changes: 8 additions & 0 deletions tests/pos-macros/i17434a/Macro.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import scala.quoted.*

object SelectDynamicMacroImpl {
def selectImpl[E: Type](
ref: Expr[SQLSyntaxProvider[_]],
name: Expr[String]
)(using Quotes): Expr[SQLSyntax] = '{SQLSyntax("foo")}
}
23 changes: 23 additions & 0 deletions tests/pos-macros/i17434a/Test.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// test.scala
import scala.language.dynamics

trait SQLSyntaxProvider[A] extends Dynamic{
def field(name: String): SQLSyntax = ???

inline def selectDynamic(inline name: String): SQLSyntax =
select[A](this, name)

inline def select[E](ref: SQLSyntaxProvider[A], inline name: String): SQLSyntax =
${ SelectDynamicMacroImpl.selectImpl[E]('ref, 'name) }
}

class SQLSyntax(value: String)
trait SQLSyntaxSupport[A]
case class ColumnSQLSyntaxProvider[S <: SQLSyntaxSupport[A], A](support: S) extends SQLSyntaxProvider[A]

case class Account(id: Long, name: String)
object Account extends SQLSyntaxSupport[Account]

def Test() =
val p = ColumnSQLSyntaxProvider[Account.type, Account](Account)
assert(p.name == SQLSyntax("name"))
29 changes: 29 additions & 0 deletions tests/pos-macros/i17434b/Macro.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
trait NameOf:
transparent inline def nameOf(inline expr: Any): String = ${NameOfImpl.nameOf('expr)}
transparent inline def nameOf[T](inline expr: T => Any): String = ${NameOfImpl.nameOf('expr)}
object NameOf extends NameOf

import scala.compiletime.*

import scala.annotation.tailrec
import scala.quoted.*

object NameOfImpl {
def nameOf(expr: Expr[Any])(using Quotes): Expr[String] = {
import quotes.reflect.*
@tailrec def extract(tree: Tree): String = tree match {
case Ident(name) => name
case Select(_, name) => name
case Block(List(stmt), term) => extract(stmt)
case DefDef("$anonfun", _, _, Some(term)) => extract(term)
case Block(_, term) => extract(term)
case Apply(term, _) if term.symbol.fullName != "<special-ops>.throw" => extract(term)
case TypeApply(term, _) => extract(term)
case Inlined(_, _, term) => extract(term)
case Typed(term, _) => extract(term)
case _ => throw new MatchError(s"Unsupported expression: ${expr.show}")
}
val name = extract(expr.asTerm)
Expr(name)
}
}
6 changes: 6 additions & 0 deletions tests/pos-macros/i17434b/Test.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import NameOf._
def test() =
def func1(x: Int): String = ???
val funcVal = func1 _
assert(nameOf(funcVal) == "funcVal")
assert(nameOf(func1 _) == "func1")
3 changes: 3 additions & 0 deletions tests/pos-macros/i17434c/Macro.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import scala.quoted.*
inline def foo[T](expr: T => Any): Unit = ${impl('expr)}
def impl(expr: Expr[Any])(using Quotes): Expr[Unit] = '{}
1 change: 1 addition & 0 deletions tests/pos-macros/i17434c/Test.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
def test(f: Int => Any) = foo(f)
2 changes: 2 additions & 0 deletions tests/pos-macros/i17434d/Macro.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import scala.quoted.*
def impl[E: Type](ref: Expr[Foo[_]])(using Quotes): Expr[Unit] = '{ }
4 changes: 4 additions & 0 deletions tests/pos-macros/i17434d/Test.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
trait Foo[A]:
inline def foo(): Unit = bar[this.type](this)
inline def bar[E](ref: Foo[A]): Unit = ${ impl[E]('ref) }
def test(p: Foo[Int]) = p.foo()