-
Notifications
You must be signed in to change notification settings - Fork 1.1k
false-positive "unused import" warning when importing givens defined in common trait for several objects #19657
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
Comments
Reproduced on the Minimised code in non-script format (to be passed straight to the compiler) trait Schema[A]
case class Foo()
case class Bar()
trait SchemaGenerator[A] {
given Schema[A] = new Schema[A]{}
}
object FooCodec extends SchemaGenerator[Foo]
object BarCodec extends SchemaGenerator[Bar]
def summonSchemas(using Schema[Foo], Schema[Bar]) = ()
@main def main = {
import FooCodec.given
import BarCodec.given
summonSchemas
} output: scalac -d . -Wunused:imports repro.scala
-- Warning: repro.scala:17:18 --------------------------------------------------
17 | import BarCodec.given
| ^^^^^
| unused import
1 warning found |
This error is very annoying. It basically prevents us from using given imports with |
@grzegorz-bielski
It seems to avoid warning about wildcard import selectors which may reference any implicit.
I don't think optional warnings (especially this one) are intended to invite architectural rewrites. |
@som-snytt I didn't know about
My thoughts exactly! However, this was the only workaround I could think of at the time that did not involve the removal of |
Waiting on #20321 which obviates my working branch. |
…20894) This PR changes the `CheckUnused` phase to rely on the `MiniPhase` API (instead of custom traversal). That improves fidelity to `Context` (instead of approximate scoping). The phase should work seamlessly with subsequent linting phases (currently, `CheckShadowed`). It is a goal of the PR to eliminate false reports. It is also a goal not to regress previous work on efficiency. A remaining limitation of the current approach is that contexts don't provide a nesting level. Practically, this means that for a wildcard import nested below a higher precedence named import, the wildcard is deemed "unused". (A more general tool for "managing" or "formatting" imports could do more to pick a preferred scope.) This PR adds `-Wunused:patvars`, as forward-ported from Scala 2: it relies on attachments for some details about desugaring, but otherwise uses positions (where only the original patvar has a non-synthetic position). As in Scala 2, it does not warn about patvars with the "canonical" name of a case class element (this is complicated by type tests and the quotes API); other exclusions are to be ported, such as "name derived from the match selector". Support is added for `-Wconf:origin=full.path.selector`, as in Scala 2. That allows, for example: ``` -Wconf:origin=scala.util.chaining.given:s ``` to exclude certain blessed imports from warnings, or to work around false positives (should they arise). Support is added to `-rewrite` unused imports. There are no options to "format"; instead, textual deletions preserve existing formatting, except that blank lines are removed and braces removed when there is only one selector. Notable fixes are to support `compiletime` and `inline`; there are more fixes to pursue in this area. Fixes #19657 Fixes #20520 Fixes #19998 Fixes #18313 Fixes #17371 Fixes #18708 Fixes #21917 Fixes #21420 Fixes #20951 Fixes #19252 Fixes #18289 Fixes #17667 Fixes #17252 Fixes #21807 Fixes #17753 Fixes #17318 Fixes #18564 Fixes #22376 Fixes #21525
Compiler version
Scala 3.3.1, JVM 17
Minimized code
Run code with
Output
Expectation
Warning should not be reported
Comment
It seems that some sort of names collision occurs due to the common
SchemaGenerator
trait because defining givens explicitly works as expectedcompiles without warning message
The text was updated successfully, but these errors were encountered: