Skip to content

Commit 025cfa5

Browse files
authored
Merge pull request #2528 from abeln/java-packages
Fix #2456: eliminate syntactic references to Java packages
2 parents ca5b489 + 434a7ee commit 025cfa5

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

compiler/src/dotty/tools/dotc/Compiler.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ class Compiler {
4848
List(new sbt.ExtractAPI), // Sends a representation of the API of classes to sbt via callbacks
4949
List(new Pickler), // Generate TASTY info
5050
List(new FirstTransform, // Some transformations to put trees into a canonical form
51-
new CheckReentrant), // Internal use only: Check that compiled program has no data races involving global vars
51+
new CheckReentrant, // Internal use only: Check that compiled program has no data races involving global vars
52+
new ElimJavaPackages), // Eliminate syntactic references to Java packages
5253
List(new CheckStatic, // Check restrictions that apply to @static members
5354
new CheckPhantomCast, // Checks that no Phantom types in are in casts
5455
new ElimRepeated, // Rewrite vararg parameters and arguments
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package dotty.tools.dotc.transform
2+
3+
import dotty.tools.dotc.ast.tpd._
4+
import dotty.tools.dotc.core.Contexts.Context
5+
import dotty.tools.dotc.core.Flags._
6+
import dotty.tools.dotc.core.Types.{Type, TypeRef}
7+
import dotty.tools.dotc.transform.TreeTransforms.{MiniPhaseTransform, TransformerInfo}
8+
9+
/**
10+
* Eliminates syntactic references to Java packages, so that there's no chance
11+
* they accidentally end up in the backend.
12+
*/
13+
class ElimJavaPackages extends MiniPhaseTransform {
14+
15+
override def phaseName: String = "elimJavaPackages"
16+
17+
override def transformSelect(tree: Select)(implicit ctx: Context, info: TransformerInfo): Tree = {
18+
if (isJavaPackage(tree)) {
19+
assert(tree.tpe.isInstanceOf[TypeRef], s"Expected tree with type TypeRef, but got ${tree.tpe.show}")
20+
Ident(tree.tpe.asInstanceOf[TypeRef])
21+
} else {
22+
tree
23+
}
24+
}
25+
26+
override def checkPostCondition(tree: Tree)(implicit ctx: Context): Unit = {
27+
tree match {
28+
case tree: Select =>
29+
assert(!isJavaPackage(tree), s"Unexpected reference to Java package in ${tree.show}")
30+
case _ => ()
31+
}
32+
}
33+
34+
/**
35+
* Is the given tree a syntactic reference to a Java package?
36+
*/
37+
private def isJavaPackage(tree: Select)(implicit ctx: Context): Boolean = {
38+
tree.tpe match {
39+
case TypeRef(prefix, _) =>
40+
val flags = prefix.termSymbol.flags
41+
// Testing for each flag separately is more efficient than using FlagConjunction.
42+
flags.is(Package) && flags.is(JavaDefined)
43+
case _ => false
44+
}
45+
}
46+
}

0 commit comments

Comments
 (0)