Search code examples
scalascala-compiler

Method in sealed trait gives Duplicate field name & signature in class file


When I add process() on IntList it gives a runtime "Duplicate field name&signature in class file Cons$3...". This happens only when the sealed trait is defined inside a function.

scala>   def t4() = {
     |
     |     sealed trait IntList {
     |       def process (baseCase: Int, f: (Int, Int) => Int): Int =
     |         this match {
     |           case End => baseCase
     |           case Cons(hd, tl) => f( hd, tl.process(baseCase, f))
     |         }
     |
     |     }
     |
     |     final case object End extends IntList
     |
     |     final case class Cons ( hd: Int, tl: IntList) extends IntList
     |
     |     val l1: IntList = Cons(1, Cons(3, Cons(5, End)))
     | }
t4: ()Unit

scala> t4()
java.lang.ClassFormatError: Duplicate field name&signature in class file Cons$3
  at java.lang.ClassLoader.defineClass1(Native Method)
  at java.lang.ClassLoader.defineClass(ClassLoader.java:760)
  at scala.reflect.internal.util.AbstractFileClassLoader.findClass(AbstractFileC
  at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
  at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
  at Cons$4$.apply(<console>:24)
  at .t4(<console>:26)
  ... 32 elided

Solution

  • Proved to be a known issue. Track it here: https://issues.scala-lang.org/browse/SI-5252