Skip to content

False positive unused private object in 3.7 nightly when givens in object are used #22744

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
mrdziuban opened this issue Mar 7, 2025 · 3 comments · Fixed by #22751
Closed
Assignees
Labels
area:linting Linting warnings enabled with -W or -Xlint itype:bug
Milestone

Comments

@mrdziuban
Copy link

Compiler version

3.7.0-RC1-bin-20250306-73ba485-NIGHTLY

Minimized code

With -Wunused:privates enabled:

object test {
  private trait Foo[A] { val value: A }

  private object Foo {
    given int: Foo[Int] = new Foo[Int] { val value = 1 }
  }

  val i = summon[Foo[Int]].value
}

Output

-- [E198] Unused Symbol Warning: /Users/matt/scala3.7-nightly-unused/src/main/scala/example/Test.scala:4:2 -------------
4 |  private object Foo {
  |  ^
  |  unused private member

Expectation

object Foo should not be reported unused since given int from it is summoned and used

@mrdziuban mrdziuban added itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label labels Mar 7, 2025
@som-snytt som-snytt added 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 Mar 7, 2025
@som-snytt som-snytt self-assigned this Mar 7, 2025
@som-snytt
Copy link
Contributor

som-snytt commented Mar 7, 2025

This is a bit interesting.

Current handling of Inlined is clearly wrong. This is still gray area, but the idea is that inlined code can resolve references but not introduce new definitions which may go unused.

But the expanded tree is just int and not Foo.int, so the lint doesn't see the selection (!).

The call side should resolve the reference:

Apply(TypeApply(Ident(summon),List(AppliedTypeTree(Ident(Foo),List(Ident(Int))))),List(Ident(int)))

Ah, it sees the Foo but I'll have to understand this comment

// if in an inline expansion, resolve at summonInline (synthetic pos) or in an enclosing call site

The history was squashed so I can't reconstruct my reasoning. I assume I was wrong about something.

@mrdziuban
Copy link
Author

@som-snytt I'm not sure it has to do with inlining, even without any inline methods involved I still get the warning

object test {
  private trait Foo[A] { val value: A }

  private object Foo {
    given int: Foo[Int] = new Foo[Int] { val value = 1 }
  }

  private def fooValue[A](using f: Foo[A]): A = f.value

  val i = fooValue[Int]
}

@som-snytt
Copy link
Contributor

Aside from inlined, this is a regression in not "resolving" the prefixes of Ident. (Implicit values are not Selects.)

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.

3 participants