Skip to content

Commit 725bf5e

Browse files
committed
Revert previous changes and simplify Nothing | T to T and Null | T to if T is nullable
1 parent 31428b2 commit 725bf5e

File tree

3 files changed

+7
-31
lines changed

3 files changed

+7
-31
lines changed

compiler/src/dotty/tools/dotc/core/Types.scala

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -370,32 +370,6 @@ object Types extends TypeUtils {
370370
loop(this)
371371
}
372372

373-
/* Is this type exactly Nothing or is an AndOrType with a type that is exactly Nothing? */
374-
def hasNothing(using Context): Boolean = {
375-
def loop(tp: Type): Boolean = tp match {
376-
case tp: TypeRef =>
377-
tp.isExactlyNothing
378-
case tp: AndOrType =>
379-
loop(tp.tp1) || loop(tp.tp2)
380-
case _ =>
381-
false
382-
}
383-
loop(this)
384-
}
385-
386-
/* Is this type exactly Null or is an AndOrType with a type that is exactly Null? */
387-
def hasNull(using Context): Boolean = {
388-
def loop(tp: Type): Boolean = tp match {
389-
case tp: TypeRef =>
390-
tp.isExactlyNull
391-
case tp: AndOrType =>
392-
loop(tp.tp1) || loop(tp.tp2)
393-
case _ =>
394-
false
395-
}
396-
loop(this)
397-
}
398-
399373
/** Is this type guaranteed not to have `null` as a value? */
400374
final def isNotNull(using Context): Boolean = this match {
401375
case tp: ConstantType => tp.value.value != null

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -418,8 +418,6 @@ object PatternMatcher {
418418
&& !hasExplicitTypeArgs(extractor)
419419
case _ => false
420420
}
421-
422-
if (tpt.tpe.hasNull || tpt.tpe.hasNothing) report.error(em"${tpt.tpe} cannot be used in runtime type tests", tpt)
423421

424422
TestPlan(TypeTest(tpt, isTrusted), scrutinee, tree.span,
425423
letAbstract(ref(scrutinee).cast(tpt.tpe)) { casted =>

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -330,13 +330,17 @@ object TypeTestsCasts {
330330
expr.isInstance(testType).withSpan(tree.span)
331331
case OrType(tp1, tp2) =>
332332
evalOnce(expr) { e =>
333-
transformTypeTest(e, tp1, flagUnrelated = false)
334-
.or(transformTypeTest(e, tp2, flagUnrelated = false))
333+
lazy val tp1Tree = transformTypeTest(e, tp1, flagUnrelated = false)
334+
lazy val tp2Tree = transformTypeTest(e, tp2, flagUnrelated = false)
335+
336+
if (tp1.isNothingType || (tp1.isNullType && !tp2.isNotNull)) tp2Tree
337+
else if (tp2.isNothingType || (tp2.isNullType && !tp1.isNotNull)) tp1Tree
338+
else tp1Tree.or(tp2Tree)
335339
}
336340
case AndType(tp1, tp2) =>
337341
evalOnce(expr) { e =>
338342
transformTypeTest(e, tp1, flagUnrelated)
339-
.and(transformTypeTest(e, tp2, flagUnrelated))
343+
.and(transformTypeTest(e, tp2, flagUnrelated))
340344
}
341345
case defn.MultiArrayOf(elem, ndims) if isGenericArrayElement(elem, isScala2 = false) =>
342346
def isArrayTest(arg: Tree) =

0 commit comments

Comments
 (0)