Skip to content

Commit

Permalink
Compiler plugin: fix IrLinkageError with K/Js on Kotlin 1.9
Browse files Browse the repository at this point in the history
Fixes #3556 – IrLinkageError if objects or abstract subclasses of
AbstractProjectConfig exist in K/Js sources

This commit makes the Kotest compiler plugin skip non-instantiable
classes and objects, which derive from AbstractProjectConfig.

Note: To use objects deriving from AbstractProjectConfig on K/Js, they
must be referenced at least once in code that executes, otherwise they
will be discarded (with the IDE warning "object ... is never used)".
  • Loading branch information
OliverO2 committed Jun 7, 2023
1 parent 9d31ad7 commit 0a41c8a
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ abstract class Transformer(

override fun visitClassNew(declaration: IrClass): IrStatement {
super.visitClassNew(declaration)
if (declaration.isProjectConfig()) configs.add(declaration)
if (declaration.isInstantiableProjectConfig()) configs.add(declaration)
return declaration
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.kotest.framework.multiplatform.embeddablecompiler

import org.jetbrains.kotlin.descriptors.Modality
import org.jetbrains.kotlin.descriptors.isClass
import org.jetbrains.kotlin.ir.declarations.IrClass
import org.jetbrains.kotlin.ir.declarations.IrFile
import org.jetbrains.kotlin.ir.types.IrType
Expand Down Expand Up @@ -27,9 +29,11 @@ val abstractProjectConfigFqName = FqName("io.kotest.core.config.AbstractProjectC
fun IrFile.specs() = declarations.filterIsInstance<IrClass>().filter { it.isSpecClass() }

/**
* Returns true if this IrClass is a project config
* Returns true if this IrClass is an instantiable project config
*/
fun IrClass.isProjectConfig() = superTypes().any { it.classFqName == abstractProjectConfigFqName }
fun IrClass.isInstantiableProjectConfig() =
kind.isClass && modality != Modality.ABSTRACT && modality != Modality.SEALED &&
superTypes().any { it.classFqName == abstractProjectConfigFqName }

/**
* Recursively returns all supertypes for an [IrClass] to the top of the type tree.
Expand Down

0 comments on commit 0a41c8a

Please sign in to comment.