From 9132c9b2d0adb0b7649ef22ad3b19fcc3bb4c484 Mon Sep 17 00:00:00 2001 From: Dmitry Petrov Date: Wed, 29 Dec 2021 20:09:45 +0300 Subject: [PATCH] JVM don't choke on exceptions in ClassFileFactory#createText --- .../kotlin/codegen/ClassFileFactory.java | 39 +++++++++++++------ 1 file changed, 27 insertions(+), 12 deletions(-) diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/ClassFileFactory.java b/compiler/backend/src/org/jetbrains/kotlin/codegen/ClassFileFactory.java index 87678e3e871..6d907a768ba 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/ClassFileFactory.java +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/ClassFileFactory.java @@ -178,9 +178,18 @@ public class ClassFileFactory implements OutputFileCollection { return createText(null); } + private static class ModuleMappingException extends RuntimeException { + public ModuleMappingException(String message) { + super(message); + } + } + @NotNull @TestOnly public String createText(@Nullable String ignorePrefixPath) { + // NB this method is frequently used in JVM BE tests to display generated bytecode in case of test failure. + // It should be permissive, and should try to handle exceptions gracefully (otherwise you would make JVM BE devs unhappy). + StringBuilder answer = new StringBuilder(); for (OutputFile file : asList()) { @@ -192,21 +201,27 @@ public class ClassFileFactory implements OutputFileCollection { answer.append(file.asText()); break; case "kotlin_module": { - ModuleMapping mapping = ModuleMappingUtilKt.loadModuleMapping( - ModuleMapping.Companion, file.asByteArray(), relativePath.getPath(), - CompilerDeserializationConfiguration.Default.INSTANCE, version -> { - throw new IllegalStateException("Version of the generated module cannot be incompatible: " + version); - } - ); - for (Map.Entry entry : mapping.getPackageFqName2Parts().entrySet()) { - FqName packageFqName = new FqName(entry.getKey()); - PackageParts packageParts = entry.getValue(); - answer.append("\n"); + try { + ModuleMapping mapping = ModuleMappingUtilKt.loadModuleMapping( + ModuleMapping.Companion, file.asByteArray(), relativePath.getPath(), + CompilerDeserializationConfiguration.Default.INSTANCE, + version -> { + throw new ModuleMappingException("Generated module has incompatible JVM metadata version: " + version); + } + ); + for (Map.Entry entry : mapping.getPackageFqName2Parts().entrySet()) { + FqName packageFqName = new FqName(entry.getKey()); + PackageParts packageParts = entry.getValue(); + answer.append("\n"); + } + break; + } catch (ModuleMappingException e) { + answer.append(relativePath).append(": ").append(e.getMessage()).append("\n"); + break; } - break; } default: - throw new UnsupportedOperationException("Unknown OutputFile: " + file); + answer.append("Unknown output file: ").append(file); } }