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