KTIJ-20826 Approximate builder inference stub types as type parameters

This is a temporary fix to avoid crashes in FIR IDE

Of all stub types, only `ConeStubTypeForChainInference` is handled now,
so we can catch the other possible errors with stub types (in the
delegates' inference, for example)

The fix introduced in this commit should be reworked when KT-50916 is
fixed

^KTIJ-20826 Fixed
This commit is contained in:
Roman Golyshev
2022-01-25 13:48:06 +03:00
committed by TeamCityServer
parent baf4b3158c
commit 8befbc6375
6 changed files with 87 additions and 0 deletions
@@ -172,6 +172,12 @@ public class Fe10ResolveCallTestGenerated extends AbstractResolveCallTest {
runTest("analysis/analysis-api/testData/components/callResolver/resolveCall/arrayOfInAnnotation.kt");
}
@Test
@TestMetadata("builderInference.kt")
public void testBuilderInference() throws Exception {
runTest("analysis/analysis-api/testData/components/callResolver/resolveCall/builderInference.kt");
}
@Test
@TestMetadata("calleeExpressionOfImplicitInvoke.kt")
public void testCalleeExpressionOfImplicitInvoke() throws Exception {
@@ -30,10 +30,12 @@ import org.jetbrains.kotlin.fir.declarations.impl.FirOuterClassTypeParameterRef
import org.jetbrains.kotlin.fir.java.declarations.FirJavaField
import org.jetbrains.kotlin.fir.resolve.getContainingClass
import org.jetbrains.kotlin.fir.resolve.getSymbolByLookupTag
import org.jetbrains.kotlin.fir.resolve.inference.ConeTypeParameterBasedTypeVariable
import org.jetbrains.kotlin.fir.resolve.originalConstructorIfTypeAlias
import org.jetbrains.kotlin.fir.resolve.providers.symbolProvider
import org.jetbrains.kotlin.fir.resolve.substitution.ConeSubstitutor
import org.jetbrains.kotlin.fir.resolve.substitution.ConeSubstitutorByMap
import org.jetbrains.kotlin.fir.scopes.impl.toConeType
import org.jetbrains.kotlin.fir.symbols.ConeClassLikeLookupTag
import org.jetbrains.kotlin.fir.symbols.ConeTypeParameterLookupTag
import org.jetbrains.kotlin.fir.symbols.FirBasedSymbol
@@ -438,6 +440,15 @@ internal class KtSymbolByFirBuilder private constructor(
is ConeDefinitelyNotNullType -> KtFirDefinitelyNotNullType(coneType, token, this@KtSymbolByFirBuilder)
is ConeCapturedType -> KtFirCapturedType(coneType, token, this@KtSymbolByFirBuilder)
is ConeIntegerLiteralType -> KtFirIntegerLiteralType(coneType, token, this@KtSymbolByFirBuilder)
is ConeStubTypeForChainInference -> {
// TODO this is a temporary hack to prevent FIR IDE from crashing on builder inference, see KT-50916
val typeVariable = coneType.constructor.variable as? ConeTypeParameterBasedTypeVariable
val typeParameterSymbol = typeVariable?.typeParameterSymbol ?: throwUnexpectedElementError(coneType)
val coneTypeParameterType = typeParameterSymbol.toConeType() as ConeTypeParameterType
KtFirTypeParameterType(coneTypeParameterType, token, this@KtSymbolByFirBuilder)
}
else -> throwUnexpectedElementError(coneType)
}
}
@@ -172,6 +172,12 @@ public class FirResolveCallTestGenerated extends AbstractResolveCallTest {
runTest("analysis/analysis-api/testData/components/callResolver/resolveCall/arrayOfInAnnotation.kt");
}
@Test
@TestMetadata("builderInference.kt")
public void testBuilderInference() throws Exception {
runTest("analysis/analysis-api/testData/components/callResolver/resolveCall/builderInference.kt");
}
@Test
@TestMetadata("calleeExpressionOfImplicitInvoke.kt")
public void testCalleeExpressionOfImplicitInvoke() throws Exception {
@@ -0,0 +1,25 @@
KtSuccessCallInfo:
call = KtSimpleFunctionCall:
isImplicitInvoke = false
partiallyAppliedSymbol = KtPartiallyAppliedSymbol:
dispatchReceiver = KtImplicitReceiverValue:
symbol = KtReceiverParameterSymbol:
origin: SOURCE
type: test/Target<kotlin/String>
extensionReceiver = null
signature = KtFunctionLikeSignature:
receiverType = null
returnType = kotlin.Unit
symbol = test/Target.add(<dispatch receiver>: test.Target<T>, t: T): kotlin.Unit
valueParameters = [
KtVariableLikeSignature:
receiverType = null
returnType = kotlin.String
symbol = t: T
]
argumentMapping = {
s -> (KtVariableLikeSignature:
receiverType = null
returnType = kotlin.String
symbol = t: T)
}
@@ -0,0 +1,14 @@
// WITH_STDLIB
package test
class Target<T> {
fun add(t: T) {}
}
fun <T> buildTarget(@BuilderInference block: Target<T>.() -> Unit): Target<T> {
return Target<T>().apply { block() }
}
fun test(s: String) {
val result = buildTarget { <expr>add</expr>(s) }
}
@@ -0,0 +1,25 @@
KtSuccessCallInfo:
call = KtSimpleFunctionCall:
isImplicitInvoke = false
partiallyAppliedSymbol = KtPartiallyAppliedSymbol:
dispatchReceiver = KtImplicitReceiverValue:
symbol = KtReceiverParameterSymbol:
origin: SOURCE
type: test/Target<kotlin/String>
extensionReceiver = null
signature = KtFunctionLikeSignature:
receiverType = null
returnType = kotlin.Unit
symbol = test/Target.add(<dispatch receiver>: test.Target<T>, t: T): kotlin.Unit
valueParameters = [
KtVariableLikeSignature:
receiverType = null
returnType = T
symbol = t: T
]
argumentMapping = {
s -> (KtVariableLikeSignature:
receiverType = null
returnType = T
symbol = t: T)
}