From 583584be7e959dd6a77f9d6b91c6c7feb99de3ec Mon Sep 17 00:00:00 2001 From: Kirill Rakhman Date: Wed, 25 Jan 2023 12:57:12 +0100 Subject: [PATCH] FIR: Resolve error expression in initializer of FirField ^KT-54775 Fixed --- ...DiagnosisCompilerFirTestdataTestGenerated.java | 6 ++++++ ...sNotTouchedTilContractsPhaseTestGenerated.java | 5 +++++ .../testData/resolve/kt54775.fir.txt | 15 +++++++++++++++ .../analysis-tests/testData/resolve/kt54775.kt | 8 ++++++++ .../test/runners/FirDiagnosticTestGenerated.java | 6 ++++++ .../FirDiagnosticsWithLightTreeTestGenerated.java | 6 ++++++ .../lightTree/converter/ExpressionsConverter.kt | 12 +++++++----- .../resolve/FirDeclarationsResolveTransformer.kt | 5 ++++- .../kotlin/fir/expressions/FirExpressionUtil.kt | 9 ++++++++- 9 files changed, 65 insertions(+), 7 deletions(-) create mode 100644 compiler/fir/analysis-tests/testData/resolve/kt54775.fir.txt create mode 100644 compiler/fir/analysis-tests/testData/resolve/kt54775.kt diff --git a/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/DiagnosisCompilerFirTestdataTestGenerated.java b/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/DiagnosisCompilerFirTestdataTestGenerated.java index 0de50e087df..d9fa3c259e5 100644 --- a/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/DiagnosisCompilerFirTestdataTestGenerated.java +++ b/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/DiagnosisCompilerFirTestdataTestGenerated.java @@ -368,6 +368,12 @@ public class DiagnosisCompilerFirTestdataTestGenerated extends AbstractDiagnosis runTest("compiler/fir/analysis-tests/testData/resolve/kt54220.kt"); } + @Test + @TestMetadata("kt54775.kt") + public void testKt54775() throws Exception { + runTest("compiler/fir/analysis-tests/testData/resolve/kt54775.kt"); + } + @Test @TestMetadata("labelAndReceiverForInfix.kt") public void testLabelAndReceiverForInfix() throws Exception { diff --git a/compiler/fir/analysis-tests/legacy-fir-tests/tests-gen/org/jetbrains/kotlin/fir/LazyBodyIsNotTouchedTilContractsPhaseTestGenerated.java b/compiler/fir/analysis-tests/legacy-fir-tests/tests-gen/org/jetbrains/kotlin/fir/LazyBodyIsNotTouchedTilContractsPhaseTestGenerated.java index a5f309ee32c..e7487c585c2 100644 --- a/compiler/fir/analysis-tests/legacy-fir-tests/tests-gen/org/jetbrains/kotlin/fir/LazyBodyIsNotTouchedTilContractsPhaseTestGenerated.java +++ b/compiler/fir/analysis-tests/legacy-fir-tests/tests-gen/org/jetbrains/kotlin/fir/LazyBodyIsNotTouchedTilContractsPhaseTestGenerated.java @@ -314,6 +314,11 @@ public class LazyBodyIsNotTouchedTilContractsPhaseTestGenerated extends Abstract runTest("compiler/fir/analysis-tests/testData/resolve/kt54220.kt"); } + @TestMetadata("kt54775.kt") + public void testKt54775() throws Exception { + runTest("compiler/fir/analysis-tests/testData/resolve/kt54775.kt"); + } + @TestMetadata("labelAndReceiverForInfix.kt") public void testLabelAndReceiverForInfix() throws Exception { runTest("compiler/fir/analysis-tests/testData/resolve/labelAndReceiverForInfix.kt"); diff --git a/compiler/fir/analysis-tests/testData/resolve/kt54775.fir.txt b/compiler/fir/analysis-tests/testData/resolve/kt54775.fir.txt new file mode 100644 index 00000000000..5e7669df645 --- /dev/null +++ b/compiler/fir/analysis-tests/testData/resolve/kt54775.fir.txt @@ -0,0 +1,15 @@ +FILE: kt54775.kt + public final fun bar(): R|T| { + ^bar (Null(null) as R|T|) + } + public final class X : { + public constructor(): R|X| { + super() + } + + private final field $$delegate_0: = ERROR_EXPR(Should have delegate) + + public final val prop: = ERROR_EXPR(Should have initializer) + public get(): + + } diff --git a/compiler/fir/analysis-tests/testData/resolve/kt54775.kt b/compiler/fir/analysis-tests/testData/resolve/kt54775.kt new file mode 100644 index 00000000000..561e9e930d6 --- /dev/null +++ b/compiler/fir/analysis-tests/testData/resolve/kt54775.kt @@ -0,0 +1,8 @@ +fun bar(): T { + return null as T +} + +class X() : B by get() = bar() { + val prop = bar() = 2 +} + diff --git a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirDiagnosticTestGenerated.java b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirDiagnosticTestGenerated.java index e60e9ebad65..4f94ce68651 100644 --- a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirDiagnosticTestGenerated.java +++ b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirDiagnosticTestGenerated.java @@ -368,6 +368,12 @@ public class FirDiagnosticTestGenerated extends AbstractFirDiagnosticTest { runTest("compiler/fir/analysis-tests/testData/resolve/kt54220.kt"); } + @Test + @TestMetadata("kt54775.kt") + public void testKt54775() throws Exception { + runTest("compiler/fir/analysis-tests/testData/resolve/kt54775.kt"); + } + @Test @TestMetadata("labelAndReceiverForInfix.kt") public void testLabelAndReceiverForInfix() throws Exception { diff --git a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirDiagnosticsWithLightTreeTestGenerated.java b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirDiagnosticsWithLightTreeTestGenerated.java index 7cd0457bc72..084218067e6 100644 --- a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirDiagnosticsWithLightTreeTestGenerated.java +++ b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirDiagnosticsWithLightTreeTestGenerated.java @@ -368,6 +368,12 @@ public class FirDiagnosticsWithLightTreeTestGenerated extends AbstractFirDiagnos runTest("compiler/fir/analysis-tests/testData/resolve/kt54220.kt"); } + @Test + @TestMetadata("kt54775.kt") + public void testKt54775() throws Exception { + runTest("compiler/fir/analysis-tests/testData/resolve/kt54775.kt"); + } + @Test @TestMetadata("labelAndReceiverForInfix.kt") public void testLabelAndReceiverForInfix() throws Exception { diff --git a/compiler/fir/raw-fir/light-tree2fir/src/org/jetbrains/kotlin/fir/lightTree/converter/ExpressionsConverter.kt b/compiler/fir/raw-fir/light-tree2fir/src/org/jetbrains/kotlin/fir/lightTree/converter/ExpressionsConverter.kt index c7a133410a6..6867a242a75 100644 --- a/compiler/fir/raw-fir/light-tree2fir/src/org/jetbrains/kotlin/fir/lightTree/converter/ExpressionsConverter.kt +++ b/compiler/fir/raw-fir/light-tree2fir/src/org/jetbrains/kotlin/fir/lightTree/converter/ExpressionsConverter.kt @@ -63,11 +63,13 @@ class ExpressionsConverter( get() = declarationsConverter.offset inline fun getAsFirExpression(expression: LighterASTNode?, errorReason: String = ""): R { - return expression?.let { - convertExpression(it, errorReason) - } as? R ?: buildErrorExpression( - expression?.toFirSourceElement(), ConeSimpleDiagnostic(errorReason, DiagnosticKind.ExpressionExpected) - ) as R + val converted = expression?.let { convertExpression(it, errorReason) } + return converted as? R + ?: buildErrorExpression( + expression?.toFirSourceElement(), + ConeSimpleDiagnostic(errorReason, DiagnosticKind.ExpressionExpected), + converted, + ) as R } /***** EXPRESSIONS *****/ diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/body/resolve/FirDeclarationsResolveTransformer.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/body/resolve/FirDeclarationsResolveTransformer.kt index 6a2a8b0f25c..ffc0570acca 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/body/resolve/FirDeclarationsResolveTransformer.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/body/resolve/FirDeclarationsResolveTransformer.kt @@ -1011,7 +1011,10 @@ open class FirDeclarationsResolveTransformer(transformer: FirAbstractBodyResolve } private val FirVariable.initializerResolved: Boolean - get() = initializer?.typeRef is FirResolvedTypeRef + get() { + val initializer = initializer ?: return false + return initializer.typeRef is FirResolvedTypeRef && initializer !is FirErrorExpression + } protected val FirFunction.bodyResolved: Boolean get() = body !is FirLazyBlock && body?.typeRef is FirResolvedTypeRef diff --git a/compiler/fir/tree/src/org/jetbrains/kotlin/fir/expressions/FirExpressionUtil.kt b/compiler/fir/tree/src/org/jetbrains/kotlin/fir/expressions/FirExpressionUtil.kt index 089ca13f0fc..afd8f1e29d7 100644 --- a/compiler/fir/tree/src/org/jetbrains/kotlin/fir/expressions/FirExpressionUtil.kt +++ b/compiler/fir/tree/src/org/jetbrains/kotlin/fir/expressions/FirExpressionUtil.kt @@ -6,6 +6,7 @@ package org.jetbrains.kotlin.fir.expressions import org.jetbrains.kotlin.KtSourceElement +import org.jetbrains.kotlin.fir.FirElement import org.jetbrains.kotlin.fir.declarations.FirDeclarationOrigin import org.jetbrains.kotlin.fir.declarations.FirValueParameter import org.jetbrains.kotlin.fir.diagnostics.ConeDiagnostic @@ -87,10 +88,16 @@ fun buildErrorLoop(source: KtSourceElement?, diagnostic: ConeDiagnostic): FirErr } } -fun buildErrorExpression(source: KtSourceElement?, diagnostic: ConeDiagnostic): FirErrorExpression { +fun buildErrorExpression( + source: KtSourceElement?, + diagnostic: ConeDiagnostic, + element: FirElement? = null +): FirErrorExpression { return buildErrorExpression { this.source = source this.diagnostic = diagnostic + this.expression = element as? FirExpression + this.nonExpressionElement = element.takeUnless { it is FirExpression } } }