Skip to content
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

Remove Importee.Rename if it does not shadow anything #923

Merged

Conversation

marcelocenerine
Copy link
Contributor

@marcelocenerine marcelocenerine commented Dec 16, 2018

Fixes #915

/cc @hepin1989

UPDATE: I tested the fix on the akka repo after reverting the changes to scala files in this commit: akka/akka@e9fb3a0. These are the only diffs:

diff --git a/akka-bench-jmh/src/main/scala/akka/actor/typed/TypedBenchmarkActors.scala b/akka-bench-jmh/src/main/scala/akka/actor/typed/TypedBenchmarkActors.scala
index 8f0842359a..203c1d70a0 100644
--- a/akka-bench-jmh/src/main/scala/akka/actor/typed/TypedBenchmarkActors.scala
+++ b/akka-bench-jmh/src/main/scala/akka/actor/typed/TypedBenchmarkActors.scala
@@ -6,7 +6,6 @@ package akka.actor.typed

 import akka.Done
 import akka.actor.typed.scaladsl.Behaviors
-import akka.actor.typed.scaladsl.{ ActorContext ⇒ _ }

 import scala.concurrent.duration._

diff --git a/akka-cluster-sharding-typed/src/main/scala/akka/cluster/sharding/typed/scaladsl/ClusterSharding.scala b/akka-cluster-sharding-typed/src/main/scala/akka/cluster/sharding/typed/scaladsl/ClusterSharding.scala
index 890c85e7ff..4e6d93e23d 100644
--- a/akka-cluster-sharding-typed/src/main/scala/akka/cluster/sharding/typed/scaladsl/ClusterSharding.scala
+++ b/akka-cluster-sharding-typed/src/main/scala/akka/cluster/sharding/typed/scaladsl/ClusterSharding.scala
@@ -7,7 +7,6 @@ package scaladsl

 import scala.concurrent.Future

-import scala.collection.{ immutable ⇒ _ }
 import scala.reflect.ClassTag

 import akka.util.Timeout
diff --git a/akka-cluster-typed/src/main/scala/akka/cluster/ddata/typed/scaladsl/DistributedData.scala b/akka-cluster-typed/src/main/scala/akka/cluster/ddata/typed/scaladsl/DistributedData.scala
index 8905fcf26a..ed5dfadda8 100644
--- a/akka-cluster-typed/src/main/scala/akka/cluster/ddata/typed/scaladsl/DistributedData.scala
+++ b/akka-cluster-typed/src/main/scala/akka/cluster/ddata/typed/scaladsl/DistributedData.scala
@@ -11,7 +11,6 @@ import akka.actor.typed.ActorRef
 import akka.actor.ExtendedActorSystem
 import akka.actor.typed.Props
 import akka.cluster.{ ddata ⇒ dd }
-import akka.cluster.typed.{ Cluster ⇒ _ }
 import akka.cluster.ddata.SelfUniqueAddress

 object DistributedData extends ExtensionId[DistributedData] {

fix(doc.tree, unusedTermsPosition.toSet, unusedImportsPosition.toSet)
}
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Algorithm:

  • traverses the tree using depth-first traversal
  • for each enclosing scope (source, package, object, class, def, block, ...)
    • any used import wildcards AND unused import renames are tracked and passed down into nested scopes following them
    • any other unused import gets removed
    • at the end of each scope
      • any unused import rename (from outer our current scope) whose package is the same as any wildcard import in scope (outer or current) gets unimported
      • any remaining unused import rename belonging exclusively to the current scope (not the outer) gets removed

Copy link
Contributor

Choose a reason for hiding this comment

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

👍 would be good to include this description in the comments

@marcelocenerine marcelocenerine force-pushed the fix_rename_unused_import2 branch 2 times, most recently from 77d72f3 to 5ad04a2 Compare December 16, 2018 12:42
Copy link
Contributor

@olafurpg olafurpg left a comment

Choose a reason for hiding this comment

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

Thank you @marcelocenerine! Great job with the thorough test suite and evaluation 👍

I am a bit concerned about the increased complexity from this change. I am wondering if we can achieve the same goal while keeping a stateless tree traverser. I took a stab at this issue here master...olafurpg:fix_rename_unused_import2

In short, we add a new guard for the "unimport rename" case to only unimport renames when they're not part of an importer with a used wildcard.

          val isUsedWildcard = importer.importees.exists {
            case i: Importee.Wildcard => !isUnusedImport(importPosition(i))
            case _ => false
          }
          importer.importees.collect {
            case i @ Importee.Rename(_, to)
                if isUsedWildcard &&
                  isUnusedImport(importPosition(i)) => // unimport rename

This change caused several imports to get removed from the test suite. Consider the following case

bash-3.2$ cat err.scala
import scala.util._
import scala.util.{Success => S}

object A {
  Success(1)
}
bash-3.2$ scalac -Ywarn-unused err.scala
err.scala:2: warning: Unused import
import scala.util.{Success => S}
                   ^
one warning found

I think we are save to remove the rename because Success is anyways brought in through the wildcard import.

fix(doc.tree, unusedTermsPosition.toSet, unusedImportsPosition.toSet)
}
}

Copy link
Contributor

Choose a reason for hiding this comment

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

👍 would be good to include this description in the comments

@marcelocenerine
Copy link
Contributor Author

I am a bit concerned about the increased complexity from this change

Indeed it adds some complexity to handle the case where the rename and wildcard are in different import clauses. If the original ask (#614) was only about unused renames in the same import as the wildcard, then your suggestion is good enough.

@marcelocenerine
Copy link
Contributor Author

@olafurpg do you prefer the simpler approach and restrict the fix for the original issue (#614) only to unused renames in the same import as the wildcard?

@olafurpg
Copy link
Contributor

I think the simple solution fixes also #915

In the diff here 38db60c#diff-5a5912bc5da72f0e5da63e0919c8d2d0 for example it removes import scala.io.StdIn.{readByte => _} imports

Copy link
Contributor

@olafurpg olafurpg left a comment

Choose a reason for hiding this comment

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

Can we add back the test suite you wrote in the original PR? It was very thorough and I think it can be helpful to catch regressions in the future. Otherwise, LGTM 👍

I'm sorry for the slow response!

Copy link
Contributor

@olafurpg olafurpg left a comment

Choose a reason for hiding this comment

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

Can we add back the test suite you wrote in the original PR? It was very thorough and I think it can be helpful to catch regressions in the future. Otherwise, LGTM 👍

I'm sorry for the slow response!

Copy link
Contributor

@olafurpg olafurpg left a comment

Choose a reason for hiding this comment

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

Can we add back the test suite you wrote in the original PR? It was very thorough and I think it can be helpful to catch regressions in the future. Otherwise, LGTM 👍

I'm sorry for the slow response!

@marcelocenerine
Copy link
Contributor Author

Hey @olafurpg, many of those test cases were more relevant to the original algorithm. I added most of them back, except the ones related to scoping. The new implementation doesn't care about scopes

Copy link
Contributor

@olafurpg olafurpg left a comment

Choose a reason for hiding this comment

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

LGTM 👍 Thank you @marcelocenerine !

@dwijnand
Copy link
Contributor

Thanks, @marcelocenerine!

@marcelocenerine marcelocenerine deleted the fix_rename_unused_import2 branch November 23, 2023 16:43
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.

error fix:unused import
3 participants