RFR: 8322003: JShell - Incorrect type inference in lists of records implementing interfaces [v2]
Jan Lahoda
jlahoda at openjdk.org
Fri Jan 5 20:00:57 UTC 2024
> 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
-------------
Changes: https://git.openjdk.org/jdk/pull/17223/files
Webrev: https://webrevs.openjdk.org/?repo=jdk&pr=17223&range=01
Stats: 121 lines in 3 files changed: 87 ins; 11 del; 23 mod
Patch: https://git.openjdk.org/jdk/pull/17223.diff
Fetch: git fetch https://git.openjdk.org/jdk.git pull/17223/head:pull/17223
PR: https://git.openjdk.org/jdk/pull/17223
More information about the compiler-dev
mailing list