scala / scala3

The Scala 3 compiler, also known as Dotty.
https://dotty.epfl.ch
Apache License 2.0
5.8k stars 1.05k forks source link

Regression for `-Wunused:import` in `business4s/decisions4s` #21420

Open WojciechMazur opened 3 weeks ago

WojciechMazur commented 3 weeks ago

Compiler version

3.5.0 and later Bisect points to 975df4a136910fe0451514ef0dc34aa29a60aef7

Minimized code

//> using options -Wunused:imports -Werror
object decisions4s{
  trait HKD
  trait DecisionTable
}

object DiagnosticsExample {
  import decisions4s.HKD
  case class Input[F[_]]() extends HKD
  import decisions4s.*
  val decisionTable: DecisionTable = ??? // warn
}

Output

[warn] ./test.scala:8:22
[warn] unused import
[warn]   import decisions4s.HKD
[warn]                

Expectation

Should not report warnings if wildcard import is placed after usages of explicit import statement.

som-snytt commented 2 weeks ago

I have an improvement PR and also a branch for a rewrite of the unused check, where I noticed that the scoping in "super position" is not correct:

object Constants:
  final val i = 42

class `scope of super`:
  import Constants.i // bad warn
  class C(x: Int):
    def y = x
  class D extends C(i):
    import Constants.* // does not resolve i in C(i)
    def m = i

confirming with current scalacQ

➜  snips ~/projects/dotty/bin/scalacQ -d /tmp/sandbox -Wunused:imports import-scope.scala
-- [E198] Unused Symbol Warning: import-scope.scala:5:19 -----------------------
5 |  import Constants.i // bad warn
  |                   ^
  |                   unused import
1 warning found

I don't think the crude scoping in the check could ever quite work; if it is a regression here in the May improvements, I assume that is because more is correctly detected.

By "crude", I mean that typer contexts are approximated.

I noticed it (on my rewrite branch, which doesn't go far) by turning on the lint in the dotty build, i.e., dogfooding.