Skip to content

Commit c1b7e84

Browse files
Minor TypeTestsCasts refactoring
1 parent df8f79f commit c1b7e84

File tree

1 file changed

+13
-15
lines changed

1 file changed

+13
-15
lines changed

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

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -164,19 +164,20 @@ object TypeTestsCasts {
164164
else tp.classSymbol
165165

166166
def foundCls = effectiveClass(expr.tpe.widen)
167-
// println(i"ta $tree, found = $foundCls")
168167

169168
def inMatch =
170169
fun.symbol == defn.Any_typeTest || // new scheme
171170
expr.symbol.is(Case) // old scheme
172171

173-
def transformIsInstanceOf(expr:Tree, testType: Type, flagUnrelated: Boolean): Tree = {
172+
def transformIsInstanceOf(expr: Tree, testType: Type, flagUnrelated: Boolean): Tree = {
174173
def testCls = effectiveClass(testType.widen)
175174

176-
def unreachable(why: => String) =
175+
def unreachable(why: => String): Boolean = {
177176
if (flagUnrelated)
178177
if (inMatch) ctx.error(em"this case is unreachable since $why", expr.sourcePos)
179178
else ctx.warning(em"this will always yield false since $why", expr.sourcePos)
179+
false
180+
}
180181

181182
/** Are `foundCls` and `testCls` classes that allow checks
182183
* whether a test would be always false?
@@ -191,25 +192,22 @@ object TypeTestsCasts {
191192
// we don't have the logic to handle derived value classes
192193

193194
/** Check whether a runtime test that a value of `foundCls` can be a `testCls`
194-
* can be true in some cases. Issure a warning or an error if that's not the case.
195+
* can be true in some cases. Issues a warning or an error otherwise.
195196
*/
196197
def checkSensical: Boolean =
197198
if (!isCheckable) true
198199
else if (foundCls.isPrimitiveValueClass && !testCls.isPrimitiveValueClass) {
199-
ctx.error("cannot test if value types are references", tree.sourcePos)
200-
false
201-
}
200+
ctx.error("cannot test if value types are references", tree.sourcePos)
201+
false
202+
}
202203
else if (!foundCls.derivesFrom(testCls)) {
203-
if (foundCls.is(Final)) {
204+
val unrelated = !testCls.derivesFrom(foundCls) && (
205+
testCls.is(Final) || !testCls.is(Trait) && !foundCls.is(Trait)
206+
)
207+
if (foundCls.is(Final))
204208
unreachable(i"$foundCls is not a subclass of $testCls")
205-
false
206-
}
207-
else if (!testCls.derivesFrom(foundCls) &&
208-
(testCls.is(Final) ||
209-
!testCls.is(Trait) && !foundCls.is(Trait))) {
209+
else if (unrelated)
210210
unreachable(i"$foundCls and $testCls are unrelated")
211-
false
212-
}
213211
else true
214212
}
215213
else true

0 commit comments

Comments
 (0)