Bug 177194 - [1.5][compiler] preserveAllLocals option has undesirable side-effect when invoking generic method
Summary: [1.5][compiler] preserveAllLocals option has undesirable side-effect when inv...
Status: VERIFIED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.3   Edit
Hardware: PC Windows XP
: P3 normal (vote)
Target Milestone: 3.3 M6   Edit
Assignee: Philipe Mulet CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2007-03-13 15:34 EDT by Olivier Thomann CLA
Modified: 2007-03-20 09:51 EDT (History)
1 user (show)

See Also:


Attachments
In progress patch (8.77 KB, patch)
2007-03-13 19:08 EDT, Philipe Mulet CLA
no flags Details | Diff
Proposed fix (24.58 KB, patch)
2007-03-13 20:54 EDT, Olivier Thomann CLA
no flags Details | Diff
Proposed patch (46.50 KB, patch)
2007-03-15 08:02 EDT, Philipe Mulet CLA
no flags Details | Diff
Better patch (49.02 KB, patch)
2007-03-15 09:03 EDT, Philipe Mulet CLA
no flags Details | Diff
Proposed patch for 3.2.x (47.38 KB, patch)
2007-03-15 13:50 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 2007-03-13 15:34:48 EDT
Using v741, the following code doesn't fail with a ClassCastException if the unused local is optimized out. If -preserveAllLocals is set, then the code fails as expected.

Test case:
class A<T> {
	public T foo(Object o) {
		return (T) o; // should get unchecked warning
	}
}

public class X {
	public static void main(String[] args) {
		A<X> a = new A<X>();
		X s = a.foo(new Object());
	}
}

Actual output after compilation:
----------
1. WARNING in D:\tests_sources\X.java (at line 3)
	return (T) o; // should get unchecked warning
	       ^^^^^
Type safety: Unchecked cast from Object to T
----------
2. WARNING in D:\tests_sources\X.java (at line 10)
	X s = a.foo(new Object());
	  ^
The local variable s is never read
----------
2 problems (2 warnings)
This is expected.

At runtime, a class cast exception is expected. So the -preserveAllLocals option has a side-effect at runtime.
Comment 1 Philipe Mulet CLA 2007-03-13 18:40:23 EDT
Other scenario broken as well:
class A<T> {
        public T foo;
}

public class X {
        public static void main(String[] args) {
                A<X> a = new A<X>();
				 A ua = a;
				 ua.foo = new Object();
                try {
	                X s = a.foo;
                } catch(ClassCastException e) {
                	System.out.println("SUCCESS");
                	return;
                }
            	System.out.println("FAILED");
        }
}
Comment 2 Philipe Mulet CLA 2007-03-13 18:54:47 EDT
Other variation:
class A<T> {
        public T foo;
}

public class X extends A<X>{
        public static void main(String[] args) {
			new X().foo();
		 }
 		 public void foo() {
				 A ua = this;
				 ua.foo = new Object();
                try {
	                X s = foo;
                } catch(ClassCastException e) {
                	System.out.println("SUCCESS");
                	return;
                }
            	System.out.println("FAILED");
        }
}
Comment 3 Philipe Mulet CLA 2007-03-13 18:55:18 EDT
More:
class A<T> {
        public T foo;
}

public class X extends A<X>{
        public static void main(String[] args) {
			new X().foo();
		 }
 		 public void foo() {
				 A ua = this;
				 ua.foo = new Object();
                try {
	                X s = this.foo;
                } catch(ClassCastException e) {
                	System.out.println("SUCCESS");
                	return;
                }
            	System.out.println("FAILED");
        }
}
Comment 4 Philipe Mulet CLA 2007-03-13 19:08:35 EDT
Created attachment 60750 [details]
In progress patch

Olivier - pls check test1111 which seems to raise a stackmap issue (verifyError)

(patch is still incomplete - need support for qname, allocation)
Comment 5 Olivier Thomann CLA 2007-03-13 20:54:26 EDT
Created attachment 60757 [details]
Proposed fix

This patch is fixing the VerifyError issue in the SingleNameReference by adding a extra pop of the required value.
This patch replaces the previous patch for the org.eclipse.jdt.core plugin. It doesn't contain the regression tests added in GenericTypeTests.
Comment 6 Philipe Mulet CLA 2007-03-15 07:59:58 EDT
Added GenericTypeTest#test1111-test1117.

Comment 7 Philipe Mulet CLA 2007-03-15 08:02:59 EDT
Created attachment 60920 [details]
Proposed patch

Includes fix for bug 177372
Comment 8 Philipe Mulet CLA 2007-03-15 09:03:18 EDT
Created attachment 60927 [details]
Better patch

better tests
Comment 9 Philipe Mulet CLA 2007-03-15 13:49:24 EDT
Released for 3.3M6

backporting to 3.2 maintenance
Comment 10 Philipe Mulet CLA 2007-03-15 13:50:12 EDT
Created attachment 60990 [details]
Proposed patch for 3.2.x
Comment 11 Olivier Thomann CLA 2007-03-20 09:51:56 EDT
Verified for 3.3 M6 using build I20070320-0010