Skip to content

Make the breakOut rewrite rule cross compatible (fix #80) (fix #93) #102

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
Jul 23, 2018

Conversation

MasseGuillaume
Copy link
Contributor

No description provided.

@MasseGuillaume MasseGuillaume changed the title Cross breakout Make the breakOut rewrite rule cross compatible (fix #80) Jul 18, 2018

val patchSpecificCollection =
toCollection match {
case "scala.collection.immutable.Map" => ctx.addRight(ap0, ".toMap")
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@julienrf do you have more cases in mind where xs.to(C) is not cross compatible?

Copy link
Contributor

Choose a reason for hiding this comment

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

Any type constructor which has a kind different from * -> *. For instance, SortedMap, TreeMap, HashMap.

However, there are no toSortedMap or toTreeMap methods that we can use…

Copy link
Contributor Author

Choose a reason for hiding this comment

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

is it safe to cast to the target collection?

Copy link
Contributor

Choose a reason for hiding this comment

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

No.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

also: IntMap

patchSpecificCollection
}

def extractColFromBreakout(breakout: Tree): Term = {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is the trick I found.

import scala.reflect.runtime.universe._
reify { List(1).map(identity)(scala.collection.breakOut): Set[Int] }


List.apply(1).map(identity)(breakOut(Set.canBuildFrom)): Set[Int]
//                                   ^^^ available in synthetics

@MasseGuillaume
Copy link
Contributor Author

CI should be green, we are getting the same Scala.js runner error: https://travis-ci.org/scala/scala-collection-compat/jobs/405370378#L637

@sjrd does this stack trace ring a bell?

[info] Fast optimizing /home/travis/build/scala/scala-collection-compat/compat/.js/target/scala-2.11/scala-collection-compat-test-fastopt.js
java.lang.NullPointerException
	at org.scalajs.jsenv.nodejs.AbstractNodeJSEnv$NodeComJSRunner$class.send(AbstractNodeJSEnv.scala:247)
	at org.scalajs.jsenv.nodejs.NodeJSEnv$ComNodeRunner.send(NodeJSEnv.scala:70)
	at org.scalajs.testadapter.ComJSEnvRPC.send(ComJSEnvRPC.scala:67)
	at org.scalajs.testcommon.RPCCore.call(RPCCore.scala:137)
	at org.scalajs.testadapter.TestAdapter.loadFrameworks(TestAdapter.scala:53)
	at org.scalajs.sbtplugin.ScalaJSPluginInternal$$anonfun$79.apply(ScalaJSPluginInternal.scala:1012)
	at org.scalajs.sbtplugin.ScalaJSPluginInternal$$anonfun$79.apply(ScalaJSPluginInternal.scala:986)
	at scala.Function1$$anonfun$compose$1.apply(Function1.scala:47)
	at sbt.$tilde$greater$$anonfun$$u2219$1.apply(TypeFunctions.scala:40)
	at sbt.std.Transform$$anon$4.work(System.scala:63)
	at sbt.Execute$$anonfun$submit$1$$anonfun$apply$1.apply(Execute.scala:228)
	at sbt.Execute$$anonfun$submit$1$$anonfun$apply$1.apply(Execute.scala:228)
	at sbt.ErrorHandling$.wideConvert(ErrorHandling.scala:17)
	at sbt.Execute.work(Execute.scala:237)
	at sbt.Execute$$anonfun$submit$1.apply(Execute.scala:228)
	at sbt.Execute$$anonfun$submit$1.apply(Execute.scala:228)
	at sbt.ConcurrentRestrictions$$anon$4$$anonfun$1.apply(ConcurrentRestrictions.scala:159)
	at sbt.CompletionService$$anon$2.call(CompletionService.scala:28)
	at java.util.concurrent.FutureTask.run(FutureTask.java:262)
	at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:473)
	at java.util.concurrent.FutureTask.run(FutureTask.java:262)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1152)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:622)
	at java.lang.Thread.run(Thread.java:748)
[error] (compatJS/test:loadedTestFrameworks) java.lang.NullPointerException
[error] Total time: 35 s, completed Jul 18, 2018 2:05:32 PM

@sjrd
Copy link
Member

sjrd commented Jul 18, 2018

Looks like scala-js/scala-js#3408
Try restarting the build, as it is a race condition which only happens every once in a while.

@MasseGuillaume MasseGuillaume changed the title Make the breakOut rewrite rule cross compatible (fix #80) Make the breakOut rewrite rule cross compatible (fix #80) (fix #93) Jul 19, 2018

val `TraversableLike ++` = Symbol("_root_.scala.collection.TraversableLike#`++`(Lscala/collection/GenTraversableOnce;Lscala/collection/generic/CanBuildFrom;)Ljava/lang/Object;.")
val `Vector ++` = Symbol("_root_.scala.collection.immutable.Vector#`++`(Lscala/collection/GenTraversableOnce;Lscala/collection/generic/CanBuildFrom;)Ljava/lang/Object;.")
val `List ++` = Symbol("_root_.scala.collection.immutable.List#`++`(Lscala/collection/GenTraversableOnce;Lscala/collection/generic/CanBuildFrom;)Ljava/lang/Object;.")
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This should only be TraversableLike. The way symbols work in scalafix right now is similar to go-to-definition in an IDE, It will hit the most concrete class first. @olafurpg Is there a way only to hit the base class?

// == rule ==
def apply(ctx: RuleCtx): Patch = {

var requiresCompatImport = false
Copy link
Contributor Author

Choose a reason for hiding this comment

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

if we only use .toMap we don't need the compat import.

@MasseGuillaume MasseGuillaume force-pushed the cross-breakout branch 2 times, most recently from ab8f965 to b8c613a Compare July 19, 2018 17:23
MasseGuillaume added a commit to MasseGuillaume/akka that referenced this pull request Jul 19, 2018
@MasseGuillaume
Copy link
Contributor Author

I just successfully removed breakout from Akka: akka/akka@3be0535

@MasseGuillaume
Copy link
Contributor Author

@julienrf this one is good to go. CI is green, works on akka codebase.

import scala.collection.{immutable => i, mutable => m}

class TraversableOnceKVExtensionMethods[K, V](private val self: TraversableOnce[(K, V)]) extends AnyVal {
def toImmutableSortedMap(implicit ordering: Ordering[K]): i.SortedMap[K, V] = {
Copy link
Contributor

Choose a reason for hiding this comment

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

I’m on the fence. I think we should only try to provide the 2.13 syntax on 2.11 and 2.12, but we shouldn’t try to introduce new operations. Why can’t to(immutable.SortedMap) cross-compile?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Because to returns Coll[A], in the case of SortedMap it returns a Iterable[(K, V)].

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, I remember this one. Then I guess we have to write immutable.SortedMap.from(List(1 -> "1").map(x => x))

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is not cross-compatible.

Copy link
Contributor

Choose a reason for hiding this comment

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

It should be, with import scala.collection.compat._.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Some ToCBF are missing, I will implement them and fix: #103

Copy link
Contributor

Choose a reason for hiding this comment

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

There should be no CBF instance involved in that code. We should just enrich the immutable.SortedMap object with a from operation, like we previously did: https://github.com/scala/scala-collection-compat/blob/master/compat/src/main/scala-2.11_2.12/scala/collection/compat/package.scala#L36

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I see this is a good idea!

Copy link
Contributor Author

@MasseGuillaume MasseGuillaume Jul 21, 2018

Choose a reason for hiding this comment

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

This does not create an intermediate collection?

  implicit class IterableFactoryExtensionMethods[CC[X] <: GenTraversable[X]](private val fact: GenericCompanion[CC]) {
    def from[A](source: TraversableOnce[A]): CC[A] = fact.apply(source.toSeq: _*)
  }

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, the .toSeq call does create an intermediate collection :-/

@Test
def t(): Unit = {

}
Copy link
Contributor

Choose a reason for hiding this comment

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

What’s the purpose of this test?

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 empty test you can use as a sandbox. I keep finding myself to add it back locally, It's useful to have it here.

Copy link
Contributor

Choose a reason for hiding this comment

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

I find this a bit weird. Can’t you just have it locally if you only use it during development?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Can we keep it, it would save me some time when I want to test something quick or if I need to create another unit test.

Copy link
Contributor

Choose a reason for hiding this comment

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

I don’t understand why you can’t keep it locally only.


object Playground {

}
Copy link
Contributor

Choose a reason for hiding this comment

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

I guess this is an oversight?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ditto, it's useful.

Copy link
Contributor

Choose a reason for hiding this comment

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

What is this file testing?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@MasseGuillaume MasseGuillaume force-pushed the cross-breakout branch 3 times, most recently from cd3f48e to df2f4a2 Compare July 23, 2018 08:44
@MasseGuillaume
Copy link
Contributor Author

@julienrf ready for round 2.

Copy link
Contributor

@julienrf julienrf left a comment

Choose a reason for hiding this comment

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

Awesome, thanks!

@@ -1,25 +1,108 @@
/*
rule = "scala:fix.NewCollections"
rule = "scala:fix.CrossCompat"
Copy link
Contributor

Choose a reason for hiding this comment

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

👍

@julienrf julienrf merged commit c2bbd2d into scala:master Jul 23, 2018
@MasseGuillaume MasseGuillaume deleted the cross-breakout branch July 23, 2018 11:34
martijnhoekstra pushed a commit to martijnhoekstra/scala-collection-compat that referenced this pull request Nov 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants