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

constructor overloading with self type results in runtime error #4460

Closed
scabug opened this issue Apr 11, 2011 · 3 comments
Closed

constructor overloading with self type results in runtime error #4460

scabug opened this issue Apr 11, 2011 · 3 comments

Comments

@scabug
Copy link

scabug commented Apr 11, 2011

=== What steps will reproduce the problem (please be specific and use wikiformatting)? ===

  trait A

  class B(val x: Int) {
    self: A =>

    def this() = this()
  }

  val x = new B(2) with A
  val y = new B with A

and also

  trait A

  class B {
    self: A =>

    def this(x: Int) = this()
  }

  val x = new B(2) with A
  val y = new B with A

=== What is the expected behavior? ===
Since
val x = new B(1,2) with A
gives
compile error:

error: overloaded method constructor B with alternatives:
  (x: Int)B <and>
  ()B
 cannot be applied to (Int, Int)
       val x = new B(1,2) with A
                   ^

Either a class using the overloaded constructor or compile-time error (rather than runtime)

=== What do you see instead? ===
Runtime error!

java.lang.VerifyError: (class: B, method: <init> signature: ()V) Expecting to find unitialized object on stack

=== What versions of the following are you using? ===

  • Scala: 2.8.1 and 2.9.0RC1
  • Java: 1.6.0.24
  • Operating system: Ubuntu/scalabot
@scabug
Copy link
Author

scabug commented Apr 11, 2011

Imported From: https://issues.scala-lang.org/browse/SI-4460?orig=1
Reporter: scoder

@scabug
Copy link
Author

scabug commented Apr 12, 2011

@magarciaEPFL said:
The problem is the def this() = this(). Removing it does away with the VerifyError.

Rephrasing the example:

trait A

class B(val x: Int) {
  self: A =>

  def this() = this()
}

object C extends B(2) with A  {
  def main(args: Array[String]) {  }
}

we get the runtime error:

C:\temp\>java -verify -Xbootclasspath/a:Z:\scala-library.jar C
Exception in thread "main" java.lang.VerifyError: (class: B, method: <init> signature: ()V) Expecting to find unitialized object on stack
        at C.main(bt4.scala)

Regarding classfile B, the difference between including or not def this() = this() is the public B(); below:

C:\temp\>javap -c B
Compiled from "bt4.scala"
public class B extends java.lang.Object implements scala.ScalaObject{
public int x();
  // omitted 

public B(int);
  // omitted 

public B();
  Code:
   0:   getstatic       SI-27; //Field scala/Predef$$.MODULE$$:Lscala/Predef$$;
   3:   invokespecial   SI-28; //Method scala/Predef$$."<init>":()V
   6:   return

}

Classfile C (with or without def this() = this()) is:

C:\temp\>javap -c C
Compiled from "bt4.scala"
public final class C extends java.lang.Object{
public static final void main(java.lang.String[]);
  Code:
   0:   getstatic       SI-11; //Field C$$.MODULE$$:LC$$;
   3:   aload_0
   4:   invokevirtual   SI-13; //Method C$$.main:([Ljava/lang/String;)V
   7:   return

public static final int x();
  Code:
   0:   getstatic       SI-11; //Field C$$.MODULE$$:LC$$;
   3:   invokevirtual   SI-17; //Method C$$.x:()I
   6:   ireturn

}

@scabug
Copy link
Author

scabug commented Dec 5, 2012

@retronym said:
scala/scala#1689

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

No branches or pull requests

2 participants