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 b3ecbc597fc..5695dae87ab 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 @@ -10570,6 +10570,12 @@ public class DiagnosticCompilerTestFE10TestdataTestGenerated extends AbstractDia runTest("compiler/testData/diagnostics/tests/delegatedProperty/useTypeParameterOfExtensionProperty2.kt"); } + @Test + @TestMetadata("useTypeParameterOfExtensionProperty3.kt") + public void testUseTypeParameterOfExtensionProperty3() throws Exception { + runTest("compiler/testData/diagnostics/tests/delegatedProperty/useTypeParameterOfExtensionProperty3.kt"); + } + @Test @TestMetadata("useTypeParameterOfExtensionProperty_Disabled.kt") public void testUseTypeParameterOfExtensionProperty_Disabled() 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 148bbdcdfd6..122263f7a85 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 @@ -10570,6 +10570,12 @@ public class LLFirPreresolvedReversedDiagnosticCompilerFE10TestDataTestGenerated runTest("compiler/testData/diagnostics/tests/delegatedProperty/useTypeParameterOfExtensionProperty2.kt"); } + @Test + @TestMetadata("useTypeParameterOfExtensionProperty3.kt") + public void testUseTypeParameterOfExtensionProperty3() throws Exception { + runTest("compiler/testData/diagnostics/tests/delegatedProperty/useTypeParameterOfExtensionProperty3.kt"); + } + @Test @TestMetadata("useTypeParameterOfExtensionProperty_Disabled.kt") public void testUseTypeParameterOfExtensionProperty_Disabled() 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 5ef4ce9ddc3..c53285e7a44 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 @@ -10564,6 +10564,12 @@ public class FirLightTreeOldFrontendDiagnosticsTestGenerated extends AbstractFir runTest("compiler/testData/diagnostics/tests/delegatedProperty/useTypeParameterOfExtensionProperty2.kt"); } + @Test + @TestMetadata("useTypeParameterOfExtensionProperty3.kt") + public void testUseTypeParameterOfExtensionProperty3() throws Exception { + runTest("compiler/testData/diagnostics/tests/delegatedProperty/useTypeParameterOfExtensionProperty3.kt"); + } + @Test @TestMetadata("useTypeParameterOfExtensionProperty_Disabled.kt") public void testUseTypeParameterOfExtensionProperty_Disabled() 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 a1933493da2..fa5116a8acc 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 @@ -10570,6 +10570,12 @@ public class FirPsiOldFrontendDiagnosticsTestGenerated extends AbstractFirPsiDia runTest("compiler/testData/diagnostics/tests/delegatedProperty/useTypeParameterOfExtensionProperty2.kt"); } + @Test + @TestMetadata("useTypeParameterOfExtensionProperty3.kt") + public void testUseTypeParameterOfExtensionProperty3() throws Exception { + runTest("compiler/testData/diagnostics/tests/delegatedProperty/useTypeParameterOfExtensionProperty3.kt"); + } + @Test @TestMetadata("useTypeParameterOfExtensionProperty_Disabled.kt") public void testUseTypeParameterOfExtensionProperty_Disabled() throws Exception { diff --git a/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/declaration/FirDelegateUsesExtensionPropertyTypeParameterChecker.kt b/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/declaration/FirDelegateUsesExtensionPropertyTypeParameterChecker.kt index 175bae16445..8022e64d41d 100644 --- a/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/declaration/FirDelegateUsesExtensionPropertyTypeParameterChecker.kt +++ b/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/declaration/FirDelegateUsesExtensionPropertyTypeParameterChecker.kt @@ -12,17 +12,19 @@ import org.jetbrains.kotlin.fir.analysis.checkers.context.CheckerContext import org.jetbrains.kotlin.fir.analysis.checkers.unsubstitutedScope import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors import org.jetbrains.kotlin.fir.declarations.FirProperty -import org.jetbrains.kotlin.fir.expressions.FirFunctionCall +import org.jetbrains.kotlin.fir.declarations.utils.isExtension +import org.jetbrains.kotlin.fir.expressions.FirExpression import org.jetbrains.kotlin.fir.resolve.fullyExpandedType import org.jetbrains.kotlin.fir.resolve.toSymbol import org.jetbrains.kotlin.fir.scopes.processAllProperties -import org.jetbrains.kotlin.fir.symbols.impl.FirRegularClassSymbol +import org.jetbrains.kotlin.fir.symbols.impl.FirClassSymbol import org.jetbrains.kotlin.fir.symbols.impl.FirTypeParameterSymbol import org.jetbrains.kotlin.fir.types.* object FirDelegateUsesExtensionPropertyTypeParameterChecker : FirPropertyChecker(MppCheckerKind.Common) { override fun check(declaration: FirProperty, context: CheckerContext, reporter: DiagnosticReporter) { - val delegate = declaration.delegate as? FirFunctionCall ?: return + if (!declaration.isExtension) return + val delegate = declaration.delegate ?: return val parameters = declaration.typeParameters.mapTo(hashSetOf()) { it.symbol } val usedTypeParameterSymbol = delegate.resolvedType.findUsedTypeParameterSymbol(parameters, delegate, context, reporter) @@ -33,14 +35,14 @@ object FirDelegateUsesExtensionPropertyTypeParameterChecker : FirPropertyChecker private fun ConeKotlinType.findUsedTypeParameterSymbol( typeParameterSymbols: HashSet, - delegate: FirFunctionCall, + delegate: FirExpression, context: CheckerContext, reporter: DiagnosticReporter, ): FirTypeParameterSymbol? { val expandedDelegateClassLikeType = delegate.resolvedType.lowerBoundIfFlexible().fullyExpandedType(context.session) .unwrapDefinitelyNotNull() as? ConeClassLikeType ?: return null - val delegateClassSymbol = expandedDelegateClassLikeType.lookupTag.toSymbol(context.session) as? FirRegularClassSymbol ?: return null + val delegateClassSymbol = expandedDelegateClassLikeType.lookupTag.toSymbol(context.session) as? FirClassSymbol<*> ?: return null val delegateClassScope by lazy { delegateClassSymbol.unsubstitutedScope(context) } for (it in typeArguments) { val theType = it.type ?: continue diff --git a/compiler/testData/diagnostics/tests/delegatedProperty/useTypeParameterOfExtensionProperty3.kt b/compiler/testData/diagnostics/tests/delegatedProperty/useTypeParameterOfExtensionProperty3.kt new file mode 100644 index 00000000000..d0d148fe8e5 --- /dev/null +++ b/compiler/testData/diagnostics/tests/delegatedProperty/useTypeParameterOfExtensionProperty3.kt @@ -0,0 +1,23 @@ +// FIR_IDENTICAL +// ISSUE: KT-64102 +// !DIAGNOSTICS: -DEBUG_INFO_SMARTCAST +// !LANGUAGE: +ForbidUsingExtensionPropertyTypeParameterInDelegate +// WITH_REFLECT + +import kotlin.reflect.KProperty + +class Inv(var t: T) + +open class Delegate { + var x: X? = null + operator fun getValue(thisRef: Inv, property: KProperty<*>) { + val _x = x + if (_x != null) { + thisRef.t = _x + } else { + x = thisRef.t + } + } +} + +val Inv.x: Unit by object : Delegate() {} \ No newline at end of file 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 0dcd1fb5933..23ae7c38954 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 @@ -10570,6 +10570,12 @@ public class DiagnosticTestGenerated extends AbstractDiagnosticTest { runTest("compiler/testData/diagnostics/tests/delegatedProperty/useTypeParameterOfExtensionProperty2.kt"); } + @Test + @TestMetadata("useTypeParameterOfExtensionProperty3.kt") + public void testUseTypeParameterOfExtensionProperty3() throws Exception { + runTest("compiler/testData/diagnostics/tests/delegatedProperty/useTypeParameterOfExtensionProperty3.kt"); + } + @Test @TestMetadata("useTypeParameterOfExtensionProperty_Disabled.kt") public void testUseTypeParameterOfExtensionProperty_Disabled() throws Exception {