JDK-8068399 : Assertion failed in javac
  • Type: Bug
  • Component: tools
  • Sub-Component: javac
  • Affected Version: 8u40
  • Priority: P4
  • Status: Resolved
  • Resolution: Duplicate
  • OS: other
  • CPU: x86
  • Submitted: 2014-12-22
  • Updated: 2015-06-24
  • Resolved: 2015-02-03
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 9
9Resolved
Related Reports
Blocks :  
Duplicate :  
Duplicate :  
Duplicate :  
Duplicate :  
Duplicate :  
Relates :  
Description
FULL PRODUCT VERSION :
java version "1.8.0_40-ea"
Java(TM) SE Runtime Environment (build 1.8.0_40-ea-b19)
Java HotSpot(TM) 64-Bit Server VM (build 25.40-b23, mixed mode)

ADDITIONAL OS VERSION INFORMATION :
Mac OS Yosemite 10.10.1 (14B25)

A DESCRIPTION OF THE PROBLEM :
Assertion fails in javac when I try to compile my class (see stacktrace below). Code sample is attached.

ADDITIONAL REGRESSION INFORMATION: 
Yes, it worked in some previous java 8 release, but I don't remember which one.
Assertion fails in 8u40 early access preview.
It produces another exception (NullPointerException) in latest stable release (8u25)

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Execute "javac Main.java"

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Succesful compilation
ACTUAL -
Assertion failed in javac

ERROR MESSAGES/STACK TRACES THAT OCCUR :
An exception has occurred in the compiler (1.8.0_40-ea). Please file a bug at the Java Developer Connection (http://java.sun.com/webapps/bugreport)  after checking the Bug Parade for duplicates. Include your program and the following diagnostic in your report.  Thank you.
java.lang.AssertionError: attribution shouldn't be happening here
	at com.sun.tools.javac.util.Assert.error(Assert.java:133)
	at com.sun.tools.javac.util.Assert.check(Assert.java:94)
	at com.sun.tools.javac.comp.DeferredAttr$DeferredAttrNode.process(DeferredAttr.java:591)
	at com.sun.tools.javac.comp.DeferredAttr$DeferredAttrContext.complete(DeferredAttr.java:479)
	at com.sun.tools.javac.comp.Infer.instantiateMethod(Infer.java:182)
	at com.sun.tools.javac.comp.Resolve.rawInstantiate(Resolve.java:567)
	at com.sun.tools.javac.comp.Resolve.checkMethod(Resolve.java:604)
	at com.sun.tools.javac.comp.Attr.checkMethod(Attr.java:3797)
	at com.sun.tools.javac.comp.Attr.checkIdInternal(Attr.java:3584)
	at com.sun.tools.javac.comp.Attr.checkMethodIdInternal(Attr.java:3491)
	at com.sun.tools.javac.comp.Attr.checkMethodId(Attr.java:3470)
	at com.sun.tools.javac.comp.Attr.checkId(Attr.java:3457)
	at com.sun.tools.javac.comp.Attr.visitIdent(Attr.java:3201)
	at com.sun.tools.javac.tree.JCTree$JCIdent.accept(JCTree.java:2011)
	at com.sun.tools.javac.comp.Attr.attribTree(Attr.java:566)
	at com.sun.tools.javac.comp.Attr.visitApply(Attr.java:1806)
	at com.sun.tools.javac.tree.JCTree$JCMethodInvocation.accept(JCTree.java:1465)
	at com.sun.tools.javac.comp.Attr.attribTree(Attr.java:566)
	at com.sun.tools.javac.comp.DeferredAttr.attribSpeculative(DeferredAttr.java:386)
	at com.sun.tools.javac.comp.DeferredAttr$2.complete(DeferredAttr.java:279)
	at com.sun.tools.javac.comp.DeferredAttr$DeferredType.check(DeferredAttr.java:245)
	at com.sun.tools.javac.comp.DeferredAttr$DeferredType.check(DeferredAttr.java:232)
	at com.sun.tools.javac.comp.Resolve$MethodResultInfo.check(Resolve.java:1003)
	at com.sun.tools.javac.comp.Resolve$4.checkArg(Resolve.java:829)
	at com.sun.tools.javac.comp.Resolve$AbstractMethodCheck.argumentsAcceptable(Resolve.java:734)
	at com.sun.tools.javac.comp.Resolve$4.argumentsAcceptable(Resolve.java:838)
	at com.sun.tools.javac.comp.Resolve.rawInstantiate(Resolve.java:579)
	at com.sun.tools.javac.comp.Resolve.selectBest(Resolve.java:1441)
	at com.sun.tools.javac.comp.Resolve.findMethodInScope(Resolve.java:1628)
	at com.sun.tools.javac.comp.Resolve.findMethod(Resolve.java:1699)
	at com.sun.tools.javac.comp.Resolve.findMethod(Resolve.java:1672)
	at com.sun.tools.javac.comp.Resolve.findFun(Resolve.java:1832)
	at com.sun.tools.javac.comp.Resolve$8.doLookup(Resolve.java:2393)
	at com.sun.tools.javac.comp.Resolve$BasicLookupHelper.lookup(Resolve.java:3084)
	at com.sun.tools.javac.comp.Resolve.lookupMethod(Resolve.java:3335)
	at com.sun.tools.javac.comp.Resolve.lookupMethod(Resolve.java:3320)
	at com.sun.tools.javac.comp.Resolve.resolveMethod(Resolve.java:2389)
	at com.sun.tools.javac.comp.Attr.visitIdent(Attr.java:3134)
	at com.sun.tools.javac.tree.JCTree$JCIdent.accept(JCTree.java:2011)
	at com.sun.tools.javac.comp.Attr.attribTree(Attr.java:566)
	at com.sun.tools.javac.comp.Attr.visitApply(Attr.java:1806)
	at com.sun.tools.javac.tree.JCTree$JCMethodInvocation.accept(JCTree.java:1465)
	at com.sun.tools.javac.comp.Attr.attribTree(Attr.java:566)
	at com.sun.tools.javac.comp.Attr.attribExpr(Attr.java:608)
	at com.sun.tools.javac.comp.Attr.visitVarDef(Attr.java:1056)
	at com.sun.tools.javac.tree.JCTree$JCVariableDecl.accept(JCTree.java:852)
	at com.sun.tools.javac.comp.Attr.attribTree(Attr.java:566)
	at com.sun.tools.javac.comp.Attr.attribStat(Attr.java:635)
	at com.sun.tools.javac.comp.Attr.attribStats(Attr.java:651)
	at com.sun.tools.javac.comp.Attr.visitBlock(Attr.java:1105)
	at com.sun.tools.javac.tree.JCTree$JCBlock.accept(JCTree.java:909)
	at com.sun.tools.javac.comp.Attr.attribTree(Attr.java:566)
	at com.sun.tools.javac.comp.Attr.attribStat(Attr.java:635)
	at com.sun.tools.javac.comp.Attr.visitMethodDef(Attr.java:994)
	at com.sun.tools.javac.tree.JCTree$JCMethodDecl.accept(JCTree.java:778)
	at com.sun.tools.javac.comp.Attr.attribTree(Attr.java:566)
	at com.sun.tools.javac.comp.Attr.attribStat(Attr.java:635)
	at com.sun.tools.javac.comp.Attr.attribClassBody(Attr.java:4330)
	at com.sun.tools.javac.comp.Attr.attribClass(Attr.java:4240)
	at com.sun.tools.javac.comp.Attr.attribClass(Attr.java:4169)
	at com.sun.tools.javac.comp.Attr.attrib(Attr.java:4144)
	at com.sun.tools.javac.main.JavaCompiler.attribute(JavaCompiler.java:1248)
	at com.sun.tools.javac.main.JavaCompiler.compile2(JavaCompiler.java:901)
	at com.sun.tools.javac.main.JavaCompiler.compile(JavaCompiler.java:860)
	at com.sun.tools.javac.main.Main.compile(Main.java:523)
	at com.sun.tools.javac.main.Main.compile(Main.java:381)
	at com.sun.tools.javac.main.Main.compile(Main.java:370)
	at com.sun.tools.javac.main.Main.compile(Main.java:361)
	at com.sun.tools.javac.Main.compile(Main.java:56)
	at com.sun.tools.javac.Main.main(Main.java:42)

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
public class Main {
    public static class Spectrum {
        public double[] getEnergy() {
            return new double[0];
        }
    }

    protected Spectrum spectrum;

    public static class Ref<T> {
        T value;

        public Ref() {
        }

        public Ref(T value) {
            this.value = value;
        }

        public boolean isNull() {
            return value == null;
        }

        public T get() {
            return value;
        }

        public void set(T value) {
            this.value = value;
        }
    }

    public static <T>T maxKey(Stream<T> stream, Function<T, Double> function) {
        Ref<Double> max = new Ref<>();
        Ref<T> index = new Ref<>();
        stream.forEach(v -> {
            Double value = function.apply(v);

            if (max.isNull() || value > max.get()) {
                max.set(value);
                index.set(v);
            }
        });

        return index.get();
    }

    public static int interpolate(int x, int x0, int x1, int y0, int y1) {
        return y0 + (x - x0) * (y1 - y0) / (x1 - x0);
    }

    public static double interpolate(double x, double x0, double x1, double y0, double y1) {
        return y0 + (x - x0) * (y1 - y0) / (x1 - x0);
    }

    protected int getXByFrequency(double frequency) {
        Insets insets = null;
        return (int) Math.round(interpolate(frequency, getMinSpectrumCoord(), getMaxSpectrumCoord(), insets.left, getWidth() - insets.right));
    }

    private int getWidth() {
        return 0;
    }

    private double getMaxSpectrumCoord() {
        return 0;
    }

    private double getMinSpectrumCoord() {
        return 0;
    }

    void foo() {
        int maxBpmIndex = 0;
        int xcur = getXByFrequency(maxKey(IntStream.range(0, maxBpmIndex).boxed(), i -> Math.abs(spectrum.getEnergy()[i])));
    }
}

---------- END SOURCE ----------


Comments
I have regression tests for this defect and all three duplicates which I will piggy back on the patch for https://bugs.openjdk.java.net/browse/JDK-8069545
03-02-2015

For the record, upon further study and considering https://bugs.openjdk.java.net/browse/JDK-8069545 and the specification clarification in https://bugs.openjdk.java.net/browse/JDK-8069544, the assertion is indeed correct. But that javac reaches that point given the test case is a problem which is addressed by https://bugs.openjdk.java.net/browse/JDK-8069545.
28-01-2015

This seems to be closely related to https://bugs.openjdk.java.net/browse/JDK-8069545 and the patch under consideration there fixes the assertion failures here. I'll keep a watch on that bug and bring this one to closure by adding suitable tests and such at the right moment.
27-01-2015

Initial investigations points to an assertion introduced in the fix for https://bugs.openjdk.java.net/browse/JDK-8033483 - Perhaps the assertion is a bit more constraining than it needs to be - will need to dig in more.
20-01-2015

Reminder to self to include the snippet from duplicate bug to establish it is indeed duplicate once a resolution is available for the current issue: // ---- 8< ----- package com.company; import java.util.HashMap; import java.util.Map; public class Main { public static void main(String[] args) { Map<Integer, String> mp = new HashMap<>(); mp.put(1, "a"); mp.put(2, "b"); mp.put(3, "c"); mp.put(4, "d"); System.out.println(mp.entrySet().stream().reduce(0, (i, e) -> i + e.getKey(), (i1, i2) -> i1 + i2)); } }
20-01-2015

I believe you are right - I took a look at the code and it does indeed seem that the assertion is too strict; in the 'normal' case, the lambda would be considered 'stuck' as its target depends on an inference variable - however in this case eager inference is triggered (this is because the target type of the nested method call 'boo' is a primitive type) - meaning that when we get to 'complete' the deferred context, we already have an instantiation for T (Integer) - so that we can indeed go ahead and type-check the lambda. That said - I'm not super sure as to what performing an extra type-checking would buy you in this specific case - given that all inference variables have been instantiated, type-checking the lambda cannot really have an effect on the outcome of outer method resolution - so perhaps we should just leave things as they are, and pretend that the lambda is still stuck.
20-01-2015

Smaller test case: import java.util.stream.*; import java.util.function.*; public class X { static <T>T boo(Stream<T> stream, Function<T, Double> function) { return null; } int goo(double f) { return 0; } void foo() { int xcur = goo(boo((Stream<Integer>) null, i -> (double) 0)); } }
13-01-2015

Reproducible file.
10-01-2015

I don't think that there is any connection with JDK-8026704, sorry. That was a flow issue.
08-01-2015

Could reproduce this issue with JDK 8u40. Looks like a duplicate of JDK-8026704.
31-12-2014