Skip to content

Commit 06d9be6

Browse files
authored
Merge pull request scala#10940 from lrytz/subst-sym-refinement-owner
`substSym` replaces owners of `RefinementClassSymbol`s
2 parents b34f7a1 + 9121a9d commit 06d9be6

File tree

4 files changed

+58
-4
lines changed

4 files changed

+58
-4
lines changed

src/reflect/scala/reflect/internal/Types.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4037,13 +4037,13 @@ trait Types
40374037
private[this] val copyRefinedTypeSSM: ReusableInstance[SubstSymMap] =
40384038
ReusableInstance[SubstSymMap](SubstSymMap(), enabled = isCompilerUniverse)
40394039

4040-
def copyRefinedType(original: RefinedType, parents: List[Type], decls: Scope) =
4041-
if ((parents eq original.parents) && (decls eq original.decls)) original
4040+
def copyRefinedType(original: RefinedType, parents: List[Type], decls: Scope, owner: Symbol = null) =
4041+
if ((parents eq original.parents) && (decls eq original.decls) && (owner eq null)) original
40424042
else {
4043-
val owner = original.typeSymbol.owner
4043+
val newOwner = if (owner != null) owner else original.typeSymbol.owner
40444044
val result =
40454045
if (isIntersectionTypeForLazyBaseType(original)) intersectionTypeForLazyBaseType(parents)
4046-
else refinedType(parents, owner)
4046+
else refinedType(parents, newOwner)
40474047
if (! decls.isEmpty){
40484048
val syms1 = decls.toList
40494049
for (sym <- syms1)

src/reflect/scala/reflect/internal/tpe/TypeMaps.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -817,6 +817,10 @@ private[internal] trait TypeMaps {
817817
case SingleType(pre, sym) if pre ne NoPrefix =>
818818
val newSym = substFor(sym)
819819
(if (sym eq newSym) tpe else singleType(pre, newSym)).mapOver(this)
820+
case tp: RefinedType =>
821+
val owner = tpe.typeSymbol.owner
822+
val newOwner = substFor(owner)
823+
(if (newOwner eq owner) tpe else copyRefinedType(tp, tp.parents, tp.decls, newOwner)).mapOver(this)
820824
case _ =>
821825
super.apply(tpe)
822826
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
scala> :power
3+
Power mode enabled. :phase is at typer.
4+
import scala.tools.nsc._, intp.global._, definitions._
5+
Try :help or completions for vals._ and power._
6+
7+
scala> class C {
8+
def f = new {
9+
def g = new {
10+
def h = 1
11+
}
12+
}
13+
}
14+
class C
15+
16+
scala> val f = typeOf[C].decl(TermName("f"))
17+
val f: $r.intp.global.Symbol = method f
18+
19+
scala> val g = f.tpe.resultType.decls.head
20+
val g: $r.intp.global.Symbol = method g
21+
22+
scala> g.ownerChain.take(4)
23+
val res0: List[$r.intp.global.Symbol] = List(method g, <refinement of AnyRef>, method f, class C)
24+
25+
scala> g.tpe.resultType.typeSymbol
26+
val res1: $r.intp.global.Symbol = <refinement of AnyRef>
27+
28+
scala> g.tpe.resultType.typeSymbol.ownerChain.take(4)
29+
val res2: List[$r.intp.global.Symbol] = List(<refinement of AnyRef>, method g, <refinement of AnyRef>, method f)
30+
31+
scala> :quit
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import scala.tools.partest.ReplTest
2+
3+
object Test extends ReplTest {
4+
def code =
5+
""":power
6+
|class C {
7+
| def f = new {
8+
| def g = new {
9+
| def h = 1
10+
| }
11+
| }
12+
|}
13+
|val f = typeOf[C].decl(TermName("f"))
14+
|val g = f.tpe.resultType.decls.head
15+
|g.ownerChain.take(4)
16+
|g.tpe.resultType.typeSymbol
17+
|g.tpe.resultType.typeSymbol.ownerChain.take(4)
18+
|""".stripMargin
19+
}

0 commit comments

Comments
 (0)