@@ -10,6 +10,8 @@ import StdNames._
1010import ast .Trees ._
1111import dotty .tools .dotc .ast .tpd
1212
13+ import scala .reflect .ClassTag
14+
1315
1416/** This phase rewrites calls to `Array.apply` to primitive array instantion.
1517 *
@@ -38,9 +40,27 @@ class ArrayApply extends MiniPhase {
3840 } else tree
3941 }
4042
41- // Only optimize when classtag is `ClassTag.apply` or `ClassTag.{Byte, Boolean, ...}`
42- private def elideClassTag (ct : Tree )(implicit ctx : Context ): Boolean = {
43- ct.symbol.maybeOwner.companionModule == defn.ClassTagModule
43+ /** Only optimize when classtag if it is one of
44+ * - `ClassTag.apply(classOf[X])`
45+ * - `ClassTag.apply(java.lang.XYZ.Type)`
46+ * - `ClassTag.{Byte, Boolean, ...}`
47+ */
48+ private def elideClassTag (ct : Tree )(implicit ctx : Context ): Boolean = ct match {
49+ case Apply (_, rc :: Nil ) if ct.symbol == defn.ClassTagModule_apply =>
50+ rc match {
51+ case _ : Literal => true // classOf[X]
52+ case rc : RefTree if rc.name == nme.TYPE_ =>
53+ val owner = rc.symbol.maybeOwner.companionModule
54+ owner == defn.BoxedBooleanModule || owner == defn.BoxedByteModule ||
55+ owner == defn.BoxedShortModule || owner == defn.BoxedCharModule ||
56+ owner == defn.BoxedIntModule || owner == defn.BoxedLongModule ||
57+ owner == defn.BoxedFloatModule || owner == defn.BoxedDoubleModule ||
58+ owner == defn.BoxedUnitModule
59+ case _ => false
60+ }
61+ case Apply (ctm : RefTree , _) if ctm.symbol.maybeOwner.companionModule == defn.ClassTagModule =>
62+ nme.ScalaValueNames .contains(ctm.name)
63+ case _ => false
4464 }
4565
4666 object CleanTree {
0 commit comments