Skip to content

Rewrite: breakOut (fix #46) #70

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
Jun 29, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
25 changes: 25 additions & 0 deletions scalafix/input/src/main/scala/fix/BreakoutSrc.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
rule = "scala:fix.Scalacollectioncompat_newcollections"
*/
package fix

import scala.collection.breakOut

object BreakoutSrc {
val xs = List(1, 2, 3)

xs.collect{ case x => x }(breakOut): Set[Int]
xs.flatMap(x => List(x))(breakOut): Set[Int]
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 also try with a sorted result? SortedSet[Int] for instance.

xs.map(_ + 1)(breakOut): Set[Int]
xs.reverseMap(_ + 1)(breakOut): Set[Int]
xs.scanLeft(0)((a, b) => a + b)(breakOut): Set[Int]
xs.union(xs)(breakOut): Set[Int]
xs.updated(0, 1)(breakOut): Set[Int]
xs.zip(xs)(breakOut): Array[(Int, Int)]
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 try with a Map[Int, Int]?

xs.zipAll(xs, 0, 0)(breakOut): Array[(Int, Int)]

(xs ++ xs)(breakOut): Set[Int]
(1 +: xs)(breakOut): Set[Int]
(xs :+ 1)(breakOut): Set[Int]
(xs ++: xs)(breakOut): Set[Int]
}
24 changes: 24 additions & 0 deletions scalafix/output/src/main/scala/fix/BreakoutSrc.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@



package fix


object BreakoutSrc {
val xs = List(1, 2, 3)

xs.iterator.collect{ case x => x }.to(implicitly): Set[Int]
xs.iterator.flatMap(x => List(x)).to(implicitly): Set[Int]
xs.iterator.map(_ + 1).to(implicitly): Set[Int]
xs.reverseIterator.map(_ + 1).to(implicitly): Set[Int]
xs.iterator.scanLeft(0)((a, b) => a + b).to(implicitly): Set[Int]
xs.iterator.concat(xs).to(implicitly): Set[Int]
xs.view.updated(0, 1).to(implicitly): Set[Int]
xs.iterator.zip(xs).to(implicitly): Array[(Int, Int)]
xs.iterator.zipAll(xs, 0, 0).to(implicitly): Array[(Int, Int)]

(xs.iterator ++ xs).to(implicitly): Set[Int]
(1 +: xs.view).to(implicitly): Set[Int]
(xs.view :+ 1).to(implicitly): Set[Int]
(xs ++: xs.view).to(implicitly): Set[Int]
}
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,60 @@ case class Scalacollectioncompat_newcollections(index: SemanticdbIndex)
Symbol("_root_.scala.collection.mutable.ArrayBuilder.make(Lscala/reflect/ClassTag;)Lscala/collection/mutable/ArrayBuilder;.")
)

object Breakout {
implicit class RichSymbol(val symbol: Symbol) {
def exact(tree: Tree)(implicit index: SemanticdbIndex): Boolean =
index.symbol(tree).fold(false)(_ == symbol)
}

val breakOut = SymbolMatcher.exact(Symbol("_root_.scala.collection.package.breakOut(Lscala/collection/generic/CanBuildFrom;)Lscala/collection/generic/CanBuildFrom;."))

// infix operators
val `List ++` = Symbol("_root_.scala.collection.immutable.List#`++`(Lscala/collection/GenTraversableOnce;Lscala/collection/generic/CanBuildFrom;)Ljava/lang/Object;.")
val `List +:` = Symbol("_root_.scala.collection.immutable.List#`+:`(Ljava/lang/Object;Lscala/collection/generic/CanBuildFrom;)Ljava/lang/Object;.")
val `SeqLike :+` = Symbol("_root_.scala.collection.SeqLike#`:+`(Ljava/lang/Object;Lscala/collection/generic/CanBuildFrom;)Ljava/lang/Object;.")
val `TraversableLike ++:` = Symbol("_root_.scala.collection.TraversableLike#`++:`(Lscala/collection/Traversable;Lscala/collection/generic/CanBuildFrom;)Ljava/lang/Object;.")

val operatorsIteratorSymbols = List(`List ++`)
val operatorsViewSymbols = List(`List +:`, `SeqLike :+`, `TraversableLike ++:`)
val operatorsSymbols = operatorsViewSymbols ++ operatorsIteratorSymbols

val operatorsIterator = SymbolMatcher.exact(operatorsIteratorSymbols: _*)
val operatorsView = SymbolMatcher.exact(operatorsViewSymbols: _*)
val operators = SymbolMatcher.exact(operatorsSymbols: _*)

// select
val `List.collect` = Symbol("_root_.scala.collection.immutable.List#collect(Lscala/PartialFunction;Lscala/collection/generic/CanBuildFrom;)Ljava/lang/Object;.")
val `List.flatMap` = Symbol("_root_.scala.collection.immutable.List#flatMap(Lscala/Function1;Lscala/collection/generic/CanBuildFrom;)Ljava/lang/Object;.")
val `List.map` = Symbol("_root_.scala.collection.immutable.List#map(Lscala/Function1;Lscala/collection/generic/CanBuildFrom;)Ljava/lang/Object;.")
val `IterableLike.zip` = Symbol("_root_.scala.collection.IterableLike#zip(Lscala/collection/GenIterable;Lscala/collection/generic/CanBuildFrom;)Ljava/lang/Object;.")
val `IterableLike.zipAll` = Symbol("_root_.scala.collection.IterableLike#zipAll(Lscala/collection/GenIterable;Ljava/lang/Object;Ljava/lang/Object;Lscala/collection/generic/CanBuildFrom;)Ljava/lang/Object;.")
val `SeqLike.union` = Symbol("_root_.scala.collection.SeqLike#union(Lscala/collection/GenSeq;Lscala/collection/generic/CanBuildFrom;)Ljava/lang/Object;.")
val `SeqLike.updated` = Symbol("_root_.scala.collection.SeqLike#updated(ILjava/lang/Object;Lscala/collection/generic/CanBuildFrom;)Ljava/lang/Object;.")
val `SeqLike.reverseMap` = Symbol("_root_.scala.collection.SeqLike#reverseMap(Lscala/Function1;Lscala/collection/generic/CanBuildFrom;)Ljava/lang/Object;.")

val functionsIteratorSymbols = List(`List.collect`, `List.flatMap`, `List.map`, `IterableLike.zip`, `IterableLike.zipAll`, `SeqLike.union`)
val functionsViewSymbols = List(`SeqLike.updated`)
val functionsReverseIteratorSymbols = List(`SeqLike.reverseMap`)
val functionsSymbols = functionsIteratorSymbols ++ functionsViewSymbols ++ functionsReverseIteratorSymbols

val functionsIterator = SymbolMatcher.exact(functionsIteratorSymbols: _*)
val functionsReverseIterator = SymbolMatcher.exact(functionsReverseIteratorSymbols: _*)
val functionsView = SymbolMatcher.exact(functionsViewSymbols: _*)
val functions = SymbolMatcher.exact(functionsSymbols: _*)

// special select

// iterator
val `TraversableLike.scanLeft` = SymbolMatcher.exact(Symbol("_root_.scala.collection.TraversableLike#scanLeft(Ljava/lang/Object;Lscala/Function2;Lscala/collection/generic/CanBuildFrom;)Ljava/lang/Object;."))

def isLeftAssociative(tree: Tree): Boolean =
tree match {
case Term.Name(value) => value.last != ':'
case _ => false
}
}

def startsWithParens(tree: Tree): Boolean =
tree.tokens.headOption.map(_.is[Token.LeftParen]).getOrElse(false)

Expand Down Expand Up @@ -500,6 +554,58 @@ case class Scalacollectioncompat_newcollections(index: SemanticdbIndex)
if (useSites.nonEmpty) useSites + imports
else Patch.empty
}

def replaceBreakout(ctx: RuleCtx): Patch = {
import Breakout._

def fixIt(intermediate: String, lhs: Term, ap: Term, breakout: Tree): Patch = {
ctx.addRight(lhs, "." + intermediate) +
ctx.addRight(ap, ".to") +
ctx.replaceTree(breakout, "implicitly")
}

ctx.tree.collect {
case i: Importee if breakOut.matches(i) =>
ctx.removeImportee(i)

case Term.Apply(ap @ Term.ApplyInfix(lhs, operators(op), _, List(rhs)), List(breakOut(bo))) =>
val subject =
if(isLeftAssociative(op)) lhs
else rhs

val intermediate =
op match {
case operatorsIterator(_) => "iterator"
case operatorsView(_) => "view"
case _ => throw new Exception("impossible")
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe add a comment referring to the definition of operatorSymbols, explaining why that makes that path impossible to reach?

}

fixIt(intermediate, subject, ap, bo)

case Term.Apply(ap @ Term.Apply(Term.Select(lhs, functions(op)), _), List(breakOut(bo))) =>
val intermediate =
op match {
case functionsIterator(_) => "iterator"
case functionsView(_) => "view"
case functionsReverseIterator(_) => "reverseIterator"
case _ => throw new Exception("impossible")
}

val replaceUnion =
if (`SeqLike.union`.exact(op)) ctx.replaceTree(op, "concat")
else Patch.empty

val isReversed = `SeqLike.reverseMap`.exact(op)
val replaceReverseMap =
if (isReversed) ctx.replaceTree(op, "map")
else Patch.empty

fixIt(intermediate, lhs, ap, bo) + replaceUnion + replaceReverseMap

case Term.Apply(ap @ Term.Apply(Term.Apply(Term.Select(lhs, `TraversableLike.scanLeft`(op)), _), _), List(breakOut(bo))) =>
fixIt("iterator", lhs, ap, bo)
}.asPatch
}

override def fix(ctx: RuleCtx): Patch = {
replaceCanBuildFrom(ctx) +
Expand All @@ -516,6 +622,7 @@ case class Scalacollectioncompat_newcollections(index: SemanticdbIndex)
replaceMutMapUpdated(ctx) +
replaceArrayBuilderMake(ctx) +
replaceIterableSameElements(ctx) +
replaceMapMapValues(ctx)
replaceMapMapValues(ctx) +
replaceBreakout(ctx)
}
}