Skip to content
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

-Wunused false negative in for-comprehension #18289

Open
myazinn opened this issue Jul 25, 2023 · 7 comments
Open

-Wunused false negative in for-comprehension #18289

myazinn opened this issue Jul 25, 2023 · 7 comments
Labels
area:linting Linting warnings enabled with -W or -Xlint backlog No work planned on this by the core team for the time being. help wanted itype:bug itype:enhancement

Comments

@myazinn
Copy link

myazinn commented Jul 25, 2023

Compiler version

3.3.0, 3.3.1-RC4

Minimized code

With -Wunused:all

@main def run(): Unit =
  val dummy = 1
  for x <- Option(1)
  yield dummy

or

@main def run(): Unit =
  for
    x <- Option(1)
    y <- Option(2)
  yield y

Output

The code compiles without warnings

Expectation

The code produces an unused local definition warning for an x variable.

@myazinn myazinn added itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label labels Jul 25, 2023
@WojciechMazur WojciechMazur added itype:enhancement 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 Jul 31, 2023
@mbovel
Copy link
Member

mbovel commented Jul 1, 2024

This issue was picked for the Scala Issue Spree of tomorrow, July 2nd. @dwijnand and @aherlihy will be working on it! If you have any insight into the issue or guidance on how to fix it, please leave it here.

@som-snytt
Copy link
Contributor

Motivated by the linked duplicate, I started work on the scala 2 ticket scala/bug#10287

IIRC, it's just the tupling operation that has to be "special-cased" as not "consuming". For the rewrite in parser, I gave duplicated Ident an attachment to reference the user-written Ident. (I don't know offhand if that is helpful for scala 3.)

I haven't submitted that scala 2 PR yet, but I have started a PR for scala 3 on unused imports.

@mbovel
Copy link
Member

mbovel commented Jul 1, 2024

Cool!

Is your PR on unused imports in Scala 3 related to this issue? (—should I assign Anna and Dale to another issue?)

@som-snytt
Copy link
Contributor

Hopefully it won't conflict much? #20894

That's just a heads-up.

I think I thought that if I fixed it on scala 2 I would circle around here, but the solutions may be different anyway; also, there is the impending improvement to for-comprehensions, which may or may not alter the approach.

@som-snytt
Copy link
Contributor

The scala 2 fix has a long tail. After also spending time with Scala 3 CheckUnused, but without looking how for comprehensions are expanded or the plan for revised translation, I'm not sure I learned anything useful.

The fix was to add an attachment to patvars before the rewrite in parser which indicates the "original tree". (There are no symbols yet.) When a pattern bind is duplicated, it carries the attachment with it, so that "usages" or referenced can be charged against the original symbol later.

The tricky bits were to copy the attachment when a new tree is conjured for a var, and also not to count tuplings as usages.

My previous comment reflects my erstwhile optimism.

@Gedochao Gedochao added help wanted backlog No work planned on this by the core team for the time being. labels Oct 2, 2024
@aherlihy
Copy link
Contributor

aherlihy commented Oct 2, 2024

From my digging it seems there would need to be a nontrivial set of changes to address this issue, particularly to special case handling the tupled values so the compiler can even recognize that they are "unused". We cannot directly reuse the Scala 2 fix, and any new changes would be also be at risk for needing to be rewritten as soon as the for-comprehension changes kick in (scala/improvement-proposals#79), so I will pause on this ticket for the moment and we will add a 'community help wanted' label.

@aherlihy aherlihy removed their assignment Oct 2, 2024
@som-snytt
Copy link
Contributor

som-snytt commented Oct 2, 2024

As an update, the revised Scala 2 approach is just to identify name bindings by their positions, which are preserved by tree duplication. (Then details of desugaring or other tree manipulation don't matter. Edit: that can't be true, as the tupling of value defs is special, and fresh terms for _ <- G etc.) (I have not compared Scala 3 yet.)

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 backlog No work planned on this by the core team for the time being. help wanted itype:bug itype:enhancement
Projects
None yet
Development

No branches or pull requests

6 participants