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

ClassFormatError: Duplicate field name&signature on val in trait and subclass #7645

Open
scabug opened this issue Jul 9, 2013 · 5 comments

Comments

@scabug
Copy link

scabug commented Jul 9, 2013

The compiler accepts the following code, but it fails at runtime with:

Exception in thread "main" java.lang.ClassFormatError: Duplicate field name&signature in class file Cl

trait T {
  val x = 5
}

class Cl extends T {
  private[this] val x = 7
}

object Example {
  def main(args: Array[String]) {
    new Cl
  }
}
@scabug
Copy link
Author

scabug commented Jul 9, 2013

Imported From: https://issues.scala-lang.org/browse/SI-7645?orig=1
Reporter: Perun (perun)
Affected Versions: 2.10.2, 2.11.0-M3, 2.12.0
See #5252

@scabug
Copy link
Author

scabug commented Jul 9, 2013

@paulp said:
Two fields named x in Cl. If the trait x is private and the class x is public, scalac figures it out and generates

private final int x;
private final int T$$x;

The class should verify that its private members don't share a name with inherited ones.

@scabug
Copy link
Author

scabug commented Mar 13, 2014

Claudio Bley (cb) said (edited on Mar 13, 2014 2:43:53 PM UTC):
A more subtle instance of the same problem arises when a trait and a class close over the same variable:

{code:java|borderStyle=solid}
class Example {}

def foo(): Unit = {
val s = "TEST"

trait T {
def x(): Int = s.length // <- s is here
}

def construct = new Example with T {
def y(): Unit = println(s) // <- and here
}
}
{code}

@scabug
Copy link
Author

scabug commented Oct 18, 2015

Matthias Langer (mlangc) said:
This issue can also be triggered by this code with Scala 2.11.7:

object Bug {
  def main(args: Array[String]): Unit = {
    sealed trait Step {
      def back: Step = this match {
        case StepDown => StepBack(0)
        case StepBack(level) => StepBack(level + 1)
      }

      def targetSize(originalSize: Int): Int = this match {
        case StepDown => originalSize/2
        case StepBack(level) =>
          val baseLevel = {
            if (level == 0) StepDown.targetSize(originalSize)
            else StepBack(level-1).targetSize(originalSize)
          }

          val gap = (originalSize - baseLevel)
          baseLevel + gap/2
      }

      def isExhausted(originalSize: Int): Boolean = {
        targetSize(originalSize) == originalSize - 1
      }
    }

    case object StepDown extends Step
    case class StepBack(level: Int) extends Step

    println(StepDown.back)
  }
}

@scabug
Copy link
Author

scabug commented Nov 11, 2016

@SethTisue said:
Another riff on the basic #5252 theme.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant