[Analysis API FIR] support generated temp properties
KTIJ-23492 (array index expression), KTIJ-23138 (inc/dec desugaring)
This commit is contained in:
+12
@@ -46,6 +46,12 @@ public class Fe10IdeNormalAnalysisSourceModuleCompileTimeConstantEvaluatorTestGe
|
||||
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("analysis/analysis-api/testData/components/compileTimeConstantProvider/evaluate"), Pattern.compile("^(.+)\\.kt$"), null, true);
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("arrayWithInc.kt")
|
||||
public void testArrayWithInc() throws Exception {
|
||||
runTest("analysis/analysis-api/testData/components/compileTimeConstantProvider/evaluate/arrayWithInc.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("binaryExpressionWithString.kt")
|
||||
public void testBinaryExpressionWithString() throws Exception {
|
||||
@@ -76,6 +82,12 @@ public class Fe10IdeNormalAnalysisSourceModuleCompileTimeConstantEvaluatorTestGe
|
||||
runTest("analysis/analysis-api/testData/components/compileTimeConstantProvider/evaluate/integerLiteral_plusOne_justOne.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("interpolationWithInc.kt")
|
||||
public void testInterpolationWithInc() throws Exception {
|
||||
runTest("analysis/analysis-api/testData/components/compileTimeConstantProvider/evaluate/interpolationWithInc.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("javaFinalField.kt")
|
||||
public void testJavaFinalField() throws Exception {
|
||||
|
||||
+8
-5
@@ -5,6 +5,7 @@
|
||||
|
||||
package org.jetbrains.kotlin.analysis.api.fir.components
|
||||
|
||||
import org.jetbrains.kotlin.KtFakeSourceElementKind
|
||||
import org.jetbrains.kotlin.analysis.api.base.KtConstantValue
|
||||
import org.jetbrains.kotlin.analysis.api.components.KtCompileTimeConstantProvider
|
||||
import org.jetbrains.kotlin.analysis.api.components.KtConstantEvaluationMode
|
||||
@@ -14,10 +15,13 @@ import org.jetbrains.kotlin.analysis.api.lifetime.KtLifetimeToken
|
||||
import org.jetbrains.kotlin.analysis.low.level.api.fir.api.getOrBuildFir
|
||||
import org.jetbrains.kotlin.analysis.low.level.api.fir.api.throwUnexpectedFirElementError
|
||||
import org.jetbrains.kotlin.fir.FirElement
|
||||
import org.jetbrains.kotlin.fir.declarations.FirProperty
|
||||
import org.jetbrains.kotlin.fir.expressions.FirExpression
|
||||
import org.jetbrains.kotlin.fir.expressions.FirPropertyAccessExpression
|
||||
import org.jetbrains.kotlin.fir.expressions.FirVariableAssignment
|
||||
import org.jetbrains.kotlin.fir.expressions.FirWhenBranch
|
||||
import org.jetbrains.kotlin.fir.references.FirNamedReference
|
||||
import org.jetbrains.kotlin.psi
|
||||
import org.jetbrains.kotlin.psi.KtExpression
|
||||
|
||||
internal class KtFirCompileTimeConstantProvider(
|
||||
@@ -37,10 +41,8 @@ internal class KtFirCompileTimeConstantProvider(
|
||||
sourcePsi: KtExpression,
|
||||
mode: KtConstantEvaluationMode,
|
||||
): KtConstantValue? {
|
||||
return when (fir) {
|
||||
is FirPropertyAccessExpression,
|
||||
is FirExpression,
|
||||
is FirNamedReference -> {
|
||||
return when {
|
||||
fir is FirPropertyAccessExpression || fir is FirExpression || fir is FirNamedReference -> {
|
||||
try {
|
||||
FirCompileTimeConstantEvaluator.evaluateAsKtConstantValue(fir, mode)
|
||||
} catch (e: ArithmeticException) {
|
||||
@@ -55,7 +57,8 @@ internal class KtFirCompileTimeConstantProvider(
|
||||
// ```
|
||||
// `false` does not have a corresponding elements on the FIR side and hence the containing `FirWhenBranch` is returned. In this
|
||||
// case, we simply report null since FIR does not know about it.
|
||||
is FirWhenBranch -> null
|
||||
fir is FirWhenBranch -> null
|
||||
fir is FirVariableAssignment && fir.source?.kind == KtFakeSourceElementKind.DesugaredIncrementOrDecrement -> null
|
||||
else -> throwUnexpectedFirElementError(fir, sourcePsi)
|
||||
}
|
||||
}
|
||||
|
||||
+12
@@ -46,6 +46,12 @@ public class FirIdeDependentAnalysisSourceModuleCompileTimeConstantEvaluatorTest
|
||||
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("analysis/analysis-api/testData/components/compileTimeConstantProvider/evaluate"), Pattern.compile("^(.+)\\.kt$"), null, true);
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("arrayWithInc.kt")
|
||||
public void testArrayWithInc() throws Exception {
|
||||
runTest("analysis/analysis-api/testData/components/compileTimeConstantProvider/evaluate/arrayWithInc.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("binaryExpressionWithString.kt")
|
||||
public void testBinaryExpressionWithString() throws Exception {
|
||||
@@ -76,6 +82,12 @@ public class FirIdeDependentAnalysisSourceModuleCompileTimeConstantEvaluatorTest
|
||||
runTest("analysis/analysis-api/testData/components/compileTimeConstantProvider/evaluate/integerLiteral_plusOne_justOne.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("interpolationWithInc.kt")
|
||||
public void testInterpolationWithInc() throws Exception {
|
||||
runTest("analysis/analysis-api/testData/components/compileTimeConstantProvider/evaluate/interpolationWithInc.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("javaFinalField.kt")
|
||||
public void testJavaFinalField() throws Exception {
|
||||
|
||||
+12
@@ -46,6 +46,12 @@ public class FirIdeNormalAnalysisSourceModuleCompileTimeConstantEvaluatorTestGen
|
||||
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("analysis/analysis-api/testData/components/compileTimeConstantProvider/evaluate"), Pattern.compile("^(.+)\\.kt$"), null, true);
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("arrayWithInc.kt")
|
||||
public void testArrayWithInc() throws Exception {
|
||||
runTest("analysis/analysis-api/testData/components/compileTimeConstantProvider/evaluate/arrayWithInc.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("binaryExpressionWithString.kt")
|
||||
public void testBinaryExpressionWithString() throws Exception {
|
||||
@@ -76,6 +82,12 @@ public class FirIdeNormalAnalysisSourceModuleCompileTimeConstantEvaluatorTestGen
|
||||
runTest("analysis/analysis-api/testData/components/compileTimeConstantProvider/evaluate/integerLiteral_plusOne_justOne.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("interpolationWithInc.kt")
|
||||
public void testInterpolationWithInc() throws Exception {
|
||||
runTest("analysis/analysis-api/testData/components/compileTimeConstantProvider/evaluate/interpolationWithInc.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("javaFinalField.kt")
|
||||
public void testJavaFinalField() throws Exception {
|
||||
|
||||
+12
@@ -46,6 +46,12 @@ public class FirStandaloneNormalAnalysisSourceModuleCompileTimeConstantEvaluator
|
||||
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("analysis/analysis-api/testData/components/compileTimeConstantProvider/evaluate"), Pattern.compile("^(.+)\\.kt$"), null, true);
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("arrayWithInc.kt")
|
||||
public void testArrayWithInc() throws Exception {
|
||||
runTest("analysis/analysis-api/testData/components/compileTimeConstantProvider/evaluate/arrayWithInc.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("binaryExpressionWithString.kt")
|
||||
public void testBinaryExpressionWithString() throws Exception {
|
||||
@@ -76,6 +82,12 @@ public class FirStandaloneNormalAnalysisSourceModuleCompileTimeConstantEvaluator
|
||||
runTest("analysis/analysis-api/testData/components/compileTimeConstantProvider/evaluate/integerLiteral_plusOne_justOne.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("interpolationWithInc.kt")
|
||||
public void testInterpolationWithInc() throws Exception {
|
||||
runTest("analysis/analysis-api/testData/components/compileTimeConstantProvider/evaluate/interpolationWithInc.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("javaFinalField.kt")
|
||||
public void testJavaFinalField() throws Exception {
|
||||
|
||||
Vendored
+5
@@ -0,0 +1,5 @@
|
||||
fun main(args: Array<String>) {
|
||||
val a = ArrayList<String>()
|
||||
a.add("")
|
||||
<expr>a</expr>[0]++
|
||||
}
|
||||
Vendored
+9
@@ -0,0 +1,9 @@
|
||||
expression: a
|
||||
|
||||
CONSTANT_EXPRESSION_EVALUATION
|
||||
constant: NOT_EVALUATED
|
||||
constantValueKind: NOT_EVALUATED
|
||||
|
||||
CONSTANT_LIKE_EXPRESSION_EVALUATION
|
||||
constantLike: NOT_EVALUATED
|
||||
constantLikeValueKind: NOT_EVALUATED
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
fun main(args: Array<String>) {
|
||||
var nextUnnamedLibraryIndex = 1
|
||||
val originalName = "${<expr>nextUnnamedLibraryIndex++</expr>}"
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
expression: nextUnnamedLibraryIndex++
|
||||
|
||||
CONSTANT_EXPRESSION_EVALUATION
|
||||
constant: NOT_EVALUATED
|
||||
constantValueKind: NOT_EVALUATED
|
||||
|
||||
CONSTANT_LIKE_EXPRESSION_EVALUATION
|
||||
constantLike: NOT_EVALUATED
|
||||
constantLikeValueKind: NOT_EVALUATED
|
||||
Vendored
+6
@@ -0,0 +1,6 @@
|
||||
// WITH_STDLIB
|
||||
fun main(args: Array<String>) {
|
||||
val a = ArrayList<String>()
|
||||
a.add("")
|
||||
a[<expr>0</expr>]++
|
||||
}
|
||||
Vendored
+6
@@ -0,0 +1,6 @@
|
||||
KT element: KtConstantExpression
|
||||
FIR element: FirConstExpressionImpl
|
||||
FIR source kind: KtRealSourceElementKind
|
||||
|
||||
FIR element rendered:
|
||||
Int(0)
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
fun main(args: Array<String>) {
|
||||
var nextUnnamedLibraryIndex = 1
|
||||
val originalName = "${<expr>nextUnnamedLibraryIndex++</expr>}"
|
||||
}
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
KT element: KtPostfixExpression
|
||||
FIR element: FirVariableAssignmentImpl
|
||||
FIR source kind: DesugaredIncrementOrDecrement
|
||||
|
||||
FIR element rendered:
|
||||
R|<local>/nextUnnamedLibraryIndex| = R|<local>/<unary>|.R|kotlin/Int.inc|()
|
||||
+12
@@ -255,6 +255,12 @@ public class GetOrBuildFirTestGenerated extends AbstractGetOrBuildFirTest {
|
||||
runTest("analysis/low-level-api-fir/testdata/getOrBuildFir/expressions/arrayAccessExpression.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("arrayIndexExpressionWithInc.kt")
|
||||
public void testArrayIndexExpressionWithInc() throws Exception {
|
||||
runTest("analysis/low-level-api-fir/testdata/getOrBuildFir/expressions/arrayIndexExpressionWithInc.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("asExpression.kt")
|
||||
public void testAsExpression() throws Exception {
|
||||
@@ -315,6 +321,12 @@ public class GetOrBuildFirTestGenerated extends AbstractGetOrBuildFirTest {
|
||||
runTest("analysis/low-level-api-fir/testdata/getOrBuildFir/expressions/ifExpression.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("incExpression.kt")
|
||||
public void testIncExpression() throws Exception {
|
||||
runTest("analysis/low-level-api-fir/testdata/getOrBuildFir/expressions/incExpression.kt");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("insidePlusAssignTarget.kt")
|
||||
public void testInsidePlusAssignTarget() throws Exception {
|
||||
|
||||
+1
-1
@@ -810,7 +810,7 @@ abstract class BaseFirBuilder<T>(val baseSession: FirSession, val context: Conte
|
||||
val indexVariables = indices.mapIndexed { i, index ->
|
||||
generateTemporaryVariable(
|
||||
baseModuleData,
|
||||
index.toFirSourceElement(),
|
||||
index.toFirSourceElement(KtFakeSourceElementKind.ArrayIndexExpressionReference),
|
||||
name = SpecialNames.subscribeOperatorIndex(i),
|
||||
index.convert()
|
||||
).also { statements += it }
|
||||
|
||||
@@ -163,6 +163,11 @@ sealed class KtFakeSourceElementKind : KtSourceElementKind() {
|
||||
// list[0] -> list.get(0) where name reference will have a fake source element
|
||||
object ArrayAccessNameReference : KtFakeSourceElementKind()
|
||||
|
||||
// a[b]++
|
||||
// b -> val <index0> = b where b will have fake property
|
||||
object ArrayIndexExpressionReference : KtFakeSourceElementKind()
|
||||
|
||||
|
||||
// super.foo() --> super<Supertype>.foo()
|
||||
// where `Supertype` has a fake source
|
||||
object SuperCallImplicitType : KtFakeSourceElementKind()
|
||||
|
||||
Reference in New Issue
Block a user