From e5632198c8ac43dfd474dba0cc978aab18870968 Mon Sep 17 00:00:00 2001 From: noti0na1 Date: Thu, 3 Mar 2022 14:30:24 -0500 Subject: [PATCH] Fix intercept TypeApply in explicit nulls --- .../dotty/tools/dotc/transform/Erasure.scala | 8 +++--- .../explicit-nulls/pos-patmat/match-pat.scala | 27 +++++++++++++++++++ 2 files changed, 32 insertions(+), 3 deletions(-) create mode 100644 tests/explicit-nulls/pos-patmat/match-pat.scala diff --git a/compiler/src/dotty/tools/dotc/transform/Erasure.scala b/compiler/src/dotty/tools/dotc/transform/Erasure.scala index 739f7d6b06b4..d19084ac3d8c 100644 --- a/compiler/src/dotty/tools/dotc/transform/Erasure.scala +++ b/compiler/src/dotty/tools/dotc/transform/Erasure.scala @@ -784,9 +784,11 @@ object Erasure { } override def typedTypeApply(tree: untpd.TypeApply, pt: Type)(using Context): Tree = { - val ntree = atPhase(erasurePhase)( - interceptTypeApply(tree.asInstanceOf[TypeApply]) - ).withSpan(tree.span) + val ntree = atPhase(erasurePhase){ + // Use erased-type semantic to intercept TypeApply in explicit nulls + val interceptCtx = if ctx.explicitNulls then ctx.retractMode(Mode.SafeNulls) else ctx + interceptTypeApply(tree.asInstanceOf[TypeApply])(using interceptCtx) + }.withSpan(tree.span) ntree match { case TypeApply(fun, args) => diff --git a/tests/explicit-nulls/pos-patmat/match-pat.scala b/tests/explicit-nulls/pos-patmat/match-pat.scala new file mode 100644 index 000000000000..c9a408bde6d4 --- /dev/null +++ b/tests/explicit-nulls/pos-patmat/match-pat.scala @@ -0,0 +1,27 @@ +// Ensure we don't get "the type test for argType cannot be checked at runtime" warning + +class Symbol { + type ThisName +} + +type TermSymbol = Symbol { type ThisName = String } + +type TermSymbolOrNull = TermSymbol | Null + +def testSimple = + val x: Symbol | Null = ??? + x match + case key: Symbol => 1 + case null => 0 + +def testWithRefinedType = + val x: TermSymbol | Null = ??? + x match + case key: TermSymbol => 1 + case null => 0 + +def testWithAlias = + val x: TermSymbolOrNull = ??? + x match + case key: TermSymbol => 1 + case null => 0