Skip to content

Fix #9977: Refine unrelated types criterion #9980

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions compiler/src/dotty/tools/dotc/transform/TypeTestsCasts.scala
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
package dotty.tools.dotc
package dotty.tools
package dotc
package transform

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

def transformIsInstanceOf(expr: Tree, testType: Type, flagUnrelated: Boolean): Tree = {
def transformIsInstanceOf(
expr: Tree, testType: Type,
unboxedTestType: Type, flagUnrelated: Boolean): Tree = {
def testCls = effectiveClass(testType.widen)
def unboxedTestCls = effectiveClass(unboxedTestType.widen)

def unreachable(why: => String)(using Context): Boolean = {
if (flagUnrelated)
Expand Down Expand Up @@ -226,9 +230,10 @@ object TypeTestsCasts {
def check(foundCls: Symbol): Boolean =
if (!isCheckable(foundCls)) true
else if (!foundCls.derivesFrom(testCls)) {
val unrelated = !testCls.derivesFrom(foundCls) && (
testCls.is(Final) || !testCls.is(Trait) && !foundCls.is(Trait)
)
val unrelated =
!testCls.derivesFrom(foundCls)
&& !unboxedTestCls.derivesFrom(foundCls)
&& (testCls.is(Final) || !testCls.is(Trait) && !foundCls.is(Trait))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line seems to change semantics of the original code:

Suggested change
&& (testCls.is(Final) || !testCls.is(Trait) && !foundCls.is(Trait))
&& ((testCls.is(Final) || !testCls.is(Trait) && !foundCls.is(Trait)))

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think so. All the suggestion does is double the pair of outer parens

if (foundCls.is(Final))
unreachable(i"$exprType is not a subclass of $testCls")
else if (unrelated)
Expand Down Expand Up @@ -265,7 +270,8 @@ object TypeTestsCasts {
case List(cls) if cls.isPrimitiveValueClass =>
constant(expr, Literal(Constant(foundClsSyms.head == testCls)))
case _ =>
transformIsInstanceOf(expr, defn.boxedType(testCls.typeRef), flagUnrelated)
transformIsInstanceOf(
expr, defn.boxedType(testCls.typeRef), testCls.typeRef, flagUnrelated)
else
derivedTree(expr, defn.Any_isInstanceOf, testType)
}
Expand Down Expand Up @@ -342,7 +348,8 @@ object TypeTestsCasts {
case AppliedType(tref: TypeRef, _) if tref.symbol == defn.PairClass =>
ref(defn.RuntimeTuple_isInstanceOfNonEmptyTuple).appliedTo(expr)
case _ =>
transformIsInstanceOf(expr, erasure(testType), flagUnrelated)
val erasedTestType = erasure(testType)
transformIsInstanceOf(expr, erasedTestType, erasedTestType, flagUnrelated)
}

if (sym.isTypeTest) {
Expand Down
1 change: 1 addition & 0 deletions tests/pos/i9977.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
val pf1: PartialFunction[AnyVal, Int] = { case n: Int => n }