Dotty: Cylic reference involving class when using java interop

Created on 30 Nov 2020  路  3Comments  路  Source: lampepfl/dotty

Similar to https://github.com/lampepfl/dotty/issues/9492.

I get a compiler error when using Java code from Apache Avro. The specific file is at https://github.com/apache/avro/blob/master/lang/java/avro/src/main/java/org/apache/avro/SchemaBuilder.java

Minimized code

// build.sbt

scalaVersion := "3.0.0-M2"
scalacOptions += "-Ydebug-error"
libraryDependencies += "org.apache.avro" % "avro" % "1.10.0"
// main.scala

def oops = org.apache.avro.SchemaBuilder.builder().fixed("foo")

scastie reproduction (3.0.0-M1)

Output with -Ydebug-error:

sbt:vx1ka5jhtoax0guv87vfkq> compile
[info] Compiling 1 Scala source to /Users/benplom/Downloads/Vx1Ka5jhToaX0guV87vFkQ/target/scala-3.0.0-M2/classes ...
exception caught when loading class FixedBuilder: Cyclic reference involving class FixedBuilder
java.lang.Exception: Stack trace
    at java.base/java.lang.Thread.dumpStack(Thread.java:1379)
    at dotty.tools.dotc.report$.error(report.scala:72)
    at dotty.tools.dotc.report$.error(report.scala:75)
  | => vat dotty.tools.dotc.typer.ErrorReporting$.errorType(ErrorReporting.scala:38)
    at dotty.tools.dotc.typer.ErrorReporting$.errorTree(ErrorReporting.scala:30)
    at dotty.tools.dotc.typer.Typer.typedUnadapted(Typer.scala:2535)
    at dotty.tools.dotc.typer.Typer.typed(Typer.scala:2601)
    at dotty.tools.dotc.typer.Typer.typed(Typer.scala:2605)
    at dotty.tools.dotc.typer.Typer.typedExpr(Typer.scala:2720)
    at dotty.tools.dotc.typer.Typer.typeSelectOnTerm$1(Typer.scala:549)
    at dotty.tools.dotc.typer.Typer.typedSelect(Typer.scala:574)
    at dotty.tools.dotc.typer.Typer.typedNamed$1(Typer.scala:2441)
    at dotty.tools.dotc.typer.Typer.typedUnadapted(Typer.scala:2531)
    at dotty.tools.dotc.typer.Typer.typed(Typer.scala:2601)
    at dotty.tools.dotc.typer.Typer.typed(Typer.scala:2605)
    at dotty.tools.dotc.typer.Typer.typedExpr(Typer.scala:2720)
    at dotty.tools.dotc.typer.Applications.typedFunPart$$anonfun$1(Applications.scala:852)
    at dotty.tools.dotc.typer.Typer.tryEither(Typer.scala:2728)
    at dotty.tools.dotc.typer.Applications.typedFunPart(Applications.scala:861)
    at dotty.tools.dotc.typer.Applications.typedFunPart$(Applications.scala:236)
    at dotty.tools.dotc.typer.Typer.typedFunPart(Typer.scala:92)
    at dotty.tools.dotc.typer.Applications.realApply$1(Applications.scala:873)
    at dotty.tools.dotc.typer.Applications.typedApply(Applications.scala:1026)
    at dotty.tools.dotc.typer.Applications.typedApply$(Applications.scala:236)
    at dotty.tools.dotc.typer.Typer.typedApply(Typer.scala:92)
    at dotty.tools.dotc.typer.Typer.typedUnnamed$1(Typer.scala:2471)
    at dotty.tools.dotc.typer.Typer.typedUnadapted(Typer.scala:2532)
    at dotty.tools.dotc.typer.Typer.typed(Typer.scala:2601)
    at dotty.tools.dotc.typer.Typer.typed(Typer.scala:2605)
    at dotty.tools.dotc.typer.Namer.typedAheadExpr$$anonfun$1(Namer.scala:1232)
    at dotty.tools.dotc.typer.Namer.typedAhead(Namer.scala:1219)
    at dotty.tools.dotc.typer.Namer.typedAheadExpr(Namer.scala:1232)
    at dotty.tools.dotc.typer.Namer.rhsType$1$$anonfun$1(Namer.scala:1367)
    at dotty.tools.dotc.typer.PrepareInlineable$.dropInlineIfError(PrepareInlineable.scala:216)
    at dotty.tools.dotc.typer.Namer.rhsType$2(Namer.scala:1367)
    at dotty.tools.dotc.typer.Namer.cookedRhsType$1(Namer.scala:1378)
    at dotty.tools.dotc.typer.Namer.lhsType$1(Namer.scala:1379)
    at dotty.tools.dotc.typer.Namer.inferredType$1(Namer.scala:1390)
    at dotty.tools.dotc.typer.Namer.valOrDefDefSig(Namer.scala:1398)
    at dotty.tools.dotc.typer.Namer.defDefSig(Namer.scala:1483)
    at dotty.tools.dotc.typer.Namer$Completer.typeSig(Namer.scala:684)
    at dotty.tools.dotc.typer.Namer$Completer.completeInCreationContext(Namer.scala:801)
    at dotty.tools.dotc.typer.Namer$Completer.complete(Namer.scala:712)
    at dotty.tools.dotc.core.SymDenotations$SymDenotation.completeFrom(SymDenotations.scala:166)
    at dotty.tools.dotc.core.Denotations$Denotation.completeInfo$1(Denotations.scala:188)
    at dotty.tools.dotc.core.Denotations$Denotation.info(Denotations.scala:190)
    at dotty.tools.dotc.core.SymDenotations$SymDenotation.ensureCompleted(SymDenotations.scala:370)
    at dotty.tools.dotc.typer.Typer.retrieveSym(Typer.scala:2413)
    at dotty.tools.dotc.typer.Typer.typedNamed$1(Typer.scala:2438)
    at dotty.tools.dotc.typer.Typer.typedUnadapted(Typer.scala:2531)
    at dotty.tools.dotc.typer.Typer.typed(Typer.scala:2601)
    at dotty.tools.dotc.typer.Typer.typed(Typer.scala:2605)
    at dotty.tools.dotc.typer.Typer.traverse$1(Typer.scala:2627)
    at dotty.tools.dotc.typer.Typer.typedStats(Typer.scala:2676)
    at dotty.tools.dotc.typer.Typer.typedClassDef(Typer.scala:2131)
    at dotty.tools.dotc.typer.Typer.typedTypeOrClassDef$2(Typer.scala:2459)
    at dotty.tools.dotc.typer.Typer.typedNamed$1(Typer.scala:2463)
    at dotty.tools.dotc.typer.Typer.typedUnadapted(Typer.scala:2531)
    at dotty.tools.dotc.typer.Typer.typed(Typer.scala:2601)
    at dotty.tools.dotc.typer.Typer.typed(Typer.scala:2605)
    at dotty.tools.dotc.typer.Typer.traverse$1(Typer.scala:2627)
    at dotty.tools.dotc.typer.Typer.typedStats(Typer.scala:2676)
    at dotty.tools.dotc.typer.Typer.typedPackageDef(Typer.scala:2256)
    at dotty.tools.dotc.typer.Typer.typedUnnamed$1(Typer.scala:2503)
    at dotty.tools.dotc.typer.Typer.typedUnadapted(Typer.scala:2532)
    at dotty.tools.dotc.typer.Typer.typed(Typer.scala:2601)
    at dotty.tools.dotc.typer.Typer.typed(Typer.scala:2605)
    at dotty.tools.dotc.typer.Typer.typedExpr(Typer.scala:2720)
    at dotty.tools.dotc.typer.FrontEnd.liftedTree1$1(FrontEnd.scala:79)
    at dotty.tools.dotc.typer.FrontEnd.typeCheck$$anonfun$1(FrontEnd.scala:84)
    at dotty.runtime.function.JFunction0$mcV$sp.apply(JFunction0$mcV$sp.java:12)
    at dotty.tools.dotc.typer.FrontEnd.monitor(FrontEnd.scala:43)
    at dotty.tools.dotc.typer.FrontEnd.typeCheck(FrontEnd.scala:85)
    at dotty.tools.dotc.typer.FrontEnd.runOn$$anonfun$3(FrontEnd.scala:120)
    at dotty.runtime.function.JProcedure1.apply(JProcedure1.java:15)
    at dotty.runtime.function.JProcedure1.apply(JProcedure1.java:10)
    at scala.collection.immutable.List.foreach(List.scala:333)
    at dotty.tools.dotc.typer.FrontEnd.runOn(FrontEnd.scala:120)
    at dotty.tools.dotc.Run.runPhases$4$$anonfun$4(Run.scala:185)
    at dotty.runtime.function.JProcedure1.apply(JProcedure1.java:15)
    at dotty.runtime.function.JProcedure1.apply(JProcedure1.java:10)
    at scala.collection.ArrayOps$.foreach$extension(ArrayOps.scala:1323)
    at dotty.tools.dotc.Run.runPhases$5(Run.scala:195)
    at dotty.tools.dotc.Run.compileUnits$$anonfun$1(Run.scala:203)
    at dotty.runtime.function.JFunction0$mcV$sp.apply(JFunction0$mcV$sp.java:12)
    at dotty.tools.dotc.util.Stats$.maybeMonitored(Stats.scala:67)
    at dotty.tools.dotc.Run.compileUnits(Run.scala:210)
    at dotty.tools.dotc.Run.compileSources(Run.scala:147)
    at dotty.tools.dotc.Run.compile(Run.scala:129)
    at dotty.tools.dotc.Driver.doCompile(Driver.scala:38)
    at dotty.tools.dotc.Driver.process(Driver.scala:193)
    at dotty.tools.dotc.Main.process(Main.scala)
    at xsbt.CachedCompilerImpl.run(CachedCompilerImpl.java:69)
    at xsbt.CompilerInterface.run(CompilerInterface.java:41)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:64)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:564)
    at sbt.internal.inc.AnalyzingCompiler.call(AnalyzingCompiler.scala:248)
    at sbt.internal.inc.AnalyzingCompiler.compile(AnalyzingCompiler.scala:122)
    at sbt.internal.inc.AnalyzingCompiler.compile(AnalyzingCompiler.scala:95)
    at sbt.internal.inc.MixedAnalyzingCompiler.$anonfun$compile$4(MixedAnalyzingCompiler.scala:91)
    at scala.runtime.java8.JFunction0$mcV$sp.apply(JFunction0$mcV$sp.java:23)
    at sbt.internal.inc.MixedAnalyzingCompiler.timed(MixedAnalyzingCompiler.scala:186)
    at sbt.internal.inc.MixedAnalyzingCompiler.$anonfun$compile$3(MixedAnalyzingCompiler.scala:82)
    at sbt.internal.inc.MixedAnalyzingCompiler.$anonfun$compile$3$adapted(MixedAnalyzingCompiler.scala:77)
    at sbt.internal.inc.JarUtils$.withPreviousJar(JarUtils.scala:215)
    at sbt.internal.inc.MixedAnalyzingCompiler.compileScala$1(MixedAnalyzingCompiler.scala:77)
    at sbt.internal.inc.MixedAnalyzingCompiler.compile(MixedAnalyzingCompiler.scala:146)
    at sbt.internal.inc.IncrementalCompilerImpl.$anonfun$compileInternal$1(IncrementalCompilerImpl.scala:343)
    at sbt.internal.inc.IncrementalCompilerImpl.$anonfun$compileInternal$1$adapted(IncrementalCompilerImpl.scala:343)
    at sbt.internal.inc.Incremental$.doCompile(Incremental.scala:120)
    at sbt.internal.inc.Incremental$.$anonfun$compile$4(Incremental.scala:100)
    at sbt.internal.inc.IncrementalCommon.recompileClasses(IncrementalCommon.scala:180)
    at sbt.internal.inc.IncrementalCommon.cycle(IncrementalCommon.scala:98)
    at sbt.internal.inc.Incremental$.$anonfun$compile$3(Incremental.scala:102)
    at sbt.internal.inc.Incremental$.manageClassfiles(Incremental.scala:155)
    at sbt.internal.inc.Incremental$.compile(Incremental.scala:92)
    at sbt.internal.inc.IncrementalCompile$.apply(Compile.scala:75)
    at sbt.internal.inc.IncrementalCompilerImpl.compileInternal(IncrementalCompilerImpl.scala:348)
    at sbt.internal.inc.IncrementalCompilerImpl.$anonfun$compileIncrementally$1(IncrementalCompilerImpl.scala:301)
    at sbt.internal.inc.IncrementalCompilerImpl.handleCompilationError(IncrementalCompilerImpl.scala:168)
    at sbt.internal.inc.IncrementalCompilerImpl.compileIncrementally(IncrementalCompilerImpl.scala:248)
    at sbt.internal.inc.IncrementalCompilerImpl.compile(IncrementalCompilerImpl.scala:74)
    at sbt.Defaults$.compileIncrementalTaskImpl(Defaults.scala:1765)
    at sbt.Defaults$.$anonfun$compileIncrementalTask$1(Defaults.scala:1738)
    at scala.Function1.$anonfun$compose$1(Function1.scala:49)
    at sbt.internal.util.$tilde$greater.$anonfun$$u2219$1(TypeFunctions.scala:62)
    at sbt.std.Transform$$anon$4.work(Transform.scala:67)
    at sbt.Execute.$anonfun$submit$2(Execute.scala:281)
    at sbt.internal.util.ErrorHandling$.wideConvert(ErrorHandling.scala:19)
    at sbt.Execute.work(Execute.scala:290)
    at sbt.Execute.$anonfun$submit$1(Execute.scala:281)
    at sbt.ConcurrentRestrictions$$anon$4.$anonfun$submitValid$1(ConcurrentRestrictions.scala:178)
    at sbt.CompletionService$$anon$2.call(CompletionService.scala:37)
    at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
    at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515)
    at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1130)
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:630)
    at java.base/java.lang.Thread.run(Thread.java:832)
[error] -- [E046] Cyclic Error: /Users/benplom/Downloads/Vx1Ka5jhToaX0guV87vFkQ/src/main/scala/main.scala:1:48
[error] 1 |def oops = org.apache.avro.SchemaBuilder.builder().fixed("foo")
[error]   |                                                ^
[error]   |                             Cyclic reference involving class FixedBuilder
[error] one error found
[error] (Compile / compileIncremental) Compilation failed
[error] Total time: 0 s, completed 30 Nov 2020, 18:05:02

Expectation

It should compile

java bug

All 3 comments

Self-contained example:

// build.sbt
scalaVersion := "3.0.0-M2"
compileOrder := CompileOrder.JavaThenScala
// SchemaBuilder.java
public class SchemaBuilder {
  public static class Schema {}

  public static TypeBuilder<Schema> builder() {
    throw new UnsupportedOperationException();
  }

  public static class NamespacedBuilder<R, S extends NamespacedBuilder<R, S>> {}

  public static class FixedBuilder<R> extends NamespacedBuilder<R, FixedBuilder<R>> {}

  public static class TypeBuilder<R> {
    public FixedBuilder<R> fixed(String name) {
      throw new UnsupportedOperationException();
    }
  }
}
// Main.scala
object Test {
  val bldr = SchemaBuilder.builder()
  val oops = bldr.fixed("foo")
}

Here is another example from #10623

import org.apache.avro.{ SchemaBuilder, Schema }
import scala.collection.JavaConverters._

def schema = Schema.createRecord("A", "", "",false,
  Seq(new Schema.Field("list", SchemaBuilder.array().items(SchemaBuilder.builder.stringType))).asJava
)

that results in the same error.

My temporary workaround is to add a Java file to my project that encapsulates use of the builder api and returns the result.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

LaymanMergen picture LaymanMergen  路  3Comments

deusaquilus picture deusaquilus  路  3Comments

smarter picture smarter  路  3Comments

NightMachinary picture NightMachinary  路  3Comments

liufengyun picture liufengyun  路  3Comments