Skip to content

Commit c74bb42

Browse files
committed
Separate handling of genericArray creation from normal ones.
This allowed to simplify the code in both Applications and tpd.newArray. Now, only creation of generic arrays is handled by typer. All other arrays are handled in ArrayConstructors phase.
1 parent 9d7db0c commit c74bb42

File tree

3 files changed

+18
-18
lines changed

3 files changed

+18
-18
lines changed

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

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -367,16 +367,10 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo {
367367
def newArr(symbol: TermSymbol) =
368368
ref(defn.DottyArraysModule).select(symbol).withPos(pos)
369369

370-
if (!ctx.erasedTypes)
371-
if (TypeErasure.isUnboundedGeneric(elemTpe)) {
372-
//exists only before erasure
373-
assert(dims.elems.tail.isEmpty)
374-
assert(!ctx.isAfterTyper) // needs to infer an implicit
375-
newArr(defn.newGenericArrayMethod).appliedToType(elemTpe).appliedTo(dims.elems.head)
376-
}
377-
else
378-
newArr(defn.newArrayMethod).appliedToTypeTrees(TypeTree(returnTpe) :: Nil).appliedToArgs(clsOf(elemTpe) :: clsOf(returnTpe) :: dims :: Nil).withPos(pos)
379-
else // after erasure
370+
if (!ctx.erasedTypes) {
371+
assert(!TypeErasure.isUnboundedGeneric(elemTpe)) //needs to be done during typer. See Applications.convertNewGenericArray
372+
newArr(defn.newArrayMethod).appliedToTypeTrees(TypeTree(returnTpe) :: Nil).appliedToArgs(clsOf(elemTpe) :: clsOf(returnTpe) :: dims :: Nil).withPos(pos)
373+
} else // after erasure
380374
newArr(defn.newArrayMethod).appliedToArgs(clsOf(elemTpe) :: clsOf(returnTpe) :: dims :: Nil).withPos(pos)
381375
}
382376

@@ -754,7 +748,7 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo {
754748

755749
/** If inititializer tree is `_', the default value of its type,
756750
* otherwise the tree itself.
757-
*/
751+
* */
758752
def wildcardToDefault(implicit ctx: Context) =
759753
if (isWildcardArg(tree)) defaultValue(tree.tpe) else tree
760754

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

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -560,7 +560,7 @@ trait Applications extends Compatibility { self: Typer =>
560560
if (proto.argsAreTyped) new ApplyToTyped(tree, fun1, funRef, proto.typedArgs, pt)
561561
else new ApplyToUntyped(tree, fun1, funRef, proto, pt)(argCtx(tree))
562562
val result = app.result
563-
convertNewArray(ConstFold(result))
563+
convertNewGenericArray(ConstFold(result))
564564
} { (failedVal, failedState) =>
565565
val fun2 = tryInsertImplicitOnQualifier(fun1, proto)
566566
if (fun1 eq fun2) {
@@ -636,14 +636,20 @@ trait Applications extends Compatibility { self: Typer =>
636636
def adaptTypeArg(tree: tpd.Tree, bound: Type)(implicit ctx: Context): tpd.Tree =
637637
tree.withType(tree.tpe.etaExpandIfHK(bound))
638638

639-
/** Rewrite `new Array[T](....)` trees to calls of newXYZArray methods.
639+
/** Rewrite `new Array[T](....)` if T is an unbounded generic to calls to newGenericArray.
640640
* It is performed during typer as creation of generic arrays needs a classTag.
641-
* we rely on implicit search to find one
641+
* we rely on implicit search to find one.
642642
*/
643-
def convertNewArray(tree: tpd.Tree)(implicit ctx: Context): tpd.Tree = tree match {
644-
case Apply(TypeApply(tycon, targ :: Nil), args) if tycon.symbol == defn.ArrayConstructor =>
643+
def convertNewGenericArray(tree: tpd.Tree)(implicit ctx: Context): tpd.Tree = tree match {
644+
case Apply(TypeApply(tycon, targs@(targ :: Nil)), args) if tycon.symbol == defn.ArrayConstructor =>
645645
fullyDefinedType(tree.tpe, "array", tree.pos)
646-
newArray(targ.tpe, tree.tpe, tree.pos, JavaSeqLiteral(args, TypeTree(defn.IntClass.typeRef)))
646+
647+
def newGenericArrayCall =
648+
ref(defn.DottyArraysModule).select(defn.newGenericArrayMethod).withPos(tree.pos).appliedToTypeTrees(targs).appliedToArgs(args)
649+
650+
if (TypeErasure.isUnboundedGeneric(targ.tpe))
651+
newGenericArrayCall
652+
else tree
647653
case _ =>
648654
tree
649655
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1684,7 +1684,7 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
16841684
case _ => Nil
16851685
}
16861686
if (typeArgs.isEmpty) typeArgs = constrained(poly, tree)._2
1687-
convertNewArray(
1687+
convertNewGenericArray(
16881688
adaptInterpolated(tree.appliedToTypes(typeArgs), pt, original))
16891689
}
16901690
case wtp =>

0 commit comments

Comments
 (0)