Skip to content

Commit 93dcb98

Browse files
committed
Rework reflect SourceFile abstraction
* Change signature to `def sourceCode: Option[String]` in `PositionMethods` * Change signature to `def content: Option[String]` in `SourceFileMethods` * Replace `Source.path` with `SourceFile.current: SourceFile` and remove `Source`
1 parent 5fd25e4 commit 93dcb98

File tree

7 files changed

+21
-31
lines changed

7 files changed

+21
-31
lines changed

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

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2535,26 +2535,27 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
25352535
def endLine: Int = self.endLine
25362536
def startColumn: Int = self.startColumn
25372537
def endColumn: Int = self.endColumn
2538-
def sourceCode: String =
2539-
new String(self.source.content(), self.start, self.end - self.start)
2538+
def sourceCode: Option[String] =
2539+
// TODO detect when we do not have a source and return None
2540+
Some(new String(self.source.content(), self.start, self.end - self.start))
25402541
end extension
25412542
end PositionMethodsImpl
25422543

25432544
type SourceFile = dotc.util.SourceFile
25442545

2545-
object SourceFile extends SourceFileModule
2546+
object SourceFile extends SourceFileModule {
2547+
def current: SourceFile = ctx.compilationUnit.source
2548+
}
25462549

25472550
object SourceFileMethodsImpl extends SourceFileMethods:
25482551
extension (self: SourceFile):
25492552
def jpath: java.nio.file.Path = self.file.jpath
2550-
def content: String = new String(self.content())
2553+
def content: Option[String] =
2554+
// TODO detect when we do not have a source and return None
2555+
Some(new String(self.content()))
25512556
end extension
25522557
end SourceFileMethodsImpl
25532558

2554-
object Source extends SourceModule:
2555-
def path: java.nio.file.Path = ctx.compilationUnit.source.file.jpath
2556-
end Source
2557-
25582559
object report extends reportModule:
25592560

25602561
def error(msg: String): Unit =

library/src/scala/quoted/Quotes.scala

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4104,7 +4104,7 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
41044104
def endColumn: Int
41054105

41064106
/** Source code within the position */
4107-
def sourceCode: String
4107+
def sourceCode: Option[String]
41084108

41094109
end extension
41104110
}
@@ -4116,7 +4116,10 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
41164116
val SourceFile: SourceFileModule
41174117

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

41214124
/** Makes extension methods on `SourceFile` available without any imports */
41224125
given SourceFileMethods: SourceFileMethods = SourceFileMethodsImpl
@@ -4131,25 +4134,10 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
41314134
def jpath: java.nio.file.Path
41324135

41334136
/** Content of this source file */
4134-
def content: String
4137+
def content: Option[String]
41354138
end extension
41364139
}
41374140

4138-
///////////////
4139-
// Source //
4140-
///////////////
4141-
4142-
/** Module object of `type Source` */
4143-
val Source: SourceModule
4144-
4145-
/** Methods of the module object `val Source` */
4146-
trait SourceModule { this: Source.type =>
4147-
4148-
/** Returns the source file being compiled. The path is relative to the current working directory. */
4149-
def path: java.nio.file.Path
4150-
4151-
}
4152-
41534141
///////////////
41544142
// REPORTING //
41554143
///////////////

tests/run-macros/reflect-sourceCode/Macro_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ object api {
66

77
private def reflImpl[T](x: Expr[T])(implicit qctx: Quotes): Expr[String] = {
88
import quotes.reflect._
9-
Expr(Term.of(x).pos.sourceCode)
9+
Expr(Term.of(x).pos.sourceCode.get)
1010
}
1111
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ object SourceFiles {
88
${getThisFileImpl}
99

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

1314
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ object SourceFiles {
1010

1111
def getThisFileImpl: Macro[String] = {
1212
val qctx = tastyContext
13-
Expr(qctx.reflect.Source.path.getFileName.toString)
13+
Expr(qctx.reflect.SourceFile.current.jpath.getFileName.toString)
1414
}
1515

1616
}

tests/run-macros/tasty-getfile/Macro_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ object SourceFiles {
77
${getThisFileImpl}
88

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

1212
}

tests/run-macros/tasty-original-source/Macros_1.scala

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

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

0 commit comments

Comments
 (0)