Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4361645

Compiler generates provably dead code for finally containing return

XMLWordPrintable

    • generic, x86
    • generic, windows_98

      class Exn {

        Object myMethod() {
          Object retVal=null;
          try {
            System.out.println("try");
          } catch (Exception e) {
            System.out.println("catch");
          } finally {
            System.out.println("finally");
            return retVal;
          }
        }

      }

      javac 1.2.2 generates the following:

      Method myMethod:"()Ljava/lang/Object;"
      stack 2 locals 2
      {
      aconst_null;
      astore_1;
      try t13, t25;
      getstatic Field java/lang/System.out:"Ljava/io/PrintStream;";
      ldc String "try";
      invokevirtual Method java/io/PrintStream.println:"(Ljava/lang/String;)V";
      endtry t13;
      goto L26;
      catch t13 java/lang/Exception;
      pop;
      getstatic Field java/lang/System.out:"Ljava/io/PrintStream;";
      ldc String "catch";
      invokevirtual Method java/io/PrintStream.println:"(Ljava/lang/String;)V";
      goto L26;
      endtry t25;
      catch t25 #0;
      pop;
      L26: getstatic Field java/lang/System.out:"Ljava/io/PrintStream;";
      ldc String "finally";
      invokevirtual Method java/io/PrintStream.println:"(Ljava/lang/String;)V";
      aload_1;
      areturn;
      }

      javac 1.3 generates jsr instructions to a subroutine that can
      never complete normally (could use goto instead), and leaves
      a spurious unreachable return at the end.

      Method myMethod:"()Ljava/lang/Object;"
      stack 2 locals 5
      {
      aconst_null;
      astore_1;
      try t16, t31;
      getstatic Field java/lang/System.out:"Ljava/io/PrintStream;";
      ldc String "try";
      invokevirtual Method java/io/PrintStream.println:"(Ljava/lang/String;)V";
      endtry t16;
      jsr L37;
      goto L49;
      catch t16 java/lang/Exception;
      astore_2;
      getstatic Field java/lang/System.out:"Ljava/io/PrintStream;";
      ldc String "catch";
      invokevirtual Method java/io/PrintStream.println:"(Ljava/lang/String;)V";
      jsr L37;
      goto L49;
      endtry t31;
      catch t31 #0;
      astore_3;
      jsr L37;
      aload_3;
      athrow;
      L37: astore 4;
      getstatic Field java/lang/System.out:"Ljava/io/PrintStream;";
      ldc String "finally";
      invokevirtual Method java/io/PrintStream.println:"(Ljava/lang/String;)V";
      aload_1;
      areturn;
      L49: return;
      }

      javac 1.4 adopts a different scheme for compiling try-finally in order
      to eliminate a bug in the handling of asynchronous exceptions, however,
      the spurious return remains:

      Method myMethod:"()Ljava/lang/Object;"
      stack 2 locals 5
      {
      aconst_null;
      astore_1;
      try t16, t31;
      getstatic Field java/lang/System.out:"Ljava/io/PrintStream;";
      ldc String "try";
      invokevirtual Method java/io/PrintStream.println:"(Ljava/lang/String;)V";
      endtry t16;
      jsr L37;
      endtry t31;
      goto L49;
      catch t16 java/lang/Exception;
      try t31;
      astore_2;
      getstatic Field java/lang/System.out:"Ljava/io/PrintStream;";
      ldc String "catch";
      invokevirtual Method java/io/PrintStream.println:"(Ljava/lang/String;)V";
      jsr L37;
      endtry t31;
      goto L49;
      catch t31 #0;
      catch t31 #0;
      catch t31 #0;
      try t31;
      astore_3;
      jsr L37;
      endtry t31;
      aload_3;
      athrow;
      L37: astore 4;
      getstatic Field java/lang/System.out:"Ljava/io/PrintStream;";
      ldc String "finally";
      invokevirtual Method java/io/PrintStream.println:"(Ljava/lang/String;)V";
      aload_1;
      areturn;
      L49: return;
      }

      william.maddox@Eng 2000-08-10

            gafter Neal Gafter (Inactive)
            wmaddoxsunw William Maddox (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: