Skip to content

Rework reflect SourceFile abstraction #10626

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 2 commits into from
Dec 4, 2020
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: 1 addition & 1 deletion community-build/community-projects/utest
17 changes: 9 additions & 8 deletions compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2556,26 +2556,27 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
def endLine: Int = self.endLine
def startColumn: Int = self.startColumn
def endColumn: Int = self.endColumn
def sourceCode: String =
new String(self.source.content(), self.start, self.end - self.start)
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))
end extension
end PositionMethodsImpl

type SourceFile = dotc.util.SourceFile

object SourceFile extends SourceFileModule
object SourceFile extends SourceFileModule {
def current: SourceFile = ctx.compilationUnit.source
}

object SourceFileMethodsImpl extends SourceFileMethods:
extension (self: SourceFile):
def jpath: java.nio.file.Path = self.file.jpath
def content: String = new String(self.content())
def content: Option[String] =
// TODO detect when we do not have a source and return None
Some(new String(self.content()))
end extension
end SourceFileMethodsImpl

object Source extends SourceModule:
def path: java.nio.file.Path = ctx.compilationUnit.source.file.jpath
end Source

object report extends reportModule:

def error(msg: String): Unit =
Expand Down
24 changes: 6 additions & 18 deletions library/src/scala/quoted/Quotes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4133,7 +4133,7 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
def endColumn: Int

/** Source code within the position */
def sourceCode: String
def sourceCode: Option[String]

end extension
}
Expand All @@ -4145,7 +4145,10 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
val SourceFile: SourceFileModule

/** Methods of the module object `val SourceFile` */
trait SourceFileModule { this: SourceFile.type => }
trait SourceFileModule { this: SourceFile.type =>
/** Returns the source file being compiled. The path is relative to the current working directory. */
def current: SourceFile
}

/** Makes extension methods on `SourceFile` available without any imports */
given SourceFileMethods: SourceFileMethods = SourceFileMethodsImpl
Expand All @@ -4160,25 +4163,10 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
def jpath: java.nio.file.Path

/** Content of this source file */
def content: String
def content: Option[String]
end extension
}

///////////////
// Source //
///////////////

/** Module object of `type Source` */
val Source: SourceModule

/** Methods of the module object `val Source` */
trait SourceModule { this: Source.type =>

/** Returns the source file being compiled. The path is relative to the current working directory. */
def path: java.nio.file.Path

}

///////////////
// REPORTING //
///////////////
Expand Down
2 changes: 1 addition & 1 deletion tests/run-macros/reflect-sourceCode/Macro_1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ object api {

private def reflImpl[T](x: Expr[T])(implicit qctx: Quotes): Expr[String] = {
import quotes.reflect._
Expr(Term.of(x).pos.sourceCode)
Expr(Term.of(x).pos.sourceCode.get)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ object SourceFiles {
${getThisFileImpl}

def getThisFileImpl: Macro[String] =
Expr(quotes.reflect.Source.path.getFileName.toString)
val q = quotes // Quotes is ByName and hence not stable (q stabilizes it)
Expr(q.reflect.SourceFile.current.jpath.getFileName.toString)

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ object SourceFiles {

def getThisFileImpl: Macro[String] = {
val qctx = tastyContext
Expr(qctx.reflect.Source.path.getFileName.toString)
Expr(qctx.reflect.SourceFile.current.jpath.getFileName.toString)
}

}
2 changes: 1 addition & 1 deletion tests/run-macros/tasty-getfile/Macro_1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ object SourceFiles {
${getThisFileImpl}

private def getThisFileImpl(using Quotes) : Expr[String] =
Expr(quotes.reflect.Source.path.getFileName.toString)
Expr(quotes.reflect.SourceFile.current.jpath.getFileName.toString)

}
2 changes: 1 addition & 1 deletion tests/run-macros/tasty-original-source/Macros_1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ object Macros {

private def impl(arg: Expr[Any])(using Quotes) : Expr[(String, Any)] = {
import quotes.reflect._
val source = Expr(Term.of(arg).underlyingArgument.pos.sourceCode.toString)
val source = Expr(Term.of(arg).underlyingArgument.pos.sourceCode.get.toString)
'{Tuple2($source, $arg)}
}

Expand Down