JDK 8 update 20中是否存在类型推断回归? [英] Is there a type inference regression in JDK 8 update 20?

查看:107
本文介绍了JDK 8 update 20中是否存在类型推断回归?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用以下代码时遇到问题:

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<>());

使用JDK 8直到更新11,此代码编译。
使用JDK 8 update 20,它不再编译。在最后一个语句中,我必须为最后一个 HashSet 实例化显式指定 String 类型参数。

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.

我想知道这段代码是否错误,或者是否是上次JDK更新中的回归。

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

推荐答案

这是JDK 8u20更新版本和当前JDK 9开发分支中存在的新错误。此代码之前有效,所以是的,这将被视为回归。 JDK langtools团队提交了以下错误报告:

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 嵌套调用的推理失败

从注释,似乎当前行为实际上符合规范(JLS),但行为显然是错误的,因此可能需要澄清规范。

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.

请注意,这是与其他StackOverflow问题中报告的错误类型推断错误 Java 1.8.20编译器错误,错误 JDK-8051402 。该错误已经修复,但修复程序不在JDK 8u20中。

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.

这篇关于JDK 8 update 20中是否存在类型推断回归?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆