JDK-8317336 : Assertion error thrown during 'this' escape analysis
  • Type: Bug
  • Component: tools
  • Sub-Component: javac
  • Affected Version: 21
  • Priority: P4
  • Status: Resolved
  • Resolution: Fixed
  • OS: generic
  • CPU: generic
  • Submitted: 2023-10-01
  • Updated: 2023-10-16
  • Resolved: 2023-10-12
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 22
22 masterFixed
Related Reports
Relates :  
Description
Certain code triggers an assertion error in the 'this' escape analysis.

Example:

$ cat Test.java
import java.util.function.Supplier;
public class Test {
    public Test() {
        System.out.println((Supplier<Object>)() -> this);
    }
}
$ javac -Xlint:this-escape Test.java
An exception has occurred in the compiler (22-internal). Please file a bug against the Java compiler via the Java bug reporting page (https://bugreport.java.com) after checking the Bug Database (https://bugs.java.com) for duplicates. Include your program, the following diagnostic, and the parameters passed to the Java compiler in your report. Thank you.
java.lang.AssertionError
	at jdk.compiler.interim/com.sun.tools.javac.util.Assert.error(Assert.java:155)
	at jdk.compiler.interim/com.sun.tools.javac.util.Assert.check(Assert.java:46)
	at jdk.compiler.interim/com.sun.tools.javac.comp.ThisEscapeAnalyzer.checkInvariants(ThisEscapeAnalyzer.java:1209)
	at jdk.compiler.interim/com.sun.tools.javac.comp.ThisEscapeAnalyzer.visitScoped(ThisEscapeAnalyzer.java:1103)
	at jdk.compiler.interim/com.sun.tools.javac.comp.ThisEscapeAnalyzer.lambda$visitLambda$24(ThisEscapeAnalyzer.java:732)
	at jdk.compiler.interim/com.sun.tools.javac.comp.ThisEscapeAnalyzer.visitDeferred(ThisEscapeAnalyzer.java:1073)
	at jdk.compiler.interim/com.sun.tools.javac.comp.ThisEscapeAnalyzer.visitLambda(ThisEscapeAnalyzer.java:732)
	at jdk.compiler.interim/com.sun.tools.javac.tree.JCTree$JCLambda.accept(JCTree.java:1993)
	at jdk.compiler.interim/com.sun.tools.javac.tree.TreeScanner.scan(TreeScanner.java:50)
	at jdk.compiler.interim/com.sun.tools.javac.comp.ThisEscapeAnalyzer.scan(ThisEscapeAnalyzer.java:462)
	at jdk.compiler.interim/com.sun.tools.javac.comp.ThisEscapeAnalyzer.visitTypeCast(ThisEscapeAnalyzer.java:949)
	at jdk.compiler.interim/com.sun.tools.javac.tree.JCTree$JCTypeCast.accept(JCTree.java:2204)
	at jdk.compiler.interim/com.sun.tools.javac.tree.TreeScanner.scan(TreeScanner.java:50)
	at jdk.compiler.interim/com.sun.tools.javac.comp.ThisEscapeAnalyzer.scan(ThisEscapeAnalyzer.java:462)
	at jdk.compiler.interim/com.sun.tools.javac.comp.ThisEscapeAnalyzer.invokeUnknown(ThisEscapeAnalyzer.java:623)
	at jdk.compiler.interim/com.sun.tools.javac.comp.ThisEscapeAnalyzer.invoke(ThisEscapeAnalyzer.java:552)
	at jdk.compiler.interim/com.sun.tools.javac.comp.ThisEscapeAnalyzer.visitApply(ThisEscapeAnalyzer.java:530)
	at jdk.compiler.interim/com.sun.tools.javac.tree.JCTree$JCMethodInvocation.accept(JCTree.java:1816)
	at jdk.compiler.interim/com.sun.tools.javac.tree.TreeScanner.scan(TreeScanner.java:50)
	at jdk.compiler.interim/com.sun.tools.javac.comp.ThisEscapeAnalyzer.scan(ThisEscapeAnalyzer.java:462)
	at jdk.compiler.interim/com.sun.tools.javac.comp.ThisEscapeAnalyzer.visitExec(ThisEscapeAnalyzer.java:915)
	at jdk.compiler.interim/com.sun.tools.javac.tree.JCTree$JCExpressionStatement.accept(JCTree.java:1603)
	at jdk.compiler.interim/com.sun.tools.javac.tree.TreeScanner.scan(TreeScanner.java:50)
	at jdk.compiler.interim/com.sun.tools.javac.comp.ThisEscapeAnalyzer.scan(ThisEscapeAnalyzer.java:462)
	at jdk.compiler.interim/com.sun.tools.javac.comp.ThisEscapeAnalyzer.analyzeStatements(ThisEscapeAnalyzer.java:419)
	at jdk.compiler.interim/com.sun.tools.javac.comp.ThisEscapeAnalyzer.lambda$analyzeTree$6(ThisEscapeAnalyzer.java:358)
	at jdk.compiler.interim/com.sun.tools.javac.comp.ThisEscapeAnalyzer.visitScoped(ThisEscapeAnalyzer.java:1102)
	at jdk.compiler.interim/com.sun.tools.javac.comp.ThisEscapeAnalyzer.visitTopLevel(ThisEscapeAnalyzer.java:1052)
	at jdk.compiler.interim/com.sun.tools.javac.comp.ThisEscapeAnalyzer.lambda$analyzeTree$7(ThisEscapeAnalyzer.java:357)
	at java.base/java.util.stream.ForEachOps$ForEachOp$OfRef.accept(ForEachOps.java:184)
	at java.base/java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.java:179)
	at java.base/java.util.Iterator.forEachRemaining(Iterator.java:133)
	at java.base/java.util.Spliterators$IteratorSpliterator.forEachRemaining(Spliterators.java:1939)
	at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:509)
	at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:499)
	at java.base/java.util.stream.ForEachOps$ForEachOp.evaluateSequential(ForEachOps.java:151)
	at java.base/java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(ForEachOps.java:174)
	at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
	at java.base/java.util.stream.ReferencePipeline.forEach(ReferencePipeline.java:596)
	at jdk.compiler.interim/com.sun.tools.javac.comp.ThisEscapeAnalyzer.analyzeTree(ThisEscapeAnalyzer.java:356)
	at jdk.compiler.interim/com.sun.tools.javac.comp.Flow.analyzeTree(Flow.java:233)
	at jdk.compiler.interim/com.sun.tools.javac.main.JavaCompiler.flow(JavaCompiler.java:1426)
	at jdk.compiler.interim/com.sun.tools.javac.main.JavaCompiler.flow(JavaCompiler.java:1400)
	at jdk.compiler.interim/com.sun.tools.javac.main.JavaCompiler.compile(JavaCompiler.java:976)
	at jdk.compiler.interim/com.sun.tools.javac.main.Main.compile(Main.java:319)
	at jdk.compiler.interim/com.sun.tools.javac.main.Main.compile(Main.java:178)
	at jdk.compiler.interim/com.sun.tools.javac.Main.compile(Main.java:66)
	at jdk.compiler.interim/com.sun.tools.javac.Main.main(Main.java:52)
$ java -version
openjdk version "21" 2023-09-19
OpenJDK Runtime Environment Homebrew (build 21)
OpenJDK 64-Bit Server VM Homebrew (build 21, mixed mode, sharing)

Comments
A pull request was submitted for review. URL: https://git.openjdk.org/jdk21u/pull/252 Date: 2023-10-13 14:36:31 +0000
13-10-2023

Changeset: a8473b70 Author: Archie Cobbs <acobbs@openjdk.org> Committer: Vicente Romero <vromero@openjdk.org> Date: 2023-10-12 16:51:53 +0000 URL: https://git.openjdk.org/jdk/commit/a8473b70797ef8fa0704774bfadbd84cb33a1d53
12-10-2023

There is also this slightly different variation: $ cat Test2.java public class Test2 { public Test2() { Test2[] array = new Test2[] { this }; for (Object obj : array) ; } } $ javac -Xlint:this-escape Test2.java An exception has occurred in the compiler (21). Please file a bug against the Java compiler via the Java bug reporting page (https://bugreport.java.com) after checking the Bug Database (https://bugs.java.com) for duplicates. Include your program, the following diagnostic, and the parameters passed to the Java compiler in your report. Thank you. java.lang.AssertionError at jdk.compiler/com.sun.tools.javac.util.Assert.error(Assert.java:155) at jdk.compiler/com.sun.tools.javac.util.Assert.check(Assert.java:46) at jdk.compiler/com.sun.tools.javac.comp.ThisEscapeAnalyzer.checkInvariants(ThisEscapeAnalyzer.java:1210) at jdk.compiler/com.sun.tools.javac.comp.ThisEscapeAnalyzer.scan(ThisEscapeAnalyzer.java:433) at jdk.compiler/com.sun.tools.javac.tree.TreeScanner.visitForeachLoop(TreeScanner.java:168) at jdk.compiler/com.sun.tools.javac.comp.ThisEscapeAnalyzer.lambda$visitForeachLoop$18(ThisEscapeAnalyzer.java:681) at jdk.compiler/com.sun.tools.javac.comp.ThisEscapeAnalyzer.lambda$visitLooped$32(ThisEscapeAnalyzer.java:1088) at jdk.compiler/com.sun.tools.javac.comp.ThisEscapeAnalyzer.visitScoped(ThisEscapeAnalyzer.java:1102) at jdk.compiler/com.sun.tools.javac.comp.ThisEscapeAnalyzer.visitLooped(ThisEscapeAnalyzer.java:1085) at jdk.compiler/com.sun.tools.javac.comp.ThisEscapeAnalyzer.visitForeachLoop(ThisEscapeAnalyzer.java:681) at jdk.compiler/com.sun.tools.javac.tree.JCTree$JCEnhancedForLoop.accept(JCTree.java:1234)
03-10-2023

Fix Request Backport rationale: This issue should be backported to 21 because the bug is severe and the backport risk is minimal, since all the fix does is make an assertion happen less often. Patch applies cleanly
01-10-2023

A pull request was submitted for review. URL: https://git.openjdk.org/jdk/pull/16001 Date: 2023-10-01 19:48:46 +0000
01-10-2023