Skip to content

Commit dd6782a

Browse files
committed
Don't do null checks in pattern matching if explicit nulls is enabled
1 parent dba4dc3 commit dd6782a

File tree

2 files changed

+9
-17
lines changed

2 files changed

+9
-17
lines changed

compiler/src/dotty/tools/dotc/transform/patmat/Space.scala

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -308,9 +308,7 @@ class SpaceEngine(implicit ctx: Context) extends SpaceLogic {
308308

309309
override def intersectUnrelatedAtomicTypes(tp1: Type, tp2: Type): Space = {
310310
// Precondition: !isSubType(tp1, tp2) && !isSubType(tp2, tp1).
311-
if (ctx.explicitNulls) {
312-
// No additional checks required: since subtyping already reflects nullability.
313-
} else if (tp1.isNullType || tp2.isNullType) {
311+
if (!ctx.explicitNulls && (tp1.isNullType || tp2.isNullType)) {
314312
// Since projections of types don't include null, intersection with null is empty.
315313
return Empty
316314
}
@@ -791,20 +789,14 @@ class SpaceEngine(implicit ctx: Context) extends SpaceLogic {
791789
}
792790

793791
// if last case is `_` and only matches `null`, produce a warning
794-
if (i == cases.length - 1 && !isNullLit(pat) ) {
792+
// If explicit nulls are enabled, this check isn't needed because most of the cases
793+
// that would trigger it would also trigger unreachability warnings.
794+
if (!ctx.explicitNulls && i == cases.length - 1 && !isNullLit(pat) ) {
795795
val simpl = simplify(minus(covered, prevs))
796-
if (ctx.explicitNulls) {
797-
simpl match {
798-
case space if isNullSpace(space) =>
799-
ctx.warning(MatchCaseOnlyNullWarning(), pat.sourcePos)
800-
case _ =>
801-
}
802-
} else {
803-
simpl match {
804-
case Typ(`constantNullType`, _) =>
805-
ctx.warning(MatchCaseOnlyNullWarning(), pat.sourcePos)
806-
case _ =>
807-
}
796+
simpl match {
797+
case Typ(`constantNullType`, _) =>
798+
ctx.warning(MatchCaseOnlyNullWarning(), pat.sourcePos)
799+
case _ =>
808800
}
809801
}
810802
}

tests/explicit-nulls/neg-patmat/patmat1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class Foo {
2626
a2 match {
2727
case Dog(_) => 100
2828
case Cat => 200
29-
case _ => 300 // error: only matches null
29+
case _ => 300
3030
}
3131

3232
val a3: Animal|Null = ???

0 commit comments

Comments
 (0)