RFR: 8322003: JShell - Incorrect type inference in lists of records implementing interfaces [v2]
Vicente Romero
vromero at openjdk.org
Fri Jan 5 21:01:24 UTC 2024
On Fri, 5 Jan 2024 20:00:57 GMT, Jan Lahoda <jlahoda at openjdk.org> wrote:
>> Consider JShell snippet like:
>>
>> var v = List.of("", 0);
>>
>>
>> The type of `v` is along the lines of `List<Serializable&Comparable<? extends Serializable&Comparable<?>&java.lang.constant.Constable&java.lang.constant.ConstantDesc>&java.lang.constant.Constable&java.lang.constant.ConstantDesc>` - i.e. a List with an intersection type as a type argument.
>>
>> The problem is that JShell compiles snippets separately in separate classes. And hence `v` is upgraded to a field. But, the field cannot have the above non-denotable type, so its type is demoted to `List<Serializable>`. When the field is read when a subsequent snippet is processed, JShell tries to reconstitute the type, but a type with an intersection type as a type argument cannot be parsed by the parser. As a consequence, continuing with the example above:
>>
>> jshell> List<java.lang.constant.Constable> l = v;
>> | Error:
>> | incompatible types: java.util.List<java.io.Serializable> cannot be converted to java.util.List<java.lang.constant.Constable>
>> | List<java.lang.constant.Constable> l = v;
>> | ^
>>
>>
>> The proposed solution here is to enhance the parser used by JShell so that it can parse intersection types at any position. JShell then can reconstitute the type, and the above snippets work:
>>
>> jshell> var v = List.of("", 0);
>> v ==> [, 0]
>>
>> jshell> List<java.lang.constant.Constable> l = v;
>> l ==> [, 0]
>
> Jan Lahoda has updated the pull request with a new target base due to a merge or a rebase. The pull request now contains two commits:
>
> - Merge branch 'master' into JDK-8322003
> - 8322003: JShell - Incorrect type inference in lists of records implementing interfaces
lgtm
-------------
Marked as reviewed by vromero (Reviewer).
PR Review: https://git.openjdk.org/jdk/pull/17223#pullrequestreview-1806836651
More information about the kulla-dev
mailing list