Skip to content

Fix #4785: Change typing rule for wildcard star args #5577

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 2 commits into from
Dec 9, 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/core/TypeApplications.scala
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,7 @@ class TypeApplications(val self: Type) extends AnyVal {
}

/** The element type of a sequence or array */
def elemType(implicit ctx: Context): Type = self match {
def elemType(implicit ctx: Context): Type = self.widenDealias match {
case defn.ArrayOf(elemtp) => elemtp
case JavaArrayType(elemtp) => elemtp
case _ => self.baseType(defn.SeqClass).argInfos.headOption.getOrElse(NoType)
Expand Down
39 changes: 19 additions & 20 deletions compiler/src/dotty/tools/dotc/transform/ElimRepeated.scala
Original file line number Diff line number Diff line change
Expand Up @@ -71,40 +71,39 @@ class ElimRepeated extends MiniPhase with InfoTransformer { thisPhase =>
transformTypeOfTree(tree)

override def transformApply(tree: Apply)(implicit ctx: Context): Tree = {
val formals =
ctx.atPhase(thisPhase) { implicit ctx =>
tree.fun.tpe.widen.asInstanceOf[MethodType].paramInfos
}
val args1 = tree.args.zipWithConserve(formals) { (arg, formal) =>
arg match {
case arg: Typed if isWildcardStarArg(arg) =>
if (tree.fun.symbol.is(JavaDefined) && arg.expr.tpe.derivesFrom(defn.SeqClass))
seqToArray(arg.expr, formal.underlyingIfRepeated(isJava = true))
else arg.expr
case arg => arg
}
val args = tree.args.mapConserve {
case arg: Typed if isWildcardStarArg(arg) =>
val isJavaDefined = tree.fun.symbol.is(JavaDefined)
val tpe = arg.expr.tpe
if (isJavaDefined && tpe.derivesFrom(defn.SeqClass))
seqToArray(arg.expr)
else if (!isJavaDefined && tpe.derivesFrom(defn.ArrayClass))
arrayToSeq(arg.expr)
else
arg.expr
case arg => arg
}
transformTypeOfTree(cpy.Apply(tree)(tree.fun, args1))
transformTypeOfTree(cpy.Apply(tree)(tree.fun, args))
}

/** Convert sequence argument to Java array of type `pt` */
private def seqToArray(tree: Tree, pt: Type)(implicit ctx: Context): Tree = tree match {
/** Convert sequence argument to Java array */
private def seqToArray(tree: Tree)(implicit ctx: Context): Tree = tree match {
case SeqLiteral(elems, elemtpt) =>
JavaSeqLiteral(elems, elemtpt)
case app@Apply(fun, args) if defn.WrapArrayMethods().contains(fun.symbol) => // rewrite a call to `wrapXArray(arr)` to `arr`
Copy link
Contributor

Choose a reason for hiding this comment

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

Why was this optimization dropped?

Copy link
Contributor Author

@allanrenucci allanrenucci Dec 9, 2018

Choose a reason for hiding this comment

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

It is no longer needed. When calling into java repeated we used to insert an implicit conversion from Array to Seq that we later reverted. For example:

def test(xs: Array[String]) = Paths.get("Hello", xs: _*)

Before this patch:

// after frontend:
def test(xs: Array[String]) = Paths.get("Hello", wrapRefArray[String](xs):String*)

// after elimRepeated
def test(xs: Array[String]): = Paths.get("Hello", xs)

After this patch:

// after frontend:
def test(xs: Array[String]) = Paths.get("Hello", xs:String*) // no more call to wrapRefArray

// after elimRepeated
def test(xs: Array[String]): = Paths.get("Hello", xs)

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for explaining!

args.head
case _ =>
val elemType = tree.tpe.elemType
var elemClass = elemType.classSymbol
if (defn.NotRuntimeClasses contains elemClass) elemClass = defn.ObjectClass
if (defn.NotRuntimeClasses.contains(elemClass)) elemClass = defn.ObjectClass
ref(defn.DottyArraysModule)
.select(nme.seqToArray)
.appliedToType(elemType)
.appliedTo(tree, Literal(Constant(elemClass.typeRef)))
.ensureConforms(pt)
// Because of phantomclasses, the Java array's type might not conform to the return type
}

/** Convert Java array argument to Scala Seq */
private def arrayToSeq(tree: Tree)(implicit ctx: Context): Tree =
tpd.wrapArray(tree, tree.tpe.elemType)

override def transformTypeApply(tree: TypeApply)(implicit ctx: Context): Tree =
transformTypeOfTree(tree)

Expand Down
8 changes: 6 additions & 2 deletions compiler/src/dotty/tools/dotc/typer/TypeAssigner.scala
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,12 @@ trait TypeAssigner {
if (!sym.is(SyntheticOrPrivate) && sym.owner.isClass) checkNoPrivateLeaks(sym, pos)
else sym.info

def seqToRepeated(tree: Tree)(implicit ctx: Context): Tree =
Typed(tree, TypeTree(tree.tpe.widen.translateParameterized(defn.SeqClass, defn.RepeatedParamClass)))
private def toRepeated(tree: Tree, from: ClassSymbol)(implicit ctx: Context): Tree =
Typed(tree, TypeTree(tree.tpe.widen.translateParameterized(from, defn.RepeatedParamClass)))

def seqToRepeated(tree: Tree)(implicit ctx: Context): Tree = toRepeated(tree, defn.SeqClass)

def arrayToRepeated(tree: Tree)(implicit ctx: Context): Tree = toRepeated(tree, defn.ArrayClass)

/** A denotation exists really if it exists and does not point to a stale symbol. */
final def reallyExists(denot: Denotation)(implicit ctx: Context): Boolean = try
Expand Down
20 changes: 18 additions & 2 deletions compiler/src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,7 @@ class Typer extends Namer
}
case _ => ifExpr
}

def ascription(tpt: Tree, isWildcard: Boolean) = {
val underlyingTreeTpe =
if (isRepeatedParamType(tpt)) TypeTree(defn.SeqType.appliedTo(pt :: Nil))
Expand All @@ -557,11 +558,26 @@ class Typer extends Namer
else typed(tree.expr, tpt.tpe.widenSkolem)
assignType(cpy.Typed(tree)(expr1, tpt), underlyingTreeTpe)
}
if (untpd.isWildcardStarArg(tree))

if (untpd.isWildcardStarArg(tree)) {
def typedWildcardStarArgExpr = {
val tpdExpr = typedExpr(tree.expr)
tpdExpr.tpe.widenDealias match {
case defn.ArrayOf(_) =>
val starType = defn.ArrayType.appliedTo(WildcardType)
val exprAdapted = adapt(tpdExpr, starType)
arrayToRepeated(exprAdapted)
case _ =>
val starType = defn.SeqType.appliedTo(defn.AnyType)
val exprAdapted = adapt(tpdExpr, starType)
seqToRepeated(exprAdapted)
}
}
cases(
ifPat = ascription(TypeTree(defn.RepeatedParamType.appliedTo(pt)), isWildcard = true),
ifExpr = seqToRepeated(typedExpr(tree.expr, defn.SeqType.appliedTo(defn.AnyType))),
ifExpr = typedWildcardStarArgExpr,
wildName = nme.WILDCARD_STAR)
}
else {
def typedTpt = checkSimpleKinded(typedType(tree.tpt))
def handlePattern: Tree = {
Expand Down
4 changes: 2 additions & 2 deletions tests/neg/repeatedArgs213.scala
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ class repeatedArgs {
bar("a", "b", "c")
bar(xs: _*)
bar(ys: _*) // error: immutable.Seq expected, found Seq
bar(zs: _*) // error: immutable.Seq expected, found Array
bar(zs: _*) // old-error: Remove (compiler generated) Array to Seq convertion in 2.13?

Paths.get("Hello", "World")
Paths.get("Hello", xs: _*)
Paths.get("Hello", ys: _*) // error: immutable.Seq expected, found Seq
Paths.get("Hello", zs: _*) // error: immutable.Seq expected, found Array
Paths.get("Hello", zs: _*)
}
}
15 changes: 15 additions & 0 deletions tests/pos/i4785.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import scala.Predef.Set // unimport Predef.wrapRefArray

import java.nio.file.Paths

class i4785 {
def bar(xs: String*) = xs.length

def test(xs: Seq[String], ys: Array[String]) = {
Paths.get("Hello", xs: _*)
Paths.get("Hello", ys: _*)

bar(xs: _*)
bar(ys: _*)
}
}
4 changes: 4 additions & 0 deletions tests/pos/repeatedArgs.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import scala.collection.{immutable, mutable}
import java.nio.file.Paths
import java.util.concurrent.ForkJoinTask

class repeatedArgs {
def bar(xs: String*): Int = xs.length
Expand All @@ -19,4 +20,7 @@ class repeatedArgs {
val x: collection.Seq[String] = others
// val y: immutable.Seq[String] = others // ok in 2.13
}

def invokeAll[T](tasks: ForkJoinTask[T]*): Unit = ForkJoinTask.invokeAll(tasks: _*)
def invokeAll2(tasks: ForkJoinTask[_]*): Unit = ForkJoinTask.invokeAll(tasks: _*)
}