Skip to content

Commit 3d8a170

Browse files
committed
Decouple handling array constructors from typer.
It's done in a separate ArrayConstructors phase now.
1 parent e60a20d commit 3d8a170

File tree

5 files changed

+67
-14
lines changed

5 files changed

+67
-14
lines changed

src/dotty/tools/dotc/Compiler.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ class Compiler {
6363
new Getters,
6464
new ElimByName,
6565
new AugmentScala2Traits,
66-
new ResolveSuper),
66+
new ResolveSuper,
67+
new ArrayConstructors),
6768
List(new Erasure),
6869
List(new ElimErasedValueType,
6970
new VCElideAllocations,

src/dotty/tools/dotc/ast/tpd.scala

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -839,7 +839,10 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo {
839839
case tpnme.Float => TYPE(defn.BoxedFloatModule)
840840
case tpnme.Double => TYPE(defn.BoxedDoubleModule)
841841
case tpnme.Unit => TYPE(defn.BoxedUnitModule)
842-
case _ => Literal(Constant(TypeErasure.erasure(tp)))
842+
case _ =>
843+
if(ctx.erasedTypes || !tp.derivesFrom(defn.ArrayClass))
844+
Literal(Constant(TypeErasure.erasure(tp)))
845+
else Literal(Constant(tp))
843846
}
844847
}
845848

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package dotty.tools.dotc
2+
package transform
3+
4+
import core._
5+
import TreeTransforms._
6+
import Contexts.Context
7+
import Flags._
8+
import SymUtils._
9+
import Symbols._
10+
import SymDenotations._
11+
import Types._
12+
import Decorators._
13+
import DenotTransformers._
14+
import StdNames._
15+
import NameOps._
16+
import ast.Trees._
17+
import dotty.tools.dotc.ast.tpd
18+
import util.Positions._
19+
import Names._
20+
import collection.mutable
21+
import ResolveSuper._
22+
23+
24+
/** This phase rewrites calls to array constructors to newArray and newGenericArray methods
25+
* in Dotty.runtime.Arrays module.
26+
*
27+
* Additionally it optimizes calls to scala.Array.ofDim functions
28+
*
29+
*/
30+
class ArrayConstructors extends MiniPhaseTransform { thisTransform =>
31+
import ast.tpd._
32+
33+
override def phaseName: String = "arrayConstructors"
34+
35+
override def transformApply(tree: tpd.Apply)(implicit ctx: Context, info: TransformerInfo): tpd.Tree = {
36+
def rewrite(elemType: Type, dims: List[Tree]) =
37+
tpd.newArray(elemType, tree.tpe, tree.pos, JavaSeqLiteral(dims, TypeTree(defn.IntClass.typeRef)).asInstanceOf[JavaSeqLiteral])
38+
39+
if (tree.fun.symbol eq defn.ArrayConstructor) {
40+
tree.fun match {
41+
case TypeApply(tycon, targ :: Nil) =>
42+
rewrite(targ.tpe, tree.args)
43+
case _ =>
44+
???
45+
}
46+
} else if ((tree.fun.symbol.maybeOwner eq defn.ArrayModule) && (tree.fun.symbol.name eq nme.ofDim) && !tree.tpe.isInstanceOf[MethodicType]) {
47+
48+
tree.fun match {
49+
case Apply(TypeApply(t: Ident, targ), dims) =>
50+
rewrite(targ.head.tpe, dims)
51+
case Apply(TypeApply(t: Select, targ), dims) =>
52+
Block(t.qualifier :: Nil, rewrite(targ.head.tpe, dims))
53+
case _ => tree
54+
}
55+
56+
} else tree
57+
}
58+
}
59+

src/dotty/tools/dotc/typer/Applications.scala

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -556,7 +556,7 @@ trait Applications extends Compatibility { self: Typer =>
556556
if (proto.argsAreTyped) new ApplyToTyped(tree, fun1, funRef, proto.typedArgs, pt)
557557
else new ApplyToUntyped(tree, fun1, funRef, proto, pt)(argCtx)
558558
val result = app.result
559-
convertNewArray(ConstFold(result))
559+
ConstFold(result)
560560
} { (failedVal, failedState) =>
561561
val fun2 = tryInsertImplicitOnQualifier(fun1, proto)
562562
if (fun1 eq fun2) {
@@ -632,15 +632,6 @@ trait Applications extends Compatibility { self: Typer =>
632632
def adaptTypeArg(tree: tpd.Tree, bound: Type)(implicit ctx: Context): tpd.Tree =
633633
tree.withType(tree.tpe.etaExpandIfHK(bound))
634634

635-
/** Rewrite `new Array[T](....)` trees to calls of newXYZArray methods. */
636-
def convertNewArray(tree: tpd.Tree)(implicit ctx: Context): tpd.Tree = tree match {
637-
case Apply(TypeApply(tycon, targ :: Nil), args) if tycon.symbol == defn.ArrayConstructor =>
638-
fullyDefinedType(tree.tpe, "array", tree.pos)
639-
tpd.cpy.Apply(tree)(newArray(targ, tree.pos), args)
640-
case _ =>
641-
tree
642-
}
643-
644635
def typedUnApply(tree: untpd.Apply, selType: Type)(implicit ctx: Context): Tree = track("typedUnApply") {
645636
val Apply(qual, args) = tree
646637

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1671,8 +1671,7 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
16711671
case _ => Nil
16721672
}
16731673
if (typeArgs.isEmpty) typeArgs = constrained(poly, tree)._2
1674-
convertNewArray(
1675-
adaptInterpolated(tree.appliedToTypes(typeArgs), pt, original))
1674+
adaptInterpolated(tree.appliedToTypes(typeArgs), pt, original)
16761675
}
16771676
case wtp =>
16781677
pt match {

0 commit comments

Comments
 (0)