Skip to content

-Wunused:imports falsely reports imported givens (for derivation) #18564

Closed
@MartinHH

Description

@MartinHH

Compiler version

3.3.1

Minimized code

//> using scala "3.3.1"
//> using option "-Wunused:imports"

import scala.compiletime.*
import scala.deriving.*

trait Foo

trait HasFoo[A]:
  /** true if A contains a Foo */
  val hasFoo: Boolean

// given instances that need to be imported to be in scope
object HasFooInstances:
  given defaultHasFoo[A]: HasFoo[A] with
    val hasFoo: Boolean = false
  given HasFoo[Foo] with
    val hasFoo: Boolean = true

object HasFooDeriving:

  inline private def tupleHasFoo[T <: Tuple]: Boolean =
    inline erasedValue[T] match
      case _: EmptyTuple =>
        false
      case _: (t *: ts) =>
        summonInline[HasFoo[t]].hasFoo || tupleHasFoo[ts]
  
  inline def deriveHasFoo[T](using p: Mirror.ProductOf[T]): HasFoo[T] =
    // falsely reported as unused even though it has influence on this code
    import HasFooInstances.given
    val pHasFoo = tupleHasFoo[p.MirroredElemTypes]
    new HasFoo[T]:
      val hasFoo: Boolean = pHasFoo

Output

[warn] ./HasFoo.scala:31:28
[warn] unused import
[warn]     import HasFooInstances.given
[warn]                            ^^^^^

Note:

  1. No warning is emitted if deriveHasFoo is called in the same file
  2. A warning still is emitted if deriveHasFoo is called in a different file

Expectation

No warning is emitted.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:lintingLinting warnings enabled with -W or -Xlintitype:bug

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions