diff --git a/compiler/src/dotty/tools/dotc/tastyreflect/ReflectionCompilerInterface.scala b/compiler/src/dotty/tools/dotc/tastyreflect/ReflectionCompilerInterface.scala index e5c4f938197c..1e9eab79b66f 100644 --- a/compiler/src/dotty/tools/dotc/tastyreflect/ReflectionCompilerInterface.scala +++ b/compiler/src/dotty/tools/dotc/tastyreflect/ReflectionCompilerInterface.scala @@ -306,6 +306,9 @@ class ReflectionCompilerInterface(val rootContext: core.Contexts.Context) extend case _ => None } + def Ref_term(tp: TermRef)(using ctx: Context): Ref = + withDefaultPos(tpd.ref(tp).asInstanceOf[tpd.RefTree]) + def Ref_apply(sym: Symbol)(using ctx: Context): Ref = { assert(sym.isTerm) withDefaultPos(tpd.ref(sym).asInstanceOf[tpd.RefTree]) diff --git a/library/src/scala/internal/quoted/Matcher.scala b/library/src/scala/internal/quoted/Matcher.scala index deb412f60405..a679ed93fcc4 100644 --- a/library/src/scala/internal/quoted/Matcher.scala +++ b/library/src/scala/internal/quoted/Matcher.scala @@ -271,13 +271,17 @@ private[quoted] object Matcher { case (scrutinee, Typed(expr2, _)) => scrutinee =?= expr2 - /* Match selection */ - case (Select(qual1, _), Select(qual2, _)) if scrutinee.symbol == pattern.symbol => - qual1 =?= qual2 + case (ref: Ref, Select(qual2, _)) if scrutinee.symbol == pattern.symbol || summon[Env].get(scrutinee.symbol).contains(pattern.symbol) => + ref match + case Select(qual1, _) => qual1 =?= qual2 + case ref: Ident => + ref.tpe match + case TermRef(qual: TermRef, _) => Ref.term(qual) =?= qual2 + case _ => matched /* Match reference */ - case (_: Ref, _: Ref) if scrutinee.symbol == pattern.symbol || summon[Env].get(scrutinee.symbol).contains(pattern.symbol) => + case (_: Ref, _: Ident) if scrutinee.symbol == pattern.symbol || summon[Env].get(scrutinee.symbol).contains(pattern.symbol) => matched /* Match application */ diff --git a/library/src/scala/tasty/Reflection.scala b/library/src/scala/tasty/Reflection.scala index f2f879e74553..d3f6e63719b7 100644 --- a/library/src/scala/tasty/Reflection.scala +++ b/library/src/scala/tasty/Reflection.scala @@ -673,6 +673,10 @@ class Reflection(private[scala] val internal: CompilerInterface) { self => object Ref { + /** A tree representing the same reference as the given type */ + def term(tp: TermRef)(using ctx: Context): Ref = + internal.Ref_term(tp) + /** Create a reference tree from a symbol * * If `sym` refers to a class member `foo` in class `C`, diff --git a/library/src/scala/tasty/reflect/CompilerInterface.scala b/library/src/scala/tasty/reflect/CompilerInterface.scala index 0204a7d526eb..93bffe1b72cf 100644 --- a/library/src/scala/tasty/reflect/CompilerInterface.scala +++ b/library/src/scala/tasty/reflect/CompilerInterface.scala @@ -319,6 +319,9 @@ trait CompilerInterface { def isInstanceOfRef(using ctx: Context): IsInstanceOf[Ref] + /** A tree representing the same reference as the given type */ + def Ref_term(tp: TermRef)(using ctx: Context): Ref + def Ref_apply(sym: Symbol)(using ctx: Context): Ref /** Tree representing a reference to definition with a given name */ diff --git a/tests/run-macros/i8745/Macro_1.scala b/tests/run-macros/i8745/Macro_1.scala new file mode 100644 index 000000000000..5575c4c07c85 --- /dev/null +++ b/tests/run-macros/i8745/Macro_1.scala @@ -0,0 +1,16 @@ +import scala.quoted._ + +object Companion extends Trait +trait Trait { + def fun(first: String): String = "anything" +} + +object Macro { + inline def mac(inline tree: String): String = ${ macImpl('tree) } + def macImpl(tree: Expr[String])(using qctx: QuoteContext): Expr[String] = { + tree match { + case vv @ '{ ($s: Trait).fun($arg) } => arg + case _ => Expr("not matched") + } + } +} diff --git a/tests/run-macros/i8745/Test_2.scala b/tests/run-macros/i8745/Test_2.scala new file mode 100644 index 000000000000..6d6770c14338 --- /dev/null +++ b/tests/run-macros/i8745/Test_2.scala @@ -0,0 +1,6 @@ +import Macro._ + +@main def Test() = { //hello + import Companion._ + mac(fun("blah")) +} diff --git a/tests/run-macros/i8745b/Macro_1.scala b/tests/run-macros/i8745b/Macro_1.scala new file mode 100644 index 000000000000..63af88c1ce19 --- /dev/null +++ b/tests/run-macros/i8745b/Macro_1.scala @@ -0,0 +1,15 @@ +import scala.quoted._ + +object Companion { + def fun(first: String): String = "anything" +} + +object Macro { + inline def mac(inline tree: String): String = ${ macImpl('tree) } + def macImpl(tree: Expr[String])(using qctx: QuoteContext): Expr[String] = { + tree match { + case vv @ '{ ($s: Companion.type).fun($arg) } => arg + case _ => ??? + } + } +} diff --git a/tests/run-macros/i8745b/Test_2.scala b/tests/run-macros/i8745b/Test_2.scala new file mode 100644 index 000000000000..6d6770c14338 --- /dev/null +++ b/tests/run-macros/i8745b/Test_2.scala @@ -0,0 +1,6 @@ +import Macro._ + +@main def Test() = { //hello + import Companion._ + mac(fun("blah")) +}