From 2c1ada131d739b9c3bdd4a9931f77ed04c004ae4 Mon Sep 17 00:00:00 2001 From: Dmitriy Novozhilov Date: Wed, 2 Jun 2021 10:45:24 +0300 Subject: [PATCH] [FIR] Don't generate implicit primary constructor for expect classes --- .../kotlin/fir/lightTree/converter/DeclarationsConverter.kt | 3 +++ .../src/org/jetbrains/kotlin/fir/builder/RawFirBuilder.kt | 5 ++++- .../rawBuilder/declarations/expectActual.lazyBodies.txt | 4 ---- .../testData/rawBuilder/declarations/expectActual.txt | 4 ---- compiler/psi/src/org/jetbrains/kotlin/psi/KtClassOrObject.kt | 4 ++-- 5 files changed, 9 insertions(+), 11 deletions(-) diff --git a/compiler/fir/raw-fir/light-tree2fir/src/org/jetbrains/kotlin/fir/lightTree/converter/DeclarationsConverter.kt b/compiler/fir/raw-fir/light-tree2fir/src/org/jetbrains/kotlin/fir/lightTree/converter/DeclarationsConverter.kt index 955d0a89d35..800c8fb1204 100644 --- a/compiler/fir/raw-fir/light-tree2fir/src/org/jetbrains/kotlin/fir/lightTree/converter/DeclarationsConverter.kt +++ b/compiler/fir/raw-fir/light-tree2fir/src/org/jetbrains/kotlin/fir/lightTree/converter/DeclarationsConverter.kt @@ -51,6 +51,7 @@ import org.jetbrains.kotlin.fir.types.impl.FirTypeArgumentListImpl import org.jetbrains.kotlin.lexer.KtModifierKeywordToken import org.jetbrains.kotlin.lexer.KtTokens.* import org.jetbrains.kotlin.name.* +import org.jetbrains.kotlin.psi.psiUtil.hasExpectModifier import org.jetbrains.kotlin.utils.addToStdlib.runIf class DeclarationsConverter( @@ -767,6 +768,8 @@ class DeclarationsConverter( isEnumEntry: Boolean = false ): PrimaryConstructor? { if (primaryConstructor == null && !classWrapper.isEnumEntry() && classWrapper.hasSecondaryConstructor) return null + val classKind = classWrapper.classBuilder.classKind + if (primaryConstructor == null && (classWrapper.hasExpect() && classKind == ClassKind.ENUM_CLASS)) return null if (classWrapper.isInterface()) return null var modifiers = Modifier() diff --git a/compiler/fir/raw-fir/psi2fir/src/org/jetbrains/kotlin/fir/builder/RawFirBuilder.kt b/compiler/fir/raw-fir/psi2fir/src/org/jetbrains/kotlin/fir/builder/RawFirBuilder.kt index 08da33074f1..64f46eb81e4 100644 --- a/compiler/fir/raw-fir/psi2fir/src/org/jetbrains/kotlin/fir/builder/RawFirBuilder.kt +++ b/compiler/fir/raw-fir/psi2fir/src/org/jetbrains/kotlin/fir/builder/RawFirBuilder.kt @@ -707,7 +707,10 @@ open class RawFirBuilder( // TODO: in case we have no primary constructor, // it may be not possible to determine delegated super type right here delegatedSuperTypeRef = delegatedSuperTypeRef ?: defaultDelegatedSuperTypeRef - if ((this !is KtClass || !this.isInterface()) && this.hasPrimaryConstructor()) { + val shouldNotGenerateImplicitPrimaryConstructor = hasExplicitPrimaryConstructor() || hasSecondaryConstructors() || + (hasExpectModifier() && classKind != ClassKind.ENUM_CLASS) + + if ((this !is KtClass || !this.isInterface()) && (primaryConstructor != null || !shouldNotGenerateImplicitPrimaryConstructor)) { val firPrimaryConstructor = primaryConstructor.toFirConstructor( superTypeCallEntry, delegatedSuperTypeRef, diff --git a/compiler/fir/raw-fir/psi2fir/testData/rawBuilder/declarations/expectActual.lazyBodies.txt b/compiler/fir/raw-fir/psi2fir/testData/rawBuilder/declarations/expectActual.lazyBodies.txt index 3a34e54de60..7648ee7c53a 100644 --- a/compiler/fir/raw-fir/psi2fir/testData/rawBuilder/declarations/expectActual.lazyBodies.txt +++ b/compiler/fir/raw-fir/psi2fir/testData/rawBuilder/declarations/expectActual.lazyBodies.txt @@ -1,9 +1,5 @@ FILE: expectActual.kt public? final? expect class MyClass : R|kotlin/Any| { - public? expect constructor(): R|MyClass| { - super() - } - } public? final? expect fun foo(): String public? final? expect val x: Int diff --git a/compiler/fir/raw-fir/psi2fir/testData/rawBuilder/declarations/expectActual.txt b/compiler/fir/raw-fir/psi2fir/testData/rawBuilder/declarations/expectActual.txt index e9e2201006b..f7edeb3e77c 100644 --- a/compiler/fir/raw-fir/psi2fir/testData/rawBuilder/declarations/expectActual.txt +++ b/compiler/fir/raw-fir/psi2fir/testData/rawBuilder/declarations/expectActual.txt @@ -1,9 +1,5 @@ FILE: expectActual.kt public? final? expect class MyClass : R|kotlin/Any| { - public? expect [ContainingClassKey=MyClass] constructor(): R|MyClass| { - super() - } - } public? final? expect fun foo(): String public? final? expect val x: Int diff --git a/compiler/psi/src/org/jetbrains/kotlin/psi/KtClassOrObject.kt b/compiler/psi/src/org/jetbrains/kotlin/psi/KtClassOrObject.kt index 2e369eb6865..4ab9539925f 100644 --- a/compiler/psi/src/org/jetbrains/kotlin/psi/KtClassOrObject.kt +++ b/compiler/psi/src/org/jetbrains/kotlin/psi/KtClassOrObject.kt @@ -115,7 +115,7 @@ abstract class KtClassOrObject : override fun hasPrimaryConstructor(): Boolean = hasExplicitPrimaryConstructor() || !hasSecondaryConstructors() - private fun hasSecondaryConstructors(): Boolean = !secondaryConstructors.isEmpty() + fun hasSecondaryConstructors(): Boolean = !secondaryConstructors.isEmpty() override fun getSecondaryConstructors(): List = getBody()?.secondaryConstructors.orEmpty() @@ -198,4 +198,4 @@ fun KtClassOrObject.getOrCreateBody(): KtClassBody { } val KtClassOrObject.allConstructors - get() = listOfNotNull(primaryConstructor) + secondaryConstructors \ No newline at end of file + get() = listOfNotNull(primaryConstructor) + secondaryConstructors