Skip to content

Commit 404d53c

Browse files
committed
Simplify adaptType by introducing unboxedType
1 parent 85747b5 commit 404d53c

File tree

3 files changed

+40
-35
lines changed

3 files changed

+40
-35
lines changed

compiler/src/dotty/tools/dotc/core/Definitions.scala

+14
Original file line numberDiff line numberDiff line change
@@ -1731,6 +1731,20 @@ class Definitions {
17311731
else sys.error(s"Not a primitive value type: $tp")
17321732
}.typeRef
17331733

1734+
def unboxedType(tp: Type)(using Context): TypeRef = {
1735+
val cls = tp.classSymbol
1736+
if (cls eq BoxedByteClass) ByteType
1737+
else if (cls eq BoxedShortClass) ShortType
1738+
else if (cls eq BoxedCharClass) CharType
1739+
else if (cls eq BoxedIntClass) IntType
1740+
else if (cls eq BoxedLongClass) LongType
1741+
else if (cls eq BoxedFloatClass) FloatType
1742+
else if (cls eq BoxedDoubleClass) DoubleType
1743+
else if (cls eq BoxedUnitClass) UnitType
1744+
else if (cls eq BoxedBooleanClass) BooleanType
1745+
else sys.error(s"Not a boxed primitive value type: $tp")
1746+
}
1747+
17341748
/** The JVM tag for `tp` if it's a primitive, `java.lang.Object` otherwise. */
17351749
def typeTag(tp: Type)(using Context): Name = typeTags(scalaClassName(tp))
17361750

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

+7-35
Original file line numberDiff line numberDiff line change
@@ -512,41 +512,13 @@ class SpaceEngine(using Context) extends SpaceLogic {
512512
if converted == null then tp else ConstantType(converted)
513513
case _ => tp
514514

515-
private lazy val defn = ctx.definitions
516-
private lazy val ByteClass = defn.ByteClass
517-
private lazy val ShortClass = defn.ShortClass
518-
private lazy val CharClass = defn.CharClass
519-
private lazy val IntClass = defn.IntClass
520-
private lazy val LongClass = defn.LongClass
521-
private lazy val FloatClass = defn.FloatClass
522-
private lazy val DoubleClass = defn.DoubleClass
523-
private lazy val UnitClass = defn.UnitClass
524-
private lazy val BooleanClass = defn.BooleanClass
525-
526-
/** Adapt types by performing primitive value unboxing or boxing, or numeric constant conversion. #12805 */
527-
def adaptType(tp1: Type, tp2: Type): Type = trace(i"adaptType($tp1, $tp2)", show = true)((tp1.classSymbol, tp2.classSymbol) match {
528-
case ( ByteClass, defn.BoxedByteClass) => defn.BoxedByteClass.typeRef.narrow
529-
case ( ShortClass, defn.BoxedShortClass) => defn.BoxedShortClass.typeRef.narrow
530-
case ( CharClass, defn.BoxedCharClass) => defn.BoxedCharClass.typeRef.narrow
531-
case ( IntClass, defn.BoxedIntClass) => defn.BoxedIntClass.typeRef.narrow // 1 <:< Integer => (<skolem> : Integer) <:< Integer = true
532-
case ( LongClass, defn.BoxedLongClass) => defn.BoxedLongClass.typeRef.narrow
533-
case ( FloatClass, defn.BoxedFloatClass) => defn.BoxedFloatClass.typeRef.narrow
534-
case ( DoubleClass, defn.BoxedDoubleClass) => defn.BoxedDoubleClass.typeRef.narrow
535-
case ( UnitClass, defn.BoxedUnitClass) => defn.BoxedUnitClass.typeRef.narrow
536-
case (BooleanClass, defn.BoxedBooleanClass) => defn.BoxedBooleanClass.typeRef.narrow
537-
538-
case ( defn.BoxedByteClass, ByteClass) => defn.ByteType.narrow
539-
case ( defn.BoxedShortClass, ShortClass) => defn.ShortType.narrow
540-
case ( defn.BoxedCharClass, CharClass) => defn.CharType.narrow
541-
case ( defn.BoxedIntClass, IntClass) => defn.IntType.narrow // ONE <:< Int => (<skolem> : Int) <:< Int = true
542-
case ( defn.BoxedLongClass, LongClass) => defn.LongType.narrow
543-
case ( defn.BoxedFloatClass, FloatClass) => defn.FloatType.narrow
544-
case ( defn.BoxedDoubleClass, DoubleClass) => defn.DoubleType.narrow
545-
case ( defn.BoxedUnitClass, UnitClass) => defn.UnitType.narrow
546-
case (defn.BoxedBooleanClass, BooleanClass) => defn.BooleanType.narrow
547-
548-
case _ => convertConstantType(tp1, tp2)
549-
})
515+
def adaptType(tp1: Type, tp2: Type): Type = trace(i"adaptType($tp1, $tp2)", show = true) {
516+
def isPrimToBox(tp: Type, pt: Type) =
517+
tp.classSymbol.isPrimitiveValueClass && (defn.boxedType(tp).classSymbol eq pt.classSymbol)
518+
if isPrimToBox(tp1, tp2) then defn.boxedType(tp1).narrow // 1 <:< Integer => (<skolem> : Integer) <:< Integer = true
519+
else if isPrimToBox(tp2, tp1) then defn.unboxedType(tp1).narrow // ONE <:< Int => (<skolem> : Int) <:< Int = true
520+
else convertConstantType(tp1, tp2)
521+
}
550522

551523
/** Is `tp1` a subtype of `tp2`? */
552524
def isSubType(tp1: Type, tp2: Type): Boolean = trace(i"$tp1 <:< $tp2", debug, show = true) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package dotty.tools
2+
package dotc
3+
package transform
4+
5+
import org.junit.*, Assert.*
6+
7+
import core.*, Contexts.*, Decorators.*, Symbols.*, Types.*
8+
9+
class SpaceEngineTest extends DottyTest:
10+
@Test def testAdaptTest(): Unit =
11+
given Context = ctx
12+
val defn = ctx.definitions
13+
import defn._
14+
val e = patmat.SpaceEngine()
15+
16+
val BoxedIntType = BoxedIntClass.typeRef
17+
18+
assertEquals(BoxedIntType, e.adaptType(IntType, BoxedIntType).widenSingleton)
19+
assertEquals(IntType, e.adaptType(BoxedIntType, IntType).widenSingleton)

0 commit comments

Comments
 (0)