Skip to content

Commit 3be078d

Browse files
authored
Merge pull request #13177 from dotty-staging/fix-#13007
Add reflect SourceFile.{getJPath,name,path}
2 parents 1520ef0 + 035e163 commit 3be078d

File tree

7 files changed

+29
-5
lines changed

7 files changed

+29
-5
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2747,6 +2747,9 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
27472747
given SourceFileMethods: SourceFileMethods with
27482748
extension (self: SourceFile)
27492749
def jpath: java.nio.file.Path = self.file.jpath
2750+
def getJPath: Option[java.nio.file.Path] = Option(self.file.jpath)
2751+
def name: String = self.name
2752+
def path: String = self.path
27502753
def content: Option[String] =
27512754
// TODO detect when we do not have a source and return None
27522755
Some(new String(self.content()))

library/src/scala/quoted/Quotes.scala

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4151,9 +4151,23 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
41514151
/** Extension methods of `SourceFile` */
41524152
trait SourceFileMethods {
41534153
extension (self: SourceFile)
4154-
/** Path to this source file */
4154+
/** Path to this source file. May be `null` for virtual files such as in the REPL. */
4155+
@deprecated("Use getJPath, name, or path instead of jpath", "3.0.2")
41554156
def jpath: java.nio.file.Path
41564157

4158+
/** Path to this source file. May be `None` for virtual files such as in the REPL. */
4159+
def getJPath: Option[java.nio.file.Path]
4160+
4161+
/** Name of the source file */
4162+
def name: String
4163+
4164+
/** Path of the source file.
4165+
*
4166+
* It does not necessarily point to a path in the filesystem, it could be the path of a virtual file.
4167+
* Use `getJPath` to get paths to the filesystem.
4168+
*/
4169+
def path: String
4170+
41574171
/** Content of this source file */
41584172
def content: Option[String]
41594173
end extension

project/MiMaFilters.scala

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,12 @@ import com.typesafe.tools.mima.core.ProblemFilters._
44

55
object MiMaFilters {
66
val Library: Seq[ProblemFilter] = Seq(
7+
// New APIs that will be introduced in 3.1.0
8+
exclude[ReversedMissingMethodProblem]("scala.quoted.Quotes#reflectModule#SourceFileMethods.getJPath"),
9+
exclude[ReversedMissingMethodProblem]("scala.quoted.Quotes#reflectModule#SourceFileMethods.name"),
10+
exclude[ReversedMissingMethodProblem]("scala.quoted.Quotes#reflectModule#SourceFileMethods.path"),
11+
exclude[DirectMissingMethodProblem]("scala.quoted.Quotes#reflectModule#SourceFileMethods.getJPath"),
12+
exclude[DirectMissingMethodProblem]("scala.quoted.Quotes#reflectModule#SourceFileMethods.name"),
13+
exclude[DirectMissingMethodProblem]("scala.quoted.Quotes#reflectModule#SourceFileMethods.path"),
714
)
815
}

scaladoc/src/dotty/tools/scaladoc/tasty/SymOps.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ object SymOps:
4040
else None
4141

4242
def source =
43-
val path = sym.pos.map(_.sourceFile.jpath).filter(_ != null).map(_.toAbsolutePath)
43+
val path = sym.pos.flatMap(_.sourceFile.getJPath).map(_.toAbsolutePath)
4444
path.map(TastyMemberSource(_, sym.pos.get.startLine))
4545

4646
//TODO: Retrieve string that will match scaladoc anchors

tests/run-macros/tasty-getfile-implicit-by-name-fun-context/Macro_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ object SourceFiles {
99

1010
def getThisFileImpl: Macro[String] =
1111
val q = quotes // Quotes is ByName and hence not stable (q stabilizes it)
12-
Expr(q.reflect.SourceFile.current.jpath.getFileName.toString)
12+
Expr(q.reflect.SourceFile.current.name)
1313

1414
}

tests/run-macros/tasty-macro-positions/quoted_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,6 @@ object Macros {
3030

3131
def posStr(using Quotes)(pos: quotes.reflect.Position): Expr[String] = {
3232
import quotes.reflect.*
33-
Expr(s"${pos.sourceFile.jpath.getFileName.toString}:[${pos.start}..${pos.end}]")
33+
Expr(s"${pos.sourceFile.name}:[${pos.start}..${pos.end}]")
3434
}
3535
}

tests/run-macros/tasty-positioned/quoted_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ object Positioned {
1313
import quotes.reflect.{Position as Pos, *}
1414
val pos = Pos.ofMacroExpansion
1515

16-
val path = Expr(pos.sourceFile.jpath.toString)
16+
val path = Expr(pos.sourceFile.getJPath.get.toString)
1717
val start = Expr(pos.start)
1818
val end = Expr(pos.end)
1919
val startLine = Expr(pos.startLine)

0 commit comments

Comments
 (0)