Skip to content

CheckUnused reports retained bodies #22507

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
som-snytt opened this issue Feb 3, 2025 · 0 comments · Fixed by #22510
Closed

CheckUnused reports retained bodies #22507

som-snytt opened this issue Feb 3, 2025 · 0 comments · Fixed by #22510
Assignees
Labels
area:linting Linting warnings enabled with -W or -Xlint itype:bug
Milestone

Comments

@som-snytt
Copy link
Contributor

Compiler version

3.7

Minimized code

//> using options -Werror -Wunused:privates

case class BinaryFen(value: Array[Byte]) extends AnyVal:

  def read: Unit =
    val reader = new Iterator[Byte]:
      val inner                            = value.iterator
      override inline def hasNext: Boolean = inner.hasNext // nowarn
      override inline def next: Byte       = if hasNext then inner.next else 0.toByte // nowarn

    if reader.hasNext then
      val b: Byte = reader.next
      println(b)

Output

-- [E198] Unused Symbol Warning: tests/warn/unused-inline-private.scala:9:45 ---
9 |      override inline def hasNext: Boolean = inner.hasNext // nowarn
  |                                             ^
  |                                             unused private member
-- [E198] Unused Symbol Warning: tests/warn/unused-inline-private.scala:10:45 --
10 |      override inline def next: Byte       = if hasNext then inner.next else 0.toByte // nowarn
   |                                             ^
   |                                             unused private member
2 warnings found

Expectation

Quick println shows that it is reporting

PRIVATE method next$retainedBody
PRIVATE method hasNext$retainedBody

but it should exclude those special methods.

Worth adding that the lichess code probably does not do what was probably expected.

  public final void read$extension(byte[]);
    descriptor: ([B)V
    flags: (0x0011) ACC_PUBLIC, ACC_FINAL
    Code:
      stack=3, locals=4, args_size=2
         0: new           #9                  // class BinaryFen$$anon$1
         3: dup
         4: aload_1
         5: invokespecial #98                 // Method BinaryFen$$anon$1."<init>":([B)V
         8: astore_2
         9: aload_2
        10: invokeinterface #104,  1          // InterfaceMethod scala/collection/Iterator.hasNext:()Z
        15: ifeq          39
        18: aload_2
        19: invokeinterface #107,  1          // InterfaceMethod scala/collection/Iterator.next:()Ljava/lang/Object;
        24: invokestatic  #111                // Method scala/runtime/BoxesRunTime.unboxToByte:(Ljava/lang/Object;)B
        27: istore_3
        28: getstatic     #116                // Field scala/Predef$.MODULE$:Lscala/Predef$;
        31: iload_3
        32: invokestatic  #120                // Method scala/runtime/BoxesRunTime.boxToByte:(B)Ljava/lang/Byte;
        35: invokevirtual #124                // Method scala/Predef$.println:(Ljava/lang/Object;)V
        38: return
        39: return

It uses the unspecialized next, which boxes the result of specialized next, which unboxes the result of underlying inner.next.

Also normally the anon iterator should extend AbstractIterator.

@som-snytt som-snytt added itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label area:linting Linting warnings enabled with -W or -Xlint and removed stat:needs triage Every issue needs to have an "area" and "itype" label labels Feb 3, 2025
@som-snytt som-snytt self-assigned this Feb 3, 2025
@sjrd sjrd closed this as completed in d75ca7f Feb 4, 2025
@WojciechMazur WojciechMazur added this to the 3.7.0 milestone Mar 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:linting Linting warnings enabled with -W or -Xlint itype:bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants