arrow-kt / arrow-analysis

Arrow Analysis Plugin
https://arrow-kt.io/docs/meta/analysis/
Apache License 2.0
28 stars 3 forks source link

[BUG] ClassCastException for KotlinAnnotatedExpression #65

Open milgner opened 1 year ago

milgner commented 1 year ago

Describe the bug

After integrating Arrow Analysis, the Gradle task compileKotlin stumbles over this snippet:

inline fun <reified T> List<*>.asListOfType(): Option<List<T>> =
    if (all { it is T }) {
        @Suppress("UNCHECKED_CAST")
        (this as List<T>).some()
    } else {
        None
    }

Expected behavior

Compilation proceeds or outputs a descriptive error about what the problem might be.

Environment (please complete the following information):

Additional context

Full stacktrace:

exception during analysis: java.lang.ClassCastException
class arrow.meta.plugins.analysis.phases.analysis.solver.ast.kotlin.elements.KotlinAnnotatedExpression cannot be cast to class arrow.meta.plugins.analysis.phases.analysis.solver.ast.context.elements.Expression (arrow.meta.plugins.analysis.phases.analysis.solver.ast.kotlin.elements.KotlinAnnotatedExpression and arrow.meta.plugins.analysis.phases.analysis.solver.ast.context.elements.Expression are in unnamed module of loader java.net.URLClassLoader @23177e71)
java.lang.ClassCastException: class arrow.meta.plugins.analysis.phases.analysis.solver.ast.kotlin.elements.KotlinAnnotatedExpression cannot be cast to class arrow.meta.plugins.analysis.phases.analysis.solver.ast.context.elements.Expression (arrow.meta.plugins.analysis.phases.analysis.solver.ast.kotlin.elements.KotlinAnnotatedExpression and arrow.meta.plugins.analysis.phases.analysis.solver.ast.context.elements.Expression are in unnamed module of loader java.net.URLClassLoader @23177e71)
        at arrow.meta.plugins.analysis.phases.analysis.solver.ast.kotlin.elements.KotlinBlockExpression.getStatements(KotlinBlockExpression.kt:14)
        at arrow.meta.plugins.analysis.phases.analysis.solver.check.ExpressionsKt$checkExpressionConstraints$2$2.invoke(Expressions.kt:172)
        at arrow.meta.plugins.analysis.phases.analysis.solver.check.ExpressionsKt$checkExpressionConstraints$2$2.invoke(Expressions.kt:169)
        at arrow.meta.plugins.analysis.phases.analysis.solver.check.ExpressionsKt.inScope(Expressions.kt:1980)
        at arrow.meta.plugins.analysis.phases.analysis.solver.check.ExpressionsKt.access$inScope(Expressions.kt:1)
        at arrow.meta.plugins.analysis.phases.analysis.solver.check.ExpressionsKt$checkExpressionConstraints$2.invoke(Expressions.kt:169)
        at arrow.meta.plugins.analysis.phases.analysis.solver.check.ExpressionsKt$checkExpressionConstraints$2.invoke(Expressions.kt:156)
        at arrow.meta.continuations.ContSeq$flatMap$1.invokeSuspend(ContSeq.kt:60)
        at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
        at arrow.meta.continuations.ContSeqBuilder.hasNext(ContSeq.kt:233)
        at arrow.meta.continuations.ContSeq$map$1.invokeSuspend(ContSeq.kt:292)
        at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
        at arrow.meta.continuations.ContSeqBuilder.hasNext(ContSeq.kt:233)
        at arrow.meta.continuations.ContSeqBuilder.yieldAll(ContSeq.kt:274)
        at arrow.meta.continuations.ContSeqSyntax$DefaultImpls.yieldAll(ContSeq.kt:165)
        at arrow.meta.continuations.ContSeqBuilder.yieldAll(ContSeq.kt:203)
        at arrow.meta.continuations.ContSeq$flatMap$1.invokeSuspend(ContSeq.kt:60)
        at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
        at arrow.meta.continuations.ContSeqBuilder.hasNext(ContSeq.kt:233)
        at arrow.meta.continuations.ContSeqBuilder.yieldAll(ContSeq.kt:274)
        at arrow.meta.continuations.ContSeqSyntax$DefaultImpls.yieldAll(ContSeq.kt:165)
        at arrow.meta.continuations.ContSeqBuilder.yieldAll(ContSeq.kt:203)
        at arrow.meta.continuations.ContSeq$flatMap$1.invokeSuspend(ContSeq.kt:60)
        at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
        at arrow.meta.continuations.ContSeqBuilder.hasNext(ContSeq.kt:233)
        at arrow.meta.continuations.ContSeqBuilder.yieldAll(ContSeq.kt:274)
        at arrow.meta.continuations.ContSeqSyntax$DefaultImpls.yieldAll(ContSeq.kt:165)
        at arrow.meta.continuations.ContSeqBuilder.yieldAll(ContSeq.kt:203)
        at arrow.meta.continuations.ContSeq$flatMap$1.invokeSuspend(ContSeq.kt:60)
        at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
        at arrow.meta.continuations.ContSeqBuilder.hasNext(ContSeq.kt:233)
        at arrow.meta.continuations.ContSeqBuilder.yieldAll(ContSeq.kt:274)
        at arrow.meta.continuations.ContSeqSyntax$DefaultImpls.yieldAll(ContSeq.kt:165)
        at arrow.meta.continuations.ContSeqBuilder.yieldAll(ContSeq.kt:203)
        at arrow.meta.continuations.ContSeq$flatMap$1.invokeSuspend(ContSeq.kt:60)
        at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
        at arrow.meta.continuations.ContSeqBuilder.hasNext(ContSeq.kt:233)
        at arrow.meta.continuations.ContSeq$map$1.invokeSuspend(ContSeq.kt:292)
        at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
        at arrow.meta.continuations.ContSeqBuilder.hasNext(ContSeq.kt:233)
        at arrow.meta.continuations.ContSeq$map$1.invokeSuspend(ContSeq.kt:292)
        at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
        at arrow.meta.continuations.ContSeqBuilder.hasNext(ContSeq.kt:233)
        at arrow.meta.continuations.ContSeqBuilder.yieldAll(ContSeq.kt:274)
        at arrow.meta.continuations.ContSeqSyntax$DefaultImpls.yieldAll(ContSeq.kt:165)
        at arrow.meta.continuations.ContSeqBuilder.yieldAll(ContSeq.kt:203)
        at arrow.meta.continuations.ContSeq$flatMap$1.invokeSuspend(ContSeq.kt:60)
        at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
        at arrow.meta.continuations.ContSeqBuilder.hasNext(ContSeq.kt:233)
        at arrow.meta.continuations.ContSeqBuilder.yieldAll(ContSeq.kt:274)
        at arrow.meta.continuations.ContSeqSyntax$DefaultImpls.yieldAll(ContSeq.kt:165)
        at arrow.meta.continuations.ContSeqBuilder.yieldAll(ContSeq.kt:203)
        at arrow.meta.continuations.ContSeq$flatMap$1.invokeSuspend(ContSeq.kt:60)
        at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
        at arrow.meta.continuations.ContSeqBuilder.hasNext(ContSeq.kt:233)
        at arrow.meta.continuations.ContSeq.drain(ContSeq.kt:297)
        at arrow.meta.plugins.analysis.phases.analysis.solver.check.EntryPointKt.checkDeclarationConstraints(EntryPoint.kt:130)
        at arrow.meta.plugins.analysis.phases.PhasesKt$analysisPhases$4.invoke(Phases.kt:136)
        at arrow.meta.plugins.analysis.phases.PhasesKt$analysisPhases$4.invoke(Phases.kt:129)
        at arrow.meta.dsl.config.ConfigSyntax$storageComponent$1.check(ConfigSyntax.kt:72)
        at arrow.meta.internal.registry.InternalRegistry$DelegatingContributor$registerModuleComponents$2.check(InternalRegistry.kt:651)
        at org.jetbrains.kotlin.resolve.ModifiersChecker$ModifiersCheckingProcedure.runDeclarationCheckers(ModifiersChecker.java:285)
        at org.jetbrains.kotlin.resolve.ModifiersChecker$ModifiersCheckingProcedure.checkModifierListCommon(ModifiersChecker.java:224)
        at org.jetbrains.kotlin.resolve.ModifiersChecker$ModifiersCheckingProcedure.checkModifiersForDeclaration(ModifiersChecker.java:183)
        at org.jetbrains.kotlin.resolve.DeclarationsChecker.process(DeclarationsChecker.kt:101)
        at org.jetbrains.kotlin.resolve.BodyResolver.resolveBodies(BodyResolver.java:258)
        at org.jetbrains.kotlin.resolve.LazyTopDownAnalyzer.analyzeDeclarations(LazyTopDownAnalyzer.kt:227)
        at org.jetbrains.kotlin.resolve.LazyTopDownAnalyzer.analyzeDeclarations$default(LazyTopDownAnalyzer.kt:58)
        at org.jetbrains.kotlin.cli.jvm.compiler.TopDownAnalyzerFacadeForJVM.analyzeFilesWithJavaIntegration(TopDownAnalyzerFacadeForJVM.kt:119)
        at org.jetbrains.kotlin.cli.jvm.compiler.TopDownAnalyzerFacadeForJVM.analyzeFilesWithJavaIntegration$default(TopDownAnalyzerFacadeForJVM.kt:88)
        at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler$analyze$1.invoke(KotlinToJVMBytecodeCompiler.kt:256)
        at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler$analyze$1.invoke(KotlinToJVMBytecodeCompiler.kt:42)
        at org.jetbrains.kotlin.cli.common.messages.AnalyzerWithCompilerReport.analyzeAndReport(AnalyzerWithCompilerReport.kt:115)
        at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.analyze(KotlinToJVMBytecodeCompiler.kt:247)
        at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.repeatAnalysisIfNeeded(KotlinToJVMBytecodeCompiler.kt:181)
        at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.repeatAnalysisIfNeeded(KotlinToJVMBytecodeCompiler.kt:181)
        at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.compileModules$cli(KotlinToJVMBytecodeCompiler.kt:87)
        at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.compileModules$cli$default(KotlinToJVMBytecodeCompiler.kt:47)
        at org.jetbrains.kotlin.cli.jvm.K2JVMCompiler.doExecute(K2JVMCompiler.kt:168)
        at org.jetbrains.kotlin.cli.jvm.K2JVMCompiler.doExecute(K2JVMCompiler.kt:53)
        at org.jetbrains.kotlin.cli.common.CLICompiler.execImpl(CLICompiler.kt:100)
        at org.jetbrains.kotlin.cli.common.CLICompiler.execImpl(CLICompiler.kt:46)
        at org.jetbrains.kotlin.cli.common.CLITool.exec(CLITool.kt:101)
        at org.jetbrains.kotlin.incremental.IncrementalJvmCompilerRunner.runCompiler(IncrementalJvmCompilerRunner.kt:460)
        at org.jetbrains.kotlin.incremental.IncrementalJvmCompilerRunner.runCompiler(IncrementalJvmCompilerRunner.kt:62)
        at org.jetbrains.kotlin.incremental.IncrementalCompilerRunner.doCompile(IncrementalCompilerRunner.kt:476)
        at org.jetbrains.kotlin.incremental.IncrementalCompilerRunner.compileImpl(IncrementalCompilerRunner.kt:399)
        at org.jetbrains.kotlin.incremental.IncrementalCompilerRunner.compileNonIncrementally(IncrementalCompilerRunner.kt:280)
        at org.jetbrains.kotlin.incremental.IncrementalCompilerRunner.compile(IncrementalCompilerRunner.kt:124)
        at org.jetbrains.kotlin.daemon.CompileServiceImplBase.execIncrementalCompiler(CompileServiceImpl.kt:636)
        at org.jetbrains.kotlin.daemon.CompileServiceImplBase.access$execIncrementalCompiler(CompileServiceImpl.kt:101)
        at org.jetbrains.kotlin.daemon.CompileServiceImpl.compile(CompileServiceImpl.kt:1598)
        at jdk.internal.reflect.GeneratedMethodAccessor112.invoke(Unknown Source)
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.base/java.lang.reflect.Method.invoke(Method.java:566)
        at java.rmi/sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:359)
        at java.rmi/sun.rmi.transport.Transport$1.run(Transport.java:200)
        at java.rmi/sun.rmi.transport.Transport$1.run(Transport.java:197)
        at java.base/java.security.AccessController.doPrivileged(Native Method)
        at java.rmi/sun.rmi.transport.Transport.serviceCall(Transport.java:196)
        at java.rmi/sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:562)
        at java.rmi/sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:796)
        at java.rmi/sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run$0(TCPTransport.java:677)
        at java.base/java.security.AccessController.doPrivileged(Native Method)
        at java.rmi/sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:676)
        at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
        at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
        at java.base/java.lang.Thread.run(Thread.java:829)
milgner commented 1 year ago

After deleting the code in question, another stacktrace occurs:

e: java.lang.NoSuchMethodError: 'void org.jetbrains.kotlin.psi2ir.generators.TypeTranslatorImpl.<init>(org.jetbrains.kotlin.ir.util.ReferenceSymbolTable, org.jetbrains.kotlin.config.LanguageVersionSettings, org.jetbrains.kotlin.descriptors.ModuleDescriptor, kotlin.jvm.functions.Function0, boolean, org.jetbrains.kotlin.ir.util.StubGeneratorExtensions, org.jetbrains.kotlin.psi.KtFile, int, kotlin.jvm.internal.DefaultConstructorMarker)'
        at arrow.meta.phases.codegen.ir.IrUtils.<init>(IrUtils.kt:60)
        at arrow.meta.dsl.codegen.ir.IrSyntax$irFunction$1$1.visitFunction(IrSyntax.kt:186)
        at arrow.meta.dsl.codegen.ir.IrSyntax$irFunction$1$1.visitFunction(IrSyntax.kt:183)
        at org.jetbrains.kotlin.ir.visitors.IrElementTransformer$DefaultImpls.visitConstructor(IrElementTransformer.kt:123)
        at arrow.meta.dsl.codegen.ir.IrSyntax$irFunction$1$1.visitConstructor(IrSyntax.kt:183)
        at arrow.meta.dsl.codegen.ir.IrSyntax$irFunction$1$1.visitConstructor(IrSyntax.kt:183)
        at org.jetbrains.kotlin.ir.declarations.IrConstructor.accept(IrConstructor.kt:30)
        at org.jetbrains.kotlin.ir.IrElementBase.transform(IrElementBase.kt:24)
        at org.jetbrains.kotlin.ir.util.TransformKt.transformInPlace(transform.kt:35)
        at org.jetbrains.kotlin.ir.declarations.IrClass.transformChildren(IrClass.kt:83)
        at org.jetbrains.kotlin.ir.visitors.IrElementTransformer$DefaultImpls.visitDeclaration(IrElementTransformer.kt:103)
        at arrow.meta.dsl.codegen.ir.IrSyntax$irFunction$1$1.visitDeclaration(IrSyntax.kt:183)
        at arrow.meta.dsl.codegen.ir.IrSyntax$irFunction$1$1.visitDeclaration(IrSyntax.kt:183)
        at org.jetbrains.kotlin.ir.visitors.IrElementTransformer$DefaultImpls.visitClass(IrElementTransformer.kt:110)
        at arrow.meta.dsl.codegen.ir.IrSyntax$irFunction$1$1.visitClass(IrSyntax.kt:183)
        at arrow.meta.dsl.codegen.ir.IrSyntax$irFunction$1$1.visitClass(IrSyntax.kt:183)
        at org.jetbrains.kotlin.ir.declarations.IrClass.accept(IrClass.kt:73)
        at org.jetbrains.kotlin.ir.IrElementBase.transform(IrElementBase.kt:24)
        at org.jetbrains.kotlin.ir.util.TransformKt.transformInPlace(transform.kt:35)
        at org.jetbrains.kotlin.ir.declarations.IrFile.transformChildren(IrFile.kt:41)
        at org.jetbrains.kotlin.ir.visitors.IrElementTransformer$DefaultImpls.visitFile(IrElementTransformer.kt:168)
        at arrow.meta.dsl.codegen.ir.IrSyntax$irFunction$1$1.visitFile(IrSyntax.kt:183)
        at arrow.meta.dsl.codegen.ir.IrSyntax$irFunction$1$1.visitFile(IrSyntax.kt:183)
        at org.jetbrains.kotlin.ir.declarations.IrFile.accept(IrFile.kt:31)
        at org.jetbrains.kotlin.ir.declarations.IrFile.transform(IrFile.kt:34)
        at org.jetbrains.kotlin.ir.declarations.IrFile.transform(IrFile.kt:22)
        at org.jetbrains.kotlin.ir.util.TransformKt.transformInPlace(transform.kt:35)
        at org.jetbrains.kotlin.ir.declarations.IrModuleFragment.transformChildren(IrModuleFragment.kt:52)
        at arrow.meta.dsl.codegen.ir.IrSyntax$irFunction$1.invoke(IrSyntax.kt:182)
        at arrow.meta.dsl.codegen.ir.IrSyntax$irFunction$1.invoke(IrSyntax.kt:181)
        at arrow.meta.dsl.codegen.ir.IrSyntax$IrGeneration$1.generate(IrSyntax.kt:117)
        at arrow.meta.internal.registry.InternalRegistry$registerIRGeneration$1.generate(InternalRegistry.kt:324)
        at org.jetbrains.kotlin.backend.jvm.JvmIrCodegenFactory.convertToIr$lambda$1(JvmIrCodegenFactory.kt:222)
        at org.jetbrains.kotlin.psi2ir.Psi2IrTranslator.generateModuleFragment(Psi2IrTranslator.kt:107)
        at org.jetbrains.kotlin.backend.jvm.JvmIrCodegenFactory.convertToIr(JvmIrCodegenFactory.kt:255)
        at org.jetbrains.kotlin.backend.jvm.JvmIrCodegenFactory.convertToIr(JvmIrCodegenFactory.kt:59)
        at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.convertToIr(KotlinToJVMBytecodeCompiler.kt:224)
        at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.compileModules$cli(KotlinToJVMBytecodeCompiler.kt:101)
        at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.compileModules$cli$default(KotlinToJVMBytecodeCompiler.kt:47)
        at org.jetbrains.kotlin.cli.jvm.K2JVMCompiler.doExecute(K2JVMCompiler.kt:168)
        at org.jetbrains.kotlin.cli.jvm.K2JVMCompiler.doExecute(K2JVMCompiler.kt:53)
        at org.jetbrains.kotlin.cli.common.CLICompiler.execImpl(CLICompiler.kt:100)
        at org.jetbrains.kotlin.cli.common.CLICompiler.execImpl(CLICompiler.kt:46)
        at org.jetbrains.kotlin.cli.common.CLITool.exec(CLITool.kt:101)
        at org.jetbrains.kotlin.incremental.IncrementalJvmCompilerRunner.runCompiler(IncrementalJvmCompilerRunner.kt:460)
        at org.jetbrains.kotlin.incremental.IncrementalJvmCompilerRunner.runCompiler(IncrementalJvmCompilerRunner.kt:62)
        at org.jetbrains.kotlin.incremental.IncrementalCompilerRunner.doCompile(IncrementalCompilerRunner.kt:476)
        at org.jetbrains.kotlin.incremental.IncrementalCompilerRunner.compileImpl(IncrementalCompilerRunner.kt:399)
        at org.jetbrains.kotlin.incremental.IncrementalCompilerRunner.compileNonIncrementally(IncrementalCompilerRunner.kt:280)
        at org.jetbrains.kotlin.incremental.IncrementalCompilerRunner.compile(IncrementalCompilerRunner.kt:124)
        at org.jetbrains.kotlin.daemon.CompileServiceImplBase.execIncrementalCompiler(CompileServiceImpl.kt:636)
        at org.jetbrains.kotlin.daemon.CompileServiceImplBase.access$execIncrementalCompiler(CompileServiceImpl.kt:101)
        at org.jetbrains.kotlin.daemon.CompileServiceImpl.compile(CompileServiceImpl.kt:1598)
        at jdk.internal.reflect.GeneratedMethodAccessor112.invoke(Unknown Source)
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.base/java.lang.reflect.Method.invoke(Method.java:566)
        at java.rmi/sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:359)
        at java.rmi/sun.rmi.transport.Transport$1.run(Transport.java:200)
        at java.rmi/sun.rmi.transport.Transport$1.run(Transport.java:197)
        at java.base/java.security.AccessController.doPrivileged(Native Method)
        at java.rmi/sun.rmi.transport.Transport.serviceCall(Transport.java:196)
        at java.rmi/sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:562)
        at java.rmi/sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:796)
        at java.rmi/sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run$0(TCPTransport.java:677)
        at java.base/java.security.AccessController.doPrivileged(Native Method)
        at java.rmi/sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:676)
        at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
        at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
        at java.base/java.lang.Thread.run(Thread.java:829)

What surprises me is that it contains mentions of K2JVMCompiler but K2 support is as-of-yet not activated. I then did create a branch where I first enabled K2, the project compiled without a problem. Then I added back the Arrow Analysis plugin into Gradle and this backtrace came back, too.