Skip to content

Add Tasty compilation unit source file #4627

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
Jun 22, 2018
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
2 changes: 2 additions & 0 deletions compiler/src/dotty/tools/dotc/tastyreflect/TastyImpl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ object TastyImpl extends scala.tasty.Tasty {

def ContextDeco(ctx: Context): ContextAPI = new ContextAPI {
def owner: Definition = FromSymbol.definition(ctx.owner)(ctx)

def source: java.nio.file.Path = ctx.compilationUnit.source.file.jpath
}

// ===== Id =======================================================
Expand Down
3 changes: 3 additions & 0 deletions library/src/scala/tasty/Tasty.scala
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ abstract class Tasty { tasty =>

trait ContextAPI {
def owner: Definition

/** Returns the source file being compiled. The path is relative to the current working directory. */
def source: java.nio.file.Path
}
implicit def ContextDeco(ctx: Context): ContextAPI

Expand Down
1 change: 1 addition & 0 deletions tests/run/tasty-getfile.check
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
App_2.scala
6 changes: 6 additions & 0 deletions tests/run/tasty-getfile/App_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

object Test {
def main(args: Array[String]): Unit = {
println(SourceFiles.getThisFile)
}
}
15 changes: 15 additions & 0 deletions tests/run/tasty-getfile/Macro_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import scala.quoted._

import scala.tasty.Universe

object SourceFiles {

implicit inline def getThisFile: String =
~getThisFileImpl(Universe.compilationUniverse) // FIXME infer Universe.compilationUniverse within top level ~

private def getThisFileImpl(implicit u: Universe): Expr[String] = {
import u.tasty._
u.context.source.getFileName.toString.toExpr
}

}