<commit_before>package dotty.tools.dotc.transform

import dotty.tools.dotc.ast.tpd._
import dotty.tools.dotc.core.Contexts.Context
import dotty.tools.dotc.core.Flags._
import dotty.tools.dotc.core.Types.{Type, TypeRef}
import dotty.tools.dotc.transform.MegaPhase.MiniPhase

/**
  * Eliminates syntactic references to Java packages, so that there's no chance
  * they accidentally end up in the backend.
  */
class ElimJavaPackages extends MiniPhase {

  override def phaseName: String = "elimJavaPackages"

  override def transformSelect(tree: Select)(implicit ctx: Context): Tree = {
    if (isJavaPackage(tree)) {
      assert(tree.tpe.isInstanceOf[TypeRef], s"Expected tree with type TypeRef, but got ${tree.tpe.show}")
      Ident(tree.tpe.asInstanceOf[TypeRef])
    } else {
      tree
    }
  }

  override def checkPostCondition(tree: Tree)(implicit ctx: Context): Unit = {
    tree match {
      case tree: Select =>
        assert(!isJavaPackage(tree), s"Unexpected reference to Java package in ${tree.show}")
      case _ => ()
    }
  }

  /**
    * Is the given tree a syntactic reference to a Java package?
    */
  private def isJavaPackage(tree: Select)(implicit ctx: Context): Boolean = {
    tree.tpe match {
      case TypeRef(prefix, _) =>
        val flags = prefix.termSymbol.flags
        // Testing for each flag separately is more efficient than using FlagConjunction.
        flags.is(Package) && flags.is(JavaDefined)
      case _ => false
    }
  }
}
<commit_msg>Cleanup of ElimJavaPackages<commit_after>package dotty.tools.dotc
package transform

import core._
import Decorators._, Flags._, Types._, Contexts._, Symbols._
import ast.tpd._
import Flags._
import MegaPhase.MiniPhase

/** Eliminates syntactic references to package terms as prefixes of classes, so that there's no chance
 *  they accidentally end up in the backend.
 */
class ElimPackagePrefixes extends MiniPhase {

  override def phaseName: String = "elimPackagePrefixes"

  override def transformSelect(tree: Select)(implicit ctx: Context): Tree =
    if (isPackageClassRef(tree)) Ident(tree.tpe.asInstanceOf[TypeRef]) else tree

  override def checkPostCondition(tree: Tree)(implicit ctx: Context): Unit = tree match {
    case tree: Select =>
      assert(!isPackageClassRef(tree), i"Unexpected reference to package in $tree")
    case _ =>
  }

  /** Is the given tree a reference to a type in a package? */
  private def isPackageClassRef(tree: Select)(implicit ctx: Context): Boolean = tree.tpe match {
    case TypeRef(prefix, _) => prefix.termSymbol.is(Package)
    case _ => false
  }
}

{'fraction_non_alphanumeric': 0.10231660231660232, 'fraction_numerical': 0.0, 'mean_word_length': 3.7194899817850637, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6201326', 'n_tokens_mistral': 783, 'n_tokens_neox': 771, 'n_words': 231}