Fix delegated property resolve on number literals and proper types

There is no need to update type of delegate expression if it's already
 resolved correctly (doesn't include non-proper types). In almost all
 cases it was fine except number literals as there we didn't box
 expression in backend and got problems at bytecode verification stage

 #KT-40057 Fixed
This commit is contained in:
Mikhail Zarechenskiy
2020-07-14 12:58:27 +03:00
parent e5bca3ce29
commit fcf7a55ccc
9 changed files with 71 additions and 2 deletions
@@ -9611,6 +9611,11 @@ public class FirBlackBoxCodegenTestGenerated extends AbstractFirBlackBoxCodegenT
runTest("compiler/testData/codegen/box/delegatedProperty/provideDelegate/genericDelegateWithNoAdditionalInfo.kt");
}
@TestMetadata("genericProvideDelegateOnNumberLiteral.kt")
public void testGenericProvideDelegateOnNumberLiteral() throws Exception {
runTest("compiler/testData/codegen/box/delegatedProperty/provideDelegate/genericProvideDelegateOnNumberLiteral.kt");
}
@TestMetadata("hostCheck.kt")
public void testHostCheck() throws Exception {
runTest("compiler/testData/codegen/box/delegatedProperty/provideDelegate/hostCheck.kt");
@@ -551,6 +551,8 @@ class DelegatedPropertyResolver(
if (delegateTypeConstructor is IntegerLiteralTypeConstructor)
delegateType = delegateTypeConstructor.getApproximatedType()
val delegateTypeForProperType = if (delegateType.isProperType()) delegateType else null
if (languageVersionSettings.supportsFeature(LanguageFeature.OperatorProvideDelegate)) {
val traceForProvideDelegate = TemporaryBindingTrace.create(traceToResolveDelegatedProperty, "Trace to resolve provide delegate")
@@ -584,7 +586,9 @@ class DelegatedPropertyResolver(
}
}
return inferDelegateTypeFromGetSetValueMethods(
delegateExpression, variableDescriptor, scopeForDelegate, traceToResolveDelegatedProperty, delegateType, delegateDataFlow
delegateExpression, variableDescriptor, scopeForDelegate,
traceToResolveDelegatedProperty, delegateType, delegateTypeForProperType,
delegateDataFlow
)
}
@@ -626,6 +630,7 @@ class DelegatedPropertyResolver(
scopeForDelegate: LexicalScope,
trace: TemporaryBindingTrace,
delegateType: KotlinType,
delegateTypeForProperType: KotlinType?,
delegateDataFlow: DataFlowInfo
): UnwrappedType {
val expectedType = if (variableDescriptor.type !is DeferredType) variableDescriptor.type.unwrap() else null
@@ -683,7 +688,7 @@ class DelegatedPropertyResolver(
}
val resolvedDelegateType = extractResolvedDelegateType(delegateExpression, trace, delegateType)
trace.recordType(delegateExpression, resolvedDelegateType)
trace.recordType(delegateExpression, delegateTypeForProperType ?: resolvedDelegateType)
trace.commit()
return resolvedDelegateType.unwrap()
}
@@ -0,0 +1,29 @@
// !LANGUAGE: +NewInference
// WITH_RUNTIME
import kotlin.properties.ReadOnlyProperty
import kotlin.reflect.KProperty
operator fun <C, T> T.provideDelegate(thisRef: C, property: KProperty<*>) =
object : ReadOnlyProperty<C, T> {
override operator fun getValue(thisRef: C, property: KProperty<*>) = this@provideDelegate
}
val byInt by 42
val byIntAsLong: Long by 42L
val byIntNullable: Int? by 42
val byString by "str"
val byStringNullable: String? = "strNullable"
fun box(): String {
if (byInt != 42) return "fail1"
if (byIntAsLong != 42L) return "fail2"
if (byIntNullable != 42) return "fail3"
if (byString != "str") return "fail4"
if (byStringNullable != "strNullable") return "fail5"
return "OK"
}
@@ -10836,6 +10836,11 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
runTest("compiler/testData/codegen/box/delegatedProperty/provideDelegate/genericDelegateWithNoAdditionalInfo.kt");
}
@TestMetadata("genericProvideDelegateOnNumberLiteral.kt")
public void testGenericProvideDelegateOnNumberLiteral() throws Exception {
runTest("compiler/testData/codegen/box/delegatedProperty/provideDelegate/genericProvideDelegateOnNumberLiteral.kt");
}
@TestMetadata("hostCheck.kt")
public void testHostCheck() throws Exception {
runTest("compiler/testData/codegen/box/delegatedProperty/provideDelegate/hostCheck.kt");
@@ -10836,6 +10836,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes
runTest("compiler/testData/codegen/box/delegatedProperty/provideDelegate/genericDelegateWithNoAdditionalInfo.kt");
}
@TestMetadata("genericProvideDelegateOnNumberLiteral.kt")
public void testGenericProvideDelegateOnNumberLiteral() throws Exception {
runTest("compiler/testData/codegen/box/delegatedProperty/provideDelegate/genericProvideDelegateOnNumberLiteral.kt");
}
@TestMetadata("hostCheck.kt")
public void testHostCheck() throws Exception {
runTest("compiler/testData/codegen/box/delegatedProperty/provideDelegate/hostCheck.kt");
@@ -9611,6 +9611,11 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes
runTest("compiler/testData/codegen/box/delegatedProperty/provideDelegate/genericDelegateWithNoAdditionalInfo.kt");
}
@TestMetadata("genericProvideDelegateOnNumberLiteral.kt")
public void testGenericProvideDelegateOnNumberLiteral() throws Exception {
runTest("compiler/testData/codegen/box/delegatedProperty/provideDelegate/genericProvideDelegateOnNumberLiteral.kt");
}
@TestMetadata("hostCheck.kt")
public void testHostCheck() throws Exception {
runTest("compiler/testData/codegen/box/delegatedProperty/provideDelegate/hostCheck.kt");
@@ -8266,6 +8266,11 @@ public class IrJsCodegenBoxES6TestGenerated extends AbstractIrJsCodegenBoxES6Tes
runTest("compiler/testData/codegen/box/delegatedProperty/provideDelegate/genericDelegateWithNoAdditionalInfo.kt");
}
@TestMetadata("genericProvideDelegateOnNumberLiteral.kt")
public void testGenericProvideDelegateOnNumberLiteral() throws Exception {
runTest("compiler/testData/codegen/box/delegatedProperty/provideDelegate/genericProvideDelegateOnNumberLiteral.kt");
}
@TestMetadata("hostCheck.kt")
public void testHostCheck() throws Exception {
runTest("compiler/testData/codegen/box/delegatedProperty/provideDelegate/hostCheck.kt");
@@ -8266,6 +8266,11 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest {
runTest("compiler/testData/codegen/box/delegatedProperty/provideDelegate/genericDelegateWithNoAdditionalInfo.kt");
}
@TestMetadata("genericProvideDelegateOnNumberLiteral.kt")
public void testGenericProvideDelegateOnNumberLiteral() throws Exception {
runTest("compiler/testData/codegen/box/delegatedProperty/provideDelegate/genericProvideDelegateOnNumberLiteral.kt");
}
@TestMetadata("hostCheck.kt")
public void testHostCheck() throws Exception {
runTest("compiler/testData/codegen/box/delegatedProperty/provideDelegate/hostCheck.kt");
@@ -8266,6 +8266,11 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest {
runTest("compiler/testData/codegen/box/delegatedProperty/provideDelegate/genericDelegateWithNoAdditionalInfo.kt");
}
@TestMetadata("genericProvideDelegateOnNumberLiteral.kt")
public void testGenericProvideDelegateOnNumberLiteral() throws Exception {
runTest("compiler/testData/codegen/box/delegatedProperty/provideDelegate/genericProvideDelegateOnNumberLiteral.kt");
}
@TestMetadata("hostCheck.kt")
public void testHostCheck() throws Exception {
runTest("compiler/testData/codegen/box/delegatedProperty/provideDelegate/hostCheck.kt");