Skip to content

Commit b9b8561

Browse files
committed
Add reflect SourceFile.{getJPath,name,path}
And deprecate SourceFile.jpath Fixes #13007
1 parent 23be42c commit b9b8561

File tree

7 files changed

+27
-6
lines changed

7 files changed

+27
-6
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: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4151,9 +4151,19 @@ 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+
def path: String
4166+
41574167
/** Content of this source file */
41584168
def content: Option[String]
41594169
end extension

project/MiMaFilters.scala

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@ object MiMaFilters {
88
exclude[MissingClassProblem]("scala.annotation.internal.ProvisionalSuperClass"),
99

1010
// New APIs marked @experimental in 3.0.2
11-
exclude[MissingClassProblem]("scala.Selectable$WithoutPreciseParameterTypes")
11+
exclude[MissingClassProblem]("scala.Selectable$WithoutPreciseParameterTypes"),
12+
13+
// New APIs that will be introduced in 3.1.0
14+
exclude[ReversedMissingMethodProblem]("scala.quoted.Quotes#reflectModule#SourceFileMethods.getJPath"),
15+
exclude[ReversedMissingMethodProblem]("scala.quoted.Quotes#reflectModule#SourceFileMethods.name"),
16+
exclude[ReversedMissingMethodProblem]("scala.quoted.Quotes#reflectModule#SourceFileMethods.path"),
17+
exclude[DirectMissingMethodProblem]("scala.quoted.Quotes#reflectModule#SourceFileMethods.getJPath"),
18+
exclude[DirectMissingMethodProblem]("scala.quoted.Quotes#reflectModule#SourceFileMethods.name"),
19+
exclude[DirectMissingMethodProblem]("scala.quoted.Quotes#reflectModule#SourceFileMethods.path"),
1220
)
1321
}

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)