Bug 139931 - [1.5][compiler] Unnecessary cast warning and varargs
Summary: [1.5][compiler] Unnecessary cast warning and varargs
Status: VERIFIED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.2   Edit
Hardware: PC Linux
: P3 normal (vote)
Target Milestone: 3.2 RC4   Edit
Assignee: Philipe Mulet CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2006-05-03 08:38 EDT by Pekka Enberg CLA
Modified: 2006-05-11 22:09 EDT (History)
2 users (show)

See Also:


Attachments
Regression test (5.67 KB, patch)
2006-05-03 12:37 EDT, Olivier Thomann CLA
no flags Details | Diff
Proposed patch (6.00 KB, patch)
2006-05-03 18:32 EDT, Philipe Mulet CLA
no flags Details | Diff
Better patch (2.37 KB, patch)
2006-05-09 04:43 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 Pekka Enberg CLA 2006-05-03 08:38:18 EDT
I upgraded from 3.2 M5a to 3.2 RC2 and noticed a problem with unnecessary cast warning decetion.  I can't get the following example to compile cleanly with 3.2 RC2.  The one with 'extra' cast worked fine with 3.2 M5a.

public class EclipseCastWarningTest extends TestCase {
    private Method method;
    
    @Override
    protected void setUp() throws Exception {
        method = Long.class.getMethods()[0];
    }
    
    public void testShouldNotProduceWarning() {
        // Unnecessary cast from Class<?>[] to Class[]
        Long.class.getMethod("hello", (Class[]) method.getParameterTypes());
    }

    public void testShouldProduceWarning() throws Exception {
        // The argument of type Class<?>[] should explicitly be cast to
        // Class[] for the invocation of the varargs method getMethod(String,
        // Class...) from type Class<Long>. It could alternatively be cast to
        // Class for a varargs invocation.
        Long.class.getMethod("hello", method.getParameterTypes());
    }
}
Comment 1 Jerome Lanneluc CLA 2006-05-03 09:15:55 EDT
Standalone test case:

import java.lang.reflect.Method;
public class EclipseCastWarningTest {
    private Method method;

    public void testShouldNotProduceWarning() throws SecurityException, NoSuchMethodException {
        // Unnecessary cast from Class<?>[] to Class[]
        Long.class.getMethod("hello", (Class[]) this.method.getParameterTypes());
    }

    public void testShouldProduceWarning() throws Exception {
        // The argument of type Class<?>[] should explicitly be cast to
        // Class[] for the invocation of the varargs method getMethod(String,
        // Class...) from type Class<Long>. It could alternatively be cast to
        // Class for a varargs invocation.
        Long.class.getMethod("hello", this.method.getParameterTypes());
    }
}
Comment 2 Jerome Lanneluc CLA 2006-05-03 09:31:16 EDT
Even simpler test case:
public class X {
	Y<?> [] foo() {
		return null;
	}
	void bar(Y... y) {
	}
	void fred() {
		bar(foo());
		bar((Y[])foo());
	}
}
class Y<E> {
}
Comment 3 Jerome Lanneluc CLA 2006-05-03 09:41:55 EDT
In 3.1.2, there is no warning for:
  bar((Y[])foo());
Comment 4 Olivier Thomann CLA 2006-05-03 12:37:05 EDT
Created attachment 40256 [details]
Regression test
Comment 5 Philipe Mulet CLA 2006-05-03 13:58:07 EDT
These are actually 2 places to fix. 
One in the method arguments invocation check to tune the test for varargs cast insertion.
The other in CastExpression check for unnecessary warning; following same semantics.

Comment 6 Philipe Mulet CLA 2006-05-03 18:32:34 EDT
Created attachment 40311 [details]
Proposed patch
Comment 7 Philipe Mulet CLA 2006-05-03 18:33:39 EDT
Need to test the fix extensively. A bit risky.
Comment 8 Philipe Mulet CLA 2006-05-09 04:41:20 EDT
Actually the cast in "bar((Y[])foo());" should still be reported as unnecessary.
Comment 9 Philipe Mulet CLA 2006-05-09 04:43:00 EDT
Created attachment 40688 [details]
Better patch
Comment 10 Philipe Mulet CLA 2006-05-11 06:00:22 EDT
Martin - does the presence of the extra warning alter JDT/UI behaviour ? i.e. bogus cleanup/refactoring etc... I have a fix I believe is right at hand.
Comment 11 Philipe Mulet CLA 2006-05-11 06:05:57 EDT
Dani/Martin - may consider for 3.2RC4 if it has ramifications in your layer.
Comment 12 Martin Aeschlimann CLA 2006-05-11 06:06:08 EDT
I support the fix as clean up works on the whole project and will remove the supposedly unnecessary casts and might introduce errors that users won't find that easely.
Comment 13 Philipe Mulet CLA 2006-05-11 06:17:57 EDT
Ok, candidating for RC4.

Dani - I need an additional vote
Comment 14 Dani Megert CLA 2006-05-11 06:20:17 EDT
The patch makes sense. Approving for 3.2 RC4.
Comment 15 Philipe Mulet CLA 2006-05-11 06:29:50 EDT
Fix released. Added VarargsTest#test048
Comment 16 Olivier Thomann CLA 2006-05-11 22:09:56 EDT
Verified with I20060511-2000 for 3.2RC4