Skip to content

Commit

Permalink
Associate KotlinCompilation errors to input sources.
Browse files Browse the repository at this point in the history
fixes #24
  • Loading branch information
traceyyoshima committed Feb 23, 2023
1 parent edc877b commit 1386016
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
18 changes: 11 additions & 7 deletions src/main/java/org/openrewrite/kotlin/KotlinParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ public List<K.CompilationUnit> parseInputs(Iterable<Input> sources, @Nullable Pa
* @param ctx Execution context to use for collecting parsing failures.
* @return FirSession associated to type attributing the CompiledKotlinSources.
*/
Map<FirSession, List<CompiledKotlinSource>> parseInputsToCompilerAst(Disposable disposable, Iterable<Input> sources, @Nullable Path relativeTo, ParsingExecutionContextView ctx) {
Map<FirSession, List<CompiledKotlinSource>> parseInputsToCompilerAst(Disposable disposable, Iterable<Input> sources, @Nullable Path relativeTo, ExecutionContext ctx) {
CompilerConfiguration compilerConfiguration = compilerConfiguration();

File buildFile = null;
Expand Down Expand Up @@ -267,14 +267,14 @@ Map<FirSession, List<CompiledKotlinSource>> parseInputsToCompilerAst(Disposable
`LightVirtualFile` are created to support tests and in the future, Kotlin template.
We might want to extract the generation of `platformSources` later on.
*/
int i = 0;
for (Input source : sources) {
List<Input> inputs = acceptedInputs(sources);
for (int i = 0; i < inputs.size(); i++) {
Input source = inputs.get(i);
String fileName = "openRewriteFile.kt".equals(source.getPath().toString()) ? "openRewriteFile.kt" + i : source.getPath().toString();
VirtualFile vFile = new LightVirtualFile(fileName, KotlinFileType.INSTANCE, source.getSource(ctx).readFully());
platformSources.add(new KtVirtualFileSourceFile(vFile));
}

BaseDiagnosticsCollector diagnosticsReporter = DiagnosticReporterFactory.INSTANCE.createReporter(false);
ModuleCompilerInput compilerInput = new ModuleCompilerInput(
new TargetId(module.getModuleName(), module.getModuleType()),
CommonPlatforms.INSTANCE.getDefaultCommonPlatform(),
Expand All @@ -285,6 +285,7 @@ Map<FirSession, List<CompiledKotlinSource>> parseInputsToCompilerAst(Disposable
emptyList()
);

BaseDiagnosticsCollector diagnosticsReporter = DiagnosticReporterFactory.INSTANCE.createReporter(false);
ModuleCompilerEnvironment compilerEnvironment = new ModuleCompilerEnvironment(projectEnvironment, diagnosticsReporter);
CommonCompilerPerformanceManager performanceManager = compilerConfiguration.get(PERF_MANAGER);
ModuleCompilerAnalyzedOutput output = compileModuleToAnalyzedFir(
Expand All @@ -295,11 +296,14 @@ Map<FirSession, List<CompiledKotlinSource>> parseInputsToCompilerAst(Disposable
diagnosticsReporter,
performanceManager
);
convertAnalyzedFirToIr(compilerInput, output, compilerEnvironment);

try {
convertAnalyzedFirToIr(compilerInput, output, compilerEnvironment);
} catch (Throwable ignored) {
// Defer the exception until the Source that caused the compilation error is parsed to create a PlainText for the input.
}

List<FirFile> firFiles = output.getFir();
List<Input> inputs = new ArrayList<>(firFiles.size());
sources.iterator().forEachRemaining(inputs::add);
assert firFiles.size() == inputs.size();

List<CompiledKotlinSource> cus = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -976,6 +976,10 @@ private J mapFunctionalCall(FirFunctionCall functionCall, boolean isInfix) {
Space prefix = whitespace();

FirNamedReference namedReference = functionCall.getCalleeReference();
if (namedReference instanceof FirErrorNamedReference) {
throw new IllegalStateException("Unresolved name reference: " + ((FirErrorNamedReference) namedReference).getDiagnostic());
}

if (namedReference instanceof FirResolvedNamedReference &&
((FirResolvedNamedReference) namedReference).getResolvedSymbol() instanceof FirConstructorSymbol) {
TypeTree name = (J.Identifier) visitElement(namedReference, null);
Expand Down

0 comments on commit 1386016

Please sign in to comment.