Skip to content

Commit a43d8c1

Browse files
committed
Refine tpd.isInstance
This now special cases `x.isInstance[y.type]` to `x eq y` and similarly for constant types.
1 parent 4057fb2 commit a43d8c1

File tree

3 files changed

+16
-15
lines changed

3 files changed

+16
-15
lines changed

compiler/src/dotty/tools/dotc/ast/tpd.scala

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -757,9 +757,20 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo {
757757
def ensureApplied(implicit ctx: Context): Tree =
758758
if (tree.tpe.widen.isParameterless) tree else tree.appliedToNone
759759

760-
/** `tree.isInstanceOf[tp]` */
761-
def isInstance(tp: Type)(implicit ctx: Context): Tree =
762-
tree.select(defn.Any_isInstanceOf).appliedToType(tp)
760+
/** `tree == that` */
761+
def equal(that: Tree)(implicit ctx: Context) =
762+
applyOverloaded(tree, nme.EQ, that :: Nil, Nil, defn.BooleanType)
763+
764+
/** `tree.isInstanceOf[tp]`, with special treatment of singleton types */
765+
def isInstance(tp: Type)(implicit ctx: Context): Tree = tp match {
766+
case tp: SingletonType =>
767+
if (tp.widen.derivesFrom(defn.ObjectClass))
768+
tree.ensureConforms(defn.ObjectType).select(defn.Object_eq).appliedTo(singleton(tp))
769+
else
770+
singleton(tp).equal(tree)
771+
case _ =>
772+
tree.select(defn.Any_isInstanceOf).appliedToType(tp)
773+
}
763774

764775
/** tree.asInstanceOf[`tp`] */
765776
def asInstance(tp: Type)(implicit ctx: Context): Tree = {

compiler/src/dotty/tools/dotc/transform/PatMat.scala

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,6 @@ object PatMat {
3939
}
4040
}
4141

42-
case class BinderInfo(rhs: Tree)
43-
4442
val binding = mutable.Map[Symbol, AnyRef/*Tree | Node*/]()
4543
val nonNull = mutable.Set[Symbol]()
4644

@@ -54,7 +52,6 @@ object PatMat {
5452
binding(sym).asInstanceOf[Node]
5553
}
5654

57-
// assert(owner ne null); assert(owner ne NoSymbol)
5855
def freshSym(info: Type, pos: Position, owner: Symbol = ctx.owner): TermSymbol =
5956
ctx.newSymbol(owner, PatMatStdBinderName.fresh(), Synthetic | Case, info, coord = pos)
6057

@@ -122,12 +119,7 @@ object PatMat {
122119

123120
def condition = expectedTp.dealias match {
124121
case expectedTp: SingletonType =>
125-
val test =
126-
if (expectedTp.widen.derivesFrom(defn.ObjectClass))
127-
scrutinee.ensureConforms(defn.ObjectType).select(defn.Object_eq)
128-
else
129-
scrutinee.select(defn.Any_==)
130-
test.appliedTo(singleton(expectedTp))
122+
scrutinee.isInstance(expectedTp)
131123
case _ =>
132124
val typeTest = scrutinee.select(defn.Any_typeTest).appliedToType(tpt.tpe)
133125
if (outerTestNeeded) typeTest.and(outerTest) else typeTest

compiler/src/dotty/tools/dotc/transform/TypeTestsCasts.scala

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,7 @@ object TypeTestsCasts {
133133
*/
134134
def transformTypeTest(expr: Tree, testType: Type, flagUnrelated: Boolean): Tree = testType.dealias match {
135135
case _: SingletonType =>
136-
val cmpOp =
137-
if (testType derivesFrom defn.AnyValClass) defn.Any_equals else defn.Object_eq
138-
expr.select(cmpOp).appliedTo(singleton(testType))
136+
expr.isInstance(testType).withPos(tree.pos)
139137
case OrType(tp1, tp2) =>
140138
evalOnce(expr) { e =>
141139
transformTypeTest(e, tp1, flagUnrelated = false)

0 commit comments

Comments
 (0)