scala / scala3

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

Renaming `specialized` on import results in "unused import" warnings #20536

Open satorg opened 1 month ago

satorg commented 1 month ago

Compiler version

v3.3.3, v3.4.2

Minimized code

import scala.{specialized => sp}

trait ImportUnusedSpecializedRename[@sp Int]

Script that I used for testing:

set -v

scala-cli clean ImportUnusedSpecializedRename.scala

scala-cli compile --server=false -j 17 -S 2.12.19 -Xlint:unused ImportUnusedSpecializedRename.scala
scala-cli compile --server=false -j 17 -S 2.13.14 -Wunused ImportUnusedSpecializedRename.scala
scala-cli compile --server=false -j 17 -S 3.3.3 -Wunused:imports ImportUnusedSpecializedRename.scala
scala-cli compile --server=false -j 17 -S 3.4.2 -Wunused:imports ImportUnusedSpecializedRename.scala

Output

scala-cli clean ImportUnusedSpecializedRename.scala

scala-cli compile --server=false -j 17 -S 2.12.19 -Xlint:unused ImportUnusedSpecializedRename.scala
scala-cli compile --server=false -j 17 -S 2.13.14 -Wunused ImportUnusedSpecializedRename.scala
scala-cli compile --server=false -j 17 -S 3.3.3 -Wunused:imports ImportUnusedSpecializedRename.scala
-- Warning: /Users/.../ImportUnusedSpecializedRename.scala:1:26 
1 |import scala.{specialized => sp}
  |              ^^^^^^^^^^^^^^^^^
  |              unused import
1 warning found
scala-cli compile --server=false -j 17 -S 3.4.2 -Wunused:imports ImportUnusedSpecializedRename.scala
-- Warning: /Users/.../ImportUnusedSpecializedRename.scala:1:26 
1 |import scala.{specialized => sp}
  |              ^^^^^^^^^^^^^^^^^
  |              unused import
1 warning found

Expectation

Scala3 should compile the above example without the "unused import" warnings. As long as Scala2 compilers can do.

Additional Information

This issues causes a lot of extra warnings added up while compiling Cats for Scala3. Historically, Cats uses import scala.{specialized => sp} in a bunch of files across the repository.

som-snytt commented 3 weeks ago

The Scala 3 check was intentionally skipping the type parameter, but inadvertently skipping the annotation.

The rename was not germane.

Worth adding that Scala 2:

$ scalac -d /tmp/sandbox -Xlint i20536.scala
i20536.scala:3: warning: type parameter Int defined in trait ImportUnusedSpecializedRename shadows class Int defined in package scala. You may want to rename your type parameter, or possibly remove it.
trait ImportUnusedSpecializedRename[@sp Int]
                                        ^
1 warning

Scala 3

$ scalac -d /tmp/sandbox -Wshadow:all i20536.scala
-- Warning: i20536.scala:3:40 ------------------------------------------------------------------------------------------
3 |trait ImportUnusedSpecializedRename[@sp Int]
  |                                    ^^^^^^^
  | Type parameter Int for trait ImportUnusedSpecializedRename shadows the type defined by class Int in package scala
1 warning found
satorg commented 3 weeks ago

Ah, sorry, I overworked it a little. In Cats it is a generic A, not Int: https://github.com/typelevel/cats/blob/90db3cd0269cf2b621ad6460ded594aee2e6a135/kernel/src/main/scala/cats/kernel/Enumerable.scala#L25-L34

som-snytt commented 3 weeks ago

I didn't intend to rub it in. My point was going to be that scala 2 will lint that, but then I looked and scala 3 does it now too. I'll have to find some other clever thing to point out.