mike-neckのブログ

Java or Groovy or Swift or Golang

ラムダでいろいろやってたら、コンパイラーのバグにハマった件

こんにちわ、みけです。

表題の件ですが、すでにOpenJDKにバグ登録されて、ステータスがfixedになっているのですが、 まあしらなかったのでハマってしまいました。

JDK-8054210 NullPointerException when compiling specific code.

バグの出るコード例

タイプパラメーターで指定した型の例外を送出できるラムダと、送出される例外(型パラメーターで指定)を引数に取る別のラムダを二つ準備します。 そして、この二つのラムダを引数にとるメソッドを使うようなコードをコンパイルすると発生するようです。

// 例外を送出するラムダ
@FunctionalInterface
public interface <E extends Exception> Task<E> {
    public void run() throws E;
}

//例外を引数に取るラムダ
@FunctionalInterface
public interface <E> Handler<E> {
    public void handle(E e);
}

public class Some {
    public <E extends Exception> void tryAndHandle(Task<E> task, Handler<E> handler) {
        try {
            task.run();
        } catch (Exception e) {
            handler.handle(e);
        }
    }
}

public class Main {
    public static void main(String... args) {
        new Some().tryAndHandle(() -> {throw new Exception();}, System.out::println);
    }
}

出てくるエラー

Information:Using javac 1.8.0_25 to compile java sources
Information:java: コンパイラで例外が発生しました(1.8.0_25)。Bug Paradeで重複がないかをご確認のうえ、Java Developer Connection (http://java.sun.com/webapps/bugreport)でbugの登録をお願いいたします。レポートには、そのプログラムと下記の診断内容を含めてください。ご協力ありがとうございます。
Information:java: java.lang.NullPointerException
Information:java:   at com.sun.tools.javac.code.Scope.includes(Scope.java:296)
Information:java:   at com.sun.tools.javac.comp.Flow$1.trackable(Flow.java:247)
Information:java:   at com.sun.tools.javac.comp.Flow$AbstractAssignAnalyzer.visitVarDef(Flow.java:1832)
Information:java:   at com.sun.tools.javac.comp.Flow$AssignAnalyzer.visitVarDef(Flow.java:2569)
Information:java:   at com.sun.tools.javac.tree.JCTree$JCVariableDecl.accept(JCTree.java:852)
Information:java:   at com.sun.tools.javac.tree.TreeScanner.scan(TreeScanner.java:49)
Information:java:   at com.sun.tools.javac.comp.Flow$BaseAnalyzer.scan(Flow.java:398)
Information:java:   at com.sun.tools.javac.comp.Flow$AbstractAssignAnalyzer.scan(Flow.java:1376)
Information:java:   at com.sun.tools.javac.comp.Flow$AbstractAssignAnalyzer.visitLambda(Flow.java:2256)
Information:java:   at com.sun.tools.javac.tree.JCTree$JCLambda.accept(JCTree.java:1624)
Information:java:   at com.sun.tools.javac.comp.Flow$AbstractAssignAnalyzer.scanExpr(Flow.java:1627)
Information:java:   at com.sun.tools.javac.comp.Flow$AbstractAssignAnalyzer.scanExprs(Flow.java:1639)
Information:java:   at com.sun.tools.javac.comp.Flow$AbstractAssignAnalyzer.visitApply(Flow.java:2236)
Information:java:   at com.sun.tools.javac.tree.JCTree$JCMethodInvocation.accept(JCTree.java:1465)
Information:java:   at com.sun.tools.javac.comp.Flow$AbstractAssignAnalyzer.analyzeTree(Flow.java:2423)
Information:java:   at com.sun.tools.javac.comp.Flow$AbstractAssignAnalyzer.analyzeTree(Flow.java:2406)
Information:java:   at com.sun.tools.javac.comp.Flow.analyzeLambdaThrownTypes(Flow.java:250)
Information:java:   at com.sun.tools.javac.comp.Attr.visitLambda(Attr.java:2423)
Information:java:   at com.sun.tools.javac.comp.Attr.attribTree(Attr.java:607)
Information:java:   at com.sun.tools.javac.comp.DeferredAttr$2.complete(DeferredAttr.java:284)
Information:java:   at com.sun.tools.javac.comp.DeferredAttr$DeferredType.check(DeferredAttr.java:245)
Information:java:   at com.sun.tools.javac.comp.DeferredAttr$DeferredType.check(DeferredAttr.java:232)
Information:java:   at com.sun.tools.javac.comp.Resolve$MethodResultInfo.check(Resolve.java:993)
Information:java:   at com.sun.tools.javac.comp.Resolve$4.checkArg(Resolve.java:826)
Information:java:   at com.sun.tools.javac.comp.Resolve$AbstractMethodCheck.argumentsAcceptable(Resolve.java:731)
Information:java:   at com.sun.tools.javac.comp.Resolve$4.argumentsAcceptable(Resolve.java:835)
Information:java:   at com.sun.tools.javac.comp.Infer.instantiateMethod(Infer.java:162)
Information:java:   at com.sun.tools.javac.comp.Resolve.rawInstantiate(Resolve.java:564)
Information:java:   at com.sun.tools.javac.comp.Resolve.checkMethod(Resolve.java:601)
Information:java:   at com.sun.tools.javac.comp.Attr.checkMethod(Attr.java:3809)
Information:java:   at com.sun.tools.javac.comp.Attr.checkIdInternal(Attr.java:3615)
Information:java:   at com.sun.tools.javac.comp.Attr.checkMethodIdInternal(Attr.java:3522)
Information:java:   at com.sun.tools.javac.comp.Attr.checkMethodId(Attr.java:3501)
Information:java:   at com.sun.tools.javac.comp.Attr.checkId(Attr.java:3488)
Information:java:   at com.sun.tools.javac.comp.Attr.visitIdent(Attr.java:3237)
Information:java:   at com.sun.tools.javac.tree.JCTree$JCIdent.accept(JCTree.java:2011)
Information:java:   at com.sun.tools.javac.comp.Attr.visitApply(Attr.java:1843)
Information:java:   at com.sun.tools.javac.comp.DeferredAttr.attribSpeculative(DeferredAttr.java:386)
Information:java:   at com.sun.tools.javac.comp.DeferredAttr$2.complete(DeferredAttr.java:279)
Information:java:   at com.sun.tools.javac.comp.Resolve.selectBest(Resolve.java:1431)
Information:java:   at com.sun.tools.javac.comp.Resolve.findMethodInScope(Resolve.java:1618)
Information:java:   at com.sun.tools.javac.comp.Resolve.findMethod(Resolve.java:1689)
Information:java:   at com.sun.tools.javac.comp.Resolve.findMethod(Resolve.java:1662)
Information:java:   at com.sun.tools.javac.comp.Resolve$9.doLookup(Resolve.java:2415)
Information:java:   at com.sun.tools.javac.comp.Resolve$BasicLookupHelper.lookup(Resolve.java:3074)
Information:java:   at com.sun.tools.javac.comp.Resolve.lookupMethod(Resolve.java:3325)
Information:java:   at com.sun.tools.javac.comp.Resolve.resolveQualifiedMethod(Resolve.java:2412)
Information:java:   at com.sun.tools.javac.comp.Resolve.resolveQualifiedMethod(Resolve.java:2406)
Information:java:   at com.sun.tools.javac.comp.Attr.selectSym(Attr.java:3395)
Information:java:   at com.sun.tools.javac.comp.Attr.visitSelect(Attr.java:3281)
Information:java:   at com.sun.tools.javac.tree.JCTree$JCFieldAccess.accept(JCTree.java:1897)
Information:java:   at com.sun.tools.javac.comp.Attr.attribExpr(Attr.java:649)
Information:java:   at com.sun.tools.javac.comp.Attr.visitVarDef(Attr.java:1093)
Information:java:   at com.sun.tools.javac.comp.Attr.attribStat(Attr.java:676)
Information:java:   at com.sun.tools.javac.comp.Attr.attribStats(Attr.java:692)
Information:java:   at com.sun.tools.javac.comp.Attr.visitBlock(Attr.java:1142)
Information:java:   at com.sun.tools.javac.tree.JCTree$JCBlock.accept(JCTree.java:909)
Information:java:   at com.sun.tools.javac.comp.Attr.visitMethodDef(Attr.java:1035)
Information:java:   at com.sun.tools.javac.tree.JCTree$JCMethodDecl.accept(JCTree.java:778)
Information:java:   at com.sun.tools.javac.comp.Attr.attribClassBody(Attr.java:4342)
Information:java:   at com.sun.tools.javac.comp.Attr.attribClass(Attr.java:4252)
Information:java:   at com.sun.tools.javac.comp.Attr.attribClass(Attr.java:4181)
Information:java:   at com.sun.tools.javac.comp.Attr.visitClassDef(Attr.java:892)
Information:java:   at com.sun.tools.javac.tree.JCTree$JCClassDecl.accept(JCTree.java:693)
Information:java:   at com.sun.tools.javac.comp.Attr.attrib(Attr.java:4156)
Information:java:   at com.sun.tools.javac.main.JavaCompiler.attribute(JavaCompiler.java:1248)
Information:java:   at com.sun.tools.javac.main.JavaCompiler.compile2(JavaCompiler.java:901)
Information:java:   at com.sun.tools.javac.main.JavaCompiler.compile(JavaCompiler.java:860)
Information:java:   at com.sun.tools.javac.main.Main.compile(Main.java:523)
Information:java:   at com.sun.tools.javac.api.JavacTaskImpl.doCall(JavacTaskImpl.java:129)
Information:java:   at com.sun.tools.javac.api.JavacTaskImpl.call(JavacTaskImpl.java:138)

とりあえず、openJDKのjdk8/jdk8/langtoolsをクローンしました。

以上

【2014/10/20追記】

なお、2つ目のラムダをタイプパラメーターで指定せずに<Exception>と型を明確に指定すると、コンパイル時にエラーが発生しません。

【例】これは落ちない

public class Some {
    public <E extends Exception> void tryAndHandle(Task<? extends E> task, Handler<Exception> handler) {
        try {
            task.run();
        } catch (Exception e) {
            handler.handle(e);
        }
    }
}