From 39639e08f94c5bf6ea23f8e21a3148724ff935d9 Mon Sep 17 00:00:00 2001 From: "Denis.Zharkov" Date: Thu, 6 Apr 2023 18:54:33 +0200 Subject: [PATCH] K2: Fix incorrect inference of delegated property type It was working incorrectly, because we've been trying to fix P1 variable to intersectTypes(String?, StubForP2) that should result to String? because we've got stubEqualsToAnything enabled there, but nullability was being chosen incorrectly because `StubForP2.isNullableType()` returned false NB: The code inside `is ConeTypeVariable` case wasn't working properly because it always `lookupTag.toSymbol(session)` always returned null, thus there was effectively five dead lines of code there. ^KT-57814 Fixed ^KT-57921 Related --- ...CompilerTestFE10TestdataTestGenerated.java | 6 ++++++ ...sticCompilerFE10TestDataTestGenerated.java | 6 ++++++ ...eeOldFrontendDiagnosticsTestGenerated.java | 6 ++++++ ...siOldFrontendDiagnosticsTestGenerated.java | 6 ++++++ .../kotlin/fir/resolve/LookupTagUtils.kt | 2 +- .../kotlin/fir/types/ConeTypeContext.kt | 13 ++++++------ .../inference/decoratedLambda.kt | 21 +++++++++++++++++++ .../test/runners/DiagnosticTestGenerated.java | 6 ++++++ 8 files changed, 58 insertions(+), 8 deletions(-) create mode 100644 compiler/testData/diagnostics/tests/delegatedProperty/inference/decoratedLambda.kt diff --git a/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/DiagnosticCompilerTestFE10TestdataTestGenerated.java b/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/DiagnosticCompilerTestFE10TestdataTestGenerated.java index c998b088c93..87ac4fa0f75 100644 --- a/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/DiagnosticCompilerTestFE10TestdataTestGenerated.java +++ b/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/DiagnosticCompilerTestFE10TestdataTestGenerated.java @@ -8829,6 +8829,12 @@ public class DiagnosticCompilerTestFE10TestdataTestGenerated extends AbstractDia runTest("compiler/testData/diagnostics/tests/delegatedProperty/inference/callableReferenceArgumentInDelegatedExpression.kt"); } + @Test + @TestMetadata("decoratedLambda.kt") + public void testDecoratedLambda() throws Exception { + runTest("compiler/testData/diagnostics/tests/delegatedProperty/inference/decoratedLambda.kt"); + } + @Test @TestMetadata("delegateExpressionAsLambda.kt") public void testDelegateExpressionAsLambda() throws Exception { diff --git a/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/LLFirPreresolvedReversedDiagnosticCompilerFE10TestDataTestGenerated.java b/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/LLFirPreresolvedReversedDiagnosticCompilerFE10TestDataTestGenerated.java index c88e3125643..47ae74e9aff 100644 --- a/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/LLFirPreresolvedReversedDiagnosticCompilerFE10TestDataTestGenerated.java +++ b/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/LLFirPreresolvedReversedDiagnosticCompilerFE10TestDataTestGenerated.java @@ -8829,6 +8829,12 @@ public class LLFirPreresolvedReversedDiagnosticCompilerFE10TestDataTestGenerated runTest("compiler/testData/diagnostics/tests/delegatedProperty/inference/callableReferenceArgumentInDelegatedExpression.kt"); } + @Test + @TestMetadata("decoratedLambda.kt") + public void testDecoratedLambda() throws Exception { + runTest("compiler/testData/diagnostics/tests/delegatedProperty/inference/decoratedLambda.kt"); + } + @Test @TestMetadata("delegateExpressionAsLambda.kt") public void testDelegateExpressionAsLambda() throws Exception { diff --git a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirLightTreeOldFrontendDiagnosticsTestGenerated.java b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirLightTreeOldFrontendDiagnosticsTestGenerated.java index ec59ea89476..25ca8ed4da5 100644 --- a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirLightTreeOldFrontendDiagnosticsTestGenerated.java +++ b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirLightTreeOldFrontendDiagnosticsTestGenerated.java @@ -8829,6 +8829,12 @@ public class FirLightTreeOldFrontendDiagnosticsTestGenerated extends AbstractFir runTest("compiler/testData/diagnostics/tests/delegatedProperty/inference/callableReferenceArgumentInDelegatedExpression.kt"); } + @Test + @TestMetadata("decoratedLambda.kt") + public void testDecoratedLambda() throws Exception { + runTest("compiler/testData/diagnostics/tests/delegatedProperty/inference/decoratedLambda.kt"); + } + @Test @TestMetadata("delegateExpressionAsLambda.kt") public void testDelegateExpressionAsLambda() throws Exception { diff --git a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirPsiOldFrontendDiagnosticsTestGenerated.java b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirPsiOldFrontendDiagnosticsTestGenerated.java index 5f62ff7cb91..9ab54e0771a 100644 --- a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirPsiOldFrontendDiagnosticsTestGenerated.java +++ b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirPsiOldFrontendDiagnosticsTestGenerated.java @@ -8835,6 +8835,12 @@ public class FirPsiOldFrontendDiagnosticsTestGenerated extends AbstractFirPsiDia runTest("compiler/testData/diagnostics/tests/delegatedProperty/inference/callableReferenceArgumentInDelegatedExpression.kt"); } + @Test + @TestMetadata("decoratedLambda.kt") + public void testDecoratedLambda() throws Exception { + runTest("compiler/testData/diagnostics/tests/delegatedProperty/inference/decoratedLambda.kt"); + } + @Test @TestMetadata("delegateExpressionAsLambda.kt") public void testDelegateExpressionAsLambda() throws Exception { diff --git a/compiler/fir/providers/src/org/jetbrains/kotlin/fir/resolve/LookupTagUtils.kt b/compiler/fir/providers/src/org/jetbrains/kotlin/fir/resolve/LookupTagUtils.kt index 39e65f1d828..cf3d4e3fad0 100644 --- a/compiler/fir/providers/src/org/jetbrains/kotlin/fir/resolve/LookupTagUtils.kt +++ b/compiler/fir/providers/src/org/jetbrains/kotlin/fir/resolve/LookupTagUtils.kt @@ -31,6 +31,7 @@ fun ConeClassifierLookupTag.toSymbol(useSiteSession: FirSession): FirClassifierS when (this) { is ConeClassLikeLookupTag -> toSymbol(useSiteSession) is ConeClassifierLookupTagWithFixedSymbol -> this.symbol + // TODO: replace null with error(), see KT-57921 else -> null } @@ -136,4 +137,3 @@ private fun Collection.findClassRepresentationThatIsSubtypeOf( supertype: ConeKotlinType, session: FirSession ): ConeClassLikeLookupTag? = firstOrNull { it.isSubtypeOf(supertype, session) }?.findClassRepresentation(supertype, session) - diff --git a/compiler/fir/providers/src/org/jetbrains/kotlin/fir/types/ConeTypeContext.kt b/compiler/fir/providers/src/org/jetbrains/kotlin/fir/types/ConeTypeContext.kt index 53685eb1521..e8b238eabc5 100644 --- a/compiler/fir/providers/src/org/jetbrains/kotlin/fir/types/ConeTypeContext.kt +++ b/compiler/fir/providers/src/org/jetbrains/kotlin/fir/types/ConeTypeContext.kt @@ -480,13 +480,12 @@ interface ConeTypeContext : TypeSystemContext, TypeSystemOptimizationContext, Ty return when (this) { is ConeFlexibleType -> this.upperBound.isNullableType() is ConeTypeParameterType -> lookupTag.symbol.allBoundsAreNullableOrUnresolved() - is ConeTypeVariableType -> { - val symbol = lookupTag.toSymbol(session) ?: return false - when (symbol) { - is FirClassSymbol -> false - is FirTypeAliasSymbol -> symbol.fir.expandedConeType?.isNullableType() ?: false - is FirTypeParameterSymbol -> symbol.allBoundsAreNullableOrUnresolved() - } + // NB: There's no branch for ConeTypeVariableType, i.e. it always returns false for them + // And while it seems reasonable to have similar semantics as for stub types, it would make some diagnostic test failing + // Thus, we leave the same semantics only for stubs (similar to TypeUtils.isNullableType) + is ConeStubType -> { + val symbol = (this.constructor.variable.defaultType.lookupTag.originalTypeParameter as? ConeTypeParameterLookupTag)?.symbol + symbol == null || symbol.allBoundsAreNullableOrUnresolved() } is ConeIntersectionType -> intersectedTypes.all { it.isNullableType() } is ConeClassLikeType -> directExpansionType(session)?.isNullableType() ?: false diff --git a/compiler/testData/diagnostics/tests/delegatedProperty/inference/decoratedLambda.kt b/compiler/testData/diagnostics/tests/delegatedProperty/inference/decoratedLambda.kt new file mode 100644 index 00000000000..bfd88e7cfcd --- /dev/null +++ b/compiler/testData/diagnostics/tests/delegatedProperty/inference/decoratedLambda.kt @@ -0,0 +1,21 @@ +// FIR_IDENTICAL +// ISSUE: KT-57814 +import kotlin.reflect.KProperty + +fun xComponent( + builder: (prop1: P2) -> Unit +): (prop1: P2) -> Unit = {} + +operator fun ((prop1: P1) -> Unit).getValue( + thisRef: Any?, + property: KProperty<*> +): (prop1: P1) -> Unit = this + +val pdfDocumentViewer by xComponent { _: String? -> +} + +fun xPDFDocumentViewer( + href: String? +) = pdfDocumentViewer( + href // Should be OK to pass nullable String? there +) diff --git a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/DiagnosticTestGenerated.java b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/DiagnosticTestGenerated.java index 8443c0c25c3..4b2f2c42daa 100644 --- a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/DiagnosticTestGenerated.java +++ b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/DiagnosticTestGenerated.java @@ -8835,6 +8835,12 @@ public class DiagnosticTestGenerated extends AbstractDiagnosticTest { runTest("compiler/testData/diagnostics/tests/delegatedProperty/inference/callableReferenceArgumentInDelegatedExpression.kt"); } + @Test + @TestMetadata("decoratedLambda.kt") + public void testDecoratedLambda() throws Exception { + runTest("compiler/testData/diagnostics/tests/delegatedProperty/inference/decoratedLambda.kt"); + } + @Test @TestMetadata("delegateExpressionAsLambda.kt") public void testDelegateExpressionAsLambda() throws Exception {