Integrated: 8322003: JShell - Incorrect type inference in lists of records implementing interfaces

Jan Lahoda jlahoda at openjdk.org
Mon Jan 8 14:12:30 UTC 2024


On Tue, 2 Jan 2024 15:00:46 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]

This pull request has now been integrated.

Changeset: 57a65fe4
Author:    Jan Lahoda <jlahoda at openjdk.org>
URL:       https://git.openjdk.org/jdk/commit/57a65fe436a3617d64bbf0b02d4c7f7c2551448f
Stats:     121 lines in 3 files changed: 87 ins; 11 del; 23 mod

8322003: JShell - Incorrect type inference in lists of records implementing interfaces

Reviewed-by: vromero

-------------

PR: https://git.openjdk.org/jdk/pull/17223


More information about the compiler-dev mailing list