Need help with error message
Boaz Nahum
boaznahum at gmail.com
Mon Sep 9 12:29:47 PDT 2013
Boaz checked its saying twice:
This code is old code compiled with JDK 7 1.7.0_04
He also compiled the test before sending the email.
On Sep 9, 2013 8:41 PM, "Alex Buckley" <alex.buckley at oracle.com> wrote:
> Hi Vicente,
>
> When 'keys' is passed to getList, the method get(List<Integer>) is
> applicable by subtyping (JLS 15.12.2.2). This is because we allow an
> unchecked conversion to be performed on the type of 'keys', from List to
> List<Integer>. javac does this correctly.
>
> Having chosen getList(List<Integer>) as the most specific method, we must
> determine its return type. Since unchecked conversion was necessary for the
> method to be applicable, the return type is the erasure of the method's
> declared return type (JLS 15.12.2.6). So, the return type of
> getList(List<Integer>) is List in your code.
>
> The get(0) invocation thus has a return type of Object. That's not
> assignment-compatible with String, so the JDK8 error is correct.
>
> Boaz says no error was given in JDK7, but I am skeptical of that.
>
> Separately, if the result of getList(List<Integer>) is immediately
> assigned to a List<String> variable, then of course the get(0) invocation
> returns String and there is no error.
>
> Alex
>
> On 9/9/2013 7:25 AM, Vicente-Arturo Romero-Zaldivar wrote:
>
>> Hi Boaz,
>>
>> Ok let's make sure we are talking about the same thing.
>>
>> I have this code:
>>
>> MyData.java
>>
>> import java.util.*;
>>
>> class MyData {
>>
>> public List<String> getList(List<Integer> keys) {
>> int i = keys.get(0);
>> return null;
>> }
>>
>> MyData data = new MyData();
>>
>> //this is wrong, should be List<Integer>
>> List keys = new ArrayList();
>>
>> String item= data.getList(keys).get(0);
>> }
>>
>> if compiled with last tl/langtools, not lambda, here I could have used
>> bold or capital letters as you did but I think it's not necessary. I got
>> the following:
>>
>> MyData.java:15: error: incompatible types: Object cannot be converted to
>> String
>> String item= data.getList(keys).get(0);
>> ^
>> Note: MyData.java uses unchecked or unsafe operations.
>> Note: Recompile with -Xlint:unchecked for details.
>> 1 error
>>
>> the if I compile with -Xlint:unchecked**as the compiler is suggesting I
>> get:
>>
>> MyData.java:15: warning: [unchecked] unchecked method invocation: method
>> getList in class MyData is applied to given types
>> String item= data.getList(keys).get(0);
>> ^
>> required: List<Integer>
>> found: List
>> MyData.java:15: warning: [unchecked] unchecked conversion
>> String item= data.getList(keys).get(0);
>> ^
>> required: List<Integer>
>> found: List
>> MyData.java:15: error: incompatible types: Object cannot be converted to
>> String
>> String item= data.getList(keys).get(0);
>> ^
>> 1 error
>> 2 warnings
>>
>> I guess I have reproduced you test case, let me know if you consider
>> that there is something missing.
>>
>> Thanks,
>> Vicente
>>
>>
>>
>> On 09/09/13 14:55, Boaz Nahum wrote:
>>
>>> Thanks.
>>> But I think You ignore my question.
>>> Sure the compiler should warn on unchecked. That what it does in JDK
>>> 7, but in JDK 8 it doesn't. Instead it raise an error on the
>>> assignment of the result of get(0) to String.
>>>
>>> You also ignored the fact the splitting the code into two lines
>>> eliminated the error message although the unchecked call, and
>>> _*doesn't produce unchecked warning message.*_
>>>
>>> IMHO it is compiler bug.
>>>
>>>
>>>
>>>
>>> On Mon, Sep 9, 2013 at 4:43 PM, Vicente-Arturo Romero-Zaldivar
>>> <vicente.romero at oracle.com <mailto:vicente.romero at oracle.**com<vicente.romero at oracle.com>>>
>>> wrote:
>>>
>>> Hi Boaz,
>>>
>>> The compiler is indicating that there are unchecked warnings in
>>> the code, if you don't solve them and go on, then you can't expect
>>> the compiler to do all the checking or figure out what the correct
>>> type should be.
>>>
>>> Here the code should be:
>>>
>>> List<Integer> keys = new ArrayList<>(); or:
>>>
>>> List<Integer> keys = new ArrayList<Integer>()
>>>
>>> also lambda repo is not up to date anymore, you should use
>>> tl/langtools instead.
>>>
>>> Thanks,
>>> Vicente
>>>
>>>
>>> On 09/09/13 13:53, Boaz Nahum wrote:
>>>
>>>> Build lambda/lambda repo today.
>>>>
>>>> The compiler error is strange
>>>>
>>>> public List<String> getList(List<Integer> keys) {
>>>> return null;
>>>> }
>>>>
>>>> MyData data = new MyData();
>>>>
>>>> //this is wrong, should be List<Integer>
>>>> List keys = new ArrayList();
>>>>
>>>> String item= data.getList(keys).get(0);
>>>>
>>>> The error message is:
>>>> java: incompatible types: java.lang.Object cannot be converted to
>>>> java.lang.String
>>>>
>>>> But if I split it to:
>>>> List<String> list = data.getList(keys);
>>>> String item = list.get(0);
>>>>
>>>> then it compile fine.
>>>>
>>>> The error message remind smells like compiler treats getList as
>>>> generic method.
>>>>
>>>> Thanks
>>>> Boa
>>>>
>>>>
>>>
>>>
>>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.openjdk.java.net/pipermail/compiler-dev/attachments/20130909/fffeca32/attachment.html
More information about the compiler-dev
mailing list