From b47d37094e982edbbae643fb71a9e40deb890091 Mon Sep 17 00:00:00 2001 From: Stepan Koltsov Date: Thu, 19 Apr 2012 16:41:34 +0400 Subject: [PATCH] properly report exception if analyze failed #KT-1831 Fixed --- .../compiler/KotlinToJVMBytecodeCompiler.java | 2 ++ .../resolve/java/AnalyzerFacadeForJVM.java | 3 +- .../jet/analyzer/AnalyzeExhaust.java | 31 +++++++++++++++++-- .../jetbrains/jet/asJava/JetLightClass.java | 4 +++ .../jet/codegen/CodegenTestCase.java | 1 + .../jet/codegen/GenerationUtils.java | 1 + .../plugin/debugger/JetPositionManager.java | 1 + .../codewindow/BytecodeToolwindow.java | 12 +++++-- .../project/AnalyzerFacadeWithCache.java | 2 +- .../project/JSAnalyzerFacadeForIDEA.java | 4 +-- 10 files changed, 51 insertions(+), 10 deletions(-) diff --git a/compiler/cli/src/org/jetbrains/jet/compiler/KotlinToJVMBytecodeCompiler.java b/compiler/cli/src/org/jetbrains/jet/compiler/KotlinToJVMBytecodeCompiler.java index 51557493b91..ecc239820ad 100644 --- a/compiler/cli/src/org/jetbrains/jet/compiler/KotlinToJVMBytecodeCompiler.java +++ b/compiler/cli/src/org/jetbrains/jet/compiler/KotlinToJVMBytecodeCompiler.java @@ -71,6 +71,8 @@ public class KotlinToJVMBytecodeCompiler { return null; } + exhaust.throwIfError(); + return generate(environment, dependencies, messageCollector, exhaust, stubs); } diff --git a/compiler/frontend.java/src/org/jetbrains/jet/lang/resolve/java/AnalyzerFacadeForJVM.java b/compiler/frontend.java/src/org/jetbrains/jet/lang/resolve/java/AnalyzerFacadeForJVM.java index 8b07dbc02a6..05fef231d78 100644 --- a/compiler/frontend.java/src/org/jetbrains/jet/lang/resolve/java/AnalyzerFacadeForJVM.java +++ b/compiler/frontend.java/src/org/jetbrains/jet/lang/resolve/java/AnalyzerFacadeForJVM.java @@ -96,7 +96,8 @@ public enum AnalyzerFacadeForJVM implements AnalyzerFacade { injector.getTopDownAnalyzer().analyzeFiles(files); - return new AnalyzeExhaust(bindingTraceContext.getBindingContext(), JetStandardLibrary.getInstance()); + + return AnalyzeExhaust.success(bindingTraceContext.getBindingContext(), JetStandardLibrary.getInstance()); } public static AnalyzeExhaust shallowAnalyzeFiles(Collection files, diff --git a/compiler/frontend/src/org/jetbrains/jet/analyzer/AnalyzeExhaust.java b/compiler/frontend/src/org/jetbrains/jet/analyzer/AnalyzeExhaust.java index 3a2a9af1ef9..4c2950bfb42 100644 --- a/compiler/frontend/src/org/jetbrains/jet/analyzer/AnalyzeExhaust.java +++ b/compiler/frontend/src/org/jetbrains/jet/analyzer/AnalyzeExhaust.java @@ -27,12 +27,22 @@ import org.jetbrains.jet.lang.types.lang.JetStandardLibrary; public class AnalyzeExhaust { @NotNull private final BindingContext bindingContext; - @Nullable private final JetStandardLibrary standardLibrary; + private final Throwable error; - public AnalyzeExhaust(@NotNull BindingContext bindingContext, @Nullable JetStandardLibrary standardLibrary) { + private AnalyzeExhaust(@NotNull BindingContext bindingContext, + @Nullable JetStandardLibrary standardLibrary, @Nullable Throwable error) { this.bindingContext = bindingContext; this.standardLibrary = standardLibrary; + this.error = error; + } + + public static AnalyzeExhaust success(@NotNull BindingContext bindingContext, @NotNull JetStandardLibrary standardLibrary) { + return new AnalyzeExhaust(bindingContext, standardLibrary, null); + } + + public static AnalyzeExhaust error(@NotNull BindingContext bindingContext, @NotNull Throwable error) { + return new AnalyzeExhaust(bindingContext, null, error); } @NotNull @@ -40,8 +50,23 @@ public class AnalyzeExhaust { return bindingContext; } - @Nullable + @NotNull public JetStandardLibrary getStandardLibrary() { return standardLibrary; } + + @NotNull + public Throwable getError() { + return error; + } + + public boolean isError() { + return error != null; + } + + public void throwIfError() { + if (isError()) { + throw new IllegalStateException("failed to analyze: " + error, error); + } + } } diff --git a/compiler/jet.as.java.psi/src/org/jetbrains/jet/asJava/JetLightClass.java b/compiler/jet.as.java.psi/src/org/jetbrains/jet/asJava/JetLightClass.java index 8a89bb36ab0..0108405be00 100644 --- a/compiler/jet.as.java.psi/src/org/jetbrains/jet/asJava/JetLightClass.java +++ b/compiler/jet.as.java.psi/src/org/jetbrains/jet/asJava/JetLightClass.java @@ -165,6 +165,10 @@ public class JetLightClass extends AbstractLightClass implements JetJavaMirrorMa // TODO: wrong environment // stepan.koltsov@ 2012-04-09 CompilerSpecialMode.REGULAR, CompilerDependencies.compilerDependenciesForProduction(CompilerSpecialMode.REGULAR)); + if (context.isError()) { + throw new IllegalStateException("failed to analyze: " + context.getError(), context.getError()); + } + final GenerationState state = new GenerationState(project, builderFactory, context, Collections.singletonList(file)) { @Override protected void generateNamespace(JetFile namespace) { diff --git a/compiler/tests/org/jetbrains/jet/codegen/CodegenTestCase.java b/compiler/tests/org/jetbrains/jet/codegen/CodegenTestCase.java index 842f18efc63..c9f64026018 100644 --- a/compiler/tests/org/jetbrains/jet/codegen/CodegenTestCase.java +++ b/compiler/tests/org/jetbrains/jet/codegen/CodegenTestCase.java @@ -128,6 +128,7 @@ public abstract class CodegenTestCase extends JetLiteFixture { final AnalyzeExhaust analyzeExhaust = AnalyzerFacadeForJVM.analyzeOneFileWithJavaIntegrationAndCheckForErrors( myFile, JetControlFlowDataTraceFactory.EMPTY, CompileCompilerDependenciesTest.compilerDependenciesForTests(CompilerSpecialMode.REGULAR)); + analyzeExhaust.throwIfError(); GenerationState state = new GenerationState(getProject(), classBuilderFactory, analyzeExhaust, Collections.singletonList(myFile)); state.compileCorrectFiles(CompilationErrorHandler.THROW_EXCEPTION); return state; diff --git a/compiler/tests/org/jetbrains/jet/codegen/GenerationUtils.java b/compiler/tests/org/jetbrains/jet/codegen/GenerationUtils.java index e7829724525..63f3b2c796c 100644 --- a/compiler/tests/org/jetbrains/jet/codegen/GenerationUtils.java +++ b/compiler/tests/org/jetbrains/jet/codegen/GenerationUtils.java @@ -39,6 +39,7 @@ public class GenerationUtils { final AnalyzeExhaust analyzeExhaust = AnalyzerFacadeForJVM.analyzeOneFileWithJavaIntegrationAndCheckForErrors( psiFile, JetControlFlowDataTraceFactory.EMPTY, CompileCompilerDependenciesTest.compilerDependenciesForTests(compilerSpecialMode)); + analyzeExhaust.throwIfError(); GenerationState state = new GenerationState(psiFile.getProject(), ClassBuilderFactories.binaries(false), analyzeExhaust, Collections.singletonList(psiFile)); state.compileCorrectFiles(CompilationErrorHandler.THROW_EXCEPTION); return state; diff --git a/idea/src/org/jetbrains/jet/plugin/debugger/JetPositionManager.java b/idea/src/org/jetbrains/jet/plugin/debugger/JetPositionManager.java index 7b53f454aa2..5c2f3d532e5 100644 --- a/idea/src/org/jetbrains/jet/plugin/debugger/JetPositionManager.java +++ b/idea/src/org/jetbrains/jet/plugin/debugger/JetPositionManager.java @@ -155,6 +155,7 @@ public class JetPositionManager implements PositionManager { return mapper; } final AnalyzeExhaust analyzeExhaust = WholeProjectAnalyzerFacade.analyzeProjectWithCacheOnAFile(file); + analyzeExhaust.throwIfError(); JetTypeMapper typeMapper = new InjectorForJetTypeMapper( analyzeExhaust.getStandardLibrary(), analyzeExhaust.getBindingContext(), Collections.singletonList(file)).getJetTypeMapper(); myTypeMappers.put(file, typeMapper); diff --git a/idea/src/org/jetbrains/jet/plugin/internal/codewindow/BytecodeToolwindow.java b/idea/src/org/jetbrains/jet/plugin/internal/codewindow/BytecodeToolwindow.java index b7bfe2551a2..81c51834404 100644 --- a/idea/src/org/jetbrains/jet/plugin/internal/codewindow/BytecodeToolwindow.java +++ b/idea/src/org/jetbrains/jet/plugin/internal/codewindow/BytecodeToolwindow.java @@ -206,13 +206,14 @@ public class BytecodeToolwindow extends JPanel implements Disposable { try { AnalyzeExhaust binding = WholeProjectAnalyzerFacade.analyzeProjectWithCacheOnAFile(file); // AnalyzingUtils.throwExceptionOnErrors(binding); + if (binding.isError()) { + return printStackTraceToString(binding.getError()); + } state = new GenerationState(myProject, ClassBuilderFactories.TEXT, binding, Collections.singletonList(file)); state.compileCorrectFiles(CompilationErrorHandler.THROW_EXCEPTION); } catch (Exception e) { - StringWriter out = new StringWriter(1024); - e.printStackTrace(new PrintWriter(out)); - return out.toString().replaceAll("\r", ""); + return printStackTraceToString(e); } @@ -229,6 +230,11 @@ public class BytecodeToolwindow extends JPanel implements Disposable { return answer.toString(); } + private static String printStackTraceToString(Throwable e) {StringWriter out = new StringWriter(1024); + e.printStackTrace(new PrintWriter(out)); + return out.toString().replaceAll("\r", ""); + } + @Override public void dispose() { EditorFactory.getInstance().releaseEditor(myEditor); diff --git a/idea/src/org/jetbrains/jet/plugin/project/AnalyzerFacadeWithCache.java b/idea/src/org/jetbrains/jet/plugin/project/AnalyzerFacadeWithCache.java index c5ce3144984..fac916c618c 100644 --- a/idea/src/org/jetbrains/jet/plugin/project/AnalyzerFacadeWithCache.java +++ b/idea/src/org/jetbrains/jet/plugin/project/AnalyzerFacadeWithCache.java @@ -96,7 +96,7 @@ public final class AnalyzerFacadeWithCache { private Result emptyExhaustWithDiagnosticOnFile(Throwable e) { BindingTraceContext bindingTraceContext = new BindingTraceContext(); bindingTraceContext.report(Errors.EXCEPTION_WHILE_ANALYZING.on(file, e)); - AnalyzeExhaust analyzeExhaust = new AnalyzeExhaust(bindingTraceContext.getBindingContext(), null); + AnalyzeExhaust analyzeExhaust = AnalyzeExhaust.error(bindingTraceContext.getBindingContext(), e); return new Result(analyzeExhaust, PsiModificationTracker.MODIFICATION_COUNT); } }, false); diff --git a/idea/src/org/jetbrains/jet/plugin/project/JSAnalyzerFacadeForIDEA.java b/idea/src/org/jetbrains/jet/plugin/project/JSAnalyzerFacadeForIDEA.java index a9b0292b39f..80976459e39 100644 --- a/idea/src/org/jetbrains/jet/plugin/project/JSAnalyzerFacadeForIDEA.java +++ b/idea/src/org/jetbrains/jet/plugin/project/JSAnalyzerFacadeForIDEA.java @@ -47,6 +47,6 @@ public enum JSAnalyzerFacadeForIDEA implements AnalyzerFacade { @NotNull Predicate filesToAnalyzeCompletely, @NotNull JetControlFlowDataTraceFactory flowDataTraceFactory) { BindingContext context = AnalyzerFacadeForJS.analyzeFiles(files, new IDEAConfig(project)); - return new AnalyzeExhaust(context, JetStandardLibrary.getInstance()); + return AnalyzeExhaust.success(context, JetStandardLibrary.getInstance()); } -} \ No newline at end of file +} -- GitLab