[FIR] Check DELEGATE_USES_EXTENSION_PROPERTY_TYPE_PARAMETER for anonymous objects

^KT-64102 Fixed
This commit is contained in:
Ivan Kochurkin
2024-02-05 20:43:31 +01:00
committed by Space Team
parent 8ee54a9045
commit c4e0cafe9c
7 changed files with 60 additions and 5 deletions
@@ -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 {
@@ -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 {
@@ -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 {
@@ -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 {
@@ -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<FirTypeParameterSymbol>,
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
@@ -0,0 +1,23 @@
// FIR_IDENTICAL
// ISSUE: KT-64102
// !DIAGNOSTICS: -DEBUG_INFO_SMARTCAST
// !LANGUAGE: +ForbidUsingExtensionPropertyTypeParameterInDelegate
// WITH_REFLECT
import kotlin.reflect.KProperty
class Inv<T>(var t: T)
open class Delegate<X> {
var x: X? = null
operator fun getValue(thisRef: Inv<X>, property: KProperty<*>) {
val _x = x
if (_x != null) {
thisRef.t = _x
} else {
x = thisRef.t
}
}
}
val <T> Inv<T>.x: Unit <!DELEGATE_USES_EXTENSION_PROPERTY_TYPE_PARAMETER_ERROR!>by object : Delegate<T>() {}<!>
@@ -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 {