Skip to content

Commit 102284b

Browse files
Merge pull request #8770 from dotty-staging/fix-#8745
Fix #8745: Match Ident prefixes to find potential holes
2 parents 0db5976 + b903bd9 commit 102284b

File tree

8 files changed

+61
-4
lines changed

8 files changed

+61
-4
lines changed

compiler/src/dotty/tools/dotc/tastyreflect/ReflectionCompilerInterface.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,9 @@ class ReflectionCompilerInterface(val rootContext: core.Contexts.Context) extend
306306
case _ => None
307307
}
308308

309+
def Ref_term(tp: TermRef)(using ctx: Context): Ref =
310+
withDefaultPos(tpd.ref(tp).asInstanceOf[tpd.RefTree])
311+
309312
def Ref_apply(sym: Symbol)(using ctx: Context): Ref = {
310313
assert(sym.isTerm)
311314
withDefaultPos(tpd.ref(sym).asInstanceOf[tpd.RefTree])

library/src/scala/internal/quoted/Matcher.scala

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -274,13 +274,17 @@ private[quoted] object Matcher {
274274
case (scrutinee, Typed(expr2, _)) =>
275275
scrutinee =?= expr2
276276

277-
278277
/* Match selection */
279-
case (Select(qual1, _), Select(qual2, _)) if scrutinee.symbol == pattern.symbol =>
280-
qual1 =?= qual2
278+
case (ref: Ref, Select(qual2, _)) if scrutinee.symbol == pattern.symbol || summon[Env].get(scrutinee.symbol).contains(pattern.symbol) =>
279+
ref match
280+
case Select(qual1, _) => qual1 =?= qual2
281+
case ref: Ident =>
282+
ref.tpe match
283+
case TermRef(qual: TermRef, _) => Ref.term(qual) =?= qual2
284+
case _ => matched
281285

282286
/* Match reference */
283-
case (_: Ref, _: Ref) if scrutinee.symbol == pattern.symbol || summon[Env].get(scrutinee.symbol).contains(pattern.symbol) =>
287+
case (_: Ref, _: Ident) if scrutinee.symbol == pattern.symbol || summon[Env].get(scrutinee.symbol).contains(pattern.symbol) =>
284288
matched
285289

286290
/* Match application */

library/src/scala/tasty/Reflection.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -673,6 +673,10 @@ class Reflection(private[scala] val internal: CompilerInterface) { self =>
673673

674674
object Ref {
675675

676+
/** A tree representing the same reference as the given type */
677+
def term(tp: TermRef)(using ctx: Context): Ref =
678+
internal.Ref_term(tp)
679+
676680
/** Create a reference tree from a symbol
677681
*
678682
* If `sym` refers to a class member `foo` in class `C`,

library/src/scala/tasty/reflect/CompilerInterface.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,9 @@ trait CompilerInterface {
319319

320320
def isInstanceOfRef(using ctx: Context): IsInstanceOf[Ref]
321321

322+
/** A tree representing the same reference as the given type */
323+
def Ref_term(tp: TermRef)(using ctx: Context): Ref
324+
322325
def Ref_apply(sym: Symbol)(using ctx: Context): Ref
323326

324327
/** Tree representing a reference to definition with a given name */

tests/run-macros/i8745/Macro_1.scala

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import scala.quoted._
2+
3+
object Companion extends Trait
4+
trait Trait {
5+
def fun(first: String): String = "anything"
6+
}
7+
8+
object Macro {
9+
inline def mac(inline tree: String): String = ${ macImpl('tree) }
10+
def macImpl(tree: Expr[String])(using qctx: QuoteContext): Expr[String] = {
11+
tree match {
12+
case vv @ '{ ($s: Trait).fun($arg) } => arg
13+
case _ => Expr("not matched")
14+
}
15+
}
16+
}

tests/run-macros/i8745/Test_2.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import Macro._
2+
3+
@main def Test() = { //hello
4+
import Companion._
5+
mac(fun("blah"))
6+
}

tests/run-macros/i8745b/Macro_1.scala

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import scala.quoted._
2+
3+
object Companion {
4+
def fun(first: String): String = "anything"
5+
}
6+
7+
object Macro {
8+
inline def mac(inline tree: String): String = ${ macImpl('tree) }
9+
def macImpl(tree: Expr[String])(using qctx: QuoteContext): Expr[String] = {
10+
tree match {
11+
case vv @ '{ ($s: Companion.type).fun($arg) } => arg
12+
case _ => ???
13+
}
14+
}
15+
}

tests/run-macros/i8745b/Test_2.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import Macro._
2+
3+
@main def Test() = { //hello
4+
import Companion._
5+
mac(fun("blah"))
6+
}

0 commit comments

Comments
 (0)