-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Warn unused imports #14524
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
Warn unused imports #14524
Conversation
It doesn't like the mutation in Contexts:
|
It looks like Putting this at the top of my test is supposed to mean I don't have to jury-rig something with "tests that require a setting" or however that works:
|
Not my imagination that this works:
and
but
|
maybe I'll just |
The fixup edits are moved to #14533 The rewrite result didn't get manual edits so it could be re-run any time. That was mostly just a test. (Still managed a few conflicts, however.) |
2f618ed
to
185f193
Compare
Rebased and squashed, but I haven't made the test work under all regimes. This PR feels like a long time ago but it says just a fortnight. Edit: test should be neg-with-compiler
Edit: or maybe that is not enough to survive vulpix |
seems to be #14637 but I also haven't drilled into how vulpix is compiling it. Yes, got it to pass by removing Or, vulpix is not a great experience. I remember hating partest when it was half-baked.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see that the magic "source version" (e.g. 3.0
, future
, etc.) imports are currently always flagged as unused. Consider this snippet:
import language.future
class Baz[T]
def foo[T: Baz](x: T)(using Ordering[T]) = ???
val result = foo(42)(using Baz[Int])
$ scalac -Wunused:imports snippet.scala
-- Warning: snippet.scala:1:16 -----------------------------------
1 |import language.future
| ^^^^^^
| Unused import
but if I remove the language.future
import:
$ scalac -Wunused:imports snippet.scala
-- Error: snippet.scala:5:20 ---------------------------------------------------------------------------------------
5 |val result = foo(42)(using Baz[Int])
| ^^^^^^^^^^^^^^^^^^^^^^^
| not enough arguments for method foo: (implicit evidence$1: Baz[Int], x$2: Ordering[Int]): Nothing
IMO, the "source version" magic imports are distinct from the "feature" magic imports, and should always be considered as used.
@griggt Thanks, I see the test has Imports with corresponding compiler options are tricky. Do I want to see a warning for I see I had a commit for feature imports before I squashed. |
The issue with the test is weird. I don't see any reason that it needs to be in The symptoms lead me to believe it has something to do with parallelism, as it works when run singly using Try putting the test file in its own subdirectory under |
Hey, don't break my brain! The comment in the test says
Under neg-with, it only runs when bootstrapped, so that's correct. If vulpix is making me think too hard, the overhead isn't worth it. (Normally I enjoy testing puzzles like any other.) I see the language version code, so I will try to do something not bad. Actually, sourceVersion in file shadows compiler options, but language feature in options means redundant import is not checked. |
Ignore my previous theory on how to use vulpix. I'll look at what sbt command it's actually running and then try what you said.
It doesn't list multiple unfulfilled expectations on a line. |
I've looked into the test problem some more and I'm convinced the issue is shared state corruption when multiple compilations are running in parallel. I think the fix is something like this: diff --git a/compiler/src/dotty/tools/dotc/core/Contexts.scala b/compiler/src/dotty/tools/dotc/core/Contexts.scala
index a0ceddce9d..ea616b5843 100644
--- a/compiler/src/dotty/tools/dotc/core/Contexts.scala
+++ b/compiler/src/dotty/tools/dotc/core/Contexts.scala
@@ -60,7 +60,7 @@ object Contexts:
private val (notNullInfosLoc, store8) = store7.newLocation[List[NotNullInfo]]()
private val (importInfoLoc, store9) = store8.newLocation[ImportInfo | Null]()
private val (typeAssignerLoc, store10) = store9.newLocation[TypeAssigner](TypeAssigner)
- private val (usagesLoc, store11) = store10.newLocation[Usages](Usages())
+ private val (usagesLoc, store11) = store10.newLocation[Usages]()
private val initialStore = store11
@@ -837,6 +837,7 @@ object Contexts:
store = initialStore
.updated(settingsStateLoc, settingsGroup.defaultState)
.updated(notNullInfosLoc, Nil)
+ .updated(usagesLoc, Usages())
.updated(compilationUnitLoc, NoCompilationUnit)
searchHistory = new SearchRoot
gadt = EmptyGadtConstraint The test should be able to live in |
@griggt Thanks for the hint! |
Update includes all of @griggt suggestions. Intuitively, I'd like to know if my Similarly, masking imports are still ignored. Even if you track that something was masked, such imports can be useful as a defense mechanism.
Noting that it's much easier to add compiler options to test code as "directives" than touch the test rig; |
I'm currently running this through the community build ( I'll post the results when complete, but thus far it's definitely uncovered some issues. |
Rebased, but I haven't looked at the latest comments from May 11 or commit from May 6. It looks like the follow-up is for locating selectors to rewrite, handling language imports (including version), and cross-compilation (such as If a language flag is required under Scala 2 but not 3, I don't think it's a goal here to differentiate? or my previous suggestions was yet another |
e8ed694
to
55f2e25
Compare
55f2e25
to
86e293c
Compare
Tweaked the rewrite facility. It might be useful for projects who can't depend on scalafix, not that you can't depend on scalafix. It surely needs tweaks for weird multiline imports. |
2be561c
to
481d1f9
Compare
|
Handle language feature imports. Ignore language version imports. Skip Java sources. Support rewrite.
Reopen to mark import used after implicit ranking, preparatory to seeing if it works the same way on Scala 2. |
481d1f9
to
5d2ed53
Compare
5d2ed53
to
3bbd9e3
Compare
Not sure how the build diff, reverted in the last commit, caused the settings error |
For those who is also wondering why this one is closed, I believe the issue is superseded with |
Forward port the Scala 2 feature, tracking imports when contexts are created, and registering when selectors are used.
Some support for
-rewrite
to remove unused selectors (if there is only one import on the source line), disabled.