Hi all. I’m not sure this is the right place to as...
# compiler
s
Hi all. I’m not sure this is the right place to ask this (I’m completely new here, so please let me know if this belongs elsewhere). I’m trying to capture the idea of an abstract mathematical field and element in the field (which are tightly coupled), and then create the easiest concrete example of this. Any help would be greatly appreciated. See code and compiler exceptions in the thread.
🧵 1
Here is the code:
Copy code
package org.example

abstract class AbstractField<F: AbstractField<F, E>, E: AbstractField<F, E>.AbstractFieldElement<E>> {
    abstract val zero: E

    abstract inner class AbstractFieldElement<E: AbstractFieldElement<E>> {
        abstract operator fun plus(other: E): E
    }
}

class Fp(val prime: Int): AbstractField<Fp, Fp.EFp>() {
    override val zero = EFp(0)

    inner class EFp(val value: Int): AbstractFieldElement<EFp>() {
        override operator fun plus(other: EFp): EFp =
            EFp((value + other.value) % prime)

        override fun toString(): String =
            "$value (mod $prime)"
    }
}

fun main() {
    val field = Fp(13)
    val elem1 = field.EFp(8)
    val elem2 = field.EFp(10)
    println("$elem1 + $elem2 = ${elem1 + elem2}")
}
And here are the compiler errors where I try to run it through the compiler. Seems there is some issue with generating intermediate representation.
Copy code
org.jetbrains.kotlin.backend.common.BackendException: Backend Internal error: Exception during IR lowering
File being compiled: /Users/vorpal/dev/kotlin/field_bug/src/main/kotlin/Main.kt
The root cause java.lang.RuntimeException was thrown at: org.jetbrains.kotlin.backend.jvm.codegen.FunctionCodegen.generate(FunctionCodegen.kt:51)
	at org.jetbrains.kotlin.backend.common.CodegenUtil.reportBackendException(CodegenUtil.kt:253)
	at org.jetbrains.kotlin.backend.common.CodegenUtil.reportBackendException$default(CodegenUtil.kt:237)
	at org.jetbrains.kotlin.backend.common.phaser.PerformByIrFilePhase.invokeSequential(performByIrFile.kt:65)
	at org.jetbrains.kotlin.backend.common.phaser.PerformByIrFilePhase.invoke(performByIrFile.kt:52)
	at org.jetbrains.kotlin.backend.common.phaser.PerformByIrFilePhase.invoke(performByIrFile.kt:38)
	at org.jetbrains.kotlin.backend.common.phaser.NamedCompilerPhase.phaseBody(CompilerPhase.kt:147)
	at org.jetbrains.kotlin.backend.common.phaser.AbstractNamedCompilerPhase.invoke(CompilerPhase.kt:94)
	at org.jetbrains.kotlin.backend.common.phaser.CompositePhase.invoke(PhaseBuilders.kt:29)
	at org.jetbrains.kotlin.backend.common.phaser.CompositePhase.invoke(PhaseBuilders.kt:16)
	at org.jetbrains.kotlin.backend.common.phaser.NamedCompilerPhase.phaseBody(CompilerPhase.kt:147)
	at org.jetbrains.kotlin.backend.common.phaser.AbstractNamedCompilerPhase.invoke(CompilerPhase.kt:94)
	at org.jetbrains.kotlin.backend.common.phaser.CompilerPhaseKt.invokeToplevel(CompilerPhase.kt:43)
	at org.jetbrains.kotlin.backend.jvm.JvmIrCodegenFactory.invokeCodegen(JvmIrCodegenFactory.kt:361)
	at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.runCodegen(KotlinToJVMBytecodeCompiler.kt:347)
	at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.compileModules$cli(KotlinToJVMBytecodeCompiler.kt:122)
	at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.compileModules$cli$default(KotlinToJVMBytecodeCompiler.kt:43)
	at org.jetbrains.kotlin.cli.jvm.K2JVMCompiler.doExecute(K2JVMCompiler.kt:165)
	at org.jetbrains.kotlin.cli.jvm.K2JVMCompiler.doExecute(K2JVMCompiler.kt:50)
	at org.jetbrains.kotlin.cli.common.CLICompiler.execImpl(CLICompiler.kt:104)
	at org.jetbrains.kotlin.cli.common.CLICompiler.execImpl(CLICompiler.kt:48)
	at org.jetbrains.kotlin.cli.common.CLITool.exec(CLITool.kt:101)
	at org.jetbrains.kotlin.incremental.IncrementalJvmCompilerRunner.runCompiler(IncrementalJvmCompilerRunner.kt:463)
	at org.jetbrains.kotlin.incremental.IncrementalJvmCompilerRunner.runCompiler(IncrementalJvmCompilerRunner.kt:62)
	at org.jetbrains.kotlin.incremental.IncrementalCompilerRunner.doCompile(IncrementalCompilerRunner.kt:477)
	at org.jetbrains.kotlin.incremental.IncrementalCompilerRunner.compileImpl(IncrementalCompilerRunner.kt:400)
	at org.jetbrains.kotlin.incremental.IncrementalCompilerRunner.compileNonIncrementally(IncrementalCompilerRunner.kt:281)
	at org.jetbrains.kotlin.incremental.IncrementalCompilerRunner.compile(IncrementalCompilerRunner.kt:125)
	at org.jetbrains.kotlin.daemon.CompileServiceImplBase.execIncrementalCompiler(CompileServiceImpl.kt:657)
	at org.jetbrains.kotlin.daemon.CompileServiceImplBase.access$execIncrementalCompiler(CompileServiceImpl.kt:105)
	at org.jetbrains.kotlin.daemon.CompileServiceImpl.compile(CompileServiceImpl.kt:1624)
	at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
	at java.base/java.lang.reflect.Method.invoke(Method.java:578)
	at java.rmi/sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:360)
	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(AccessController.java:714)
	at java.rmi/sun.rmi.transport.Transport.serviceCall(Transport.java:196)
	at java.rmi/sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:598)
	at java.rmi/sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:844)
	at java.rmi/sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run$0(TCPTransport.java:721)
	at java.base/java.security.AccessController.doPrivileged(AccessController.java:400)
	at java.rmi/sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:720)
	at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1144)
	at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:642)
	at java.base/java.lang.Thread.run(Thread.java:1623)
Caused by: java.lang.RuntimeException: Exception while generating code for:
FUN BRIDGE name:getZero visibility:public modality:OPEN <> ($this:org.example.Fp) returnType:org.example.AbstractField.AbstractFieldElement<*>
  $this: VALUE_PARAMETER INSTANCE_RECEIVER name:<this> type:org.example.Fp
  EXPRESSION_BODY
    TYPE_OP type=org.example.AbstractField.AbstractFieldElement<*, *, *> origin=IMPLICIT_CAST typeOperand=org.example.AbstractField.AbstractFieldElement<*, *, *>
      RETURN type=kotlin.Nothing from='public open fun getZero (): org.example.AbstractField.AbstractFieldElement<*> declared in org.example.Fp'
        CALL 'public open fun <get-zero> (): org.example.Fp.EFp declared in org.example.Fp' type=org.example.Fp.EFp origin=BRIDGE_DELEGATION
          $this: GET_VAR '<this>: org.example.Fp declared in org.example.Fp.getZero' type=org.example.Fp origin=null

	at org.jetbrains.kotlin.backend.jvm.codegen.FunctionCodegen.generate(FunctionCodegen.kt:51)
	at org.jetbrains.kotlin.backend.jvm.codegen.FunctionCodegen.generate$default(FunctionCodegen.kt:43)
	at org.jetbrains.kotlin.backend.jvm.codegen.ClassCodegen.generateMethodNode(ClassCodegen.kt:390)
	at org.jetbrains.kotlin.backend.jvm.codegen.ClassCodegen.generateMethod(ClassCodegen.kt:407)
	at org.jetbrains.kotlin.backend.jvm.codegen.ClassCodegen.generate(ClassCodegen.kt:169)
	at org.jetbrains.kotlin.backend.jvm.FileCodegen.lower(JvmPhases.kt:41)
	at org.jetbrains.kotlin.backend.common.phaser.FileLoweringPhaseAdapter.invoke(PhaseBuilders.kt:120)
	at org.jetbrains.kotlin.backend.common.phaser.FileLoweringPhaseAdapter.invoke(PhaseBuilders.kt:116)
	at org.jetbrains.kotlin.backend.common.phaser.NamedCompilerPhase.phaseBody(CompilerPhase.kt:147)
	at org.jetbrains.kotlin.backend.common.phaser.AbstractNamedCompilerPhase.invoke(CompilerPhase.kt:94)
	at org.jetbrains.kotlin.backend.common.phaser.PerformByIrFilePhase.invokeSequential(performByIrFile.kt:62)
	... 42 more
Caused by: java.lang.AssertionError: -2 trailing arguments were found in this type: org.example.AbstractField.AbstractFieldElement<*>
	at org.jetbrains.kotlin.backend.jvm.mapping.IrTypeMappingKt.buildPossiblyInnerType(IrTypeMapping.kt:71)
	at org.jetbrains.kotlin.backend.jvm.mapping.IrTypeMappingKt.buildPossiblyInnerType(IrTypeMapping.kt:81)
	at org.jetbrains.kotlin.backend.jvm.mapping.IrTypeMappingKt.buildPossiblyInnerType(IrTypeMapping.kt:64)
	at org.jetbrains.kotlin.backend.jvm.mapping.IrTypeMapper.writeGenericType(IrTypeMapper.kt:148)
	at org.jetbrains.kotlin.backend.jvm.mapping.IrTypeMapper.writeGenericType(IrTypeMapper.kt:41)
	at org.jetbrains.kotlin.types.AbstractTypeMapper.mapClassType(AbstractTypeMapper.kt:192)
	at org.jetbrains.kotlin.types.AbstractTypeMapper.mapType(AbstractTypeMapper.kt:89)
	at org.jetbrains.kotlin.types.AbstractTypeMapper.mapType(AbstractTypeMapper.kt:53)
	at org.jetbrains.kotlin.backend.jvm.mapping.IrTypeMapper.mapType(IrTypeMapper.kt:134)
	at org.jetbrains.kotlin.backend.jvm.codegen.ClassCodegen$typeMapper$1.mapType(ClassCodegen.kt:101)
	at org.jetbrains.kotlin.backend.jvm.mapping.MethodSignatureMapper.mapReturnType(MethodSignatureMapper.kt:189)
	at org.jetbrains.kotlin.backend.jvm.mapping.MethodSignatureMapper.mapReturnType(MethodSignatureMapper.kt:169)
	at org.jetbrains.kotlin.backend.jvm.mapping.MethodSignatureMapper.mapSignature(MethodSignatureMapper.kt:277)
	at org.jetbrains.kotlin.backend.jvm.mapping.MethodSignatureMapper.mapSignature$default(MethodSignatureMapper.kt:227)
	at org.jetbrains.kotlin.backend.jvm.mapping.MethodSignatureMapper.mapSignatureWithGeneric(MethodSignatureMapper.kt:225)
	at org.jetbrains.kotlin.backend.jvm.codegen.FunctionCodegen.doGenerate(FunctionCodegen.kt:55)
	at org.jetbrains.kotlin.backend.jvm.codegen.FunctionCodegen.generate(FunctionCodegen.kt:47)
	... 52 more
k
This seems to happen only with 2.0.0.beta3, which is a Beta version. The released versions (such as 1.9.22) are ok with it. Maybe create a bug report for 2.0.
s
@Klitos Kyriacou I’m not clear on what you mean. I am trying to compile this using 1.9.22 and having the problem: I’ve never played with the beta versions. Were you able to get it to compile with 1.9.22? I’m wondering if I’m doing something wrong if that’s the case. For now, I’ve had to do some complex workarounds that leave my code pretty unsatisfactory and result in other problems (the compiler not being able to resolve operator overloads correctly). Any help if you got this to compile would be so incredibly appreciated!
k
I compiled it in the online compiler on play.kotlinlang.org