scala/scala3

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

satorg opened this issue · 3 comments

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.

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

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.