diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/ThisEscapeAnalyzer.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/ThisEscapeAnalyzer.java index 7f7f09b3391..2dd054619e0 100644 --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/ThisEscapeAnalyzer.java +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/ThisEscapeAnalyzer.java @@ -678,7 +678,11 @@ class ThisEscapeAnalyzer extends TreeScanner { @Override public void visitForeachLoop(JCEnhancedForLoop tree) { - visitLooped(tree, super::visitForeachLoop); + visitLooped(tree, foreach -> { + scan(foreach.expr); + refs.discardExprs(depth); // we don't handle iterator() yet + scan(foreach.body); + }); } @Override @@ -729,7 +733,10 @@ class ThisEscapeAnalyzer extends TreeScanner { @Override public void visitLambda(JCLambda lambda) { - visitDeferred(() -> visitScoped(false, () -> super.visitLambda(lambda))); + visitDeferred(() -> visitScoped(false, () -> { + scan(lambda.body); + refs.discardExprs(depth); // needed in case body is a JCExpression + })); } @Override diff --git a/test/langtools/tools/javac/warnings/ThisEscape.java b/test/langtools/tools/javac/warnings/ThisEscape.java index 65bca36923b..d6ae9f8e8f4 100644 --- a/test/langtools/tools/javac/warnings/ThisEscape.java +++ b/test/langtools/tools/javac/warnings/ThisEscape.java @@ -601,4 +601,20 @@ public class ThisEscape { public static final class Sub2 extends ThisEscapeSealed { } } + + // Verify no assertion error occurs (JDK-8317336) + public static class ThisEscapeAssertionError { + public ThisEscapeAssertionError() { + System.out.println((Supplier)() -> this); + } + } + + // Verify no assertion error occurs (JDK-8317336) + public static class ThisEscapeAssertionError2 { + public ThisEscapeAssertionError2() { + ThisEscapeAssertionError2[] array = new ThisEscapeAssertionError2[] { this }; + for (Object obj : array) + ; + } + } }