Skip to content

Commit 1e1a3a3

Browse files
committed
implemented requested changes
1 parent 0afdbcd commit 1e1a3a3

File tree

8 files changed

+52
-45
lines changed

8 files changed

+52
-45
lines changed

compiler/src/dotty/tools/dotc/fromtasty/JavaCompilationUnit.scala

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@ package dotty.tools.dotc.fromtasty
33
import dotty.tools.dotc.CompilationUnit
44
import dotty.tools.dotc.util.NoSource
55

6-
/**
7-
* A marker CompilationUnit to return up the call stack from ReadTasty. This will tell us that we've
8-
* encountered, and attempted to inspect, a Java class file. We can't TASTy-inspect a Java class obviously,
9-
* but we want to return the fact we found it so that higher-up we can take appropriate action if desired.
6+
/** A marker CompilationUnit to return up the call stack from ReadTasty. This will tell us that we've
7+
* encountered, and attempted to inspect, a Java class file. We can't TASTy-inspect a Java class obviously,
8+
* but we want to return the fact we found it so that higher-up we can take appropriate action if desired.
109
*/
1110
class JavaCompilationUnit(val className: String) extends CompilationUnit(NoSource) {
1211
override def toString: String = s"Java class file $className"

compiler/src/dotty/tools/dotc/fromtasty/NonTastyScalaCompilationUnit.scala

Lines changed: 0 additions & 14 deletions
This file was deleted.

compiler/src/dotty/tools/dotc/fromtasty/ReadTasty.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class ReadTasty extends Phase {
4949
}
5050
case tree: Tree[?] =>
5151
cls.denot.infoOrCompleter match {
52-
case _: NoLoader => Some(NonTastyScalaCompilationUnit(cls.fullName.toString))
52+
case _: NoLoader => Some(Scala2CompilationUnit(cls.fullName.toString))
5353
case _ if cls.flags.is(Flags.JavaDefined) => Some(JavaCompilationUnit(cls.fullName.toString))
5454
case _ => alreadyLoaded()
5555
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package dotty.tools.dotc.fromtasty
2+
3+
import dotty.tools.dotc.CompilationUnit
4+
import dotty.tools.dotc.util.NoSource
5+
6+
/** A marker CompilationUnit to return up the call stack from ReadTasty. This will tell us that we've
7+
* encountered, and attempted to inspect, a Scala2 class file (which has no .tasty file).
8+
* In this case we still want to return the fact we found it so that higher-up we can take appropriate
9+
* action if desired.
10+
*/
11+
class Scala2CompilationUnit(val className: String) extends CompilationUnit(NoSource) {
12+
override def toString: String = s"Scala2 class file $className"
13+
}

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

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,15 +65,13 @@ class ReflectionCompilerInterface(val rootContext: core.Contexts.Context) extend
6565
def Context_requiredClass(self: Context)(path: String): Symbol = self.requiredClass(path)
6666
def Context_requiredModule(self: Context)(path: String): Symbol = self.requiredModule(path)
6767
def Context_requiredMethod(self: Context)(path: String): Symbol = self.requiredMethod(path)
68-
def Context_javaCompilationUnitClassname(self: Context): Option[String] =
68+
def Context_isJavaCompilationUnit(self: Context): Boolean = self.compilationUnit.isInstanceOf[fromtasty.JavaCompilationUnit]
69+
def Context_isScala2CompilationUnit(self: Context): Boolean = self.compilationUnit.isInstanceOf[fromtasty.Scala2CompilationUnit]
70+
def Context_compilationUnitClassname(self: Context): String =
6971
self.compilationUnit match {
70-
case j: fromtasty.JavaCompilationUnit => Some(j.className)
71-
case _ => None
72-
}
73-
def Context_nonTastyScalaCompilationUnitClassname(self: Context): Option[String] =
74-
self.compilationUnit match {
75-
case s: fromtasty.NonTastyScalaCompilationUnit => Some(s.className)
76-
case _ => None
72+
case cu: fromtasty.JavaCompilationUnit => cu.className
73+
case cu: fromtasty.Scala2CompilationUnit => cu.className
74+
case cu => ""
7775
}
7876

7977

library/src/scala/tasty/Reflection.scala

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -473,11 +473,14 @@ class Reflection(private[scala] val internal: CompilerInterface) { self =>
473473
/** Get method symbol if method is either defined in current compilation run or present on classpath. Throws if the method has an overload. */
474474
def requiredMethod(path: String): Symbol = internal.Context_requiredMethod(self)(path)
475475

476-
/** Get Java class name if we've accidentally tried to reflect on a Java class. None returned if TASTy class. */
477-
def javaCompilationUnitClassname(): Option[String] = internal.Context_javaCompilationUnitClassname(self)
476+
/** Returns true if we've tried to reflect on a Java class. */
477+
def isJavaCompilationUnit(): Boolean = internal Context_isJavaCompilationUnit(self)
478478

479-
/** Get Scala class name if attempted reflection is performed on an older Scala file w/o Tasty information present. */
480-
def nonTastyScalaCompilationUnitClassname(): Option[String] = internal.Context_nonTastyScalaCompilationUnitClassname(self)
479+
/** Returns true if we've tried to reflect on a Scala2 (non-Tasty) class. */
480+
def isScala2CompilationUnit(): Boolean = internal Context_isScala2CompilationUnit(self)
481+
482+
/** Class name of the current CompilationUnit */
483+
def compilationUnitClassname(): String = internal.Context_compilationUnitClassname(self)
481484
}
482485

483486

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -162,11 +162,14 @@ trait CompilerInterface {
162162
/** Get method symbol if method is either defined in current compilation run or present on classpath. Throws if the method has an overload. */
163163
def Context_requiredMethod(self: Context)(path: String): Symbol
164164

165-
/** Get Java class name if we've accidentally tried to reflect on a Java class. None returned if TASTy class. */
166-
def Context_javaCompilationUnitClassname(self: Context): Option[String]
165+
/** Returns true if we've tried to reflect on a Java class. */
166+
def Context_isJavaCompilationUnit(self: Context): Boolean
167167

168-
/** Get Scala class name if attempted reflection is performed on an older Scala file w/o Tasty information present. */
169-
def Context_nonTastyScalaCompilationUnitClassname(self: Context): Option[String]
168+
/** Returns true if we've tried to reflect on a Scala2 (non-Tasty) class. */
169+
def Context_isScala2CompilationUnit(self: Context): Boolean
170+
171+
/** Class name of the current CompilationUnit */
172+
def Context_compilationUnitClassname(self: Context): String
170173

171174

172175
///////////////

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

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,29 +9,34 @@ object Test {
99
// Tasty Scala Class
1010
val inspect1 = new TestInspector()
1111
inspect1.inspect("", List("I8215"))
12-
assert(inspect1.gotJava == None)
13-
assert(inspect1.gotNonTastyScala == None)
12+
assert(inspect1.isJava == false)
13+
assert(inspect1.isScala2 == false)
14+
assert(inspect1.className == "")
1415

1516
// Java Class
1617
val inspect2 = new TestInspector()
1718
inspect2.inspect("", List("java.util.UUID"))
18-
assert(inspect2.gotJava == Some("java.util.UUID"))
19-
assert(inspect2.gotNonTastyScala == None)
19+
assert(inspect2.isJava == true)
20+
assert(inspect2.isScala2 == false)
21+
assert(inspect2.className == "java.util.UUID")
2022

2123
// Legacy non-Tasty Scala class
2224
val inspect3 = new TestInspector()
2325
inspect3.inspect("", List("scala.collection.immutable.RedBlackTree"))
24-
assert(inspect3.gotJava == None)
25-
assert(inspect3.gotNonTastyScala == Some("scala.collection.immutable.RedBlackTree"))
26+
assert(inspect3.isJava == false)
27+
assert(inspect3.isScala2 == true)
28+
assert(inspect3.className == "scala.collection.immutable.RedBlackTree")
2629
}
2730
}
2831

2932
class TestInspector() extends TastyInspector
3033

31-
var gotJava: Option[String] = None
32-
var gotNonTastyScala: Option[String] = None
34+
var isJava: Boolean = false
35+
var isScala2: Boolean = false
36+
var className: String = ""
3337

3438
protected def processCompilationUnit(reflect: Reflection)(root: reflect.Tree): Unit =
3539
import reflect.{given,_}
36-
gotJava = reflect.rootContext.javaCompilationUnitClassname()
37-
gotNonTastyScala = reflect.rootContext.nonTastyScalaCompilationUnitClassname()
40+
isJava = reflect.rootContext.isJavaCompilationUnit()
41+
isScala2 = reflect.rootContext.isScala2CompilationUnit()
42+
className = reflect.rootContext.compilationUnitClassname()

0 commit comments

Comments
 (0)