Skip to content

Commit 0452155

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 3005b92 commit 0452155

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
@@ -368,16 +368,10 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo {
368368
def newArr(symbol: TermSymbol) =
369369
ref(defn.DottyArraysModule).select(symbol).withPos(pos)
370370

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

@@ -755,7 +749,7 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo {
755749

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

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

Lines changed: 12 additions & 6 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+
convertNewGenericArray(ConstFold(result))
560560
} { (failedVal, failedState) =>
561561
val fun2 = tryInsertImplicitOnQualifier(fun1, proto)
562562
if (fun1 eq fun2) {
@@ -632,14 +632,20 @@ 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.
635+
/** Rewrite `new Array[T](....)` if T is an unbounded generic to calls to newGenericArray.
636636
* It is performed during typer as creation of generic arrays needs a classTag.
637-
* we rely on implicit search to find one
637+
* we rely on implicit search to find one.
638638
*/
639-
def convertNewArray(tree: tpd.Tree)(implicit ctx: Context): tpd.Tree = tree match {
640-
case Apply(TypeApply(tycon, targ :: Nil), args) if tycon.symbol == defn.ArrayConstructor =>
639+
def convertNewGenericArray(tree: tpd.Tree)(implicit ctx: Context): tpd.Tree = tree match {
640+
case Apply(TypeApply(tycon, targs@(targ :: Nil)), args) if tycon.symbol == defn.ArrayConstructor =>
641641
fullyDefinedType(tree.tpe, "array", tree.pos)
642-
newArray(targ.tpe, tree.tpe, tree.pos, JavaSeqLiteral(args, TypeTree(defn.IntClass.typeRef)))
642+
643+
def newGenericArrayCall =
644+
ref(defn.DottyArraysModule).select(defn.newGenericArrayMethod).withPos(tree.pos).appliedToTypeTrees(targs).appliedToArgs(args)
645+
646+
if (TypeErasure.isUnboundedGeneric(tree.tpe))
647+
newGenericArrayCall
648+
else tree
643649
case _ =>
644650
tree
645651
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1671,7 +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(
1674+
convertNewGenericArray(
16751675
adaptInterpolated(tree.appliedToTypes(typeArgs), pt, original))
16761676
}
16771677
case wtp =>

0 commit comments

Comments
 (0)