Bug 103672 - compiler problem VARARGS_ARGUMENT_NEED_CAST only explains one of two solutions
Summary: compiler problem VARARGS_ARGUMENT_NEED_CAST only explains one of two solutions
Status: VERIFIED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.1   Edit
Hardware: All All
: P3 trivial (vote)
Target Milestone: 4.4 M4   Edit
Assignee: Markus Keller CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2005-07-13 12:03 EDT by Markus Keller CLA
Modified: 2014-01-03 06:28 EST (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Markus Keller CLA 2005-07-13 12:03:11 EDT
3.1

The compiler problem COMPILER_PB_VARARGS_ARGUMENT_NEED_CAST is a bit misleading,
since it only explains one of two solutions to the underlying problem (see bug
103667) when it says:
"Varargs argument null should be cast to Object[] when passed to the method
addElements(Object...) from type VarargExample".

The compiler preference page calls this 'Inexact type match for varargs
argument', and I think the error message should rather explain this problem than
propose a solution.

My proposal for a more neutral error message:
"Varargs argument null does not match argument type Object[] of method 
 addElements(Object...) from type VarargExample".
Comment 1 Philipe Mulet CLA 2006-06-08 09:22:16 EDT
Error message got improved during 3.2 cycle, it now reads:

----------
1. WARNING in d:\eclipse\workspaces\dev3.2.1\plugins\Crap\src\X.java
 (at line 11)
	array(new Integer[] {5, 6}); //warns (as javac does)
	^^^^^^^^^^^^^^^^^^^^^^^^^^^
The argument of type Integer[] should explicitly be cast to Serializable[] for the invocation of the varargs method array(Serializable...) from type X. It could alternatively be cast to Serializable for a varargs invocation
----------
2. WARNING in d:\eclipse\workspaces\dev3.2.1\plugins\Crap\src\X.java
 (at line 12)
	array(null); //warns (as javac does)
	^^^^^^^^^^^
The argument of type null should explicitly be cast to Serializable[] for the invocation of the varargs method array(Serializable...) from type X. It could alternatively be cast to Serializable for a varargs invocation
----------


on the following testcase

import java.io.Serializable;
import java.util.Arrays;

public class X {
    static void array(Serializable... items) {
        System.out.print(Arrays.deepToString(items));
    }
    @SuppressWarnings({"boxing"})
    public static void main(String[] args) {
        array(new Serializable[] {3, 4});
        array(new Integer[] {5, 6}); //warns (as javac does)
        array(null); //warns (as javac does)
    }
}
Comment 2 Philipe Mulet CLA 2006-06-08 09:22:46 EDT
Closing, pls reopen if you dislike the new message
Comment 3 Dani Megert CLA 2013-08-20 02:23:09 EDT
See bug 103667 comment 6 why this got reopened.
Comment 4 Markus Keller CLA 2013-12-05 10:48:01 EST
Fixed with http://git.eclipse.org/c/jdt/eclipse.jdt.core.git/commit/?id=366617dbc4fc1d9363db17217a7ae199f0f005ea


Regexes used to fix usages in test cases:

Search:
The argument of type ([\w\[\]]+) should explicitly be cast to ([\w\[\]]+) for the invocation of the varargs method (\w+)\(([^)]+)\) from type (\w+)\. It could alternatively be cast to ([\w\[\]]+) for a varargs invocation

Replace:
Type \1 of the last argument to method \3(\4) doesn't exactly match the vararg parameter type. Cast to \2 to confirm the non-varargs invocation, or pass individual arguments of type \6 for a varargs invocation.

Search:
The argument of type ([\w\[\]]+) should explicitly be cast to ([\w\[\]]+) for the invocation of the varargs constructor (\w+)\(([^)]+)\). It could alternatively be cast to ([\w\[\]]+) for a varargs invocation

Replace:
Type \1 of the last argument to constructor \3(\4) doesn't exactly match the vararg parameter type. Cast to \2 to confirm the non-varargs invocation, or pass individual arguments of type \5 for a varargs invocation.
Comment 5 Jay Arthanareeswaran CLA 2013-12-10 04:37:15 EST
I have this example:

import java.io.Serializable;
import java.util.Arrays;

public class X {
    static void array(Serializable... items) {
        System.out.print(Arrays.deepToString(items));
    }
    static void array(Object obj, Serializable... items) {
        System.out.print(Arrays.deepToString(items));
    }
    static void array(Object obj1, Object obj2, Serializable... items) {
        System.out.print(Arrays.deepToString(items));
    }
    public static void main(String[] args) {
        array(null, null);
    }
}

And it produces this warning:
"Type null of the last argument to method array(Object, Serializable...) doesn't exactly match the vararg parameter type. Cast to Serializable[] to confirm the non-varargs invocation, or pass individual arguments of type Serializable for a varargs invocation."

But the second suggestion doesn't make sense. In fact, it results in an error. I am wondering if we should just leave the suggestions out of the message and just report what the problem is.
Comment 6 Markus Keller CLA 2013-12-11 14:32:47 EST
I think the ecj warning needs some explanations, since this warning is not defined by the spec, cannot be disabled by the user, and is hard to understand.

Not every Java developer is aware of the complicated three-phase method resolution process, and without any further explanation, the pure problem statement looks quite odd. The current wording not only proposes solutions, but it also implies the default behavior if the warning is not addressed, and it explains that this warning is about varargs vs. non-varargs invocations. This information is important to guide developers to the correct fix for their situation.


The second suggestion ("pass individual arguments of type Serializable for a varargs invocation") does resolve the given problem, but that doesn't mean that this solution can't produce other problems.

Your example is a nice test case, but that doesn't justify a less helpful warning message for the common case (non-overloaded varargs method).

Of course, I'm open for improvements to the wording.
Comment 7 Jay Arthanareeswaran CLA 2013-12-15 22:45:00 EST
(In reply to Markus Keller from comment #6)
> Of course, I'm open for improvements to the wording.

Sorry, I don't have a better suggestion for the message without writing a full sentence for the error message :). I guess we can live with this unless someone complains about it.

Verified for 4.4 M4 with build I20131209-2000.