为什么编译此代码会导致编译器堆栈溢出? [英] Why does compiling this code cause a compiler stack overflow?

查看:482
本文介绍了为什么编译此代码会导致编译器堆栈溢出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

interface Pong<T> {}
class Ping<T> implements Pong<Pong<? super Ping<Ping<T>>>> {
    static void Ping() {
        Pong<? super Ping<Long>> Ping = new Ping<Long>();
    }
}

尝试编译这会产生错误:

Trying to compile this gives the error:

The system is out of resources.
Consult the following stack trace for details.
java.lang.StackOverflowError
    at com.sun.tools.javac.code.Types$23.visitClassType(Types.java:2579)
    at com.sun.tools.javac.code.Type$ClassType.accept(Type.java:554)
    at com.sun.tools.javac.code.Types$UnaryVisitor.visit(Types.java:3260)
    at com.sun.tools.javac.code.Types$23.visitClassType(Types.java:2592)
    at com.sun.tools.javac.code.Types$23.visitClassType(Types.java:2579)
    at com.sun.tools.javac.code.Type$ClassType.accept(Type.java:554)
    ...

代码由github上的etorreborre提供。

推荐答案

显然,这是Java编译器中的一个错误。编译器不应该崩溃,尤其是在程序如此之小的情况下。

Clearly, it is a bug in the Java compiler. The compiler shouldn't crash, especially on a program so small.

甚至可能是Java语言规范中的漏洞;即JLS作者未考虑的泛型中的一个模糊边缘案例。

It could even be a hole in the Java Language Specification; i.e. an obscure edge case in generics that the JLS authors haven't considered.

但是(IMO)这只不过是一种好奇心,除非你能想出一个打破编译器的例子并不那么明显设想。我的意思是,这个示例代码并不完全有意义......

But (IMO) this is nothing more than a curiosity, unless you can come up with an example that isn't so obviously contrived to break the compiler. I mean, this example code isn't exactly meaningful ...

对Java编译器实现有深刻理解的人可能会弄清楚为什么会导致堆栈溢出。但除非那个人也要修复这个bug,否则它几乎不相关。除非有人能够提出一个触发同样问题的有意义的例子,否则我看不出修复它的任何价值。

Someone with a deep understanding of the Java compiler's implementation could probably figure out why this causes a stack overflow. But it is hardly relevant unless that person is also going to fix the bug. And unless someone can come up with a meaningful example that triggers the same problem, I can't see any value in fixing it.

这篇关于为什么编译此代码会导致编译器堆栈溢出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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