Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoiding crash during indexing when a record has a component with a wrong name. #6649

Merged
merged 1 commit into from
Nov 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
import com.sun.tools.javac.util.FatalError;
import com.sun.tools.javac.util.ListBuffer;
import com.sun.tools.javac.util.Log;
import com.sun.tools.javac.util.Names;
import com.sun.tools.javac.util.Pair;
import java.io.File;
import java.io.IOException;
Expand Down Expand Up @@ -541,6 +542,7 @@ private void dropMethodsAndErrors(com.sun.tools.javac.util.Context ctx, Compilat
return ;
}
hu.handled.add(cut);
Names names = Names.instance(ctx);
Symtab syms = Symtab.instance(ctx);
Trees trees = Trees.instance(BasicJavacTask.instance(ctx));
Types types = Types.instance(ctx);
Expand Down Expand Up @@ -732,6 +734,13 @@ public Void visitClass(ClassTree node, Void p) {
for (RecordComponent rc : clazz.sym.getRecordComponents()) {
rc.type = error2Object(rc.type);
scan(rc.accessorMeth, p);
if (rc.accessor == null) {
//the accessor is not created when the component type matches
//a non-arg j.l.Object method (which is a compile-time error)
//but the missing accessor will break Lower.
//initialize the field:
rc.accessor = new MethodSymbol(0, names.empty, new MethodType(com.sun.tools.javac.util.List.nil(), syms.errType, com.sun.tools.javac.util.List.nil(), syms.methodClass), clazz.sym);
}
}
for (JCTree def : clazz.defs) {
boolean errorClass = isErroneousClass(def);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2297,6 +2297,51 @@ public void testTypeTest() throws Exception {
assertEquals(expected, file2Fixed);
}

public void testWrongRecordComponent() throws Exception {
setSourceLevel("17");

Map<String, String> file2Fixed = new HashMap<>();
VanillaCompileWorker.fixedListener = (file, cut) -> {
try {
FileObject source = URLMapper.findFileObject(file.toUri().toURL());
file2Fixed.put(FileUtil.getRelativePath(getRoot(), source), cut.toString());
} catch (MalformedURLException ex) {
throw new IllegalStateException(ex);
}
};
ParsingOutput result = runIndexing(Arrays.asList(compileTuple("test/Test.java",
"package test;\n" +
"public record Test(int wait) {\n" +
"}\n")),
Arrays.asList());

assertFalse(result.lowMemory);
assertTrue(result.success);

Set<String> createdFiles = new HashSet<String>();

for (File created : result.createdFiles) {
createdFiles.add(getWorkDir().toURI().relativize(created.toURI()).getPath());
}

assertEquals(new HashSet<String>(Arrays.asList("cache/s1/java/15/classes/test/Test.sig")),
createdFiles);
Map<String, String> expected = Collections.singletonMap("test/Test.java",
"package test;\n" +
"\n" +
"public class Test {\n" +
" static {\n" +
" throw new java.lang.RuntimeException(\"Uncompilable code - compiler.err.illegal.record.component.name\");\n" +
" }\n" +
" \n" +
" public Test(int wait) {\n" +
" super();\n" +
" }\n" +
" private final int wait;\n" +
"}");
assertEquals(expected, file2Fixed);
}

public static void noop() {}

@Override
Expand Down