From 6136526a3a30ce97d6fa197b0e26bbb066366f1a Mon Sep 17 00:00:00 2001 From: "Denis.Zharkov" Date: Wed, 26 May 2021 11:33:16 +0300 Subject: [PATCH] FIR: Avoid reporting inference errors from DelegatedPropertyConstraintPosition All necessary diagnostics have already been reported through the checkers --- .../analysis/diagnostics/coneDiagnosticToFirDiagnostic.kt | 4 ++++ .../inference/FirDelegatedPropertyInferenceSession.kt | 8 ++++---- .../calls/inference/model/ConstraintPositionAndErrors.kt | 2 +- .../inference/genericMethodInGenericClass.fir.kt | 2 +- .../inference/noErrorsForImplicitConstraints.fir.kt | 4 ++-- .../tests/delegatedProperty/inference/useExpectedType.kt | 8 ++++---- .../tests/delegatedProperty/nonDefaultAccessors.kt | 2 +- .../provideDelegate/hostAndReceiver2.fir.kt | 2 +- .../tests/delegatedProperty/setterThisTypeMismatch.kt | 4 ++-- .../tests/delegatedProperty/setterWithSupertype.kt | 2 +- .../delegatedProperty/typeMismatchForSetParameter.kt | 4 ++-- .../nullabilityWarnings/delegatedProperties.kt | 4 ++-- .../diagnostics/tests/typeParameters/kt42472.fir.kt | 4 ++-- .../testData/diagnostics/tests/typeParameters/kt42472.kt | 2 +- 14 files changed, 28 insertions(+), 24 deletions(-) diff --git a/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/diagnostics/coneDiagnosticToFirDiagnostic.kt b/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/diagnostics/coneDiagnosticToFirDiagnostic.kt index 54ca97114f6..101881289e8 100644 --- a/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/diagnostics/coneDiagnosticToFirDiagnostic.kt +++ b/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/diagnostics/coneDiagnosticToFirDiagnostic.kt @@ -272,6 +272,10 @@ private fun ConstraintSystemError.toDiagnostic( upperConeType, ) } + is DelegatedPropertyConstraintPosition<*> -> { + errorsToIgnore.add(this) + return null + } else -> null } } diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/inference/FirDelegatedPropertyInferenceSession.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/inference/FirDelegatedPropertyInferenceSession.kt index ce8f413e2d7..f6c196092f9 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/inference/FirDelegatedPropertyInferenceSession.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/inference/FirDelegatedPropertyInferenceSession.kt @@ -25,7 +25,7 @@ import org.jetbrains.kotlin.resolve.calls.inference.NewConstraintSystem import org.jetbrains.kotlin.resolve.calls.inference.buildAbstractResultingSubstitutor import org.jetbrains.kotlin.resolve.calls.inference.components.ConstraintSystemCompletionMode import org.jetbrains.kotlin.resolve.calls.inference.model.ConstraintStorage -import org.jetbrains.kotlin.resolve.calls.inference.model.SimpleConstraintSystemConstraintPosition +import org.jetbrains.kotlin.resolve.calls.inference.model.DelegatedPropertyConstraintPosition import org.jetbrains.kotlin.util.OperatorNameConventions import org.jetbrains.kotlin.utils.addToStdlib.safeAs @@ -113,7 +113,7 @@ class FirDelegatedPropertyInferenceSession( val unsubstitutedReturnType = accessor.returnTypeRef.coneType val substitutedReturnType = substitutor.substituteOrSelf(unsubstitutedReturnType) - commonSystem.addSubtypeConstraint(substitutedReturnType, expectedType!!, SimpleConstraintSystemConstraintPosition) + commonSystem.addSubtypeConstraint(substitutedReturnType, expectedType!!, DelegatedPropertyConstraintPosition(callInfo.callSite)) } addConstraintForThis(commonSystem) @@ -125,7 +125,7 @@ class FirDelegatedPropertyInferenceSession( val unsubstitutedParameterType = accessor.valueParameters.getOrNull(2)?.returnTypeRef?.coneType ?: return val substitutedReturnType = substitutor.substituteOrSelf(unsubstitutedParameterType) - commonSystem.addSubtypeConstraint(substitutedReturnType, expectedType!!, SimpleConstraintSystemConstraintPosition) + commonSystem.addSubtypeConstraint(expectedType!!, substitutedReturnType, DelegatedPropertyConstraintPosition(callInfo.callSite)) } addConstraintForThis(commonSystem) @@ -141,7 +141,7 @@ class FirDelegatedPropertyInferenceSession( } ?: components.session.builtinTypes.nullableNothingType.type val valueParameterForThis = (symbol as? FirFunctionSymbol<*>)?.fir?.valueParameters?.firstOrNull() ?: return val substitutedType = substitutor.substituteOrSelf(valueParameterForThis.returnTypeRef.coneType) - commonSystem.addSubtypeConstraint(typeOfThis, substitutedType, SimpleConstraintSystemConstraintPosition) + commonSystem.addSubtypeConstraint(typeOfThis, substitutedType, DelegatedPropertyConstraintPosition(callInfo.callSite)) } override fun writeOnlyStubs(call: T): Boolean where T : FirResolvable, T : FirStatement = false diff --git a/compiler/resolution.common/src/org/jetbrains/kotlin/resolve/calls/inference/model/ConstraintPositionAndErrors.kt b/compiler/resolution.common/src/org/jetbrains/kotlin/resolve/calls/inference/model/ConstraintPositionAndErrors.kt index fdc93703b50..9b3f1f81b21 100644 --- a/compiler/resolution.common/src/org/jetbrains/kotlin/resolve/calls/inference/model/ConstraintPositionAndErrors.kt +++ b/compiler/resolution.common/src/org/jetbrains/kotlin/resolve/calls/inference/model/ConstraintPositionAndErrors.kt @@ -68,7 +68,7 @@ abstract class LambdaArgumentConstraintPosition(val lambda: T) : ConstraintPo } } -abstract class DelegatedPropertyConstraintPosition(val topLevelCall: T) : ConstraintPosition() { +open class DelegatedPropertyConstraintPosition(val topLevelCall: T) : ConstraintPosition() { override fun toString(): String = "Constraint from call $topLevelCall for delegated property" } diff --git a/compiler/testData/diagnostics/tests/delegatedProperty/inference/genericMethodInGenericClass.fir.kt b/compiler/testData/diagnostics/tests/delegatedProperty/inference/genericMethodInGenericClass.fir.kt index 10fdfcfffcb..660122e8850 100644 --- a/compiler/testData/diagnostics/tests/delegatedProperty/inference/genericMethodInGenericClass.fir.kt +++ b/compiler/testData/diagnostics/tests/delegatedProperty/inference/genericMethodInGenericClass.fir.kt @@ -16,7 +16,7 @@ class B() { } var b1: Int by B() -var b2: Int by B() +var b2: Int by B() class C() { operator fun getValue(t: Any?, p: KProperty<*>): R = null!! diff --git a/compiler/testData/diagnostics/tests/delegatedProperty/inference/noErrorsForImplicitConstraints.fir.kt b/compiler/testData/diagnostics/tests/delegatedProperty/inference/noErrorsForImplicitConstraints.fir.kt index 22e4ad68cdc..a9081d1a357 100644 --- a/compiler/testData/diagnostics/tests/delegatedProperty/inference/noErrorsForImplicitConstraints.fir.kt +++ b/compiler/testData/diagnostics/tests/delegatedProperty/inference/noErrorsForImplicitConstraints.fir.kt @@ -3,8 +3,8 @@ package foo import kotlin.reflect.KProperty class A { - var a5: String by MyProperty1() - var b5: String by getMyProperty1() + var a5: String by MyProperty1() + var b5: String by getMyProperty1() } fun getMyProperty1() = MyProperty1() diff --git a/compiler/testData/diagnostics/tests/delegatedProperty/inference/useExpectedType.kt b/compiler/testData/diagnostics/tests/delegatedProperty/inference/useExpectedType.kt index 44559aa738f..6c3179ee4d1 100644 --- a/compiler/testData/diagnostics/tests/delegatedProperty/inference/useExpectedType.kt +++ b/compiler/testData/diagnostics/tests/delegatedProperty/inference/useExpectedType.kt @@ -52,12 +52,12 @@ class MyProperty2 { //-------------------------- class A3 { - var a3: String by MyProperty3() - var b3: String by getMyProperty3() + var a3: String by MyProperty3() + var b3: String by getMyProperty3() } -var c3: String by getMyProperty3() -var d3: String by MyProperty3() +var c3: String by getMyProperty3() +var d3: String by MyProperty3() fun getMyProperty3() = MyProperty3() diff --git a/compiler/testData/diagnostics/tests/delegatedProperty/nonDefaultAccessors.kt b/compiler/testData/diagnostics/tests/delegatedProperty/nonDefaultAccessors.kt index e461e495cc2..5708e28d291 100644 --- a/compiler/testData/diagnostics/tests/delegatedProperty/nonDefaultAccessors.kt +++ b/compiler/testData/diagnostics/tests/delegatedProperty/nonDefaultAccessors.kt @@ -5,7 +5,7 @@ class A { val p1 by this get - var p2 by this + var p2 by this get() = "" operator fun getValue(a: Any?, p: Any?) = "" diff --git a/compiler/testData/diagnostics/tests/delegatedProperty/provideDelegate/hostAndReceiver2.fir.kt b/compiler/testData/diagnostics/tests/delegatedProperty/provideDelegate/hostAndReceiver2.fir.kt index 1d971cfa808..fdff4a9af41 100644 --- a/compiler/testData/diagnostics/tests/delegatedProperty/provideDelegate/hostAndReceiver2.fir.kt +++ b/compiler/testData/diagnostics/tests/delegatedProperty/provideDelegate/hostAndReceiver2.fir.kt @@ -9,5 +9,5 @@ object T2 { operator fun Foo.getValue(receiver: String, p: Any?): T = TODO() val String.test1: String by delegate() - val test2: String by delegate() + val test2: String by delegate() } diff --git a/compiler/testData/diagnostics/tests/delegatedProperty/setterThisTypeMismatch.kt b/compiler/testData/diagnostics/tests/delegatedProperty/setterThisTypeMismatch.kt index f99b4b7d247..3d020f1c116 100644 --- a/compiler/testData/diagnostics/tests/delegatedProperty/setterThisTypeMismatch.kt +++ b/compiler/testData/diagnostics/tests/delegatedProperty/setterThisTypeMismatch.kt @@ -4,10 +4,10 @@ import kotlin.reflect.KProperty class D { - var c: Int by Delegate() + var c: Int by Delegate() } -var cTopLevel: Int by Delegate() +var cTopLevel: Int by Delegate() class A diff --git a/compiler/testData/diagnostics/tests/delegatedProperty/setterWithSupertype.kt b/compiler/testData/diagnostics/tests/delegatedProperty/setterWithSupertype.kt index 3c7b8fe56af..9c94281bc76 100644 --- a/compiler/testData/diagnostics/tests/delegatedProperty/setterWithSupertype.kt +++ b/compiler/testData/diagnostics/tests/delegatedProperty/setterWithSupertype.kt @@ -6,7 +6,7 @@ import kotlin.reflect.KProperty open class Base class Derived: Base() -var a: Derived by A() +var a: Derived by A() class A { operator fun getValue(t: Any?, p: KProperty<*>): Derived { diff --git a/compiler/testData/diagnostics/tests/delegatedProperty/typeMismatchForSetParameter.kt b/compiler/testData/diagnostics/tests/delegatedProperty/typeMismatchForSetParameter.kt index 7c665c3f8b5..edcd12c677e 100644 --- a/compiler/testData/diagnostics/tests/delegatedProperty/typeMismatchForSetParameter.kt +++ b/compiler/testData/diagnostics/tests/delegatedProperty/typeMismatchForSetParameter.kt @@ -4,10 +4,10 @@ import kotlin.reflect.KProperty class A { - var a: Int by Delegate() + var a: Int by Delegate() } -var aTopLevel: Int by Delegate() +var aTopLevel: Int by Delegate() class Delegate { operator fun getValue(t: Any?, p: KProperty<*>): Int { diff --git a/compiler/testData/diagnostics/tests/platformTypes/nullabilityWarnings/delegatedProperties.kt b/compiler/testData/diagnostics/tests/platformTypes/nullabilityWarnings/delegatedProperties.kt index b57e9568f15..75361e7bd2e 100644 --- a/compiler/testData/diagnostics/tests/platformTypes/nullabilityWarnings/delegatedProperties.kt +++ b/compiler/testData/diagnostics/tests/platformTypes/nullabilityWarnings/delegatedProperties.kt @@ -19,6 +19,6 @@ public class J { // FILE: k.kt -var A by J.staticNN +var A by J.staticNN var B by J.staticN -var C by J.staticJ +var C by J.staticJ diff --git a/compiler/testData/diagnostics/tests/typeParameters/kt42472.fir.kt b/compiler/testData/diagnostics/tests/typeParameters/kt42472.fir.kt index d0caa6e96e5..884516f1ee3 100644 --- a/compiler/testData/diagnostics/tests/typeParameters/kt42472.fir.kt +++ b/compiler/testData/diagnostics/tests/typeParameters/kt42472.fir.kt @@ -7,6 +7,6 @@ fun interface ReadOnlyProperty { } class Problem { - val variable: Int by delegate() // delegate returns `ReadOnlyProperty` + val variable: Int by delegate() // delegate returns `ReadOnlyProperty` fun delegate() = null as ReadOnlyProperty -} \ No newline at end of file +} diff --git a/compiler/testData/diagnostics/tests/typeParameters/kt42472.kt b/compiler/testData/diagnostics/tests/typeParameters/kt42472.kt index 9526b7e7201..24b60e5092d 100644 --- a/compiler/testData/diagnostics/tests/typeParameters/kt42472.kt +++ b/compiler/testData/diagnostics/tests/typeParameters/kt42472.kt @@ -9,4 +9,4 @@ fun interface ReadOnlyProperty { class Problem { val variable: Int by delegate() // delegate returns `ReadOnlyProperty` fun delegate() = null as ReadOnlyProperty -} \ No newline at end of file +}