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

Mixed (java-scala) compilation fails, separate works due to import ambiguity #9111

Closed
scabug opened this issue Jan 22, 2015 · 10 comments
Closed

Comments

@scabug
Copy link

scabug commented Jan 22, 2015

Very similar to #2377 (which is fixed).

Java file:

public final class A {
  public static final class T { }
  public static final class Inner {
    public static final class T { }
    public T newT() { return null; }
  }
}

Scala file:

class C {
  val i = new A.Inner()
  println(i.newT())
}

Mixed compilation:

lucmac:sandbox luc$ scalac A.java Test.scala 
A.java:5: error: reference to T is ambiguous;
it is both defined in object A and imported subsequently by
import Inner._
    public T newT() { return null; }
           ^
one error found

the reason is Scalac's desugaring which introduces an import:

object A {
  class T
  object Inner {
    class T
  }
  class Inner {
    import Inner._
    def newT1(): T = null // reference to T is ambiguous
  }
}
class A

separate compilation (java first) works


Even worse: instead of an ambiguity, the Scala compiler resolves types in Java signatures incorrectly:

A.java

public class A {
  public static class P {
    public static class T { public void f() { } }
  }
  public static class T { public void g() { } }
  public static class Inner extends P {
    public class Deeper {
      public void foo(T t) { t.f(); }
    }
  }
}

Test.scala

object Test extends App {
  val i = new A.Inner()
  val j = new i.Deeper()
  println(j.foo(new A.T())) // compiles in mixed compilation (it should not)
}

Mixed compilation works, but runtime fails. Seperate compilation fails (correctly):

lucmac:sandbox luc$ scalac A.java Test.scala 
lucmac:sandbox luc$ javac A.java

lucmac:sandbox luc$ scala Test
java.lang.NoSuchMethodError: A$Inner$Deeper.foo(LA$T;)V
	at Test$.delayedEndpoint$Test$1(Test.scala:5)

lucmac:sandbox luc$ scalac Test.scala
Test.scala:5: error: type mismatch;
 found   : A.T
 required: A.P.T
  println(j.foo(new A.T()))
                ^
one error found

In this example, the Scala compiler does not inject any import Inner._. The Java parser checks if there are any statics in the class. If not, it doesn't add the import. This is wrong because statics may be inherited.

@scabug
Copy link
Author

scabug commented Jan 22, 2015

Imported From: https://issues.scala-lang.org/browse/SI-9111?orig=1
Reporter: @lrytz
Affected Versions: 2.11.5
See #2377, #3120

@scabug
Copy link
Author

scabug commented Mar 21, 2015

@lrytz said:
not fixed, we just have a workaround in the backend to avoid a compiler crash caused by this issue

@scabug
Copy link
Author

scabug commented Dec 20, 2016

@lrytz said:
scala/scala#5606

@SethTisue
Copy link
Member

scala/scala#5606 looked promising, but didn't make it across the finish line

@thesamet
Copy link

This issue is impacting users of the latest version of protocol buffers (3.8.0). This version of protoc generates the following code:

    @java.lang.Override
    @SuppressWarnings({"unused"})
    protected java.lang.Object newInstance(
        UnusedPrivateParameter unused) {
      return new ScalaPbOptions();
    }

See https://github.com/scalapb/ScalaPB/blob/22e6d8f5ccdb2c32c2b9876bf374dfa6e37036d2/scalapb-runtime/jvm/src/main/java/scalapb/options/Scalapb.java#L430

UnusedPrivateParameter is a static inner class defined here: https://github.com/protocolbuffers/protobuf/blob/affe9c200abe49c4a4bb917e7889d21219601e79/java/core/src/main/java/com/google/protobuf/GeneratedMessageV3.java#L485

compile:doc would fail with:

[error] /home/thesamet/dev/ScalaPB/compiler-plugin/src/main/java/scalapb/options/compiler/Scalapb.java:430:9: not found: type UnusedPrivateParameter
[error]         UnusedPrivateParameter unused) {
[error]         ^

As a workaround I added scalacOptions in (Compile, doc) += "-no-java-comments"

thesamet added a commit to scalapb/ScalaPB that referenced this issue Jun 13, 2019
@thesamet
Copy link

thesamet commented Jun 13, 2019

Update: the workaround does not work for Scala 2.11 - so for the time being we can't upgrade to protobuf 3.8.0.

@SethTisue
Copy link
Member

@adriaanm @lrytz @retronym any insight?

@retronym
Copy link
Member

-no-java-comments is a new compiler option for ScalaDoc 2.12 and higher. Your build could conditionally add this to the options based on the Scala version.

scalacOptions in (Compile, doc) ++= if (scalaBinaryVersion.value == "2.11") Nil else List("-no-java-comments")

@thesamet
Copy link

@retronym thanks, that works. I missed the fact that the failure for <2.12 is due to the option not being supported. I'm satisfied with this workaround.

@retronym
Copy link
Member

BTW, this bug is fixed in 2.13.0 and will also be in the upcoming 2.12.9 release.

@lrytz lrytz closed this as completed Jun 26, 2019
metaphori added a commit to scafi/scafi that referenced this issue Apr 9, 2020
[Error] /home/travis/build/scafi/scafi/simulator-gui-new/src/main/scala/it/unibo/scafi/simulation/s2/frontend/model/simulation/implementation/mutable/StandardNodeDefinition.scala:64: Symbol 'type javafx.scene.control.TabPane.TabDragPolicy' is missing from the classpath.
This symbol is required by 'value scalafx.scene.control.TabPane.value'.
Make sure that type TabDragPolicy is in your classpath and check for conflicting dependencies with `-Ylog-classpath`.
A full rebuild may help if 'TabPane.class' was compiled against an incompatible version of javafx.scene.control.TabPane.
[Error] : Error while emitting it/unibo/scafi/simulation/s2/frontend/view/scalaFX/logger/FXLogger
Failed to get the type of class symbol javafx.scene.control.TabPane$TabDragPolicy due to scala/bug#9111.
48 warnings found
two errors found
ruebot added a commit to archivesunleashed/aut that referenced this issue Oct 25, 2021
ruebot added a commit to archivesunleashed/aut that referenced this issue Oct 25, 2021
ianmilligan1 pushed a commit to archivesunleashed/aut that referenced this issue Oct 25, 2021
- Resolves #522
  - See: scala/bug#9111 (comment)
- Update README doc links
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants