File tree Expand file tree Collapse file tree 2 files changed +23
-4
lines changed
compiler/src/dotty/tools/dotc/typer Expand file tree Collapse file tree 2 files changed +23
-4
lines changed Original file line number Diff line number Diff line change @@ -3804,10 +3804,23 @@ class Typer extends Namer
3804
3804
mapOver(tp)
3805
3805
}
3806
3806
3807
- if tree.symbol.isOneOf(Module | Enum )
3808
- && ! (tree.tpe frozen_<:< pt) // fast track
3809
- && ! (tree.tpe frozen_<:< approx(pt))
3810
- then
3807
+ // Is it certain that a value of `tree.tpe` is never a subtype of `pt`?
3808
+ // It is true if either
3809
+ // - the class of `tree.tpe` and class of `pt` cannot have common subclass, or
3810
+ // - `tree` is an object or enum value, which cannot possibly be a subtype of `pt`
3811
+ val isDefiniteNotSubtype = {
3812
+ val clsA = tree.tpe.widenDealias.classSymbol
3813
+ val clsB = pt.dealias.classSymbol
3814
+ clsA.exists && clsB.exists
3815
+ && clsA != defn.NullClass
3816
+ && (! clsA.isNumericValueClass && ! clsB.isNumericValueClass) // approximation for numeric conversion and boxing
3817
+ && ! clsA.asClass.mayHaveCommonChild(clsB.asClass)
3818
+ || tree.symbol.isOneOf(Module | Enum )
3819
+ && ! (tree.tpe frozen_<:< pt) // fast track
3820
+ && ! (tree.tpe frozen_<:< approx(pt))
3821
+ }
3822
+
3823
+ if isDefiniteNotSubtype then
3811
3824
// We could check whether `equals` is overriden.
3812
3825
// Reasons for not doing so:
3813
3826
// - it complicates the protocol
Original file line number Diff line number Diff line change
1
+ object UnitTest extends App {
2
+ def foo (m : Unit ) = m match {
3
+ case runtime.BoxedUnit .UNIT => println(" ok" ) // error
4
+ }
5
+ foo(())
6
+ }
You can’t perform that action at this time.
0 commit comments