Skip to content

Commit

Permalink
Cannot pop operand off an empty stack.
Browse files Browse the repository at this point in the history
exception: org.jetbrains.kotlin.codegen.CompilationException: Back-end (JVM) Internal error: Couldn't transform method node:
main ()V:
   L0
    LINENUMBER 16 L0
    ICONST_1
    ANEWARRAY java/lang/String
    ASTORE 1
    ALOAD 1
    ICONST_0
    LDC "foo"
    AASTORE
    ALOAD 1
    INVOKEVIRTUAL Foo$Companion.of ([Ljava/lang/String;)LFoo;
    ASTORE 0
   L1
    LINENUMBER 18 L1
    RETURN
   L2
    LOCALVARIABLE foo LFoo; L1 L2 0
    MAXSTACK = 3
    MAXLOCALS = 2

File is unknown
The root cause org.jetbrains.org.objectweb.asm.tree.analysis.AnalyzerException was thrown at: org.jetbrains.kotlin.codegen.optimization.common.FastAnalyzer.analyzeMainLoop(FastAnalyzer.kt:82)
        at org.jetbrains.kotlin.codegen.TransformationMethodVisitor.visitEnd(TransformationMethodVisitor.kt:89)
        at org.jetbrains.org.objectweb.asm.MethodVisitor.visitEnd(MethodVisitor.java:783)
        at org.jetbrains.org.objectweb.asm.tree.MethodNode.accept(MethodNode.java:772)
        at org.jetbrains.kotlin.backend.jvm.codegen.ClassCodegen.generateMethod(ClassCodegen.kt:453)
        at org.jetbrains.kotlin.backend.jvm.codegen.ClassCodegen.generate(ClassCodegen.kt:168)
        at org.jetbrains.kotlin.backend.jvm.FileCodegen.lower(JvmPhases.kt:40)
        at org.jetbrains.kotlin.backend.common.phaser.PhaseFactoriesKt.createFilePhase$lambda$4(PhaseFactories.kt:88)
        at org.jetbrains.kotlin.backend.common.phaser.PhaseBuildersKt$createSimpleNamedCompilerPhase$1.phaseBody(PhaseBuilders.kt:68)
        at org.jetbrains.kotlin.config.phaser.SimpleNamedCompilerPhase.phaseBody(CompilerPhase.kt:215)
        at org.jetbrains.kotlin.config.phaser.NamedCompilerPhase.invoke(CompilerPhase.kt:111)
        at org.jetbrains.kotlin.backend.common.phaser.PerformByIrFilePhase.invokeSequential(performByIrFile.kt:52)
        at org.jetbrains.kotlin.backend.common.phaser.PerformByIrFilePhase.invoke(performByIrFile.kt:42)
        at org.jetbrains.kotlin.backend.common.phaser.PerformByIrFilePhase.invoke(performByIrFile.kt:28)
        at org.jetbrains.kotlin.config.phaser.SameTypeNamedCompilerPhase.phaseBody(CompilerPhase.kt:160)
        at org.jetbrains.kotlin.config.phaser.NamedCompilerPhase.invoke(CompilerPhase.kt:111)
        at org.jetbrains.kotlin.backend.common.phaser.CompositePhase.invoke(PhaseBuilders.kt:28)
        at org.jetbrains.kotlin.config.phaser.SameTypeNamedCompilerPhase.phaseBody(CompilerPhase.kt:160)
        at org.jetbrains.kotlin.config.phaser.NamedCompilerPhase.invoke(CompilerPhase.kt:111)
        at org.jetbrains.kotlin.config.phaser.CompilerPhaseKt.invokeToplevel(CompilerPhase.kt:62)
        at org.jetbrains.kotlin.backend.jvm.JvmIrCodegenFactory.invokeCodegen(JvmIrCodegenFactory.kt:375)
        at org.jetbrains.kotlin.codegen.CodegenFactory.generateModule(CodegenFactory.kt:42)
        at org.jetbrains.kotlin.backend.jvm.JvmIrCodegenFactory.generateModuleInFrontendIRMode(JvmIrCodegenFactory.kt:437)
        at org.jetbrains.kotlin.cli.jvm.compiler.pipeline.JvmCompilerPipelineKt.generateCodeFromIr(jvmCompilerPipeline.kt:188)
        at org.jetbrains.kotlin.cli.jvm.compiler.pipeline.JvmCompilerPipelineKt.runBackend(jvmCompilerPipeline.kt:80)
        at org.jetbrains.kotlin.cli.jvm.compiler.pipeline.JvmCompilerPipelineLightTreeKt.compileModule(jvmCompilerPipelineLightTree.kt:264)
        at org.jetbrains.kotlin.cli.jvm.compiler.pipeline.JvmCompilerPipelineLightTreeKt.compileSingleModuleUsingFrontendIrAndLightTree(jvmCompilerPipelineLightTree.kt:231)
        at org.jetbrains.kotlin.cli.jvm.compiler.pipeline.JvmCompilerPipelineLightTreeKt.compileModulesUsingFrontendIrAndLightTree(jvmCompilerPipelineLightTree.kt:86)
        at org.jetbrains.kotlin.cli.jvm.K2JVMCompiler.doExecute(K2JVMCompiler.kt:143)
        at org.jetbrains.kotlin.cli.jvm.K2JVMCompiler.doExecute(K2JVMCompiler.kt:40)
        at org.jetbrains.kotlin.cli.common.CLICompiler.execImpl(CLICompiler.kt:102)
        at org.jetbrains.kotlin.cli.common.CLICompiler.exec(CLICompiler.kt:316)
        at org.jetbrains.kotlin.cli.common.CLICompiler.exec(CLICompiler.kt:294)
        at org.jetbrains.kotlin.cli.common.CLICompiler.exec(CLICompiler.kt:258)
        at org.jetbrains.kotlin.cli.common.CLICompiler$Companion.doMainNoExit(CLICompiler.kt:395)
        at org.jetbrains.kotlin.cli.common.CLICompiler$Companion.doMainNoExit$default(CLICompiler.kt:388)
        at org.jetbrains.kotlin.cli.common.CLICompiler$Companion.doMain(CLICompiler.kt:382)
        at org.jetbrains.kotlin.cli.jvm.K2JVMCompiler$Companion.main(K2JVMCompiler.kt:251)
        at org.jetbrains.kotlin.cli.jvm.K2JVMCompiler.main(K2JVMCompiler.kt)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.base/java.lang.reflect.Method.invoke(Method.java:568)
        at org.jetbrains.kotlin.preloading.Preloader.run(Preloader.java:87)
        at org.jetbrains.kotlin.preloading.Preloader.main(Preloader.java:44)
Caused by: org.jetbrains.org.objectweb.asm.tree.analysis.AnalyzerException: Error at instruction #10 INVOKEVIRTUAL Foo$Companion.of ([Ljava/lang/String;)LFoo;: Cannot pop operand off an empty stack.
current: {
  locals: [
    #0: org.jetbrains.org.objectweb.asm.tree.analysis.SourceValue@0
    #1: org.jetbrains.org.objectweb.asm.tree.analysis.SourceValue@4eba373c
  ]
  stack: size=0 []
}

        at org.jetbrains.kotlin.codegen.optimization.common.FastAnalyzer.analyzeMainLoop(FastAnalyzer.kt:82)
        at org.jetbrains.kotlin.codegen.optimization.common.FastAnalyzer.analyze(FastAnalyzer.kt:53)
        at org.jetbrains.kotlin.codegen.optimization.boxing.PopBackwardPropagationTransformer$Transformer.transform(PopBackwardPropagationTransformer.kt:58)
        at org.jetbrains.kotlin.codegen.optimization.boxing.PopBackwardPropagationTransformer.transform(PopBackwardPropagationTransformer.kt:41)
        at org.jetbrains.kotlin.codegen.optimization.transformer.CompositeMethodTransformer.transform(CompositeMethodTransformer.kt:25)
        at org.jetbrains.kotlin.codegen.optimization.OptimizationMethodVisitor.performTransformations(OptimizationMethodVisitor.kt:74)
        at org.jetbrains.kotlin.codegen.TransformationMethodVisitor.visitEnd(TransformationMethodVisitor.kt:67)
        ... 43 more
Caused by: java.lang.IndexOutOfBoundsException: Cannot pop operand off an empty stack.
        at org.jetbrains.org.objectweb.asm.tree.analysis.Frame.pop(Frame.java:228)
        at org.jetbrains.org.objectweb.asm.tree.analysis.Frame.executeInvokeInsn(Frame.java:643)
        at org.jetbrains.org.objectweb.asm.tree.analysis.Frame.execute(Frame.java:573)
        at org.jetbrains.kotlin.codegen.optimization.common.FastAnalyzer.analyzeInstruction(FastAnalyzer.kt:111)
        at org.jetbrains.kotlin.codegen.optimization.common.FastAnalyzer.analyzeMainLoop(FastAnalyzer.kt:74)
        ... 49 more
  • Loading branch information
nikitabobko committed Dec 3, 2024
1 parent 8a8abe3 commit 8869728
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1099,7 +1099,7 @@ class LightTreeRawFirExpressionBuilder(
sourceElement: KtSourceElement,
referenceExpression: LighterASTNode,
): FirNamedReference {
return buildSimpleNamedReference {
return buildSimpleNamedReference { // Foo.of. "Foo" is build here
source = sourceElement
name = referenceExpression.asText.nameAsSafeName()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,14 @@ import org.jetbrains.kotlin.fir.declarations.utils.addDefaultBoundIfNecessary
import org.jetbrains.kotlin.fir.expressions.*
import org.jetbrains.kotlin.fir.expressions.builder.buildArgumentList
import org.jetbrains.kotlin.fir.expressions.builder.buildFunctionCall
import org.jetbrains.kotlin.fir.expressions.builder.buildPropertyAccessExpression
import org.jetbrains.kotlin.fir.moduleData
import org.jetbrains.kotlin.fir.references.FirErrorNamedReference
import org.jetbrains.kotlin.fir.references.FirReference
import org.jetbrains.kotlin.fir.references.FirResolvedCallableReference
import org.jetbrains.kotlin.fir.references.FirResolvedNamedReference
import org.jetbrains.kotlin.fir.references.builder.buildErrorNamedReference
import org.jetbrains.kotlin.fir.references.builder.buildResolvedErrorReference
import org.jetbrains.kotlin.fir.references.builder.buildSimpleNamedReference
import org.jetbrains.kotlin.fir.references.impl.FirSimpleNamedReference
import org.jetbrains.kotlin.fir.references.impl.FirStubReference
import org.jetbrains.kotlin.fir.references.isError
Expand All @@ -44,7 +45,10 @@ import org.jetbrains.kotlin.fir.resolve.diagnostics.ConeInapplicableCandidateErr
import org.jetbrains.kotlin.fir.resolve.diagnostics.ConeUnresolvedNameError
import org.jetbrains.kotlin.fir.resolve.providers.symbolProvider
import org.jetbrains.kotlin.fir.resolve.transformers.body.resolve.resultType
import org.jetbrains.kotlin.fir.resolvedTypeFromPrototype
import org.jetbrains.kotlin.fir.scopes.getFunctions
import org.jetbrains.kotlin.fir.scopes.getProperties
import org.jetbrains.kotlin.fir.scopes.unsubstitutedScope
import org.jetbrains.kotlin.fir.symbols.FirBasedSymbol
import org.jetbrains.kotlin.fir.symbols.SyntheticCallableId
import org.jetbrains.kotlin.fir.symbols.impl.FirFunctionSymbol
import org.jetbrains.kotlin.fir.symbols.impl.FirNamedFunctionSymbol
Expand Down Expand Up @@ -94,7 +98,7 @@ class FirSyntheticCallGenerator(
}
val reference = generateCalleeReferenceWithCandidate(
whenExpression,
whenSelectFunction,
whenSelectFunction.symbol,
argumentList,
SyntheticCallableId.WHEN.callableName,
context = context,
Expand Down Expand Up @@ -122,7 +126,7 @@ class FirSyntheticCallGenerator(

val reference = generateCalleeReferenceWithCandidate(
tryExpression,
trySelectFunction,
trySelectFunction.symbol,
argumentList,
SyntheticCallableId.TRY.callableName,
context = context,
Expand All @@ -141,7 +145,7 @@ class FirSyntheticCallGenerator(

val reference = generateCalleeReferenceWithCandidate(
checkNotNullCall,
checkNotNullFunction,
checkNotNullFunction.symbol,
checkNotNullCall.argumentList,
SyntheticCallableId.CHECK_NOT_NULL.callableName,
context = context,
Expand All @@ -164,7 +168,7 @@ class FirSyntheticCallGenerator(
}
val reference = generateCalleeReferenceWithCandidate(
elvisExpression,
elvisFunction,
elvisFunction.symbol,
argumentList,
SyntheticCallableId.ELVIS_NOT_NULL.callableName,
context = context,
Expand All @@ -182,7 +186,7 @@ class FirSyntheticCallGenerator(
this.argumentList = argumentList
calleeReference = generateCalleeReferenceWithCandidate(
arrayLiteral,
idFunction,
idFunction.symbol,
argumentList,
SyntheticCallableId.ID.callableName,
context = context,
Expand All @@ -203,7 +207,75 @@ class FirSyntheticCallGenerator(
expectedTypeConeType.isSet -> generateCollectionOfCall(Name.identifier("setOf"), arrayLiteral, context, resolutionMode)
expectedTypeConeType.isMutableSet -> generateCollectionOfCall(Name.identifier("mutableSetOf"), arrayLiteral, context, resolutionMode)
expectedTypeConeType.isArrayType -> generateArrayOfCall(arrayLiteral, expectedTypeConeType, context, resolutionMode)
else -> TODO("nested static of is not yet supported")
else -> {
val toSymbol = expectedTypeConeType.toSymbol(session) ?: error("todo: expectedTypeConeType.toSymbol == null")
val klass = toSymbol.fir as? FirRegularClass ?: error("todo ${toSymbol.fir::class} is not FirRegularClass")
// val staticScope = klass.staticScope(session, components.scopeSession)
// val companionProperty = staticScope?.getProperties(Name.identifier("Companion"))?.singleOrNull()!!
val companionObjectSymbol = klass.companionObjectSymbol
val scope = companionObjectSymbol?.unsubstitutedScope(
session,
components.scopeSession,
withForcedTypeCalculator = false,
FirResolvePhase.BODY_RESOLVE
) ?: error("todo scope == null")
val name = Name.identifier("of")
val ofFunction =
scope.getFunctions(name).singleOrNull { it.valueParameterSymbols.singleOrNull()?.isVararg == true }
?: error("todo: ofFunction == null")
val function = buildFunctionCall {
argumentList = arrayLiteral.argumentList
source = arrayLiteral.source
// explicitReceiver = buildPropertyAccessExpression {
// source = arrayLiteral.source
//
// calleeReference = buildSimpleNamedReference {
// source = arrayLiteral.source
// this.name = Name.identifier("Foo") // todo different package
// }
//
// // calleeReference = generateCalleeReferenceWithCandidate(
// // arrayLiteral,
// // klass.symbol,
// // FirEmptyArgumentList,
// // Name.identifier("Foo"),
// // callKind = CallKind.VariableAccess,
// // context,
// // resolutionMode
// // )
//
// // calleeReference = generateCalleeReferenceWithCandidate(
// // arrayLiteral,
// // companionObjectSymbol,
// // FirEmptyArgumentList,
// // Name.identifier("Companion"),
// // callKind = CallKind.VariableAccess,
// // // callKind = CallKind.Function,
// // context,
// // resolutionMode
// // )
//
// }

// calleeReference = buildSimpleNamedReference {
// source = arrayLiteral.source
// this.name = Name.identifier("of")
// }

calleeReference = generateCalleeReferenceWithCandidate(
arrayLiteral,
ofFunction.fir.symbol,
argumentList,
name,
callKind = CallKind.Function,
context,
resolutionMode,
)

// origin = FirFunctionCallOrigin.Operator
}
function
}
}
}

Expand All @@ -221,7 +293,7 @@ class FirSyntheticCallGenerator(
this.argumentList = argumentList
calleeReference = generateCalleeReferenceWithCandidate(
arrayLiteral,
symbol.fir,
symbol,
argumentList,
name,
callKind = CallKind.Function,
Expand All @@ -245,7 +317,7 @@ class FirSyntheticCallGenerator(
calleeReference = arrayOfSymbol?.let {
generateCalleeReferenceWithCandidate(
arrayLiteral,
it.fir,
it,
argumentList,
ArrayFqNames.ARRAY_OF_FUNCTION,
callKind = CallKind.Function,
Expand Down Expand Up @@ -399,7 +471,7 @@ class FirSyntheticCallGenerator(

return generateCalleeReferenceWithCandidate(
callableReferenceAccess,
function,
function.symbol,
argumentList,
callableId.callableName,
CallKind.SyntheticIdForCallableReferencesResolution,
Expand All @@ -410,7 +482,7 @@ class FirSyntheticCallGenerator(

private fun generateCalleeReferenceWithCandidate(
callSite: FirExpression,
function: FirSimpleFunction,
function: FirBasedSymbol<*>,
argumentList: FirArgumentList,
name: Name,
callKind: CallKind = CallKind.SyntheticSelect,
Expand All @@ -434,12 +506,13 @@ class FirSyntheticCallGenerator(
return FirNamedReferenceWithCandidate(source, name, candidate)
}

private fun generateCandidate(callInfo: CallInfo, function: FirSimpleFunction, context: ResolutionContext): Candidate {
private fun generateCandidate(callInfo: CallInfo, function: FirBasedSymbol<*>, context: ResolutionContext): Candidate {
val candidateFactory = CandidateFactory(context, callInfo)
return candidateFactory.createCandidate(
callInfo,
symbol = function.symbol,
explicitReceiverKind = ExplicitReceiverKind.NO_EXPLICIT_RECEIVER,
symbol = function,
explicitReceiverKind = if (callInfo.name.asString() == "of") ExplicitReceiverKind.DISPATCH_RECEIVER else ExplicitReceiverKind.NO_EXPLICIT_RECEIVER,
// explicitReceiverKind = ExplicitReceiverKind.NO_EXPLICIT_RECEIVER,
scope = null
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ open class FirFunctionCallImpl @FirImplementationDetail constructor( // Foo.Comp
override val origin: FirFunctionCallOrigin,
) : FirFunctionCall() {

init {
println(calleeReference.name)
}

override fun <R, D> acceptChildren(visitor: FirVisitor<R, D>, data: D) {
annotations.forEach { it.accept(visitor, data) }
contextArguments.forEach { it.accept(visitor, data) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ package org.jetbrains.kotlin.fir.declarations
* as they will only be calculated during the [IMPLICIT_TYPES_BODY_RESOLVE] phase.
* See [isItAllowedToCallLazyResolveTo] as a reference.
*
* 3. The compiler **can request and rely on the information from the current phase only during *jumping phases***.
* 3. The compiler **can request and rely on the information from the current phase only during *jumping phases*.
* For example, during the [TYPES] phase,
* we cannot request type information for other declarations (except information from the [SUPER_TYPES] phase, such as a super type)
* as this information will be calculated only during this phase.
Expand All @@ -57,7 +57,7 @@ package org.jetbrains.kotlin.fir.declarations
*
* A *jumping phase* is a phase that can request the phase-specific information during that same phase from another declaration.
*
* Currently, we have four ***jumping phases***:
* Currently, we have four *jumping phases*:
*
* - [COMPILER_REQUIRED_ANNOTATIONS] – The compiler can jump from the use site of an annotation to the annotation class
* and resolve its annotations as well.
Expand Down

0 comments on commit 8869728

Please sign in to comment.