Bug 155423 - [compiler] Unoptimal code generation when an initializer contains a statement that returns
Summary: [compiler] Unoptimal code generation when an initializer contains a statement...
Status: VERIFIED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.2   Edit
Hardware: PC Windows XP
: P3 normal (vote)
Target Milestone: 3.2.1   Edit
Assignee: Philipe Mulet CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2006-08-28 12:45 EDT by Olivier Thomann CLA
Modified: 2006-09-18 12:41 EDT (History)
1 user (show)

See Also:


Attachments
Proposed patch (45.17 KB, patch)
2006-08-29 05:52 EDT, Philipe Mulet CLA
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Olivier Thomann CLA 2006-08-28 12:45:24 EDT
1) Comment out lines 671-672-673 in org.eclipse.jdt.internal.compiler.ast.TypeDeclaration.

2) Compile this code:
[public class X {
	{
		if (true) throw new NullPointerException();
	}
}]

3) You get:
  public X();
     0  aload_0 [this]
     1  invokespecial java.lang.Object() [8]
     4  new java.lang.NullPointerException [10]
     7  dup
     8  invokespecial java.lang.NullPointerException() [12]
    11  athrow
    12  return

The return bytecode is unreachable.
Comment 1 Philipe Mulet CLA 2006-08-28 12:53:12 EDT
Problem is actually more general than the extra #return instruction.
e.g.
public class X {
   {
      if (true) throw new NullPointerException();
   }
   X() {
      System.out.println();
   }
}

The #println() statement could also be optimized out.
Comment 2 Philipe Mulet CLA 2006-08-29 05:19:16 EDT
Need to optimize codegen for statements, but not for explicit constructor call which is always emitted.

e.g.
class Y {
	Y(Object o) {
		System.out.print(o);
	}
}

public class X extends Y {
	{
		if (true)
			throw new NullPointerException();
	}

	X() {
		super(new Object() {
			public String toString() {
				return "SUCCESS:";
			}
		});
		System.out.println();
	}
	public static void main(String[] args) {
		try {
			new X();
		} catch(NullPointerException e) {
			System.out.println("caught:NPE");
		}
	}
}
Comment 3 Philipe Mulet CLA 2006-08-29 05:52:12 EDT
Created attachment 48948 [details]
Proposed patch
Comment 4 Philipe Mulet CLA 2006-08-29 07:08:26 EDT
Released patch for 3.3M2.
Comment 5 Philipe Mulet CLA 2006-08-29 07:11:38 EDT
Added FlowAnalysisTest#test028-033
Comment 6 Olivier Thomann CLA 2006-08-30 12:01:00 EDT
Needs to be backported to 3.2.1 in order to fix test case in comment 1.
Comment 7 Philipe Mulet CLA 2006-08-31 04:28:04 EDT
Fixed.

Released for 3.2.1
Released for 3.3 M2

Olivier - pls release a stackmap regression test for test case in comment 1
Comment 8 Olivier Thomann CLA 2006-08-31 17:27:13 EDT
Added org.eclipse.jdt.core.tests.compiler.regression.StackMapAttributeTest#test021  in HEAD and 3.2 maintenance stream.
Comment 9 David Audel CLA 2006-09-11 12:15:42 EDT
Verified for 3.2.1 using build M20060908-1655
Comment 10 Frederic Fusier CLA 2006-09-18 12:41:57 EDT
Verified for 3.3 M2 using build I20060918-0010.