scala3 icon indicating copy to clipboard operation
scala3 copied to clipboard

False positive unused import of inline given needed by `derives`

Open aartigao opened this issue 6 months ago • 3 comments

I've checked other opened issues and it doesn't seem there's one like this (if that's the case, sorry and please close this).

Also I'm not sure since when the issue appears because we have excluded the unused import warnings in our projects. The minimizer, though, I tried with 3.7.1-RC1 and 3.nightly.

Compiler version

3.nightly

Minimized code

//> using scala 3.nightly
//> using dep com.github.plokhotnyuk.jsoniter-scala::jsoniter-scala-core:2.36.0
//> using dep io.github.kitlangton::neotype-jsoniter:0.3.24
//> using options -Wunused:imports

import com.github.plokhotnyuk.jsoniter_scala.core.*
import com.github.plokhotnyuk.jsoniter_scala.macros.*
import neotype.*
import neotype.interop.jsoniter.given

type Bar = Bar.Type
object Bar extends Newtype[Long]

case class Foo(bar: Bar, bars: List[Bar]) derives ConfiguredJsonValueCodec

object JsonExample:

  def main(args: Array[String]): Unit =
    val foo = Foo(Bar(42), List(Bar(1), Bar(2)))
    val jsonString = writeToString(foo)
    println(s"Serialized JSON: $jsonString")

    val parsedFoo = readFromString[Foo](jsonString)
    println(s"Deserialized object: $parsedFoo")

Output

[warn] ./issue.sc:9:33
[warn] unused import
[warn] import neotype.interop.jsoniter.given
[warn]   

Expectation

No warn is emitted as this import is needed by Jsoniter derives.

The import, when removed as suggested by the compiler, makes the compilation fail:

[error] ./issue.sc:14:51
[error] No implicit 'com.github.plokhotnyuk.jsoniter_scala.core.JsonValueCodec[_ >: scala.Nothing <: scala.Any]' defined for 'issue$_.this.Bar.Type'.
[error] case class Foo(bar: Bar, bars: List[Bar]) derives ConfiguredJsonValueCodec
[error]      

aartigao avatar May 20 '25 12:05 aartigao

The imported symbol is inline given, but recursing into Inlined was reverted and still needs to be addressed.

This is just to show the inlined newtypeCodec under -Yplain-printer.

Inlined(Thicket(List()), List(),
  Inlined(Ident(JsonCodecMaker$),
    List(),
    Block(
      List(
        Apply(
          Select(
            Inlined(Thicket(List()),
              List(), Ident(x)),
          addOne),
        List(
          Inlined(Thicket(List()),
            List(),
            Inlined(
              Ident(JsonCodecMaker$),
              List(),
              Apply(
                Select(
                  Inlined(
                    Thicket(List()),
                    List(),
                    Inlined(
                      Apply(
                        TypeApply(
                          Ident(
                            newtypeCodec
                            ),
                        List(TypeTree(),
                          TypeTree())),
                        List(
                        NamedArg(
                          newType,

som-snytt avatar May 20 '25 16:05 som-snytt

Getting a similar issue with iron and inline givens for derivation

  • https://github.com/Iltotore/iron/issues/317
//> using scala 3.nightly
//> using dep io.github.iltotore::iron-circe:3.0.1
//> using options -Werror -Wunused:all

import io.circe.*
import io.github.iltotore.iron.*
import io.github.iltotore.iron.circe.given
import io.github.iltotore.iron.constraint.collection.*

case class IronTypeEncoder(username: String :| MinLength[5]) derives Encoder.AsObject

Managed to pinpoint it to this PR

  • https://github.com/scala/scala3/pull/22815

3.7.1-RC1-bin-20250320-a5e029a-NIGHTLY compiles ✅ 3.7.1-RC1-bin-20250323-451fdcd-NIGHTLY doesn't ❌

It's probably what @som-snytt meant by

but recursing into Inlined was reverted and still needs to be addressed.

EnviousSwan avatar May 27 '25 22:05 EnviousSwan

It's the reverse of my previous comment: the expansion of Inlined is always transformed, but the newtypeCodec is in the call only at inlining. Currently, it looks at call only after typer (for transparent inline).

som-snytt avatar May 28 '25 09:05 som-snytt