@@ -164,19 +164,20 @@ object TypeTestsCasts {
164
164
else tp.classSymbol
165
165
166
166
def foundCls = effectiveClass(expr.tpe.widen)
167
- // println(i"ta $tree, found = $foundCls")
168
167
169
168
def inMatch =
170
169
fun.symbol == defn.Any_typeTest || // new scheme
171
170
expr.symbol.is(Case ) // old scheme
172
171
173
- def transformIsInstanceOf (expr: Tree , testType : Type , flagUnrelated : Boolean ): Tree = {
172
+ def transformIsInstanceOf (expr : Tree , testType : Type , flagUnrelated : Boolean ): Tree = {
174
173
def testCls = effectiveClass(testType.widen)
175
174
176
- def unreachable (why : => String ) =
175
+ def unreachable (why : => String ): Boolean = {
177
176
if (flagUnrelated)
178
177
if (inMatch) ctx.error(em " this case is unreachable since $why" , expr.sourcePos)
179
178
else ctx.warning(em " this will always yield false since $why" , expr.sourcePos)
179
+ false
180
+ }
180
181
181
182
/** Are `foundCls` and `testCls` classes that allow checks
182
183
* whether a test would be always false?
@@ -191,25 +192,22 @@ object TypeTestsCasts {
191
192
// we don't have the logic to handle derived value classes
192
193
193
194
/** 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 .
195
196
*/
196
197
def checkSensical : Boolean =
197
198
if (! isCheckable) true
198
199
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
+ }
202
203
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 ))
204
208
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)
210
210
unreachable(i " $foundCls and $testCls are unrelated " )
211
- false
212
- }
213
211
else true
214
212
}
215
213
else true
0 commit comments