<commit_before>package dotty.tools.dotc
package transform

import core._
import Contexts.Context
import Types._
import TreeTransforms._
import Decorators._
import ast.Trees._
import Flags._

/** Transform references of the form
 *
 *     C.this.m
 *
 *  where `C` is a class with explicit self type and `C` is not a
 *  subclass of the owner of `m` to
 *
 *     C.this.asInstanceOf[S].m
 *
 *  where `S` is the self type of `C`.
 */
class ExplicitSelf extends MiniPhaseTransform { thisTransform =>
  import ast.tpd._

  override def phaseName = "explicitSelf"

  override def transformSelect(tree: Select)(implicit ctx: Context, info: TransformerInfo): Tree = tree match {
    case Select(thiz: This, name) if name.isTermName =>
      val cls = thiz.symbol.asClass
      val cinfo = cls.classInfo
      if (cinfo.givenSelfType.exists && !cls.derivesFrom(tree.symbol.owner))
        cpy.Select(tree)(thiz.asInstance(cinfo.selfType), name)
      else tree
    case _ => tree
  }
}
<commit_msg>Fix ExplicitSelf phase<commit_after>package dotty.tools.dotc
package transform

import core._
import Contexts.Context
import Types._
import TreeTransforms._
import Decorators._
import ast.Trees._
import Flags._

/** Transform references of the form
 *
 *     C.this.m
 *
 *  where `C` is a class with explicit self type and `C` is not a
 *  subclass of the owner of `m` to
 *
 *     C.this.asInstanceOf[S & C.this.type].m
 *
 *  where `S` is the self type of `C`.
 *  See run/i789.scala for a test case why this is needed.
 */
class ExplicitSelf extends MiniPhaseTransform { thisTransform =>
  import ast.tpd._

  override def phaseName = "explicitSelf"

  override def transformSelect(tree: Select)(implicit ctx: Context, info: TransformerInfo): Tree = tree match {
    case Select(thiz: This, name) if name.isTermName =>
      val cls = thiz.symbol.asClass
      val cinfo = cls.classInfo
      if (cinfo.givenSelfType.exists && !cls.derivesFrom(tree.symbol.owner))
        cpy.Select(tree)(thiz.asInstance(AndType(cinfo.selfType, thiz.tpe)), name)
      else tree
    case _ => tree
  }
}

{'fraction_non_alphanumeric': 0.10321651464234277, 'fraction_numerical': 0.0014402304368698992, 'mean_word_length': 3.368972746331237, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, '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': '6809372', 'n_tokens_mistral': 678, 'n_tokens_neox': 669, 'n_words': 208}