Skip to content

Outer check should use equals rather than eq in inner class #11940

@alexarchambault

Description

@alexarchambault

reproduction steps

// class Wrapper, inner class Foo
class Wrapper {
  // all Wrapper instances are "equals"
  override def equals(obj: Any) =
    obj != null && obj.isInstanceOf[Wrapper]

  case class Foo()
}

// create two Foo instances via two Wrapper instances
val wrapper1 = new Wrapper
val foo1 = new wrapper1.Foo()
val wrapper2 = new Wrapper
val foo2 = new wrapper2.Foo()

// get a hand on the outer reference of Foo
val outerFld = classOf[Wrapper#Foo].getDeclaredField("$outer")
outerFld.setAccessible(true)

// equality checks
val outerReferencesEqual = outerFld.get(foo1) == outerFld.get(foo2)
val instancesEqual = foo1 == foo2

problem

instancesEqual is false, even though outerReferencesEqual is true.

expectation

instancesEqual is true.

This is related to but not exactly the same problem as #4440 (comment). Here, we don't have the final modifier, so we don't run in #4440. The issue here is about the use of eq, rather than equals, to compare outer references.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions