Skip to content

Commit 9566149

Browse files
committed
Detect when position is not in known source
If the position is outside of the known contents of the source we do not return any source.
1 parent 4848748 commit 9566149

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2816,8 +2816,9 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
28162816
def startColumn: Int = self.startColumn
28172817
def endColumn: Int = self.endColumn
28182818
def sourceCode: Option[String] =
2819-
// TODO detect when we do not have a source and return None
2820-
Some(new String(self.source.content(), self.start, self.end - self.start))
2819+
val contents = self.source.content()
2820+
if contents.length < self.end then None
2821+
else Some(new String(contents, self.start, self.end - self.start))
28212822
end extension
28222823
end PositionMethods
28232824

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
2+
import scala.quoted.*
3+
import scala.tasty.inspector.*
4+
5+
@main def Test: Unit = {
6+
val classpath = System.getProperty("java.class.path").split(java.io.File.pathSeparatorChar).toList
7+
val jar = classOf[dotty.tools.dotc.Compiler].getProtectionDomain.getCodeSource.getLocation.getPath
8+
println(classpath)
9+
TastyInspector.inspectAllTastyFiles(
10+
tastyFiles = Nil,
11+
jars = jar :: Nil,
12+
dependenciesClasspath = classpath.filter(_.contains("scala3-compiler_3"))
13+
)(new MyInspector)
14+
}
15+
16+
17+
class MyInspector extends Inspector {
18+
def inspect(using Quotes)(tastys: List[Tasty[quotes.type]]): Unit = {
19+
import quotes.reflect.*
20+
val traverser = new TreeTraverser {
21+
override def traverseTree(tree: Tree)(owner: Symbol): Unit = {
22+
tree.pos.sourceCode
23+
super.traverseTree(tree)(owner)
24+
}
25+
}
26+
27+
tastys.foreach { tasty =>
28+
if tasty.path == "dotty/tools/dotc/core/Phases.class" then
29+
val tree = tasty.ast
30+
traverser.traverseTree(tree)(tree.symbol)
31+
}
32+
}
33+
}

0 commit comments

Comments
 (0)