Skip to content

false-positive "unused import" warning when importing givens defined in common trait for several objects #19657

Closed
@danielleontiev

Description

@danielleontiev

Compiler version

Scala 3.3.1, JVM 17

Minimized code

bug.sc
//> using scala 3.3.1
//> using options "-Wunused:imports"

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]) = ()

import FooCodec.given
import BarCodec.given

summonSchemas

Run code with

scala-cli run bug.sc

Output

[warn] ./bug.sc:18:17
[warn] unused import
[warn] import FooCodec.given
[warn]                 ^^^^^

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 expected

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

trait Schema[A]

case class Foo()
case class Bar()

object FooCodec {
  given Schema[Foo] = new Schema[Foo] {}
}
object BarCodec {
  given Schema[Bar] = new Schema[Bar] {}
}

def summonSchemas(using Schema[Foo], Schema[Bar]) = ()

import FooCodec.given
import BarCodec.given

summonSchemas

compiles without warning message

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions