Skip to content

Commit 9d5d5e4

Browse files
committed
Handle this prefix in classes (in quote patterns) #14732
Fixes #14788
1 parent 4848748 commit 9d5d5e4

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

compiler/src/scala/quoted/runtime/impl/QuoteMatcher.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,8 @@ object QuoteMatcher {
249249
case TermRef(qual: TermRef, _) => tpd.ref(qual) =?= qual2
250250
case TermRef(qual: ThisType, _) if qual.classSymbol.is(Module, butNot = Package) =>
251251
tpd.ref(qual.classSymbol.companionModule) =?= qual2
252+
case TermRef(qual, _) if qual.typeSymbol.isClass =>
253+
tpd.This(qual.typeSymbol.asClass) =?= qual2
252254
case _ => matched
253255
/* Match reference */
254256
case _: Ident if symbolMatch(scrutinee, pattern) => matched
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
2+
import scala.quoted.*
3+
import scala.tasty.inspector.Inspector
4+
import scala.tasty.inspector.Tasty
5+
import scala.tasty.inspector.TastyInspector
6+
import scala.reflect.ClassTag
7+
8+
import scala.quoted.*
9+
import scala.tasty.inspector.*
10+
11+
@main def Test: Unit = {
12+
// Artefact of the current test infrastructure
13+
// TODO improve infrastructure to avoid needing this code on each test
14+
val classpath = dotty.tools.dotc.util.ClasspathFromClassloader(this.getClass.getClassLoader).split(java.io.File.pathSeparator).find(_.contains("runWithCompiler")).get
15+
val allTastyFiles = dotty.tools.io.Path(classpath).walkFilter(_.extension == "tasty").map(_.toString).toList
16+
val tastyFiles = allTastyFiles.filter(_.contains("MySeq"))
17+
18+
TastyInspector.inspectTastyFiles(tastyFiles)(new MyInspector)
19+
}
20+
21+
class MySeq(override val length: Int) extends collection.Seq[String] {
22+
def foo: Int = length // error
23+
24+
def apply(v1: Int): String = ???
25+
def iterator: Iterator[String] = ???
26+
}
27+
28+
class MyInspector extends Inspector {
29+
def inspect(using Quotes)(tastys: List[Tasty[quotes.type]]): Unit = {
30+
import quotes.reflect.*
31+
val traverser = new TreeTraverser {
32+
override def traverseTree(tree: Tree)(owner: Symbol): Unit = {
33+
if (tree.isExpr) {
34+
try {
35+
tree.asExpr match {
36+
case '{ ($x: collection.Seq[t]).length } =>
37+
super.traverseTree(tree)(owner)
38+
case _ =>
39+
super.traverseTree(tree)(owner)
40+
}
41+
} catch {
42+
case e =>
43+
report.error(s"unexpected error ${e}", tree.pos)
44+
throw e
45+
}
46+
} else {
47+
super.traverseTree(tree)(owner)
48+
}
49+
}
50+
}
51+
tastys.foreach{ tasty =>
52+
traverser.traverseTree(tasty.ast)(tasty.ast.symbol)
53+
}
54+
}
55+
}

0 commit comments

Comments
 (0)