Skip to content

Commit 5351621

Browse files
committed
Cannot pop operand off an empty stack.
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
1 parent 430d4a8 commit 5351621

File tree

4 files changed

+95
-18
lines changed

4 files changed

+95
-18
lines changed

compiler/fir/raw-fir/light-tree2fir/src/org/jetbrains/kotlin/fir/lightTree/converter/LightTreeRawFirExpressionBuilder.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1164,7 +1164,7 @@ class LightTreeRawFirExpressionBuilder(
11641164
sourceElement: KtSourceElement,
11651165
referenceExpression: LighterASTNode,
11661166
): FirNamedReference {
1167-
return buildSimpleNamedReference {
1167+
return buildSimpleNamedReference { // Foo.of. "Foo" is build here
11681168
source = sourceElement
11691169
name = referenceExpression.asText.nameAsSafeName()
11701170
}

compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/FirSyntheticCallGenerator.kt

Lines changed: 88 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,14 @@ import org.jetbrains.kotlin.fir.declarations.utils.addDefaultBoundIfNecessary
2525
import org.jetbrains.kotlin.fir.expressions.*
2626
import org.jetbrains.kotlin.fir.expressions.builder.buildArgumentList
2727
import org.jetbrains.kotlin.fir.expressions.builder.buildFunctionCall
28+
import org.jetbrains.kotlin.fir.expressions.builder.buildPropertyAccessExpression
2829
import org.jetbrains.kotlin.fir.moduleData
2930
import org.jetbrains.kotlin.fir.references.FirErrorNamedReference
3031
import org.jetbrains.kotlin.fir.references.FirReference
3132
import org.jetbrains.kotlin.fir.references.FirResolvedCallableReference
32-
import org.jetbrains.kotlin.fir.references.FirResolvedNamedReference
3333
import org.jetbrains.kotlin.fir.references.builder.buildErrorNamedReference
3434
import org.jetbrains.kotlin.fir.references.builder.buildResolvedErrorReference
35+
import org.jetbrains.kotlin.fir.references.builder.buildSimpleNamedReference
3536
import org.jetbrains.kotlin.fir.references.impl.FirSimpleNamedReference
3637
import org.jetbrains.kotlin.fir.references.impl.FirStubReference
3738
import org.jetbrains.kotlin.fir.references.isError
@@ -44,7 +45,10 @@ import org.jetbrains.kotlin.fir.resolve.diagnostics.ConeInapplicableCandidateErr
4445
import org.jetbrains.kotlin.fir.resolve.diagnostics.ConeUnresolvedNameError
4546
import org.jetbrains.kotlin.fir.resolve.providers.symbolProvider
4647
import org.jetbrains.kotlin.fir.resolve.transformers.body.resolve.resultType
47-
import org.jetbrains.kotlin.fir.resolvedTypeFromPrototype
48+
import org.jetbrains.kotlin.fir.scopes.getFunctions
49+
import org.jetbrains.kotlin.fir.scopes.getProperties
50+
import org.jetbrains.kotlin.fir.scopes.unsubstitutedScope
51+
import org.jetbrains.kotlin.fir.symbols.FirBasedSymbol
4852
import org.jetbrains.kotlin.fir.symbols.SyntheticCallableId
4953
import org.jetbrains.kotlin.fir.symbols.impl.FirFunctionSymbol
5054
import org.jetbrains.kotlin.fir.symbols.impl.FirNamedFunctionSymbol
@@ -94,7 +98,7 @@ class FirSyntheticCallGenerator(
9498
}
9599
val reference = generateCalleeReferenceWithCandidate(
96100
whenExpression,
97-
whenSelectFunction,
101+
whenSelectFunction.symbol,
98102
argumentList,
99103
SyntheticCallableId.WHEN.callableName,
100104
context = context,
@@ -122,7 +126,7 @@ class FirSyntheticCallGenerator(
122126

123127
val reference = generateCalleeReferenceWithCandidate(
124128
tryExpression,
125-
trySelectFunction,
129+
trySelectFunction.symbol,
126130
argumentList,
127131
SyntheticCallableId.TRY.callableName,
128132
context = context,
@@ -141,7 +145,7 @@ class FirSyntheticCallGenerator(
141145

142146
val reference = generateCalleeReferenceWithCandidate(
143147
checkNotNullCall,
144-
checkNotNullFunction,
148+
checkNotNullFunction.symbol,
145149
checkNotNullCall.argumentList,
146150
SyntheticCallableId.CHECK_NOT_NULL.callableName,
147151
context = context,
@@ -164,7 +168,7 @@ class FirSyntheticCallGenerator(
164168
}
165169
val reference = generateCalleeReferenceWithCandidate(
166170
elvisExpression,
167-
elvisFunction,
171+
elvisFunction.symbol,
168172
argumentList,
169173
SyntheticCallableId.ELVIS_NOT_NULL.callableName,
170174
context = context,
@@ -182,7 +186,7 @@ class FirSyntheticCallGenerator(
182186
this.argumentList = argumentList
183187
calleeReference = generateCalleeReferenceWithCandidate(
184188
arrayLiteral,
185-
idFunction,
189+
idFunction.symbol,
186190
argumentList,
187191
SyntheticCallableId.ID.callableName,
188192
context = context,
@@ -203,7 +207,75 @@ class FirSyntheticCallGenerator(
203207
expectedTypeConeType.isSet -> generateCollectionOfCall(Name.identifier("setOf"), arrayLiteral, context, resolutionMode)
204208
expectedTypeConeType.isMutableSet -> generateCollectionOfCall(Name.identifier("mutableSetOf"), arrayLiteral, context, resolutionMode)
205209
expectedTypeConeType.isArrayType -> generateArrayOfCall(arrayLiteral, expectedTypeConeType, context, resolutionMode)
206-
else -> TODO("nested static of is not yet supported")
210+
else -> {
211+
val toSymbol = expectedTypeConeType.toSymbol(session) ?: error("todo: expectedTypeConeType.toSymbol == null")
212+
val klass = toSymbol.fir as? FirRegularClass ?: error("todo ${toSymbol.fir::class} is not FirRegularClass")
213+
// val staticScope = klass.staticScope(session, components.scopeSession)
214+
// val companionProperty = staticScope?.getProperties(Name.identifier("Companion"))?.singleOrNull()!!
215+
val companionObjectSymbol = klass.companionObjectSymbol
216+
val scope = companionObjectSymbol?.unsubstitutedScope(
217+
session,
218+
components.scopeSession,
219+
withForcedTypeCalculator = false,
220+
FirResolvePhase.BODY_RESOLVE
221+
) ?: error("todo scope == null")
222+
val name = Name.identifier("of")
223+
val ofFunction =
224+
scope.getFunctions(name).singleOrNull { it.valueParameterSymbols.singleOrNull()?.isVararg == true }
225+
?: error("todo: ofFunction == null")
226+
val function = buildFunctionCall {
227+
argumentList = arrayLiteral.argumentList
228+
source = arrayLiteral.source
229+
// explicitReceiver = buildPropertyAccessExpression {
230+
// source = arrayLiteral.source
231+
//
232+
// calleeReference = buildSimpleNamedReference {
233+
// source = arrayLiteral.source
234+
// this.name = Name.identifier("Foo") // todo different package
235+
// }
236+
//
237+
// // calleeReference = generateCalleeReferenceWithCandidate(
238+
// // arrayLiteral,
239+
// // klass.symbol,
240+
// // FirEmptyArgumentList,
241+
// // Name.identifier("Foo"),
242+
// // callKind = CallKind.VariableAccess,
243+
// // context,
244+
// // resolutionMode
245+
// // )
246+
//
247+
// // calleeReference = generateCalleeReferenceWithCandidate(
248+
// // arrayLiteral,
249+
// // companionObjectSymbol,
250+
// // FirEmptyArgumentList,
251+
// // Name.identifier("Companion"),
252+
// // callKind = CallKind.VariableAccess,
253+
// // // callKind = CallKind.Function,
254+
// // context,
255+
// // resolutionMode
256+
// // )
257+
//
258+
// }
259+
260+
// calleeReference = buildSimpleNamedReference {
261+
// source = arrayLiteral.source
262+
// this.name = Name.identifier("of")
263+
// }
264+
265+
calleeReference = generateCalleeReferenceWithCandidate(
266+
arrayLiteral,
267+
ofFunction.fir.symbol,
268+
argumentList,
269+
name,
270+
callKind = CallKind.Function,
271+
context,
272+
resolutionMode,
273+
)
274+
275+
// origin = FirFunctionCallOrigin.Operator
276+
}
277+
function
278+
}
207279
}
208280
}
209281

@@ -221,7 +293,7 @@ class FirSyntheticCallGenerator(
221293
this.argumentList = argumentList
222294
calleeReference = generateCalleeReferenceWithCandidate(
223295
arrayLiteral,
224-
symbol.fir,
296+
symbol,
225297
argumentList,
226298
name,
227299
callKind = CallKind.Function,
@@ -245,7 +317,7 @@ class FirSyntheticCallGenerator(
245317
calleeReference = arrayOfSymbol?.let {
246318
generateCalleeReferenceWithCandidate(
247319
arrayLiteral,
248-
it.fir,
320+
it,
249321
argumentList,
250322
ArrayFqNames.ARRAY_OF_FUNCTION,
251323
callKind = CallKind.Function,
@@ -399,7 +471,7 @@ class FirSyntheticCallGenerator(
399471

400472
return generateCalleeReferenceWithCandidate(
401473
callableReferenceAccess,
402-
function,
474+
function.symbol,
403475
argumentList,
404476
callableId.callableName,
405477
CallKind.SyntheticIdForCallableReferencesResolution,
@@ -410,7 +482,7 @@ class FirSyntheticCallGenerator(
410482

411483
private fun generateCalleeReferenceWithCandidate(
412484
callSite: FirExpression,
413-
function: FirSimpleFunction,
485+
function: FirBasedSymbol<*>,
414486
argumentList: FirArgumentList,
415487
name: Name,
416488
callKind: CallKind = CallKind.SyntheticSelect,
@@ -434,12 +506,13 @@ class FirSyntheticCallGenerator(
434506
return FirNamedReferenceWithCandidate(source, name, candidate)
435507
}
436508

437-
private fun generateCandidate(callInfo: CallInfo, function: FirSimpleFunction, context: ResolutionContext): Candidate {
509+
private fun generateCandidate(callInfo: CallInfo, function: FirBasedSymbol<*>, context: ResolutionContext): Candidate {
438510
val candidateFactory = CandidateFactory(context, callInfo)
439511
return candidateFactory.createCandidate(
440512
callInfo,
441-
symbol = function.symbol,
442-
explicitReceiverKind = ExplicitReceiverKind.NO_EXPLICIT_RECEIVER,
513+
symbol = function,
514+
explicitReceiverKind = if (callInfo.name.asString() == "of") ExplicitReceiverKind.DISPATCH_RECEIVER else ExplicitReceiverKind.NO_EXPLICIT_RECEIVER,
515+
// explicitReceiverKind = ExplicitReceiverKind.NO_EXPLICIT_RECEIVER,
443516
scope = null
444517
)
445518
}

compiler/fir/tree/gen/org/jetbrains/kotlin/fir/expressions/impl/FirFunctionCallImpl.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ open class FirFunctionCallImpl @FirImplementationDetail constructor( // Foo.Comp
4141
override val origin: FirFunctionCallOrigin,
4242
) : FirFunctionCall() {
4343

44+
init {
45+
println(calleeReference.name)
46+
}
47+
4448
override fun <R, D> acceptChildren(visitor: FirVisitor<R, D>, data: D) {
4549
annotations.forEach { it.accept(visitor, data) }
4650
contextArguments.forEach { it.accept(visitor, data) }

compiler/fir/tree/src/org/jetbrains/kotlin/fir/declarations/FirResolvePhase.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ package org.jetbrains.kotlin.fir.declarations
4848
* as they will only be calculated during the [IMPLICIT_TYPES_BODY_RESOLVE] phase.
4949
* See [isItAllowedToCallLazyResolveTo] as a reference.
5050
*
51-
* 3. The compiler **can request and rely on the information from the current phase only during *jumping phases***.
51+
* 3. The compiler **can request and rely on the information from the current phase only during *jumping phases*.
5252
* For example, during the [TYPES] phase,
5353
* we cannot request type information for other declarations (except information from the [SUPER_TYPES] phase, such as a super type)
5454
* as this information will be calculated only during this phase.
@@ -57,7 +57,7 @@ package org.jetbrains.kotlin.fir.declarations
5757
*
5858
* A *jumping phase* is a phase that can request the phase-specific information during that same phase from another declaration.
5959
*
60-
* Currently, we have four ***jumping phases***:
60+
* Currently, we have four *jumping phases*:
6161
*
6262
* - [COMPILER_REQUIRED_ANNOTATIONS] – The compiler can jump from the use site of an annotation to the annotation class
6363
* and resolve its annotations as well.

0 commit comments

Comments
 (0)