RFR: JDK-8191842: JShell: Inferred type information is lost when assigning types to a "var"

Jan Lahoda jan.lahoda at oracle.com
Wed Dec 13 10:02:54 UTC 2017


Hi,

When doing:
var r = new Object() { String s; }

the type of "r" is the anonymous class itself. JShell handles this by 
"upgrading" the anonymous class to be a member class, so that it can be 
properly used from other snippets.

But JShell is not prepared for anonymous classes inside the expression 
(that are part of the resulting type), like:
---
jshell> var list = Stream.of(1, 2, 3).map(j -> new Object() { int i = j; 
}).collect(Collectors.toList());

jshell> list.forEach(a -> System.out.println(a.i));
---

So, part of the proposed fix is to upgrade all anonymous classes to 
member classes if needed. Also, intersection types are cleared from 
fields before writing (the intersection types are installed to ensure 
var semantics for cases like:
<Z extends Runnable & CharSequence> Z get1() { return null; }
var i1 = get1();
)

Bug: https://bugs.openjdk.java.net/browse/JDK-8191842
Webrev: http://cr.openjdk.java.net/~jlahoda/8191842/webrev.00/

Thanks,
     Jan



More information about the kulla-dev mailing list