Skip to content

Commit 80b9a23

Browse files
committed
Fix scala#9166: Harden check for values in patterns based on class inheritance info
1 parent 758782a commit 80b9a23

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

compiler/src/dotty/tools/dotc/typer/Typer.scala

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3808,10 +3808,19 @@ class Typer extends Namer
38083808
mapOver(tp)
38093809
}
38103810

3811-
if tree.symbol.isOneOf(Module | Enum)
3812-
&& !(tree.tpe frozen_<:< pt) // fast track
3813-
&& !(tree.tpe frozen_<:< approx(pt))
3814-
then
3811+
val sym = tree.symbol
3812+
3813+
// Is it certain that a value of `tree.tpe` never a subtype of `pt`
3814+
val definiteNotSubType = {
3815+
val clsA = sym.info.classSymbol
3816+
val clsB = pt.classSymbol
3817+
clsA.exists && clsB.exists && !clsA.asClass.mayHaveCommonChild(clsB.asClass)
3818+
|| sym.isOneOf(Module | Enum)
3819+
&& !(tree.tpe frozen_<:< pt) // fast track
3820+
&& !(tree.tpe frozen_<:< approx(pt))
3821+
}
3822+
3823+
if definiteNotSubType then
38153824
// We could check whether `equals` is overriden.
38163825
// Reasons for not doing so:
38173826
// - it complicates the protocol

tests/neg/i9166.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
object UnitTest extends App {
2+
def foo(m: Unit) = m match {
3+
case runtime.BoxedUnit.UNIT => println("ok") // error
4+
}
5+
foo(())
6+
}

0 commit comments

Comments
 (0)