Skip to content

Fix #4037: Fix code generation for generic arrays #4046

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/ast/tpd.scala
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo {
ref(defn.DottyArraysModule).select(defn.newArrayMethod).withPos(pos)

if (!ctx.erasedTypes) {
assert(!TypeErasure.isUnboundedGeneric(elemTpe)) //needs to be done during typer. See Applications.convertNewGenericArray
assert(!TypeErasure.isGeneric(elemTpe)) //needs to be done during typer. See Applications.convertNewGenericArray
newArr.appliedToTypeTrees(TypeTree(returnTpe) :: Nil).appliedToArgs(clsOf(elemTpe) :: clsOf(returnTpe) :: dims :: Nil).withPos(pos)
} else // after erasure
newArr.appliedToArgs(clsOf(elemTpe) :: clsOf(returnTpe) :: dims :: Nil).withPos(pos)
Expand Down
10 changes: 10 additions & 0 deletions compiler/src/dotty/tools/dotc/core/TypeErasure.scala
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,16 @@ object TypeErasure {
case _ => false
}

/** Is `tp` an abstract type or polymorphic type parameter, or another unbounded generic type? */
def isGeneric(tp: Type)(implicit ctx: Context): Boolean = tp.dealias match {
case tp: TypeRef => !tp.symbol.isClass
case tp: TypeParamRef => true
case tp: TypeProxy => isGeneric(tp.underlying)
case tp: AndType => isGeneric(tp.tp1) || isGeneric(tp.tp2)
case tp: OrType => isGeneric(tp.tp1) || isGeneric(tp.tp2)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you explain why for both A & B and A | B, you have isGeneric(A) || isGeneric(B)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's an approximation that makes sure that the erasure would not depend on any possible instantiation of the type. Erasure of AndTypes and OrTypes is quite complex, so it's hard to come up with a narrower condition.

case _ => false
}

/** The erased least upper bound is computed as follows
* - if both argument are arrays of objects, an array of the erased lub of the element types
* - if both arguments are arrays of same primitives, an array of this primitive
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ class ArrayConstructors extends MiniPhase {
val cs = tp.tpe.widen.classSymbol
tree.fun match {
case Apply(TypeApply(t: Ident, targ), dims)
if !TypeErasure.isUnboundedGeneric(targ.head.tpe) && !ValueClasses.isDerivedValueClass(cs) =>
if !TypeErasure.isGeneric(targ.head.tpe) && !ValueClasses.isDerivedValueClass(cs) =>
rewrite(targ.head.tpe, dims)
case Apply(TypeApply(t: Select, targ), dims)
if !TypeErasure.isUnboundedGeneric(targ.head.tpe) && !ValueClasses.isDerivedValueClass(cs) =>
if !TypeErasure.isGeneric(targ.head.tpe) && !ValueClasses.isDerivedValueClass(cs) =>
Block(t.qualifier :: Nil, rewrite(targ.head.tpe, dims))
case _ => tree
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/typer/Applications.scala
Original file line number Diff line number Diff line change
Expand Up @@ -831,7 +831,7 @@ trait Applications extends Compatibility { self: Typer with Dynamic =>
.select(defn.newGenericArrayMethod).withPos(tree.pos)
.appliedToTypeTrees(targs).appliedToArgs(args)

if (TypeErasure.isUnboundedGeneric(targ.tpe))
if (TypeErasure.isGeneric(targ.tpe))
newGenericArrayCall
else tree
case _ =>
Expand Down
8 changes: 8 additions & 0 deletions tests/run/i4037.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import scala.reflect.ClassTag

object Test {
def foo[T <: AnyRef : ClassTag] = new Array[T](42)
def main(args: Array[String]): Unit = {
val x: Array[String] = foo[String]
}
}