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

false-positive "unused import" warning #21525

Open
xuwei-k opened this issue Aug 31, 2024 · 2 comments
Open

false-positive "unused import" warning #21525

xuwei-k opened this issue Aug 31, 2024 · 2 comments
Labels
area:reporting Error reporting including formatting, implicit suggestions, etc better-errors Issues concerned with improving confusing/unhelpful diagnostic messages itype:bug

Comments

@xuwei-k
Copy link
Contributor

xuwei-k commented Aug 31, 2024

Compiler version

  • 3.6.0-RC1-bin-20240828-5e83606-NIGHTLY
  • 3.5.1-RC2
  • 3.5.0
  • 3.4.3
  • 3.3.3

Minimized example

build.sbt

scalaVersion := "3.5.1-RC2"

scalacOptions += "-Wunused:imports"

A.scala

import scala.reflect.TypeTest

trait A {
  type B
  type C <: B

  given instance: TypeTest[B, C]
}

def f(a: A, b: a.B): Boolean = {
  import a.C
  b match {
    case _: C =>
      true
    case _ =>
      false
  }
}

Output Error/Warning message

[warn] -- [E198] Unused Symbol Warning: false-unused-import-warn-example-project/A.scala:11:11 
[warn] 11 |  import a.C
[warn]    |           ^
[warn]    |           unused import

Why this Error/Warning was not helpful

a.C is not unused

Suggested improvement

don't warn

@xuwei-k xuwei-k added area:reporting Error reporting including formatting, implicit suggestions, etc better-errors Issues concerned with improving confusing/unhelpful diagnostic messages itype:enhancement stat:needs triage Every issue needs to have an "area" and "itype" label labels Aug 31, 2024
@Gedochao Gedochao added itype:bug Spree Suitable for a future Spree and removed stat:needs triage Every issue needs to have an "area" and "itype" label itype:enhancement labels Sep 2, 2024
@mbovel
Copy link
Member

mbovel commented Sep 23, 2024

This issue was picked for the Scala Issue Spree of tomorrow, June 24th. @KuceraMartin and @iusildra will be working on it. If you have any insight into the issue or guidance on how to fix it, please leave it here.

Note: it's hard for me to estimate the difficulty of this issue, as I am quite unfamiliar with the unused imports warning.

@KuceraMartin
Copy link
Contributor

KuceraMartin commented Sep 24, 2024

It turned out this is not trivial to fix. During the typer phase, the information about C being used is lost:

package <empty> {
  import scala.reflect.TypeTest
  trait A() extends Object {
    type B >: Nothing <: Any
    type C >: Nothing <: Any
    given def instance: scala.reflect.TypeTest[A.this.B, A.this.C]
  }
  final lazy module val 21525$package: 21525$package = new 21525$package()
  final module class 21525$package() extends Object() {
    this: 21525$package.type =>
    def f(a: A, b: a.B): Boolean =
      {
        import a.C
        b match
          {
            case a.instance(_) =>
              true
            case _ =>
              false
          }
      }
  }
}

Since the CheckUnused phase happens after typer, it doesn't see any usage of C and therefore it emits a warning.

To fix this, we would probably need to store some additional information when erasing C in the typer phase.

@mbovel mbovel removed the Spree Suitable for a future Spree label Sep 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:reporting Error reporting including formatting, implicit suggestions, etc better-errors Issues concerned with improving confusing/unhelpful diagnostic messages itype:bug
Projects
None yet
Development

No branches or pull requests

4 participants