Skip to content

Commit 9a9436a

Browse files
committed
CrossCastAnd: reimplement it in more compact way.
Note that there is a slight difference in behaviour: it will be unconditionally adding cast even if its not needed. For example in callsFoo1 cast is not needed(erasure will insert a correct one). ``` class A{ private def foo = 1 def callsFoo1(other: A & B): Int = other.foo def callsFoo2(other: B & A): Int = other.foo } trait B { def foo(i: Int) = i } ``` Though as AndTypes are uncommon I do not expect this to have non-negligible impact on performance.
1 parent 49e03c6 commit 9a9436a

File tree

1 file changed

+5
-23
lines changed

1 file changed

+5
-23
lines changed

src/dotty/tools/dotc/transform/CrossCastAnd.scala

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -20,29 +20,11 @@ class CrossCastAnd extends MiniPhaseTransform { thisTransform =>
2020
override def phaseName: String = "crossCast"
2121

2222
override def transformSelect(tree: tpd.Select)(implicit ctx: Context, info: TransformerInfo): tpd.Tree = {
23-
if (tree.symbol.is(Flags.Private)) {
24-
// all private member selections have a symbol
25-
tree.qualifier.tpe.widen match {
26-
case t @ AndType(l, _) =>
2723

28-
// find a component of and type that owns the symbol
29-
def findType(tp: Type): Type = {
30-
tp match {
31-
case AndType(l, r) =>
32-
findType(l).orElse(findType(r))
33-
case t =>
34-
if (t.decl(tree.symbol.name).suchThat(_ == tree.symbol).exists)
35-
t
36-
else NoType
37-
}
38-
}
39-
40-
val tp = findType(t)
41-
if (l eq tp) tree
42-
else tree.qualifier.asInstance(AndType(tp, tree.qualifier.tpe)).select(tree.symbol)
43-
case _ => tree
44-
}
45-
}
46-
else tree
24+
lazy val qtype = tree.qualifier.tpe.widen
25+
val sym = tree.symbol
26+
if (sym.is(Flags.Private) && qtype.typeSymbol != sym.owner)
27+
cpy.Select(tree)(tree.qualifier.asInstance(AndType(qtype.baseTypeWithArgs(sym.owner), tree.qualifier.tpe)), tree.name)
28+
else tree
4729
}
4830
}

0 commit comments

Comments
 (0)