Skip to content

Commit d045a73

Browse files
committed
Fix scala#8745: Match Ident prefixes to find potential holes
1 parent 1931bc7 commit d045a73

File tree

6 files changed

+39
-3
lines changed

6 files changed

+39
-3
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: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -271,10 +271,14 @@ private[quoted] object Matcher {
271271
case (scrutinee, Typed(expr2, _)) =>
272272
scrutinee =?= expr2
273273

274-
275274
/* Match selection */
276-
case (Select(qual1, _), Select(qual2, _)) if scrutinee.symbol == pattern.symbol =>
277-
qual1 =?= qual2
275+
case (ref: Ref, Select(qual2, _)) if scrutinee.symbol == pattern.symbol =>
276+
ref match
277+
case ref: Select => ref.qualifier =?= qual2
278+
case _ =>
279+
ref.tpe match
280+
case TermRef(qual: TermRef, _) => Ref.term(qual) =?= qual2
281+
case _ => matched
278282

279283
/* Match reference */
280284
case (_: Ref, _: Ref) if scrutinee.symbol == pattern.symbol || summon[Env].get(scrutinee.symbol).contains(pattern.symbol) =>

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+
}

0 commit comments

Comments
 (0)