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:imports causes an error on methods that come from an external library #17394

Closed
arainko opened this issue May 2, 2023 · 7 comments · May be fixed by #21975
Closed

-Wunused:imports causes an error on methods that come from an external library #17394

arainko opened this issue May 2, 2023 · 7 comments · May be fixed by #21975
Assignees
Labels
area:linting Linting warnings enabled with -W or -Xlint itype:bug

Comments

@arainko
Copy link
Contributor

arainko commented May 2, 2023

Compiler version

3.3.0-RC5

Minimized code

Project 1 - the library

//file AppliedBuilder.scala

package repro

import repro.internal.*

final class AppliedBuilder[Source, Dest](appliedTo: Source) {
  inline def transform: Dest = ReproObject.doNothing
}
//file Extension1.scala
package repro

import repro.internal.*

extension (source: Int) inline def whatever1 = ReproObject.doNothing
//file Extension2.scala
package repro

import repro.internal.*

extension (source: Int) inline def whatever2 = ReproObject.doNothing
//file ReproObject.scala
package repro.internal

private[repro] object ReproObject {
  inline def doNothing = ???
}
//file syntax.scala
package repro

extension [Source](source: Source) {
  def into[Dest]: AppliedBuilder[Source, Dest] = AppliedBuilder[Source, Dest](source)
}
//file project.scala
//> using scala "3.2.2"
//> using publish.organization "repro"
//> using publish.name "repro"
//> using publish.version "0.1.0"

Project 2 - usage site, depends on 'Project 1'

//file usage.scala

//> using scala "3.3.0-RC5"
//> using option "-Wunused:all"
//> using dep "repro::repro:0.1.0"

import repro.*

@main def main = "whatever".into[Int].transform

To reproduce 'Project 1' needs to be published first:

scala-cli --power publish local .

then we can just run the second project with:

scala-cli compile .

Output

aleksander@pop-os:~/repos/wunused-usage-repro$ scala-cli compile .
Compiling project (Scala 3.3.0-RC5, JVM)
exception occurred while compiling /home/aleksander/repos/wunused-usage-repro/usage.scala
Error compiling project (Scala 3.3.0-RC5, JVM)
Error: Unexpected error when compiling project_dd4f5f0c18: 'Toplevel definition inline$ReproObject$i1 is defined in
  /home/aleksander/.ivy2/local/repro/repro_3/0.1.0/jars/repro_3.jar(repro/Extension1$package.class)
and also in
  /home/aleksander/.ivy2/local/repro/repro_3/0.1.0/jars/repro_3.jar(repro/Extension2$package.class)
One of these files should be removed from the classpath.'
Compilation failed

Expectation

Compilation succeeds just like it does without -Wunused:all

Context

This issue was first reported here: arainko/ducktape#54 - a workaround for it was moving the contents of fallibleSyntax.scala to syntax.scala, that way another inline accessor for Transformations (with the name inline$Transformations$i1) is not generated, a new inline$Transformations$i3 accessor is generated instead, because these are now in the scope of the same class (syntax$package).
For the sake of completeness, here's a definition of Transformations, this file houses proxies to macros, it's also package private.

@arainko arainko added itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label labels May 2, 2023
@chuwy
Copy link

chuwy commented May 3, 2023

FYI I used -Wunused:imports

@mbovel mbovel 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 May 4, 2023
@szymon-rd szymon-rd self-assigned this Aug 28, 2023
@adpi2
Copy link
Member

adpi2 commented Oct 14, 2024

Here is a simplified version of this issue without any inline, or extension method:

In lib.scala:

//> using scala 3.3.4
//> using publish.organization "org.example"
//> using publish.name "lib"
//> using publish.version "0.1.0-SNAPSHOT"

package example:
  trait A:
    def foo: String = ???

package object example extends A:
  def foo(x: String): String = ???

In app.scala:

//> using scala "3.3.4"
//> using dep "org.example::lib:0.1.0-SNAPSHOT"

import example.*

@main def main = foo
> scala --power publish local lib.scala
> scala compile app.scala
[error] ./app.scala:7:18
[error] Toplevel definition foo is defined in
[error]   /home/piquerez/.ivy2/local/org.example/lib_3/0.1.0-SNAPSHOT/jars/lib_3.jar(example/B.class)
[error] and also in
[error]   /home/piquerez/.ivy2/local/org.example/lib_3/0.1.0-SNAPSHOT/jars/lib_3.jar(example/A.class)
[error] One of these files should be removed from the classpath.
[error] @main def main = foo
[error]       

This was inspired by sbt/sbt#7726 in sbt 2

@adpi2 adpi2 changed the title -Wunused:all causes a compiler crash in interaction with inline accessors and extension methods that come from an external library -Wunused:all causes a compiler crash in interaction with extension methods that come from an external library Oct 14, 2024
@adpi2 adpi2 changed the title -Wunused:all causes a compiler crash in interaction with extension methods that come from an external library -Wunused:imports causes an error in interaction with extension methods that come from an external library Oct 14, 2024
@adpi2 adpi2 changed the title -Wunused:imports causes an error in interaction with extension methods that come from an external library -Wunused:imports causes an error on methods that come from an external library Oct 14, 2024
@som-snytt
Copy link
Contributor

With 3.5.1 and sbt, the minimal example gives me:

[error] -- Error: /home/amarki/projects/i17394-test/src/main/scala/main.scala:4:17 -----
[error] 4 |@main def main = foo
[error]   |                 ^
[error]   |Toplevel definition foo is defined in
[error]   |  /home/amarki/.ivy2/local/org.example/lib_3/0.1.0/jars/lib_3.jar(example/package.tasty)
[error]   |and also in
[error]   |  /home/amarki/.ivy2/local/org.example/lib_3/0.1.0/jars/lib_3.jar(example/A.tasty)
[error]   |One of these files should be removed from the classpath.
[error] one error found

without any warnings turned on.

If an interaction with CheckUnused is suspected, I'll try it against my PR, if I can reproduce the issue (that is, compile cleanly without the flags). I confirmed same behavior with bin/scalacQ.

@adpi2
Copy link
Member

adpi2 commented Oct 15, 2024

@som-snytt

without any warnings turned on.

You are right and I have the same behavior on 3.3.4.

However if you take this other example, it crashes with the -Wunused:imports but it succeeds without. Also notice the given import: it also compiles successfully if I remove it.

//> using scala "3.5.1"
//> using option "-Wunused:imports"
//> using dep "org.example::lib:0.1.0-SNAPSHOT"

import example.{given, *}

object C
> scala --power publish local lib.scala
> scala compile app.scala
Compiling project (Scala 3.5.1, JVM (17))
Error compiling project (Scala 3.5.1, JVM (17))
Error: Unexpected error when compiling scala3-17394_8155ffa868-97a3a2609c: dotty.tools.dotc.core.TypeError$$anon$1: Toplevel definition foo is defined in
  /home/piquerez/.ivy2/local/org.example/lib_3/0.1.0-SNAPSHOT/jars/lib_3.jar(example/package.tasty)
and also in
  /home/piquerez/.ivy2/local/org.example/lib_3/0.1.0-SNAPSHOT/jars/lib_3.jar(example/A.tasty)
One of these files should be removed from the classpath.

Compilation failed

It seems the same error is triggered by 3 different things, in the 3 different reproduction cases of this PR.

@som-snytt
Copy link
Contributor

Thanks, for import example.given, it imp.expr.tpe.allMembers per -Ydebug-error. Probably the conflict is that collecting members for package objects (computeMembersNamed) doesn't take into account the prefix of interest. (Just a guess.)

@eed3si9n
Copy link
Member

Here's a PR to remove the error - #21975

@KacperFKorban
Copy link
Member

Should be fixed by #22190

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.

8 participants