Skip to content

Commit 22ee015

Browse files
committed
Fix #9977: Refine unrelated types criterion
Need to take account of boxing.
1 parent b22331e commit 22ee015

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

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

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
package dotty.tools.dotc
1+
package dotty.tools
2+
package dotc
23
package transform
34

45
import core._
@@ -193,8 +194,11 @@ object TypeTestsCasts {
193194
tree.fun.symbol == defn.Any_typeTest || // new scheme
194195
expr.symbol.is(Case) // old scheme
195196

196-
def transformIsInstanceOf(expr: Tree, testType: Type, flagUnrelated: Boolean): Tree = {
197+
def transformIsInstanceOf(
198+
expr: Tree, testType: Type,
199+
unboxedTestType: Type, flagUnrelated: Boolean): Tree = {
197200
def testCls = effectiveClass(testType.widen)
201+
def unboxedTestCls = effectiveClass(unboxedTestType.widen)
198202

199203
def unreachable(why: => String)(using Context): Boolean = {
200204
if (flagUnrelated)
@@ -226,9 +230,10 @@ object TypeTestsCasts {
226230
def check(foundCls: Symbol): Boolean =
227231
if (!isCheckable(foundCls)) true
228232
else if (!foundCls.derivesFrom(testCls)) {
229-
val unrelated = !testCls.derivesFrom(foundCls) && (
230-
testCls.is(Final) || !testCls.is(Trait) && !foundCls.is(Trait)
231-
)
233+
val unrelated =
234+
!testCls.derivesFrom(foundCls)
235+
&& !unboxedTestCls.derivesFrom(foundCls)
236+
&& (testCls.is(Final) || !testCls.is(Trait) && !foundCls.is(Trait))
232237
if (foundCls.is(Final))
233238
unreachable(i"$exprType is not a subclass of $testCls")
234239
else if (unrelated)
@@ -265,7 +270,8 @@ object TypeTestsCasts {
265270
case List(cls) if cls.isPrimitiveValueClass =>
266271
constant(expr, Literal(Constant(foundClsSyms.head == testCls)))
267272
case _ =>
268-
transformIsInstanceOf(expr, defn.boxedType(testCls.typeRef), flagUnrelated)
273+
transformIsInstanceOf(
274+
expr, defn.boxedType(testCls.typeRef), testCls.typeRef, flagUnrelated)
269275
else
270276
derivedTree(expr, defn.Any_isInstanceOf, testType)
271277
}
@@ -342,7 +348,8 @@ object TypeTestsCasts {
342348
case AppliedType(tref: TypeRef, _) if tref.symbol == defn.PairClass =>
343349
ref(defn.RuntimeTuple_isInstanceOfNonEmptyTuple).appliedTo(expr)
344350
case _ =>
345-
transformIsInstanceOf(expr, erasure(testType), flagUnrelated)
351+
val erasedTestType = erasure(testType)
352+
transformIsInstanceOf(expr, erasedTestType, erasedTestType, flagUnrelated)
346353
}
347354

348355
if (sym.isTypeTest) {

tests/pos/i9977.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
val pf1: PartialFunction[AnyVal, Int] = { case n: Int => n }

0 commit comments

Comments
 (0)