bug icon indicating copy to clipboard operation
bug copied to clipboard

Import renaming incompatible with super, access modifiers, etc

Open scabug opened this issue 16 years ago • 3 comments

Consider the following code:

object wrap1 {
  trait A {
    def n = 3
  }
}

object wrap2 {
  trait A {
    def n = 7
  }
}

Suppose we want to inherit from both A's above. This produces an error, as expected:

// error: error overriding method n in trait A of type => Int;
//  method n in trait A of type => Int needs `override' modifier
trait B extends wrap1.A with wrap2.A

This, of course, is ambiguous

// error: ambiguous parent class qualifier
trait B extends wrap1.A with wrap2.A {
  override def n =
    super[A].n
}

Oops! This isn't allowed (as per the spec, but it's surprising)

// error: ']' expected but '.' found.
trait B extends wrap1.A with wrap2.A {
  override def n =
    super[wrap1.A].n
}

But the most surprising thing is that renaming doesn't work!

import wrap1.{A => A1}
import wrap2.{A => A2}

// error: A1 does not name a parent class of trait B
trait B extends A1 with A2 {
  override def n =
    super[A1].n
}

The same problem arises with access modifiers. Consider:

object Outer {
  object A {
    object A {
      import Outer.{A => RootA}
      
      // error: RootA is not an enclosing class
      private[RootA] trait B
    }
  }
}

scabug avatar Apr 23 '09 21:04 scabug

Imported From: https://issues.scala-lang.org/browse/SI-1915?orig=1 Reporter: @jorgeortiz85

scabug avatar Apr 23 '09 21:04 scabug

Still a problem in Scala 3 😞

joroKr21 avatar Oct 10 '21 23:10 joroKr21

Users run into this... as pointed out by @som-snytt here: https://users.scala-lang.org/t/invoking-super-in-case-of-identical-name/10114/7

bjornregnell avatar Jul 31 '24 08:07 bjornregnell