Skip to content

Tweak zipWithConserve to eliminate 3 casts #14654

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 13, 2022
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
6 changes: 3 additions & 3 deletions compiler/src/dotty/tools/dotc/core/Decorators.scala
Original file line number Diff line number Diff line change
Expand Up @@ -144,13 +144,13 @@ object Decorators {
* `xs` to themselves. Also, it is required that `ys` is at least
* as long as `xs`.
*/
def zipWithConserve[U](ys: List[U])(f: (T, U) => T): List[T] =
def zipWithConserve[U, V <: T](ys: List[U])(f: (T, U) => V): List[V] =
if (xs.isEmpty || ys.isEmpty) Nil
else {
val x1 = f(xs.head, ys.head)
val xs1 = xs.tail.zipWithConserve(ys.tail)(f)
if ((x1.asInstanceOf[AnyRef] eq xs.head.asInstanceOf[AnyRef]) &&
(xs1 eq xs.tail)) xs
if (x1.asInstanceOf[AnyRef] eq xs.head.asInstanceOf[AnyRef]) && (xs1 eq xs.tail)
then xs.asInstanceOf[List[V]]
else x1 :: xs1
}

Expand Down
4 changes: 1 addition & 3 deletions compiler/src/dotty/tools/dotc/core/Signature.scala
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,7 @@ case class Signature(paramsSig: List[ParamSig], resSig: TypeName) {
else if (!this.paramsSig.hasSameLengthAs(that.paramsSig)) that
else {
val mapped = Signature(
// DOTTY: we shouldn't have to explicitly pass a type argument to `update`,
// see https://github.com/lampepfl/dotty/issues/4867
this.paramsSig.zipWithConserve(that.paramsSig)(update[ParamSig]),
this.paramsSig.zipWithConserve(that.paramsSig)(update),
update(this.resSig, that.resSig))
if (mapped == this) this else mapped
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,7 @@ class TastyUnpickler(reader: TastyReader) {
val end = start + length
def readSignedRest(original: TermName, target: TermName): TermName =
val result = readName().toTypeName
// DOTTY: we shouldn't have to give an explicit type to paramsSig,
// see https://github.com/lampepfl/dotty/issues/4867
val paramsSig: List[Signature.ParamSig] = until(end)(readParamSig())
val paramsSig = until(end)(readParamSig())
val sig = Signature(paramsSig, result)
SignedName(original, sig, target)

Expand Down
1 change: 0 additions & 1 deletion compiler/src/dotty/tools/dotc/transform/Erasure.scala
Original file line number Diff line number Diff line change
Expand Up @@ -825,7 +825,6 @@ object Erasure {

val args0 = outers ::: ownArgs
val args1 = args0.zipWithConserve(xmt.paramInfos)(typedExpr)
.asInstanceOf[List[Tree]]

def mkApply(finalFun: Tree, finalArgs: List[Tree]) =
val app = untpd.cpy.Apply(tree)(finalFun, finalArgs)
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/typer/ReTyper.scala
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ class ReTyper(nestingLevel: Int = 0) extends Typer(nestingLevel) with ReChecking

override def handleUnexpectedFunType(tree: untpd.Apply, fun: Tree)(using Context): Tree = fun.tpe match {
case mt: MethodType =>
val args: List[Tree] = tree.args.zipWithConserve(mt.paramInfos)(typedExpr(_, _)).asInstanceOf[List[Tree]]
val args: List[Tree] = tree.args.zipWithConserve(mt.paramInfos)(typedExpr)
assignType(untpd.cpy.Apply(tree)(fun, args), fun, args)
case _ =>
super.handleUnexpectedFunType(tree, fun)
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1969,7 +1969,7 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
}
else desugaredArg.withType(UnspecifiedErrorType)
}
args.zipWithConserve(tparams)(typedArg(_, _)).asInstanceOf[List[Tree]]
args.zipWithConserve(tparams)(typedArg)
}
val paramBounds = tparams.lazyZip(args).map {
case (tparam, untpd.WildcardTypeBoundsTree()) =>
Expand Down