Scala Programming Language
  1. Scala Programming Language
  2. SI-5380

java.lang.VerifyError: Inconsistent stack height (simple for loop/ foreach)

    Details

      Description

      This is a simple function:

      def parse: Int = {
        var texts = Array("1","2","3","123")
      
        //either foreach or for(text <- texts) DO raise same exception
        texts.foreach{ text =>
          return try { text.toInt } catch { case _ => -1 }
        }
        0
      }
      

      Please do not mind about the purpose of the function. I tried but could not know how to code to show the purpose. I shortened it from my code.

      The code can be compiled. It raises exception at runtime. I got this exception:

      java.lang.VerifyError: (class: $anonfun$parse$1, method: apply signature: (Ljava/lang/String;)Lscala/runtime/Nothing$;) Inconsistent stack height 0 != 3
              at .parse(<console>:11)
              at .<init>(<console>:9)
              at .<clinit>(<console>)
              at .<init>(<console>:11)
              at .<clinit>(<console>)
              at $print(<console>)
              at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
              at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
              at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
              at java.lang.reflect.Method.invoke(Method.java:601)
              at scala.tools.nsc.interpreter.IMain$ReadEvalPrint.call(IMain.scala:704)
              at scala.tools.nsc.interpreter.IMain$Request$$anonfun$14.apply(IMain.scala:920)
              at scala.tools.nsc.interpreter.Line$$anonfun$1.apply$mcV$sp(Line.scala:43)
              at scala.tools.nsc.io.package$$anon$2.run(package.scala:25)
              at java.lang.Thread.run(Thread.java:722)
      

      I guess this issue is similar to this one: https://issues.scala-lang.org/browse/SI-203

        Activity

        Show
        Miguel Garcia added a comment - Originally posted at http://stackoverflow.com/questions/8889489/scala-java-lang-verifyerror-with-simple-function
        Hide
        Miguel Garcia added a comment -

        It's an "Attempt to enter try block with non-empty stack"

        Input program:

          def main(args: Array[String]) { 
            for (x <- List(1))
              return try { 1 } catch { case _ => 0 }
            0
          }
        

        After cleanup:

            @SerialVersionUID(0) final <synthetic> class $anonfun$main$1 extends scala.runtime.AbstractFunction1 with Serializable {
        
              final def apply(x: Int): Nothing = 
                throw new scala.runtime.NonLocalReturnControl(
                  $anonfun$main$1.this.nonLocalReturnKey1$1, {
                var exceptionResult1: Object = _;
                try {
                  exceptionResult1 = {
                    1;
                    scala.runtime.BoxedUnit.UNIT
                  }
                } catch {
                  case _ => {
                    exceptionResult1 = {
                      0;
                      scala.runtime.BoxedUnit.UNIT
                    }
                  }
                };
                exceptionResult1
              });
        
              final <bridge> def apply(v1: Object): Object = $anonfun$main$1.this.apply(scala.Int.unbox(v1));
        
              <synthetic> <paramaccessor> private[this] val nonLocalReturnKey1$1: Object = _;
              def <init>(nonLocalReturnKey1$1: Object): anonymous class $anonfun$main$1 = {
                $anonfun$main$1.this.nonLocalReturnKey1$1 = nonLocalReturnKey1$1;
                $anonfun$main$1.super.<init>();
                ()
              }
            };
        

        What Scala.Net emits:

        .method public hidebysig virtual final instance class [mscorlib]System.Exception 
                apply(int32 x) cil managed
        {
          // Code size       52 (0x34)
          .maxstack  2
          .locals init (object V_0)
          IL_0000:  ldarg.0
          IL_0001:  ldfld      object ABC$/$anonfun$main$1::nonLocalReturnKey1$1
          IL_0006:  ldnull
          IL_0007:  stloc.0
          .try
          {
            IL_0008:  ldc.i4.1
            IL_0009:  pop
            IL_000a:  ldsfld     class [scalalib]scala.runtime.BoxedUnit [scalalib]scala.runtime.BoxedUnit::UNIT
            IL_000f:  stloc.0
            IL_0010:  leave      IL_002d
            IL_0015:  leave      IL_002d
          }  // end .try
          catch [mscorlib]System.Exception 
          {
            IL_001a:  pop
            IL_001b:  ldc.i4.0
            IL_001c:  pop
            IL_001d:  ldsfld     class [scalalib]scala.runtime.BoxedUnit [scalalib]scala.runtime.BoxedUnit::UNIT
            IL_0022:  stloc.0
            IL_0023:  leave      IL_002d
            IL_0028:  leave      IL_002d
          }  // end handler
          IL_002d:  ldloc.0
          IL_002e:  newobj     instance void [scalalib]scala.runtime.NonLocalReturnControl::.ctor(object,
                                                                                                  object)
          IL_0033:  throw
        } // end of method $anonfun$main$1::apply
        

        And the error message:

        ABC$+$anonfun$main$1::apply][offset 0x00000008] Attempt to enter a try block with nonempty stack.
        
        Show
        Miguel Garcia added a comment - It's an "Attempt to enter try block with non-empty stack" Input program: def main(args: Array[String]) { for (x <- List(1)) return try { 1 } catch { case _ => 0 } 0 } After cleanup: @SerialVersionUID(0) final <synthetic> class $anonfun$main$1 extends scala.runtime.AbstractFunction1 with Serializable { final def apply(x: Int): Nothing = throw new scala.runtime.NonLocalReturnControl( $anonfun$main$1.this.nonLocalReturnKey1$1, { var exceptionResult1: Object = _; try { exceptionResult1 = { 1; scala.runtime.BoxedUnit.UNIT } } catch { case _ => { exceptionResult1 = { 0; scala.runtime.BoxedUnit.UNIT } } }; exceptionResult1 }); final <bridge> def apply(v1: Object): Object = $anonfun$main$1.this.apply(scala.Int.unbox(v1)); <synthetic> <paramaccessor> private[this] val nonLocalReturnKey1$1: Object = _; def <init>(nonLocalReturnKey1$1: Object): anonymous class $anonfun$main$1 = { $anonfun$main$1.this.nonLocalReturnKey1$1 = nonLocalReturnKey1$1; $anonfun$main$1.super.<init>(); () } }; What Scala.Net emits: .method public hidebysig virtual final instance class [mscorlib]System.Exception apply(int32 x) cil managed { // Code size 52 (0x34) .maxstack 2 .locals init (object V_0) IL_0000: ldarg.0 IL_0001: ldfld object ABC$/$anonfun$main$1::nonLocalReturnKey1$1 IL_0006: ldnull IL_0007: stloc.0 .try { IL_0008: ldc.i4.1 IL_0009: pop IL_000a: ldsfld class [scalalib]scala.runtime.BoxedUnit [scalalib]scala.runtime.BoxedUnit::UNIT IL_000f: stloc.0 IL_0010: leave IL_002d IL_0015: leave IL_002d } // end .try catch [mscorlib]System.Exception { IL_001a: pop IL_001b: ldc.i4.0 IL_001c: pop IL_001d: ldsfld class [scalalib]scala.runtime.BoxedUnit [scalalib]scala.runtime.BoxedUnit::UNIT IL_0022: stloc.0 IL_0023: leave IL_002d IL_0028: leave IL_002d } // end handler IL_002d: ldloc.0 IL_002e: newobj instance void [scalalib]scala.runtime.NonLocalReturnControl::.ctor(object, object) IL_0033: throw } // end of method $anonfun$main$1::apply And the error message: ABC$+$anonfun$main$1::apply][offset 0x00000008] Attempt to enter a try block with nonempty stack.
        Hide
        Hai Bison added a comment -

        Thank you. However, I'll keep watching. I'm not meeting these knowledge...

        Show
        Hai Bison added a comment - Thank you. However, I'll keep watching. I'm not meeting these knowledge...
        Hide
        Paul Phillips added a comment -

        02e260a8e6

        Show
        Paul Phillips added a comment - 02e260a8e6

          People

          • Assignee:
            Miguel Garcia
            Reporter:
            Hai Bison
          • Votes:
            0 Vote for this issue
            Watchers:
            7 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development