[Raw FIR] don't add an implicit Any super reference to kotlin/Any itself

KTIJ-27966
This commit is contained in:
Pavel Kirpichenkov
2023-11-29 20:47:57 +02:00
committed by Space Team
parent d7190af72d
commit c4fe7e4453
16 changed files with 232 additions and 8 deletions
@@ -219,6 +219,18 @@ public class FirOutOfContentRootLazyBodiesCalculatorTestGenerated extends Abstra
runTest("compiler/fir/raw-fir/psi2fir/testData/rawBuilder/declarations/invalidDestructing.kt");
}
@Test
@TestMetadata("kotlinAny.kt")
public void testKotlinAny() throws Exception {
runTest("compiler/fir/raw-fir/psi2fir/testData/rawBuilder/declarations/kotlinAny.kt");
}
@Test
@TestMetadata("kotlinAnyNonTopLevel.kt")
public void testKotlinAnyNonTopLevel() throws Exception {
runTest("compiler/fir/raw-fir/psi2fir/testData/rawBuilder/declarations/kotlinAnyNonTopLevel.kt");
}
@Test
@TestMetadata("localDeclarationsInEnumEntry.kt")
public void testLocalDeclarationsInEnumEntry() throws Exception {
@@ -309,6 +321,12 @@ public class FirOutOfContentRootLazyBodiesCalculatorTestGenerated extends Abstra
runTest("compiler/fir/raw-fir/psi2fir/testData/rawBuilder/declarations/typeParameters.kt");
}
@Test
@TestMetadata("userAny.kt")
public void testUserAny() throws Exception {
runTest("compiler/fir/raw-fir/psi2fir/testData/rawBuilder/declarations/userAny.kt");
}
@Test
@TestMetadata("where.kt")
public void testWhere() throws Exception {
@@ -219,6 +219,18 @@ public class FirSourceLazyBodiesCalculatorTestGenerated extends AbstractFirSourc
runTest("compiler/fir/raw-fir/psi2fir/testData/rawBuilder/declarations/invalidDestructing.kt");
}
@Test
@TestMetadata("kotlinAny.kt")
public void testKotlinAny() throws Exception {
runTest("compiler/fir/raw-fir/psi2fir/testData/rawBuilder/declarations/kotlinAny.kt");
}
@Test
@TestMetadata("kotlinAnyNonTopLevel.kt")
public void testKotlinAnyNonTopLevel() throws Exception {
runTest("compiler/fir/raw-fir/psi2fir/testData/rawBuilder/declarations/kotlinAnyNonTopLevel.kt");
}
@Test
@TestMetadata("localDeclarationsInEnumEntry.kt")
public void testLocalDeclarationsInEnumEntry() throws Exception {
@@ -309,6 +321,12 @@ public class FirSourceLazyBodiesCalculatorTestGenerated extends AbstractFirSourc
runTest("compiler/fir/raw-fir/psi2fir/testData/rawBuilder/declarations/typeParameters.kt");
}
@Test
@TestMetadata("userAny.kt")
public void testUserAny() throws Exception {
runTest("compiler/fir/raw-fir/psi2fir/testData/rawBuilder/declarations/userAny.kt");
}
@Test
@TestMetadata("where.kt")
public void testWhere() throws Exception {
@@ -462,6 +462,9 @@ class LightTreeRawFirDeclarationBuilder(
val className = identifier.nameAsSafeName(if (modifiers.isCompanion()) "Companion" else "")
val isLocalWithinParent = classNode.getParent()?.elementType != CLASS_BODY && isClassLocal(classNode) { getParent() }
val classIsExpect = modifiers.hasExpect() || context.containerIsExpect
val classIsKotlinAny = identifier.nameAsSafeName() == StandardNames.FqNames.any.shortName()
&& classNode.getParent()?.getChildNodeByType(PACKAGE_DIRECTIVE)?.getChildNodeByType(REFERENCE_EXPRESSION)
?.getReferencedNameAsName() == StandardNames.BUILT_INS_PACKAGE_NAME
return withChildClassName(className, isExpect = classIsExpect, isLocalWithinParent) {
val isLocal = context.inLocalContext
@@ -527,7 +530,7 @@ class LightTreeRawFirDeclarationBuilder(
}
}
superTypeRefs.ifEmpty {
if (superTypeRefs.isEmpty() && !classIsKotlinAny) {
superTypeRefs += implicitAnyType
delegatedSuperTypeRef = implicitAnyType
}
@@ -551,7 +554,8 @@ class LightTreeRawFirDeclarationBuilder(
classWrapper,
delegatedConstructorSource,
containingClassIsExpectClass = status.isExpect,
isImplicitlyActual = status.isActual && (status.isInline || classKind == ClassKind.ANNOTATION_CLASS)
isImplicitlyActual = status.isActual && (status.isInline || classKind == ClassKind.ANNOTATION_CLASS),
isKotlinAny = classIsKotlinAny,
)
val firPrimaryConstructor = primaryConstructorWrapper?.firConstructor
firPrimaryConstructor?.let { declarations += it }
@@ -887,6 +891,7 @@ class LightTreeRawFirDeclarationBuilder(
isEnumEntry: Boolean = false,
containingClassIsExpectClass: Boolean,
isImplicitlyActual: Boolean = false,
isKotlinAny: Boolean = false,
): PrimaryConstructor? {
fun ClassKind.isEnumRelated(): Boolean = this == ClassKind.ENUM_CLASS || this == ClassKind.ENUM_ENTRY
val shouldGenerateImplicitConstructor =
@@ -912,7 +917,7 @@ class LightTreeRawFirDeclarationBuilder(
val modifiers = modifiersIfPresent ?: Modifier()
val defaultVisibility = classWrapper.defaultConstructorVisibility()
val firDelegatedCall = runUnless(containingClassIsExpectClass) {
val firDelegatedCall = runUnless(containingClassIsExpectClass || isKotlinAny) {
fun createDelegatedConstructorCall(
delegatedConstructorSource: KtLightSourceElement?,
delegatedSuperTypeRef: FirTypeRef,
@@ -196,6 +196,16 @@ public class LightTree2FirConverterTestCaseGenerated extends AbstractLightTree2F
runTest("compiler/fir/raw-fir/psi2fir/testData/rawBuilder/declarations/invalidDestructing.kt");
}
@TestMetadata("kotlinAny.kt")
public void testKotlinAny() throws Exception {
runTest("compiler/fir/raw-fir/psi2fir/testData/rawBuilder/declarations/kotlinAny.kt");
}
@TestMetadata("kotlinAnyNonTopLevel.kt")
public void testKotlinAnyNonTopLevel() throws Exception {
runTest("compiler/fir/raw-fir/psi2fir/testData/rawBuilder/declarations/kotlinAnyNonTopLevel.kt");
}
@TestMetadata("localDeclarationsInEnumEntry.kt")
public void testLocalDeclarationsInEnumEntry() throws Exception {
runTest("compiler/fir/raw-fir/psi2fir/testData/rawBuilder/declarations/localDeclarationsInEnumEntry.kt");
@@ -271,6 +281,11 @@ public class LightTree2FirConverterTestCaseGenerated extends AbstractLightTree2F
runTest("compiler/fir/raw-fir/psi2fir/testData/rawBuilder/declarations/typeParameters.kt");
}
@TestMetadata("userAny.kt")
public void testUserAny() throws Exception {
runTest("compiler/fir/raw-fir/psi2fir/testData/rawBuilder/declarations/userAny.kt");
}
@TestMetadata("where.kt")
public void testWhere() throws Exception {
runTest("compiler/fir/raw-fir/psi2fir/testData/rawBuilder/declarations/where.kt");
@@ -44,6 +44,7 @@ import org.jetbrains.kotlin.name.NameUtils
import org.jetbrains.kotlin.name.SpecialNames
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.*
import org.jetbrains.kotlin.psi.stubs.KotlinFileStub
import org.jetbrains.kotlin.psi.stubs.elements.KtStubElementTypes
import org.jetbrains.kotlin.types.Variance
import org.jetbrains.kotlin.types.expressions.OperatorConventions
@@ -994,15 +995,15 @@ open class PsiRawFirBuilder(
}
}
val isKotlinAny = this.isKotlinAny()
val defaultDelegatedSuperTypeRef =
when {
classKind == ClassKind.ENUM_ENTRY && this is KtClass -> delegatedEnumSuperTypeRef ?: implicitAnyType
container.superTypeRefs.isEmpty() -> implicitAnyType
container.superTypeRefs.isEmpty() && !isKotlinAny() -> implicitAnyType
else -> FirImplicitTypeRefImplWithoutSource
}
if (container.superTypeRefs.isEmpty()) {
if (container.superTypeRefs.isEmpty() && !isKotlinAny) {
container.superTypeRefs += implicitAnyType
delegatedSuperTypeRef = implicitAnyType
}
@@ -1029,7 +1030,8 @@ open class PsiRawFirBuilder(
containingClassIsExpectClass,
copyConstructedTypeRefWithImplicitSource = true,
isErrorConstructor = !hasPrimaryConstructor,
isImplicitlyActual = container.status.isActual && (container.status.isInline || classKind == ClassKind.ANNOTATION_CLASS)
isImplicitlyActual = container.status.isActual && (container.status.isInline || classKind == ClassKind.ANNOTATION_CLASS),
isKotlinAny = isKotlinAny,
)
container.declarations += firPrimaryConstructor
}
@@ -1038,6 +1040,22 @@ open class PsiRawFirBuilder(
return delegatedSuperTypeRef!! to delegateFieldsMap.takeIf { it.isNotEmpty() }
}
private fun KtClassOrObject.isKotlinAny(): Boolean {
if (nameAsName != StandardNames.FqNames.any.shortName()) return false
val classOrObjectStub = stub
if (classOrObjectStub != null) {
val parentStub = classOrObjectStub.parentStub
if (parentStub !is KotlinFileStub) return false
return parentStub.getPackageFqName().pathSegments().singleOrNull() == StandardNames.BUILT_INS_PACKAGE_NAME
} else {
if (parent !is KtFile) return false
return parent.findDescendantOfType<KtPackageDirective> { packageDirective ->
packageDirective.packageNames.singleOrNull()?.getReferencedNameAsName() == StandardNames.BUILT_INS_PACKAGE_NAME
} != null
}
}
/**
* @param delegatedSuperTypeRef can be null if containingClassIsExpectClass is true
*/
@@ -1052,6 +1070,7 @@ open class PsiRawFirBuilder(
copyConstructedTypeRefWithImplicitSource: Boolean,
isErrorConstructor: Boolean = false,
isImplicitlyActual: Boolean = false,
isKotlinAny: Boolean = false,
): FirConstructor {
val constructorSource = this?.toFirSourceElement()
?: owner.toKtPsiSourceElement(KtFakeSourceElementKind.ImplicitConstructor)
@@ -1079,7 +1098,7 @@ open class PsiRawFirBuilder(
}
}
val firDelegatedCall = runUnless(containingClassIsExpectClass) {
val firDelegatedCall = runUnless(containingClassIsExpectClass || isKotlinAny) {
if (allSuperTypeCallEntries.size <= 1) {
buildDelegatedCall(superTypeCallEntry, delegatedSuperTypeRef!!)
} else {
@@ -0,0 +1,33 @@
package kotlin
/**
* The root of the Kotlin class hierarchy. Every Kotlin class has [Any] as a superclass.
*/
public open class Any {
/**
* Indicates whether some other object is "equal to" this one. Implementations must fulfil the following
* requirements:
*
* * Reflexive: for any non-null value `x`, `x.equals(x)` should return true.
* * Symmetric: for any non-null values `x` and `y`, `x.equals(y)` should return true if and only if `y.equals(x)` returns true.
* * Transitive: for any non-null values `x`, `y`, and `z`, if `x.equals(y)` returns true and `y.equals(z)` returns true, then `x.equals(z)` should return true.
* * Consistent: for any non-null values `x` and `y`, multiple invocations of `x.equals(y)` consistently return true or consistently return false, provided no information used in `equals` comparisons on the objects is modified.
* * Never equal to null: for any non-null value `x`, `x.equals(null)` should return false.
*
* Read more about [equality](https://kotlinlang.org/docs/reference/equality.html) in Kotlin.
*/
public open operator fun equals(other: Any?): Boolean
/**
* Returns a hash code value for the object. The general contract of `hashCode` is:
*
* * Whenever it is invoked on the same object more than once, the `hashCode` method must consistently return the same integer, provided no information used in `equals` comparisons on the object is modified.
* * If two objects are equal according to the `equals()` method, then calling the `hashCode` method on each of the two objects must produce the same integer result.
*/
public open fun hashCode(): Int
/**
* Returns a string representation of the object.
*/
public open fun toString(): String
}
@@ -0,0 +1,11 @@
FILE: kotlinAny.kt
public open class Any {
public? constructor(): R|kotlin/Any|
public open operator fun equals(other: Any?): Boolean
public open fun hashCode(): Int
public open fun toString(): String
}
@@ -0,0 +1,11 @@
FILE: kotlinAny.kt
public open class Any {
public? [ContainingClassKey=Any] constructor(): R|kotlin/Any|
public open operator fun equals(other: Any?): Boolean
public open fun hashCode(): Int
public open fun toString(): String
}
@@ -0,0 +1,9 @@
package kotlin
class Owner {
class Any /* kotlin.Any should be a supertype */
}
fun foo() {
class Any /* kotlin.Any should be a supertype */
}
@@ -0,0 +1,15 @@
FILE: kotlinAnyNonTopLevel.kt
public? final? class Owner : R|kotlin/Any| {
public? constructor(): R|kotlin/Owner| {
LAZY_super<R|kotlin/Any|>
}
public? final? class Any : R|kotlin/Any| {
public? constructor(): R|kotlin/Owner.Any| {
LAZY_super<R|kotlin/Any|>
}
}
}
public? final? fun foo(): R|kotlin/Unit| { LAZY_BLOCK }
@@ -0,0 +1,23 @@
FILE: kotlinAnyNonTopLevel.kt
public? final? class Owner : R|kotlin/Any| {
public? [ContainingClassKey=Owner] constructor(): R|kotlin/Owner| {
super<R|kotlin/Any|>()
}
public? final? class Any : R|kotlin/Any| {
public? [ContainingClassKey=Any] constructor(): R|kotlin/Owner.Any| {
super<R|kotlin/Any|>()
}
}
}
public? final? fun foo(): R|kotlin/Unit| {
local final? class Any : R|kotlin/Any| {
public? [ContainingClassKey=Any] constructor(): R|<local>/Any| {
super<R|kotlin/Any|>()
}
}
}
@@ -0,0 +1,3 @@
package user
public open class Any
@@ -0,0 +1,7 @@
FILE: userAny.kt
public open class Any : R|kotlin/Any| {
public? constructor(): R|user/Any| {
LAZY_super<R|kotlin/Any|>
}
}
@@ -0,0 +1,7 @@
FILE: userAny.kt
public open class Any : R|kotlin/Any| {
public? [ContainingClassKey=Any] constructor(): R|user/Any| {
super<R|kotlin/Any|>()
}
}
@@ -196,6 +196,16 @@ public class RawFirBuilderLazyBodiesTestCaseGenerated extends AbstractRawFirBuil
runTest("compiler/fir/raw-fir/psi2fir/testData/rawBuilder/declarations/invalidDestructing.kt");
}
@TestMetadata("kotlinAny.kt")
public void testKotlinAny() throws Exception {
runTest("compiler/fir/raw-fir/psi2fir/testData/rawBuilder/declarations/kotlinAny.kt");
}
@TestMetadata("kotlinAnyNonTopLevel.kt")
public void testKotlinAnyNonTopLevel() throws Exception {
runTest("compiler/fir/raw-fir/psi2fir/testData/rawBuilder/declarations/kotlinAnyNonTopLevel.kt");
}
@TestMetadata("localDeclarationsInEnumEntry.kt")
public void testLocalDeclarationsInEnumEntry() throws Exception {
runTest("compiler/fir/raw-fir/psi2fir/testData/rawBuilder/declarations/localDeclarationsInEnumEntry.kt");
@@ -276,6 +286,11 @@ public class RawFirBuilderLazyBodiesTestCaseGenerated extends AbstractRawFirBuil
runTest("compiler/fir/raw-fir/psi2fir/testData/rawBuilder/declarations/typeParameters.kt");
}
@TestMetadata("userAny.kt")
public void testUserAny() throws Exception {
runTest("compiler/fir/raw-fir/psi2fir/testData/rawBuilder/declarations/userAny.kt");
}
@TestMetadata("where.kt")
public void testWhere() throws Exception {
runTest("compiler/fir/raw-fir/psi2fir/testData/rawBuilder/declarations/where.kt");
@@ -196,6 +196,16 @@ public class RawFirBuilderTestCaseGenerated extends AbstractRawFirBuilderTestCas
runTest("compiler/fir/raw-fir/psi2fir/testData/rawBuilder/declarations/invalidDestructing.kt");
}
@TestMetadata("kotlinAny.kt")
public void testKotlinAny() throws Exception {
runTest("compiler/fir/raw-fir/psi2fir/testData/rawBuilder/declarations/kotlinAny.kt");
}
@TestMetadata("kotlinAnyNonTopLevel.kt")
public void testKotlinAnyNonTopLevel() throws Exception {
runTest("compiler/fir/raw-fir/psi2fir/testData/rawBuilder/declarations/kotlinAnyNonTopLevel.kt");
}
@TestMetadata("localDeclarationsInEnumEntry.kt")
public void testLocalDeclarationsInEnumEntry() throws Exception {
runTest("compiler/fir/raw-fir/psi2fir/testData/rawBuilder/declarations/localDeclarationsInEnumEntry.kt");
@@ -276,6 +286,11 @@ public class RawFirBuilderTestCaseGenerated extends AbstractRawFirBuilderTestCas
runTest("compiler/fir/raw-fir/psi2fir/testData/rawBuilder/declarations/typeParameters.kt");
}
@TestMetadata("userAny.kt")
public void testUserAny() throws Exception {
runTest("compiler/fir/raw-fir/psi2fir/testData/rawBuilder/declarations/userAny.kt");
}
@TestMetadata("where.kt")
public void testWhere() throws Exception {
runTest("compiler/fir/raw-fir/psi2fir/testData/rawBuilder/declarations/where.kt");