Skip to content

Commit

Permalink
Merge pull request scala#10940 from lrytz/subst-sym-refinement-owner
Browse files Browse the repository at this point in the history
`substSym` replaces owners of `RefinementClassSymbol`s
  • Loading branch information
lrytz authored Dec 3, 2024
2 parents b34f7a1 + 9121a9d commit 06d9be6
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/reflect/scala/reflect/internal/Types.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4037,13 +4037,13 @@ trait Types
private[this] val copyRefinedTypeSSM: ReusableInstance[SubstSymMap] =
ReusableInstance[SubstSymMap](SubstSymMap(), enabled = isCompilerUniverse)

def copyRefinedType(original: RefinedType, parents: List[Type], decls: Scope) =
if ((parents eq original.parents) && (decls eq original.decls)) original
def copyRefinedType(original: RefinedType, parents: List[Type], decls: Scope, owner: Symbol = null) =
if ((parents eq original.parents) && (decls eq original.decls) && (owner eq null)) original
else {
val owner = original.typeSymbol.owner
val newOwner = if (owner != null) owner else original.typeSymbol.owner
val result =
if (isIntersectionTypeForLazyBaseType(original)) intersectionTypeForLazyBaseType(parents)
else refinedType(parents, owner)
else refinedType(parents, newOwner)
if (! decls.isEmpty){
val syms1 = decls.toList
for (sym <- syms1)
Expand Down
4 changes: 4 additions & 0 deletions src/reflect/scala/reflect/internal/tpe/TypeMaps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -817,6 +817,10 @@ private[internal] trait TypeMaps {
case SingleType(pre, sym) if pre ne NoPrefix =>
val newSym = substFor(sym)
(if (sym eq newSym) tpe else singleType(pre, newSym)).mapOver(this)
case tp: RefinedType =>
val owner = tpe.typeSymbol.owner
val newOwner = substFor(owner)
(if (newOwner eq owner) tpe else copyRefinedType(tp, tp.parents, tp.decls, newOwner)).mapOver(this)
case _ =>
super.apply(tpe)
}
Expand Down
31 changes: 31 additions & 0 deletions test/files/run/substSymRefinementOwner.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

scala> :power
Power mode enabled. :phase is at typer.
import scala.tools.nsc._, intp.global._, definitions._
Try :help or completions for vals._ and power._

scala> class C {
def f = new {
def g = new {
def h = 1
}
}
}
class C

scala> val f = typeOf[C].decl(TermName("f"))
val f: $r.intp.global.Symbol = method f

scala> val g = f.tpe.resultType.decls.head
val g: $r.intp.global.Symbol = method g

scala> g.ownerChain.take(4)
val res0: List[$r.intp.global.Symbol] = List(method g, <refinement of AnyRef>, method f, class C)

scala> g.tpe.resultType.typeSymbol
val res1: $r.intp.global.Symbol = <refinement of AnyRef>

scala> g.tpe.resultType.typeSymbol.ownerChain.take(4)
val res2: List[$r.intp.global.Symbol] = List(<refinement of AnyRef>, method g, <refinement of AnyRef>, method f)

scala> :quit
19 changes: 19 additions & 0 deletions test/files/run/substSymRefinementOwner.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import scala.tools.partest.ReplTest

object Test extends ReplTest {
def code =
""":power
|class C {
| def f = new {
| def g = new {
| def h = 1
| }
| }
|}
|val f = typeOf[C].decl(TermName("f"))
|val g = f.tpe.resultType.decls.head
|g.ownerChain.take(4)
|g.tpe.resultType.typeSymbol
|g.tpe.resultType.typeSymbol.ownerChain.take(4)
|""".stripMargin
}

0 comments on commit 06d9be6

Please sign in to comment.