Skip to content

Detect when position is not in known source #14801

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2816,8 +2816,9 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
def startColumn: Int = self.startColumn
def endColumn: Int = self.endColumn
def sourceCode: Option[String] =
// TODO detect when we do not have a source and return None
Some(new String(self.source.content(), self.start, self.end - self.start))
val contents = self.source.content()
if contents.length < self.end then None
else Some(new String(contents, self.start, self.end - self.start))
end extension
end PositionMethods

Expand Down
33 changes: 33 additions & 0 deletions tests/run-custom-args/tasty-inspector/i14785.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@

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

@main def Test: Unit = {
val classpath = System.getProperty("java.class.path").split(java.io.File.pathSeparatorChar).toList
val jar = classOf[dotty.tools.dotc.Compiler].getProtectionDomain.getCodeSource.getLocation.getPath
println(classpath)
TastyInspector.inspectAllTastyFiles(
tastyFiles = Nil,
jars = jar :: Nil,
dependenciesClasspath = classpath.filter(_.contains("scala3-compiler_3"))
)(new MyInspector)
}


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 = {
tree.pos.sourceCode
super.traverseTree(tree)(owner)
}
}

tastys.foreach { tasty =>
if tasty.path == "dotty/tools/dotc/core/Phases.class" then
val tree = tasty.ast
traverser.traverseTree(tree)(tree.symbol)
}
}
}