Skip to content

Commit

Permalink
Handle this prefix in classes (in quote patterns) scala#14732
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolasstucki committed Mar 31, 2022
1 parent 616308d commit 250533c
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
2 changes: 2 additions & 0 deletions compiler/src/scala/quoted/runtime/impl/QuoteMatcher.scala
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,8 @@ object QuoteMatcher {
case TermRef(qual: TermRef, _) => tpd.ref(qual) =?= qual2
case TermRef(qual: ThisType, _) if qual.classSymbol.is(Module, butNot = Package) =>
tpd.ref(qual.classSymbol.companionModule) =?= qual2
case TermRef(qual, _) if qual.typeSymbol.isClass && qual2.symbol == defn.QuotedRuntimePatterns_patternHole =>
tpd.desugarIdentPrefix(ref) =?= qual2
case _ => matched
/* Match reference */
case _: Ident if symbolMatch(scrutinee, pattern) => matched
Expand Down
55 changes: 55 additions & 0 deletions tests/run-custom-args/tasty-inspector/i14788.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@

import scala.quoted.*
import scala.tasty.inspector.Inspector
import scala.tasty.inspector.Tasty
import scala.tasty.inspector.TastyInspector
import scala.reflect.ClassTag

import scala.quoted.*
import scala.tasty.inspector.*

@main def Test: Unit = {
// Artefact of the current test infrastructure
// TODO improve infrastructure to avoid needing this code on each test
val classpath = dotty.tools.dotc.util.ClasspathFromClassloader(this.getClass.getClassLoader).split(java.io.File.pathSeparator).find(_.contains("runWithCompiler")).get
val allTastyFiles = dotty.tools.io.Path(classpath).walkFilter(_.extension == "tasty").map(_.toString).toList
val tastyFiles = allTastyFiles.filter(_.contains("MySeq"))

TastyInspector.inspectTastyFiles(tastyFiles)(new MyInspector)
}

class MySeq(override val length: Int) extends collection.Seq[String] {
def foo: Int = length // error

def apply(v1: Int): String = ???
def iterator: Iterator[String] = ???
}

class MyInspector extends Inspector {
def inspect(using Quotes)(tastys: List[Tasty[quotes.type]]): Unit = {
import quotes.reflect.*
val traverser = new TreeTraverser {
override def traverseTree(tree: Tree)(owner: Symbol): Unit = {
if (tree.isExpr) {
try {
tree.asExpr match {
case '{ ($x: collection.Seq[t]).length } =>
super.traverseTree(tree)(owner)
case _ =>
super.traverseTree(tree)(owner)
}
} catch {
case e =>
report.error(s"unexpected error ${e}", tree.pos)
throw e
}
} else {
super.traverseTree(tree)(owner)
}
}
}
tastys.foreach{ tasty =>
traverser.traverseTree(tasty.ast)(tasty.ast.symbol)
}
}
}

0 comments on commit 250533c

Please sign in to comment.