Skip to content

Commit 0040417

Browse files
authored
Merge pull request #14800 from dotty-staging/fix-14788
Handle this prefix in classes (in quote patterns)
2 parents fa59847 + b829b72 commit 0040417

File tree

2 files changed

+61
-5
lines changed

2 files changed

+61
-5
lines changed

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -245,11 +245,12 @@ object QuoteMatcher {
245245
ref match
246246
case Select(qual1, _) => qual1 =?= qual2
247247
case ref: Ident =>
248-
ref.tpe match
249-
case TermRef(qual: TermRef, _) => tpd.ref(qual) =?= qual2
250-
case TermRef(qual: ThisType, _) if qual.classSymbol.is(Module, butNot = Package) =>
251-
tpd.ref(qual.classSymbol.companionModule) =?= qual2
252-
case _ => matched
248+
if qual2.existsSubTree(_.symbol == defn.QuotedRuntimePatterns_patternHole) then
249+
// Prefix has a hole, so we need to match the prefix to extract the value of the hole
250+
tpd.desugarIdentPrefix(ref) =?= qual2
251+
else
252+
matched
253+
253254
/* Match reference */
254255
case _: Ident if symbolMatch(scrutinee, pattern) => matched
255256
/* Match type */
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)