Skip to content

Commit 4ce8822

Browse files
8334037: Local class creation in lambda in pre-construction context crashes javac
8333313: NullPointerException in lambda instantiating an inner local class in prologue 8333766: Stack overflow with anonymous class in super() parameter 8334679: Wrong bug number in regression test for JDK-8334252 Co-authored-by: Archie Cobbs <acobbs@openjdk.org> Reviewed-by: jlahoda, vromero
1 parent 7f6804c commit 4ce8822

File tree

15 files changed

+793
-751
lines changed

15 files changed

+793
-751
lines changed

src/jdk.compiler/share/classes/com/sun/tools/javac/comp/CompileStates.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ public enum CompileState {
6060
FLOW(5),
6161
TRANSTYPES(6),
6262
TRANSPATTERNS(7),
63-
UNLAMBDA(8),
64-
LOWER(9),
63+
LOWER(8),
64+
UNLAMBDA(9),
6565
GENERATE(10);
6666

6767
CompileState(int value) {

src/jdk.compiler/share/classes/com/sun/tools/javac/comp/LambdaToMethod.java

Lines changed: 15 additions & 660 deletions
Large diffs are not rendered by default.

src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Lower.java

Lines changed: 417 additions & 63 deletions
Large diffs are not rendered by default.

src/jdk.compiler/share/classes/com/sun/tools/javac/main/JavaCompiler.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1618,14 +1618,6 @@ public void visitSwitchExpression(JCSwitchExpression tree) {
16181618

16191619
compileStates.put(env, CompileState.TRANSPATTERNS);
16201620

1621-
if (scanner.hasLambdas) {
1622-
if (shouldStop(CompileState.UNLAMBDA))
1623-
return;
1624-
1625-
env.tree = LambdaToMethod.instance(context).translateTopLevelClass(env, env.tree, localMake);
1626-
compileStates.put(env, CompileState.UNLAMBDA);
1627-
}
1628-
16291621
if (shouldStop(CompileState.LOWER))
16301622
return;
16311623

@@ -1647,6 +1639,16 @@ public void visitSwitchExpression(JCSwitchExpression tree) {
16471639
if (shouldStop(CompileState.LOWER))
16481640
return;
16491641

1642+
if (scanner.hasLambdas) {
1643+
if (shouldStop(CompileState.UNLAMBDA))
1644+
return;
1645+
1646+
for (JCTree def : cdefs) {
1647+
LambdaToMethod.instance(context).translateTopLevelClass(env, def, localMake);
1648+
}
1649+
compileStates.put(env, CompileState.UNLAMBDA);
1650+
}
1651+
16501652
//generate code for each class
16511653
for (List<JCTree> l = cdefs; l.nonEmpty(); l = l.tail) {
16521654
JCClassDecl cdef = (JCClassDecl)l.head;

src/jdk.compiler/share/classes/com/sun/tools/javac/tree/JCTree.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2013,6 +2013,7 @@ public enum ParameterKind {
20132013
public JCTree body;
20142014
public boolean canCompleteNormally = true;
20152015
public ParameterKind paramKind;
2016+
public boolean wasMethodReference;
20162017

20172018
public JCLambda(List<JCVariableDecl> params,
20182019
JCTree body) {

src/jdk.compiler/share/classes/com/sun/tools/javac/tree/TreeInfo.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,11 +344,13 @@ public void visitExec(JCExpressionStatement stat) {
344344
@Override
345345
public void visitClassDef(JCClassDecl tree) {
346346
// don't descend any further
347+
result = tree;
347348
}
348349

349350
@Override
350351
public void visitLambda(JCLambda tree) {
351352
// don't descend any further
353+
result = tree;
352354
}
353355
}
354356

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
/*
24+
* @test
25+
* @bug 8333766
26+
* @summary Test for compiler crash when anonymous class created in early lambda
27+
*/
28+
29+
public class AnonSuperLambdaCrash {
30+
class Inner {
31+
Inner() {
32+
this(() -> new Object() { { AnonSuperLambdaCrash.this.hashCode(); } });
33+
}
34+
Inner(Runnable r) {
35+
r.run();
36+
}
37+
}
38+
39+
public static void main(String[] args) {
40+
new AnonSuperLambdaCrash().new Inner();
41+
}
42+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
/*
24+
* @test
25+
* @bug 8333313
26+
* @summary Verify references to local classes declared in early construction contexts
27+
* @enablePreview
28+
*/
29+
public class EarlyLocalTest1 {
30+
31+
class Test {
32+
Test() {
33+
class InnerLocal { }
34+
Runnable r = () -> new InnerLocal();
35+
r.run();
36+
super();
37+
}
38+
}
39+
40+
public static void main(String[] args) {
41+
new EarlyLocalTest1().new Test();
42+
}
43+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
/*
24+
* @test
25+
* @bug 8333313
26+
* @summary Verify references to local classes declared in early construction contexts
27+
* @enablePreview
28+
*/
29+
public class EarlyLocalTest4 {
30+
31+
class Test {
32+
Test() {
33+
class InnerLocal { }
34+
Runnable r = new Runnable() {
35+
public void run() {
36+
new InnerLocal();
37+
}
38+
};
39+
r.run();
40+
super();
41+
}
42+
}
43+
44+
public static void main(String[] args) {
45+
new EarlyLocalTest4().new Test();
46+
}
47+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
/*
24+
* @test
25+
* @bug 8333313
26+
* @summary Verify references to local classes declared in early construction contexts
27+
* @enablePreview
28+
*/
29+
import java.util.concurrent.atomic.AtomicReference;
30+
31+
public class EarlyLocalTest5 {
32+
33+
int y;
34+
35+
class Test extends AtomicReference<Runnable> {
36+
Test(int x) {
37+
class Foo implements Runnable {
38+
public void run() {
39+
System.out.println(x + y);
40+
}
41+
}
42+
super(new Foo());
43+
}
44+
}
45+
46+
public static void main(String[] args) {
47+
new EarlyLocalTest5().new Test(42);
48+
}
49+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
/*
24+
* @test
25+
* @bug 8333313
26+
* @summary Verify references to local classes declared in early construction contexts
27+
* @enablePreview
28+
*/
29+
import java.util.concurrent.atomic.AtomicReference;
30+
31+
public class EarlyLocalTest6 {
32+
33+
int y;
34+
35+
class Test extends AtomicReference<Runnable> {
36+
Test(int x) {
37+
super(new Runnable() {
38+
public void run() {
39+
System.out.println(x + y);
40+
}
41+
});
42+
}
43+
}
44+
45+
public static void main(String[] args) {
46+
new EarlyLocalTest6().new Test(42);
47+
}
48+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
/*
24+
* @test
25+
* @bug 8333313
26+
* @summary Verify references to local classes declared in early construction contexts
27+
* @enablePreview
28+
*/
29+
import java.util.concurrent.atomic.AtomicReference;
30+
31+
public class EarlyLocalTest7 {
32+
33+
int y;
34+
35+
class Test extends AtomicReference<Runnable> {
36+
Test(int x) {
37+
super(() -> System.out.println(x + y));
38+
}
39+
}
40+
41+
public static void main(String[] args) {
42+
new EarlyLocalTest7().new Test(42);
43+
}
44+
}

0 commit comments

Comments
 (0)