Skip to content

Commit 525d062

Browse files
committed
Simplify InterceptedMethods. Fix #439
1 parent 52d6ac3 commit 525d062

File tree

1 file changed

+28
-41
lines changed

1 file changed

+28
-41
lines changed

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

Lines changed: 28 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -40,30 +40,24 @@ import StdNames._
4040
* using the most precise overload available
4141
* - `x.getClass` for getClass in primitives becomes `x.getClass` with getClass in class Object.
4242
*/
43-
class InterceptedMethods extends MiniPhaseTransform { thisTransform =>
43+
class InterceptedMethods extends MiniPhaseTransform {
44+
thisTransform =>
4445

4546
import tpd._
4647

4748
override def phaseName: String = "intercepted"
4849

49-
private var getClassMethods: Set[Symbol] = _
50-
private var poundPoundMethods: Set[Symbol] = _
51-
private var Any_comparisons: Set[Symbol] = _
52-
private var interceptedMethods: Set[Symbol] = _
5350
private var primitiveGetClassMethods: Set[Symbol] = _
5451

5552
/** perform context-dependant initialization */
5653
override def prepareForUnit(tree: Tree)(implicit ctx: Context) = {
57-
poundPoundMethods = Set(defn.Any_##)
58-
Any_comparisons = Set(defn.Any_==, defn.Any_!=)
59-
interceptedMethods = poundPoundMethods ++ Any_comparisons
6054
primitiveGetClassMethods = Set[Symbol]() ++ defn.ScalaValueClasses.map(x => x.requiredMethod(nme.getClass_))
6155
this
6256
}
6357

6458
// this should be removed if we have guarantee that ## will get Apply node
6559
override def transformSelect(tree: tpd.Select)(implicit ctx: Context, info: TransformerInfo): Tree = {
66-
if (tree.symbol.isTerm && poundPoundMethods.contains(tree.symbol.asTerm)) {
60+
if (tree.symbol.isTerm && (Any_## eq tree.symbol.asTerm)) {
6761
val rewrite = poundPoundValue(tree.qualifier)
6862
ctx.log(s"$phaseName rewrote $tree to $rewrite")
6963
rewrite
@@ -103,43 +97,36 @@ class InterceptedMethods extends MiniPhaseTransform { thisTransform =>
10397
s"that means the intercepted methods set doesn't match the code")
10498
tree
10599
}
106-
if (tree.fun.symbol.isTerm &&
107-
(interceptedMethods contains tree.fun.symbol.asTerm)) {
108-
val rewrite: Tree = tree.fun match {
109-
case Select(qual, name) =>
110-
if (poundPoundMethods contains tree.fun.symbol.asTerm) {
111-
poundPoundValue(qual)
112-
} else if (Any_comparisons contains tree.fun.symbol.asTerm) {
113-
if (tree.fun.symbol eq defn.Any_==) {
114-
qual.selectWithSig(defn.Any_equals).appliedToArgs(tree.args)
115-
} else if (tree.fun.symbol eq defn.Any_!=) {
116-
qual.selectWithSig(defn.Any_equals).appliedToArgs(tree.args).select(defn.Boolean_!)
117-
} else unknown
118-
} /* else if (isPrimitiveValueClass(qual.tpe.typeSymbol)) {
100+
lazy val Select(qual, _) = tree.fun
101+
val Any_## = defn.Any_##
102+
val Any_!= = defn.Any_!=
103+
val rewrite: Tree = tree.fun.symbol match {
104+
case Any_## =>
105+
poundPoundValue(qual)
106+
case Any_!= =>
107+
qual.select(defn.Any_==).appliedToArgs(tree.args).select(defn.Boolean_!)
108+
/*
109+
/* else if (isPrimitiveValueClass(qual.tpe.typeSymbol)) {
119110
// todo: this is needed to support value classes
120111
// Rewrite 5.getClass to ScalaRunTime.anyValClass(5)
121112
global.typer.typed(gen.mkRuntimeCall(nme.anyValClass,
122113
List(qual, typer.resolveClassTag(tree.pos, qual.tpe.widen))))
123114
}*/
124-
else if (primitiveGetClassMethods.contains(tree.fun.symbol)) {
125-
// if we got here then we're trying to send a primitive getClass method to either
126-
// a) an Any, in which cage Object_getClass works because Any erases to object. Or
127-
//
128-
// b) a non-primitive, e.g. because the qualifier's type is a refinement type where one parent
129-
// of the refinement is a primitive and another is AnyRef. In that case
130-
// we get a primitive form of _getClass trying to target a boxed value
131-
// so we need replace that method name with Object_getClass to get correct behavior.
132-
// See SI-5568.
133-
qual.selectWithSig(defn.Any_getClass).appliedToNone
134-
} else {
135-
unknown
136-
}
137-
case _ =>
138-
unknown
139-
}
140-
ctx.log(s"$phaseName rewrote $tree to $rewrite")
141-
rewrite
115+
*/
116+
case t if primitiveGetClassMethods.contains(t) =>
117+
// if we got here then we're trying to send a primitive getClass method to either
118+
// a) an Any, in which cage Object_getClass works because Any erases to object. Or
119+
//
120+
// b) a non-primitive, e.g. because the qualifier's type is a refinement type where one parent
121+
// of the refinement is a primitive and another is AnyRef. In that case
122+
// we get a primitive form of _getClass trying to target a boxed value
123+
// so we need replace that method name with Object_getClass to get correct behavior.
124+
// See SI-5568.
125+
qual.selectWithSig(defn.Any_getClass).appliedToNone
126+
case _ =>
127+
tree
142128
}
143-
else tree
129+
ctx.log(s"$phaseName rewrote $tree to $rewrite")
130+
rewrite
144131
}
145132
}

0 commit comments

Comments
 (0)