Skip to content

Commit be658f6

Browse files
committed
Also perform primitive value boxing
1 parent 4b0f32f commit be658f6

File tree

2 files changed

+29
-5
lines changed

2 files changed

+29
-5
lines changed

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

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ trait SpaceLogic {
165165
}
166166

167167
/** Is `a` a subspace of `b`? Equivalent to `a - b == Empty`, but faster */
168-
def isSubspace(a: Space, b: Space)(using Context): Boolean = trace(s"${show(a)} < ${show(b)}", debug) {
168+
def isSubspace(a: Space, b: Space)(using Context): Boolean = trace(s"isSubspace(${show(a)}, ${show(b)})", debug) {
169169
def tryDecompose1(tp: Type) = canDecompose(tp) && isSubspace(Or(decompose(tp)), b)
170170
def tryDecompose2(tp: Type) = canDecompose(tp) && isSubspace(a, Or(decompose(tp)))
171171

@@ -515,15 +515,22 @@ class SpaceEngine(using Context) extends SpaceLogic {
515515
if converted == null then tp else ConstantType(converted)
516516
case _ => tp
517517

518+
/** Adapt types by performing primitive value boxing. #12805 */
519+
def maybeBox(tp1: Type, tp2: Type): Type =
520+
if tp1.classSymbol.isPrimitiveValueClass && !tp2.classSymbol.isPrimitiveValueClass then
521+
defn.boxedType(tp1).narrow
522+
else tp1
523+
518524
/** Is `tp1` a subtype of `tp2`? */
519525
def isSubType(_tp1: Type, tp2: Type): Boolean = {
520-
val tp1 = convertConstantType(_tp1, tp2)
521-
debug.println(TypeComparer.explained(_.isSubType(tp1, tp2)))
526+
val tp1 = maybeBox(convertConstantType(_tp1, tp2), tp2)
527+
//debug.println(TypeComparer.explained(_.isSubType(tp1, tp2)))
522528
val res = if (ctx.explicitNulls) {
523529
tp1 <:< tp2
524530
} else {
525531
(tp1 != constantNullType || tp2 == constantNullType) && tp1 <:< tp2
526532
}
533+
debug.println(i"$tp1 <:< $tp2 = $res")
527534
res
528535
}
529536

@@ -663,7 +670,6 @@ class SpaceEngine(using Context) extends SpaceLogic {
663670
parts.map(Typ(_, true))
664671
}
665672

666-
667673
/** Abstract sealed types, or-types, Boolean and Java enums can be decomposed */
668674
def canDecompose(tp: Type): Boolean =
669675
val res = tp.dealias match
@@ -679,7 +685,7 @@ class SpaceEngine(using Context) extends SpaceLogic {
679685
|| cls.isAllOf(JavaEnumTrait)
680686
|| tp.isRef(defn.BooleanClass)
681687
|| tp.isRef(defn.UnitClass)
682-
debug.println(s"decomposable: ${tp.show} = $res")
688+
//debug.println(s"decomposable: ${tp.show} = $res")
683689
res
684690

685691
/** Show friendly type name with current scope in mind

tests/patmat/boxing.scala

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class C {
2+
def matchUnboxed(i: Integer) = i match {
3+
case null => 0
4+
case 1 => 1
5+
case _ => 9
6+
}
7+
8+
def matchBoxed(i: Int) = i match {
9+
case C.ZERO => 0
10+
case C.ONE => 1
11+
case _ => 9
12+
}
13+
}
14+
15+
object C {
16+
final val ZERO: Integer = 0
17+
final val ONE: Integer = 1
18+
}

0 commit comments

Comments
 (0)