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

Compiler allows to super-call an abstract method. This leads to an AbstractMethodError at runtime. #4989

Closed
scabug opened this issue Sep 12, 2011 · 5 comments
Assignees
Milestone

Comments

@scabug
Copy link

scabug commented Sep 12, 2011

The following program compiles with no error message. At runtime, a java.lang.AbstractMethodError is thrown.

The point is, that method print defined in class A is overwritten in class B with an abstract definition.
In class C, which extends B, this abstract method is invoked with a super call. I expected a compilation error
on this super call.

object Test {

  class A {
    def print() { println("A"); }
  }
  
  abstract class B extends A {
    def print() : Unit
  }
  
  class C extends B {
    override def print() { println("C"); super.print(); }
  }

  def main(args: Array[String]): Unit = {
    val c = new C
    c.print
  }
}
@scabug
Copy link
Author

scabug commented Sep 12, 2011

Imported From: https://issues.scala-lang.org/browse/SI-4989?orig=1
Reporter: @dgruntz
Affected Versions: 2.9.1
Other Milestones: 2.10.0

@scabug
Copy link
Author

scabug commented May 13, 2012

@retronym said (edited on May 13, 2012 8:35:05 PM UTC):
A bit more analysis:

scala> abstract class A { def a() }; abstract class B extends A { def a() }
defined class A
defined class B

scala> object C extends B { override def a() = super.a() }
<console>:9: error: method a in class B is accessed from super. It may not be abstract unless it is overridden by a member declared `abstract' and `override'
       object C extends B { override def a() = super.a() }
                                                     ^

scala> import reflect.{mirror => m}
import reflect.{mirror=>m}

scala> m.classToType(classOf[B]).member(m.newTermName("a")).modifiers
res1: Set[scala.reflect.api.Modifier] = Set(deferred)

scala> class A { def a() {} }; abstract class B extends A { def a() }
defined class A
defined class B

scala> object C extends B { override def a() = super.a() }
defined module C

scala> m.classToType(classOf[B]).member(m.newTermName("a")).modifiers
res2: Set[scala.reflect.api.Modifier] = Set()

scala> C.a()
java.lang.AbstractMethodError: A.a()V
	at C$.a(<console>:10)
	at .<init>(<console>:12)
	at .<clinit>(<console>)

Key question: When does the symbol for method B#a lose the DEFERRED flag? Without it, the check in SuperAccessors doesn't catch this problem.

@scabug
Copy link
Author

scabug commented May 14, 2012

@paulp said:

Key question: When does the symbol for method B#a lose the DEFERRED flag?

It doesn't. When you request the member of B called a, you get the symbol defined in A, not in B, because concrete always overrides abstract. If you want B#a, use decl (or I guess it's called declaration in the library.)

@scabug
Copy link
Author

scabug commented Jun 17, 2012

@retronym said:
scala/scala#732

@scabug scabug closed this as completed Jun 24, 2012
@scabug
Copy link
Author

scabug commented Jun 24, 2012

@retronym said:
scala/scala@54c92ca7

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

2 participants