[K2] Avoid type check for Kotlin's property in ConstUtils

When we check Java field for constant initializer, we could
be asked to get and check the type of Kotlin's property that
is used in this Java field. But there is no guarantee that the type
resolve phase was finished and this type is available. So we just
check for `const` modifier and skip type check.

#KT-63752 Fixed
#KT-62558 Obsolete
#KT-61786 Declined
This commit is contained in:
Ivan Kylchik
2023-11-29 18:00:49 +01:00
committed by Space Team
parent 97ba3fe396
commit 79c300209e
21 changed files with 298 additions and 22 deletions
@@ -23705,6 +23705,12 @@ public class DiagnosticCompilerTestFE10TestdataTestGenerated extends AbstractDia
runTest("compiler/testData/diagnostics/tests/modifiers/const/kt57802.kt");
}
@Test
@TestMetadata("noConstKt12248.kt")
public void testNoConstKt12248() throws Exception {
runTest("compiler/testData/diagnostics/tests/modifiers/const/noConstKt12248.kt");
}
@Test
@TestMetadata("stdlibConstFun.kt")
public void testStdlibConstFun() throws Exception {
@@ -23705,6 +23705,12 @@ public class LLFirPreresolvedReversedDiagnosticCompilerFE10TestDataTestGenerated
runTest("compiler/testData/diagnostics/tests/modifiers/const/kt57802.kt");
}
@Test
@TestMetadata("noConstKt12248.kt")
public void testNoConstKt12248() throws Exception {
runTest("compiler/testData/diagnostics/tests/modifiers/const/noConstKt12248.kt");
}
@Test
@TestMetadata("stdlibConstFun.kt")
public void testStdlibConstFun() throws Exception {
@@ -27,6 +27,7 @@ import org.jetbrains.kotlin.light.classes.symbol.classes.SymbolLightClassBase
import org.jetbrains.kotlin.light.classes.symbol.modifierLists.GranularModifiersBox
import org.jetbrains.kotlin.light.classes.symbol.modifierLists.SymbolLightMemberModifierList
import org.jetbrains.kotlin.light.classes.symbol.modifierLists.with
import org.jetbrains.kotlin.load.java.structure.impl.NotEvaluatedConstAware
import org.jetbrains.kotlin.name.JvmStandardClassIds.TRANSIENT_ANNOTATION_CLASS_ID
import org.jetbrains.kotlin.name.JvmStandardClassIds.VOLATILE_ANNOTATION_CLASS_ID
import org.jetbrains.kotlin.psi.KtCallableDeclaration
@@ -41,7 +42,7 @@ internal class SymbolLightFieldForProperty private constructor(
private val isStatic: Boolean,
override val kotlinOrigin: KtCallableDeclaration?,
private val backingFieldSymbolPointer: KtSymbolPointer<KtBackingFieldSymbol>?,
) : SymbolLightField(containingClass, lightMemberOrigin) {
) : SymbolLightField(containingClass, lightMemberOrigin), NotEvaluatedConstAware {
internal constructor(
ktAnalysisSession: KtAnalysisSession,
propertySymbol: KtPropertySymbol,
@@ -231,6 +232,10 @@ internal class SymbolLightFieldForProperty private constructor(
override fun computeConstantValue(): Any? = _constantValue
override fun isNotYetComputed(): Boolean {
return withPropertySymbol { propertySymbol -> (propertySymbol as? KtKotlinPropertySymbol)?.isConst == true }
}
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other !is SymbolLightFieldForProperty || other.ktModule != ktModule || other.fieldName != fieldName) return false
@@ -1594,6 +1594,11 @@ public class FirTypeEnhancementTestGenerated extends AbstractFirTypeEnhancementT
runTest("compiler/testData/loadJava/compiledJava/static/StaticFinal.java");
}
@TestMetadata("StaticFinalConstTypes.java")
public void testStaticFinalConstTypes() throws Exception {
runTest("compiler/testData/loadJava/compiledJava/static/StaticFinalConstTypes.java");
}
@TestMetadata("StaticMembersFromParentClass.java")
public void testStaticMembersFromParentClass() throws Exception {
runTest("compiler/testData/loadJava/compiledJava/static/StaticMembersFromParentClass.java");
@@ -23699,6 +23699,12 @@ public class FirLightTreeOldFrontendDiagnosticsTestGenerated extends AbstractFir
runTest("compiler/testData/diagnostics/tests/modifiers/const/kt57802.kt");
}
@Test
@TestMetadata("noConstKt12248.kt")
public void testNoConstKt12248() throws Exception {
runTest("compiler/testData/diagnostics/tests/modifiers/const/noConstKt12248.kt");
}
@Test
@TestMetadata("stdlibConstFun.kt")
public void testStdlibConstFun() throws Exception {
@@ -23705,6 +23705,12 @@ public class FirPsiOldFrontendDiagnosticsTestGenerated extends AbstractFirPsiDia
runTest("compiler/testData/diagnostics/tests/modifiers/const/kt57802.kt");
}
@Test
@TestMetadata("noConstKt12248.kt")
public void testNoConstKt12248() throws Exception {
runTest("compiler/testData/diagnostics/tests/modifiers/const/noConstKt12248.kt");
}
@Test
@TestMetadata("stdlibConstFun.kt")
public void testStdlibConstFun() throws Exception {
@@ -125,7 +125,7 @@ class FirSignatureEnhancement(
predefinedEnhancementInfo = null
).let {
val lowerBound = it.type.lowerBoundIfFlexible()
if ((lowerBound.isString || lowerBound.isInt) && firElement.isStatic && firElement.hasConstantInitializer) {
if (lowerBound.isString && firElement.isStatic && firElement.hasConstantInitializer) {
it.withReplacedConeType(it.type.withNullability(ConeNullability.NOT_NULL, session.typeContext))
} else {
it
@@ -39,7 +39,10 @@ public class ConstUtils {
// Copy of `com.intellij.psi.util.IsConstantExpressionVisitor`.
// This copy is required to be able to handle K2 constants without triggering constant evaluation.
// The only change is done in `visitReferenceExpression` where we check for constant expression without triggering evaluation.
// There are two major changes:
// 1. In `visitReferenceExpression` where we check for constant expression without triggering evaluation.
// 2. In `visitPolyadicExpression` before we check for the type.
// At the moment when we check for constant, not all types could be resolved, and if we return a wrong type, it will be cached.
final class IsConstantExpressionVisitor extends JavaElementVisitor {
private boolean myIsConstant;
private final Map<PsiVariable, Boolean> varIsConst = new HashMap<>();
@@ -85,6 +88,7 @@ final class IsConstantExpressionVisitor extends JavaElementVisitor {
myIsConstant = false;
return;
}
PsiType type = element.getType();
if (type instanceof PsiPrimitiveType) return;
if (type.equalsToText(CommonClassNames.JAVA_LANG_STRING)) return;
@@ -112,6 +116,11 @@ final class IsConstantExpressionVisitor extends JavaElementVisitor {
for (PsiExpression operand : expression.getOperands()) {
operand.accept(this);
if (!myIsConstant) return;
// CHANGE #1
if (checkForNotYetEvaluatedConstant(operand)) return;
// END
final PsiType type = operand.getType();
if (type != null && !(type instanceof PsiPrimitiveType) && !type.equalsToText(CommonClassNames.JAVA_LANG_STRING)) {
myIsConstant = false;
@@ -165,15 +174,8 @@ final class IsConstantExpressionVisitor extends JavaElementVisitor {
return;
}
// This block is the only difference with the original `IsConstantExpressionVisitor`
if (variable instanceof ClsFieldImpl) {
PsiFieldStub stub = ((ClsFieldImpl) variable).getStub();
if (stub instanceof NotEvaluatedConstAware && ((NotEvaluatedConstAware) stub).isNotYetComputed()) {
myIsConstant = true;
varIsConst.put(variable, Boolean.TRUE);
return;
}
}
// CHANGE #2
if (checkForNotYetEvaluatedConstant(expression)) return;
// END
variable.hasInitializer();
@@ -185,4 +187,32 @@ final class IsConstantExpressionVisitor extends JavaElementVisitor {
initializer.accept(this);
varIsConst.put(variable, myIsConstant);
}
private boolean checkForNotYetEvaluatedConstant(PsiExpression operand) {
if (operand instanceof PsiReferenceExpression) {
PsiElement refElement = ((PsiReferenceExpression) operand).resolve();
NotEvaluatedConstAware notEvaluatedConstAware = getNotEvaluatedConstAware(refElement);
if (notEvaluatedConstAware != null) {
if (notEvaluatedConstAware.isNotYetComputed()) {
myIsConstant = true;
varIsConst.put((PsiVariable) refElement, Boolean.TRUE);
}
return true;
}
}
return false;
}
@Nullable
private static NotEvaluatedConstAware getNotEvaluatedConstAware(PsiElement refElement) {
if (refElement instanceof ClsFieldImpl) {
PsiFieldStub stub = ((ClsFieldImpl) refElement).getStub();
if (stub instanceof NotEvaluatedConstAware) {
return (NotEvaluatedConstAware) stub;
}
} else if (refElement instanceof NotEvaluatedConstAware) {
return (NotEvaluatedConstAware) refElement;
}
return null;
}
}
@@ -1,6 +1,3 @@
// IGNORE_REVERSED_RESOLVE
// IGNORE_CONTRACT_VIOLATIONS
// See KT-63752 about ignores
// FIR_IDENTICAL
// FILE: Bar.java
@@ -1,5 +1,3 @@
// IGNORE_REVERSED_RESOLVE
// IGNORE_CONTRACT_VIOLATIONS
// FIR_IDENTICAL
// FILE: Bar.java
@@ -1,5 +1,4 @@
// IGNORE_REVERSED_RESOLVE
// IGNORE_CONTRACT_VIOLATIONS
// FIR_IDENTICAL
// FILE: Bar.java
public class Bar {
@@ -10,12 +9,12 @@ public class Bar {
class Foo {
companion object {
const val FOO = Baz.BAZ + 1
val FOO = Baz.BAZ + 1
}
}
class Baz {
companion object {
const val BAZ = <!CONST_VAL_WITH_NON_CONST_INITIALIZER!>Bar.BAR + 1<!>
val BAZ = Bar.BAR + 1
}
}
}
@@ -0,0 +1,41 @@
package
public open class Bar {
public constructor Bar()
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
// Static members
public const final val BAR: kotlin.Int
}
public final class Baz {
public constructor Baz()
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
public companion object Companion {
private constructor Companion()
public final val BAZ: kotlin.Int
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
}
public final class Foo {
public constructor Foo()
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
public companion object Companion {
private constructor Companion()
public final val FOO: kotlin.Int
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
}
@@ -0,0 +1,64 @@
public open class StaticFinalConstTypes : R|kotlin/Any| {
public final static field stringField: R|kotlin/String| = String(0)
public final static field stringFieldNull: R|kotlin/String!|
public final static field booleanField: R|kotlin/Boolean!|
public final static field booleanFieldNull: R|kotlin/Boolean!|
public final static field booleanPrimitiveField: R|kotlin/Boolean| = Boolean(false)
public final static field characterField: R|kotlin/Char!|
public final static field characterFieldNull: R|kotlin/Char!|
public final static field characterPrimitiveField: R|kotlin/Char| = Char(0)
public final static field byteField: R|kotlin/Byte!|
public final static field byteFieldNull: R|kotlin/Byte!|
public final static field bytePrimitiveField: R|kotlin/Byte| = Byte(0)
public final static field shortField: R|kotlin/Short!|
public final static field shortFieldNull: R|kotlin/Short!|
public final static field shortPrimitiveField: R|kotlin/Short| = Short(0)
public final static field integerField: R|kotlin/Int!|
public final static field integerFieldNull: R|kotlin/Int!|
public final static field integerPrimitiveField: R|kotlin/Int| = Int(0)
public final static field longField: R|kotlin/Long!|
public final static field longFieldNull: R|kotlin/Long!|
public final static field longPrimitiveField: R|kotlin/Long| = Long(0)
public final static field floatField: R|kotlin/Float!|
public final static field floatFieldNull: R|kotlin/Float!|
public final static field floatPrimitiveField: R|kotlin/Float| = Float(0.0)
public final static field doubleField: R|kotlin/Double!|
public final static field doubleFieldNull: R|kotlin/Double!|
public final static field doublePrimitiveField: R|kotlin/Double| = Double(0.0)
public final static field uByteField: R|kotlin/UByte!|
public final static field uShortField: R|kotlin/UShort!|
public final static field uIntField: R|kotlin/UInt!|
public final static field uLongField: R|kotlin/ULong!|
public constructor(): R|test/StaticFinalConstTypes|
}
@@ -0,0 +1,45 @@
// SKIP_IN_RUNTIME_TEST because there's no stable way to determine if a field is initialized with a non-null value in runtime
package test;
public class StaticFinalConstTypes {
public static final String stringField = "0";
public static final String stringFieldNull = null;
public static final Boolean booleanField = false;
public static final Boolean booleanFieldNull = null;
public static final boolean booleanPrimitiveField = false;
public static final Character characterField = '0';
public static final Character characterFieldNull = null;
public static final char characterPrimitiveField = '0';
public static final Byte byteField = (byte) 0;
public static final Byte byteFieldNull = null;
public static final byte bytePrimitiveField = (byte) 0;
public static final Short shortField = (short) 0;
public static final Short shortFieldNull = null;
public static final short shortPrimitiveField = (short) 0;
public static final Integer integerField = 0;
public static final Integer integerFieldNull = null;
public static final int integerPrimitiveField = 0;
public static final Long longField = 0L;
public static final Long longFieldNull = null;
public static final long longPrimitiveField = 0L;
public static final Float floatField = 0.0f;
public static final Float floatFieldNull = null;
public static final float floatPrimitiveField = 0.0f;
public static final Double doubleField = 0.0;
public static final Double doubleFieldNull = null;
public static final double doublePrimitiveField = 0.0;
public static final kotlin.UByte uByteField = null;
public static final kotlin.UShort uShortField = null;
public static final kotlin.UInt uIntField = null;
public static final kotlin.ULong uLongField = null;
}
@@ -0,0 +1,37 @@
package test
public open class StaticFinalConstTypes {
public constructor StaticFinalConstTypes()
// Static members
public final val booleanField: kotlin.Boolean!
public final val booleanFieldNull: kotlin.Boolean!
public const final val booleanPrimitiveField: kotlin.Boolean = false
public final val byteField: kotlin.Byte!
public final val byteFieldNull: kotlin.Byte!
public const final val bytePrimitiveField: kotlin.Byte = 0.toByte()
public final val characterField: kotlin.Char!
public final val characterFieldNull: kotlin.Char!
public const final val characterPrimitiveField: kotlin.Char = \u0030 ('0')
public final val doubleField: kotlin.Double!
public final val doubleFieldNull: kotlin.Double!
public const final val doublePrimitiveField: kotlin.Double = 0.0.toDouble()
public final val floatField: kotlin.Float!
public final val floatFieldNull: kotlin.Float!
public const final val floatPrimitiveField: kotlin.Float = 0.0.toFloat()
public final val integerField: kotlin.Int!
public final val integerFieldNull: kotlin.Int!
public const final val integerPrimitiveField: kotlin.Int = 0
public final val longField: kotlin.Long!
public final val longFieldNull: kotlin.Long!
public const final val longPrimitiveField: kotlin.Long = 0.toLong()
public final val shortField: kotlin.Short!
public final val shortFieldNull: kotlin.Short!
public const final val shortPrimitiveField: kotlin.Short = 0.toShort()
public const final val stringField: kotlin.String = "0"
public const final val stringFieldNull: kotlin.String!
public final val uByteField: kotlin.UByte!
public final val uIntField: kotlin.UInt!
public final val uLongField: kotlin.ULong!
public final val uShortField: kotlin.UShort!
}
@@ -23705,6 +23705,12 @@ public class DiagnosticTestGenerated extends AbstractDiagnosticTest {
runTest("compiler/testData/diagnostics/tests/modifiers/const/kt57802.kt");
}
@Test
@TestMetadata("noConstKt12248.kt")
public void testNoConstKt12248() throws Exception {
runTest("compiler/testData/diagnostics/tests/modifiers/const/noConstKt12248.kt");
}
@Test
@TestMetadata("stdlibConstFun.kt")
public void testStdlibConstFun() throws Exception {
@@ -1596,6 +1596,11 @@ public class LoadJavaTestGenerated extends AbstractLoadJavaTest {
runTest("compiler/testData/loadJava/compiledJava/static/StaticFinal.java");
}
@TestMetadata("StaticFinalConstTypes.java")
public void testStaticFinalConstTypes() throws Exception {
runTest("compiler/testData/loadJava/compiledJava/static/StaticFinalConstTypes.java");
}
@TestMetadata("StaticMembersFromParentClass.java")
public void testStaticMembersFromParentClass() throws Exception {
runTest("compiler/testData/loadJava/compiledJava/static/StaticMembersFromParentClass.java");
@@ -1594,6 +1594,11 @@ public class LoadJavaWithPsiClassReadingTestGenerated extends AbstractLoadJavaWi
runTest("compiler/testData/loadJava/compiledJava/static/StaticFinal.java");
}
@TestMetadata("StaticFinalConstTypes.java")
public void testStaticFinalConstTypes() throws Exception {
runTest("compiler/testData/loadJava/compiledJava/static/StaticFinalConstTypes.java");
}
@TestMetadata("StaticMembersFromParentClass.java")
public void testStaticMembersFromParentClass() throws Exception {
runTest("compiler/testData/loadJava/compiledJava/static/StaticMembersFromParentClass.java");
@@ -1597,6 +1597,11 @@ public class IrLoadJavaTestGenerated extends AbstractIrLoadJavaTest {
runTest("compiler/testData/loadJava/compiledJava/static/StaticFinal.java");
}
@TestMetadata("StaticFinalConstTypes.java")
public void testStaticFinalConstTypes() throws Exception {
runTest("compiler/testData/loadJava/compiledJava/static/StaticFinalConstTypes.java");
}
@TestMetadata("StaticMembersFromParentClass.java")
public void testStaticMembersFromParentClass() throws Exception {
runTest("compiler/testData/loadJava/compiledJava/static/StaticMembersFromParentClass.java");
@@ -1596,6 +1596,11 @@ public class LoadJavaUsingJavacTestGenerated extends AbstractLoadJavaUsingJavacT
runTest("compiler/testData/loadJava/compiledJava/static/StaticFinal.java");
}
@TestMetadata("StaticFinalConstTypes.java")
public void testStaticFinalConstTypes() throws Exception {
runTest("compiler/testData/loadJava/compiledJava/static/StaticFinalConstTypes.java");
}
@TestMetadata("StaticMembersFromParentClass.java")
public void testStaticMembersFromParentClass() throws Exception {
runTest("compiler/testData/loadJava/compiledJava/static/StaticMembersFromParentClass.java");
@@ -3846,6 +3846,11 @@ public class JvmRuntimeDescriptorLoaderTestGenerated extends AbstractJvmRuntimeD
runTest("compiler/testData/loadJava/compiledJava/static/StaticFinal.java");
}
@TestMetadata("StaticFinalConstTypes.java")
public void testStaticFinalConstTypes() throws Exception {
runTest("compiler/testData/loadJava/compiledJava/static/StaticFinalConstTypes.java");
}
@TestMetadata("StaticMembersFromParentClass.java")
public void testStaticMembersFromParentClass() throws Exception {
runTest("compiler/testData/loadJava/compiledJava/static/StaticMembersFromParentClass.java");