Skip to content

Commit 7669c5a

Browse files
committed
Move source related method to Source
1 parent 7354e84 commit 7669c5a

File tree

7 files changed

+53
-41
lines changed

7 files changed

+53
-41
lines changed

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

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ class ReflectionCompilerInterface(val rootContext: core.Contexts.Context) extend
5151

5252
def Context_owner(self: Context): Symbol = self.owner
5353

54-
def Context_source(self: Context): java.nio.file.Path = self.compilationUnit.source.file.jpath
5554

5655
def Context_GADT_setFreshGADTBounds(self: Context): Context =
5756
self.fresh.setFreshGADTBounds.addMode(Mode.GadtConstraintInference)
@@ -62,11 +61,16 @@ class ReflectionCompilerInterface(val rootContext: core.Contexts.Context) extend
6261
def Context_GADT_approximation(self: Context)(sym: Symbol, fromBelow: Boolean): Type =
6362
self.gadt.approximation(sym, fromBelow)
6463

65-
def Context_isJavaCompilationUnit(self: Context): Boolean = self.compilationUnit.isInstanceOf[fromtasty.JavaCompilationUnit]
66-
def Context_isScala2CompilationUnit(self: Context): Boolean = self.compilationUnit.isInstanceOf[fromtasty.Scala2CompilationUnit]
67-
def Context_isAlreadyLoadedCompilationUnit(self: Context): Boolean = self.compilationUnit.isInstanceOf[fromtasty.AlreadyLoadedCompilationUnit]
68-
def Context_compilationUnitClassname(self: Context): String =
69-
self.compilationUnit match {
64+
////////////
65+
// Source //
66+
////////////
67+
68+
def Source_path(using Context): java.nio.file.Path = ctx.compilationUnit.source.file.jpath
69+
def Source_isJavaCompilationUnit(using Context): Boolean = ctx.compilationUnit.isInstanceOf[fromtasty.JavaCompilationUnit]
70+
def Source_isScala2CompilationUnit(using Context): Boolean = ctx.compilationUnit.isInstanceOf[fromtasty.Scala2CompilationUnit]
71+
def Source_isAlreadyLoadedCompilationUnit(using Context): Boolean = ctx.compilationUnit.isInstanceOf[fromtasty.AlreadyLoadedCompilationUnit]
72+
def Source_compilationUnitClassname(using Context): String =
73+
ctx.compilationUnit match {
7074
case cu: fromtasty.JavaCompilationUnit => cu.className
7175
case cu: fromtasty.Scala2CompilationUnit => cu.className
7276
case cu: fromtasty.AlreadyLoadedCompilationUnit => cu.className

library/src/scala/tasty/Reflection.scala

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -429,23 +429,32 @@ class Reflection(private[scala] val internal: CompilerInterface) { self =>
429429
extension (self: Context):
430430
/** Returns the owner of the context */
431431
def owner: Symbol = internal.Context_owner(self)
432+
end extension
433+
end Context
432434

433-
/** Returns the source file being compiled. The path is relative to the current working directory. */
434-
def source: java.nio.file.Path = internal.Context_source(self)
435435

436-
/** Returns true if we've tried to reflect on a Java class. */
437-
def isJavaCompilationUnit(): Boolean = internal Context_isJavaCompilationUnit(self)
436+
///////////////
437+
// Source //
438+
///////////////
438439

439-
/** Returns true if we've tried to reflect on a Scala2 (non-Tasty) class. */
440-
def isScala2CompilationUnit(): Boolean = internal Context_isScala2CompilationUnit(self)
440+
object Source:
441441

442-
/** Returns true if we've tried to reflect on a class that's already loaded (e.g. Option). */
443-
def isAlreadyLoadedCompilationUnit(): Boolean = internal.Context_isAlreadyLoadedCompilationUnit(self)
442+
/** Returns the source file being compiled. The path is relative to the current working directory. */
443+
def path(using ctx: Context): java.nio.file.Path = internal.Source_path
444444

445-
/** Class name of the current CompilationUnit */
446-
def compilationUnitClassname(): String = internal.Context_compilationUnitClassname(self)
447-
end extension
448-
end Context
445+
/** Returns true if we've tried to reflect on a Java class. */
446+
def isJavaCompilationUnit(using ctx: Context): Boolean = internal.Source_isJavaCompilationUnit
447+
448+
/** Returns true if we've tried to reflect on a Scala2 (non-Tasty) class. */
449+
def isScala2CompilationUnit(using ctx: Context): Boolean = internal.Source_isScala2CompilationUnit
450+
451+
/** Returns true if we've tried to reflect on a class that's already loaded (e.g. Option). */
452+
def isAlreadyLoadedCompilationUnit(using ctx: Context): Boolean = internal.Source_isAlreadyLoadedCompilationUnit
453+
454+
/** Class name of the current CompilationUnit */
455+
def compilationUnitClassname(using ctx: Context): String = internal.Source_compilationUnitClassname
456+
457+
end Source
449458

450459

451460
///////////////

library/src/scala/tasty/reflect/CompilerInterface.scala

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -143,25 +143,29 @@ trait CompilerInterface {
143143
/** Returns the owner of the context */
144144
def Context_owner(self: Context): Symbol
145145

146-
/** Returns the source file being compiled. The path is relative to the current working directory. */
147-
def Context_source(self: Context): java.nio.file.Path
148-
149146
def Context_GADT_setFreshGADTBounds(self: Context): Context
150147
def Context_GADT_addToConstraint(self: Context)(syms: List[Symbol]): Boolean
151148
def Context_GADT_approximation(self: Context)(sym: Symbol, fromBelow: Boolean): Type
152149

150+
151+
////////////
152+
// Source //
153+
////////////
154+
155+
/** Returns the source file being compiled. The path is relative to the current working directory. */
156+
def Source_path(using ctx: Context): java.nio.file.Path
157+
153158
/** Returns true if we've tried to reflect on a Java class. */
154-
def Context_isJavaCompilationUnit(self: Context): Boolean
159+
def Source_isJavaCompilationUnit(using ctx: Context): Boolean
155160

156161
/** Returns true if we've tried to reflect on a Scala2 (non-Tasty) class. */
157-
def Context_isScala2CompilationUnit(self: Context): Boolean
162+
def Source_isScala2CompilationUnit(using ctx: Context): Boolean
158163

159164
/** Returns true if we've tried to reflect on a class that's already loaded (e.g. Option). */
160-
def Context_isAlreadyLoadedCompilationUnit(self: Context): Boolean
165+
def Source_isAlreadyLoadedCompilationUnit(using ctx: Context): Boolean
161166

162167
/** Class name of the current CompilationUnit */
163-
def Context_compilationUnitClassname(self: Context): String
164-
168+
def Source_compilationUnitClassname(using ctx: Context): String
165169

166170
///////////////
167171
// REPORTING //

tests/run-custom-args/tasty-inspector/i8215.scala

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,7 @@ class TestInspector_NonTasty() extends TastyInspector:
3535
var isScala2: Boolean = false
3636
var className: String = ""
3737

38-
protected def processCompilationUnit(reflect: Reflection)(root: reflect.Tree): Unit =
39-
import reflect.{_, given _}
40-
isJava = reflect.rootContext.isJavaCompilationUnit()
41-
isScala2 = reflect.rootContext.isScala2CompilationUnit()
42-
className = reflect.rootContext.compilationUnitClassname()
38+
protected def processCompilationUnit(reflect: Reflection)(root: reflect.Tree): Unit =
39+
isJava = reflect.Source.isJavaCompilationUnit
40+
isScala2 = reflect.Source.isScala2CompilationUnit
41+
className = reflect.Source.compilationUnitClassname

tests/run-custom-args/tasty-inspector/i8558.scala

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ class TestInspector_NonTasty() extends TastyInspector:
1717
var isAlreadyLoaded: Boolean = false
1818
var className: String = ""
1919

20-
protected def processCompilationUnit(reflect: Reflection)(root: reflect.Tree): Unit =
21-
import reflect.{_, given _}
22-
isAlreadyLoaded = reflect.rootContext.isAlreadyLoadedCompilationUnit()
23-
className = reflect.rootContext.compilationUnitClassname()
20+
protected def processCompilationUnit(reflect: Reflection)(root: reflect.Tree): Unit =
21+
isAlreadyLoaded = reflect.Source.isAlreadyLoadedCompilationUnit
22+
className = reflect.Source.compilationUnitClassname

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

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

1111
def getThisFileImpl: Macro[String] = {
1212
val qctx = tastyContext
13-
import qctx.tasty._
14-
Expr(rootContext.source.getFileName.toString)
13+
Expr(qctx.tasty.Source.path.getFileName.toString)
1514
}
1615

1716
}

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@ object SourceFiles {
66
implicit inline def getThisFile: String =
77
${getThisFileImpl}
88

9-
private def getThisFileImpl(using qctx: QuoteContext) : Expr[String] = {
10-
import qctx.tasty._
11-
Expr(summon[Context].source.getFileName.toString)
12-
}
9+
private def getThisFileImpl(using qctx: QuoteContext) : Expr[String] =
10+
Expr(qctx.tasty.Source.path.getFileName.toString)
1311

1412
}

0 commit comments

Comments
 (0)