Skip to content

Commit e748ab8

Browse files
nicolasstuckiG1ng3r
authored andcommitted
Register paramProxy and thisProxy in Quote type
Before 5ae7861 we used to find those types withing the `tpt`. Fixes #17434
1 parent bfe88a1 commit e748ab8

File tree

9 files changed

+77
-0
lines changed

9 files changed

+77
-0
lines changed

compiler/src/dotty/tools/dotc/inlines/Inliner.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,7 @@ class Inliner(val call: tpd.Tree)(using Context):
486486
/** Register type of leaf node */
487487
private def registerLeaf(tree: Tree): Unit = tree match
488488
case _: This | _: Ident | _: TypeTree => registerTypes.traverse(tree.typeOpt)
489+
case tree: Quote => registerTypes.traverse(tree.bodyType)
489490
case _ =>
490491

491492
/** Make `tree` part of inlined expansion. This means its owner has to be changed

tests/pos-macros/i17434a/Macro.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import scala.quoted.*
2+
3+
object SelectDynamicMacroImpl {
4+
def selectImpl[E: Type](
5+
ref: Expr[SQLSyntaxProvider[_]],
6+
name: Expr[String]
7+
)(using Quotes): Expr[SQLSyntax] = '{SQLSyntax("foo")}
8+
}

tests/pos-macros/i17434a/Test.scala

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// test.scala
2+
import scala.language.dynamics
3+
4+
trait SQLSyntaxProvider[A] extends Dynamic{
5+
def field(name: String): SQLSyntax = ???
6+
7+
inline def selectDynamic(inline name: String): SQLSyntax =
8+
select[A](this, name)
9+
10+
inline def select[E](ref: SQLSyntaxProvider[A], inline name: String): SQLSyntax =
11+
${ SelectDynamicMacroImpl.selectImpl[E]('ref, 'name) }
12+
}
13+
14+
class SQLSyntax(value: String)
15+
trait SQLSyntaxSupport[A]
16+
case class ColumnSQLSyntaxProvider[S <: SQLSyntaxSupport[A], A](support: S) extends SQLSyntaxProvider[A]
17+
18+
case class Account(id: Long, name: String)
19+
object Account extends SQLSyntaxSupport[Account]
20+
21+
def Test() =
22+
val p = ColumnSQLSyntaxProvider[Account.type, Account](Account)
23+
assert(p.name == SQLSyntax("name"))

tests/pos-macros/i17434b/Macro.scala

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
trait NameOf:
2+
transparent inline def nameOf(inline expr: Any): String = ${NameOfImpl.nameOf('expr)}
3+
transparent inline def nameOf[T](inline expr: T => Any): String = ${NameOfImpl.nameOf('expr)}
4+
object NameOf extends NameOf
5+
6+
import scala.compiletime.*
7+
8+
import scala.annotation.tailrec
9+
import scala.quoted.*
10+
11+
object NameOfImpl {
12+
def nameOf(expr: Expr[Any])(using Quotes): Expr[String] = {
13+
import quotes.reflect.*
14+
@tailrec def extract(tree: Tree): String = tree match {
15+
case Ident(name) => name
16+
case Select(_, name) => name
17+
case Block(List(stmt), term) => extract(stmt)
18+
case DefDef("$anonfun", _, _, Some(term)) => extract(term)
19+
case Block(_, term) => extract(term)
20+
case Apply(term, _) if term.symbol.fullName != "<special-ops>.throw" => extract(term)
21+
case TypeApply(term, _) => extract(term)
22+
case Inlined(_, _, term) => extract(term)
23+
case Typed(term, _) => extract(term)
24+
case _ => throw new MatchError(s"Unsupported expression: ${expr.show}")
25+
}
26+
val name = extract(expr.asTerm)
27+
Expr(name)
28+
}
29+
}

tests/pos-macros/i17434b/Test.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import NameOf._
2+
def test() =
3+
def func1(x: Int): String = ???
4+
val funcVal = func1 _
5+
assert(nameOf(funcVal) == "funcVal")
6+
assert(nameOf(func1 _) == "func1")

tests/pos-macros/i17434c/Macro.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import scala.quoted.*
2+
inline def foo[T](expr: T => Any): Unit = ${impl('expr)}
3+
def impl(expr: Expr[Any])(using Quotes): Expr[Unit] = '{}

tests/pos-macros/i17434c/Test.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
def test(f: Int => Any) = foo(f)

tests/pos-macros/i17434d/Macro.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import scala.quoted.*
2+
def impl[E: Type](ref: Expr[Foo[_]])(using Quotes): Expr[Unit] = '{ }

tests/pos-macros/i17434d/Test.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
trait Foo[A]:
2+
inline def foo(): Unit = bar[this.type](this)
3+
inline def bar[E](ref: Foo[A]): Unit = ${ impl[E]('ref) }
4+
def test(p: Foo[Int]) = p.foo()

0 commit comments

Comments
 (0)