Skip to content

Commit 22cff0d

Browse files
committed
Fixed flags bug in ReadTasty
1 parent e870c41 commit 22cff0d

File tree

7 files changed

+49
-12
lines changed

7 files changed

+49
-12
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@ import dotty.tools.dotc.util.NoSource
99
* but we want to return the fact we found it so that higher-up we can take appropriate action if desired.
1010
*/
1111
class JavaCompilationUnit(val className: String) extends CompilationUnit(NoSource) {
12-
override def toString: String = s"java class file $className"
12+
override def toString: String = s"Java class file $className"
1313
}
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+
/**
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.
10+
*/
11+
class NonTastyScalaCompilationUnit(val className: String) extends CompilationUnit(NoSource) {
12+
override def toString: String = s"Non-Tasty Scala class file $className"
13+
}

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,11 @@ class ReadTasty extends Phase {
4848
Some(unit)
4949
}
5050
case tree: Tree[?] =>
51-
if( cls.flags.is(Flags.JavaDefined) ) // Is this a Java class we've tried to inspect?
52-
Some(JavaCompilationUnit(cls.fullName.toString))
53-
else
54-
alreadyLoaded()
51+
cls.denot.infoOrCompleter match {
52+
case _: NoLoader => Some(NonTastyScalaCompilationUnit(cls.fullName.toString))
53+
case _ if cls.flags.is(Flags.JavaDefined) => Some(JavaCompilationUnit(cls.fullName.toString))
54+
case _ => alreadyLoaded()
55+
}
5556
case _ =>
5657
cannotUnpickle(s"its class file does not have a TASTY attribute")
5758
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@ class ReflectionCompilerInterface(val rootContext: core.Contexts.Context) extend
7070
case j: fromtasty.JavaCompilationUnit => Some(j.className)
7171
case _ => None
7272
}
73+
def Context_nonTastyScalaCompilationUnitClassname(self: Context): Option[String] =
74+
self.compilationUnit match {
75+
case s: fromtasty.NonTastyScalaCompilationUnit => Some(s.className)
76+
case _ => None
77+
}
7378

7479

7580
///////////////

library/src/scala/tasty/Reflection.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,8 @@ class Reflection(private[scala] val internal: CompilerInterface) { self =>
476476
/** Get Java class name if we've accidentally tried to reflect on a Java class. None returned if TASTy class. */
477477
def javaCompilationUnitClassname(): Option[String] = internal.Context_javaCompilationUnitClassname(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)
479481
}
480482

481483

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,9 @@ trait CompilerInterface {
165165
/** Get Java class name if we've accidentally tried to reflect on a Java class. None returned if TASTy class. */
166166
def Context_javaCompilationUnitClassname(self: Context): Option[String]
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]
170+
168171

169172
///////////////
170173
// REPORTING //

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

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,33 @@ case class I8215(id: String)
55

66
object Test {
77
def main(args: Array[String]): Unit = {
8-
val inspect1 = new TestInspector()
9-
inspect1.inspect("", List("I8215"))
10-
assert(inspect1.gotJava == None)
118

12-
val inspect2 = new TestInspector()
13-
inspect2.inspect("", List("java.util.UUID"))
14-
assert(inspect2.gotJava == Some("java.util.UUID"))
9+
// Tasty Scala Class
10+
val inspect1 = new TestInspector()
11+
inspect1.inspect("", List("I8215"))
12+
assert(inspect1.gotJava == None)
13+
assert(inspect1.gotNonTastyScala == None)
14+
15+
// Java Class
16+
val inspect2 = new TestInspector()
17+
inspect2.inspect("", List("java.util.UUID"))
18+
assert(inspect2.gotJava == Some("java.util.UUID"))
19+
assert(inspect2.gotNonTastyScala == None)
20+
21+
// Legacy non-Tasty Scala class
22+
val inspect3 = new TestInspector()
23+
inspect3.inspect("", List("scala.collection.immutable.RedBlackTree"))
24+
assert(inspect3.gotJava == None)
25+
assert(inspect3.gotNonTastyScala == Some("scala.collection.immutable.RedBlackTree"))
1526
}
1627
}
1728

1829
class TestInspector() extends TastyInspector
1930

2031
var gotJava: Option[String] = None
32+
var gotNonTastyScala: Option[String] = None
2133

2234
protected def processCompilationUnit(reflect: Reflection)(root: reflect.Tree): Unit =
2335
import reflect.{given,_}
24-
gotJava = reflect.rootContext.javaCompilationUnitClassname()
36+
gotJava = reflect.rootContext.javaCompilationUnitClassname()
37+
gotNonTastyScala = reflect.rootContext.nonTastyScalaCompilationUnitClassname()

0 commit comments

Comments
 (0)