Skip to content

Commit 93a2e77

Browse files
committedFeb 23, 2024
8326129: Java Record Pattern Match leads to infinite loop
Reviewed-by: vromero
1 parent 336bbbe commit 93a2e77

File tree

2 files changed

+84
-2
lines changed

2 files changed

+84
-2
lines changed
 

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

+1
Original file line numberDiff line numberDiff line change
@@ -988,6 +988,7 @@ public void resolve(VarSymbol commonBinding,
988988
commonBinding.type.tsym == currentBinding.type.tsym &&
989989
commonBinding.isUnnamedVariable() == currentBinding.isUnnamedVariable() &&
990990
!previousNullable &&
991+
!currentNullable &&
991992
new TreeDiffer(List.of(commonBinding), List.of(currentBinding))
992993
.scan(commonNestedExpression, currentNestedExpression)) {
993994
accummulator.add(c.head);

‎test/langtools/tools/javac/patterns/TranslationTest.java

+83-2
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,15 @@
2323

2424
/**
2525
* @test
26-
* @bug 8291769
26+
* @bug 8291769 8326129
2727
* @summary Check expected translation of various pattern related constructs
2828
* @library /tools/lib
2929
* @modules jdk.compiler/com.sun.tools.javac.api
3030
* jdk.compiler/com.sun.tools.javac.comp
3131
* jdk.compiler/com.sun.tools.javac.main
3232
* jdk.compiler/com.sun.tools.javac.tree
3333
* jdk.compiler/com.sun.tools.javac.util
34-
* @build toolbox.ToolBox toolbox.JavacTask
34+
* @build toolbox.ToolBox toolbox.JavacTask toolbox.JavaTask
3535
* @run main TranslationTest
3636
*/
3737

@@ -48,6 +48,7 @@
4848
import com.sun.tools.javac.tree.TreeScanner;
4949
import com.sun.tools.javac.util.Context;
5050
import com.sun.tools.javac.util.Context.Factory;
51+
import java.io.File;
5152
import java.io.IOException;
5253
import java.nio.file.Files;
5354
import java.nio.file.Path;
@@ -56,6 +57,7 @@
5657
import java.util.List;
5758

5859
import toolbox.TestRunner;
60+
import toolbox.JavaTask;
5961
import toolbox.JavacTask;
6062
import toolbox.Task;
6163
import toolbox.ToolBox;
@@ -188,6 +190,38 @@ private int test(Object obj) {
188190
""");
189191
}
190192

193+
@Test //JDK-8326129
194+
public void testRunWithNull(Path base) throws Exception {
195+
doRunTest(base,
196+
new String[]{"""
197+
package lib;
198+
public record Box(Object o) {}
199+
"""},
200+
"""
201+
import lib.*;
202+
public class Test {
203+
public static void main(String... args) {
204+
System.err.println(new Test().test(new Box(null)));
205+
}
206+
private int test(Box b) {
207+
return switch (b) {
208+
case Box(Integer i) -> 0;
209+
case Box(Object o) when check(o) -> 1;
210+
case Box(Object o) -> 2;
211+
};
212+
}
213+
private static int c;
214+
private boolean check(Object o) {
215+
System.err.println("check: " + o);
216+
if (c++ > 10) throw new IllegalStateException();
217+
return o != null;
218+
}
219+
}
220+
""",
221+
"check: null",
222+
"2");
223+
}
224+
191225
private void doTest(Path base, String[] libraryCode, String testCode,
192226
Callback callback, String expectedOutput) throws IOException {
193227
Path current = base.resolve(".");
@@ -322,4 +356,51 @@ public JCTree translateTopLevelClass(Env<AttrContext> env, JCTree cdef, TreeMake
322356
}
323357

324358
}
359+
360+
private void doRunTest(Path base, String[] libraryCode, String testCode,
361+
String... expectedOutput) throws IOException {
362+
Path current = base.resolve(".");
363+
Path libClasses = current.resolve("libClasses");
364+
365+
Files.createDirectories(libClasses);
366+
367+
if (libraryCode.length != 0) {
368+
Path libSrc = current.resolve("lib-src");
369+
370+
for (String code : libraryCode) {
371+
tb.writeJavaFiles(libSrc, code);
372+
}
373+
374+
new JavacTask(tb)
375+
.outdir(libClasses)
376+
.files(tb.findJavaFiles(libSrc))
377+
.run();
378+
}
379+
380+
Path src = current.resolve("src");
381+
tb.writeJavaFiles(src, testCode);
382+
383+
Path classes = current.resolve("classes");
384+
385+
Files.createDirectories(classes);
386+
387+
new JavacTask(tb)
388+
.options("-Xlint:-preview",
389+
"--class-path", libClasses.toString())
390+
.outdir(classes)
391+
.files(tb.findJavaFiles(src))
392+
.run()
393+
.writeAll();
394+
395+
List<String> log = new JavaTask(tb)
396+
.classpath(libClasses.toString() + File.pathSeparatorChar + classes.toString())
397+
.classArgs("Test")
398+
.run()
399+
.getOutputLines(Task.OutputKind.STDERR);
400+
401+
if (!List.of(expectedOutput).equals(log)) {
402+
throw new AssertionError("Expected: " + expectedOutput +
403+
"but got: " + log);
404+
}
405+
}
325406
}

0 commit comments

Comments
 (0)
Please sign in to comment.