Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a type inference regression in JDK 8 update 20?

Tags:

I have a problem with the following code:

public static <T> T firstNonNull(@Nullable T first, @Nullable T second) {     return first != null ? first : second; }  public static Set<String> getStrings() {    return new HashSet<>(); }  public static Set<String> doesNotCompile = firstNonNull(getStrings(), new HashSet<>()); 

With JDK 8 until update 11, this code compile. With JDK 8 update 20, it does not compile anymore. In the last statement, I have to explicitly specify the String type argument for the last HashSet instantiation.

I was wondering if I am wrong with this code or if it is a regression in the last JDK update.

like image 657
Benoit Courtine Avatar asked Aug 25 '14 16:08

Benoit Courtine


People also ask

Does Java have type inference?

Type inference is a Java compiler's ability to look at each method invocation and corresponding declaration to determine the type argument (or arguments) that make the invocation applicable.

What is the latest version of JDK 8?

The full version string for this update release is 8u331-b09 (where "b" means "build"). The version number is 8u331.

Is JDK 1.8 is same as JDK 8?

http://www.oracle.com/technetwork/java/javase/jdk8-naming-2157130.html Java SE Development Kit 8, also known as JDK 8, has the version number 1.8. In short – 8 is product version number and 1.8 is the developer version number (or internal version number). The product is the same, JDK 8, anyways.

How does type inference work in Java?

Type inference represents the Java compiler's ability to look at a method invocation and its corresponding declaration to check and determine the type argument(s). The inference algorithm checks the types of the arguments and, if available, assigned type is returned.


1 Answers

This is a new bug that exists in the JDK 8u20 update release and in the current JDK 9 development branch. This code worked before, so yes, this would be considered a regression. The JDK langtools team has filed the following bug report:

JDK-8055963 Inference failure with nested invocation

Judging from the comments, it appears that the current behavior actually conforms to the specification (the JLS), but the behavior is clearly wrong, so it might be the case that a clarification to the specification is necessary.

Note that this is a different type inference bug from the one reported in this other StackOverflow question Java 1.8.20 Compiler Error, bug JDK-8051402. That bug has been fixed already, although but the fix isn't in JDK 8u20.

like image 142
Stuart Marks Avatar answered Sep 22 '22 12:09

Stuart Marks