Skip to content

Commit 497efb9

Browse files
Merge pull request #4627 from dotty-staging/add-tasty-ctx-file
Add Tasty compilation unit source file
2 parents d9029a6 + 837d38f commit 497efb9

File tree

5 files changed

+27
-0
lines changed

5 files changed

+27
-0
lines changed

compiler/src/dotty/tools/dotc/tastyreflect/TastyImpl.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ class TastyImpl(val rootContext: Contexts.Context) extends scala.tasty.Tasty { s
4141

4242
def ContextDeco(ctx: Context): ContextAPI = new ContextAPI {
4343
def owner: Definition = FromSymbol.definition(ctx.owner)(ctx)
44+
45+
def source: java.nio.file.Path = ctx.compilationUnit.source.file.jpath
4446
}
4547

4648
// ===== Id =======================================================

library/src/scala/tasty/Tasty.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ abstract class Tasty { tasty =>
3131

3232
trait ContextAPI {
3333
def owner: Definition
34+
35+
/** Returns the source file being compiled. The path is relative to the current working directory. */
36+
def source: java.nio.file.Path
3437
}
3538
implicit def ContextDeco(ctx: Context): ContextAPI
3639

tests/run/tasty-getfile.check

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
App_2.scala

tests/run/tasty-getfile/App_2.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
object Test {
3+
def main(args: Array[String]): Unit = {
4+
println(SourceFiles.getThisFile)
5+
}
6+
}

tests/run/tasty-getfile/Macro_1.scala

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import scala.quoted._
2+
3+
import scala.tasty.Universe
4+
5+
object SourceFiles {
6+
7+
implicit inline def getThisFile: String =
8+
~getThisFileImpl(Universe.compilationUniverse) // FIXME infer Universe.compilationUniverse within top level ~
9+
10+
private def getThisFileImpl(implicit u: Universe): Expr[String] = {
11+
import u.tasty._
12+
u.context.source.getFileName.toString.toExpr
13+
}
14+
15+
}

0 commit comments

Comments
 (0)