diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/VariableTypeResolver.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/VariableTypeResolver.kt index cdf1ee75761..ff9ef83c0f0 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/VariableTypeResolver.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/VariableTypeResolver.kt @@ -16,7 +16,7 @@ package org.jetbrains.kotlin.resolve -import org.jetbrains.kotlin.descriptors.PropertyDescriptor +import org.jetbrains.kotlin.descriptors.VariableDescriptorWithAccessors import org.jetbrains.kotlin.descriptors.impl.VariableDescriptorWithInitializerImpl import org.jetbrains.kotlin.diagnostics.Errors import org.jetbrains.kotlin.diagnostics.Errors.VARIABLE_WITH_NO_TYPE_NO_INITIALIZER @@ -62,7 +62,7 @@ class VariableTypeResolver( return type } !variable.hasInitializer() -> { - if (hasDelegate && variableDescriptor is PropertyDescriptor) { + if (hasDelegate && variableDescriptor is VariableDescriptorWithAccessors) { val property = variable as KtProperty if (property.hasDelegateExpression()) { return DeferredType.createRecursionIntolerant( @@ -131,18 +131,18 @@ class VariableTypeResolver( private fun resolveDelegatedPropertyType( property: KtProperty, - propertyDescriptor: PropertyDescriptor, + variableDescriptor: VariableDescriptorWithAccessors, scopeForInitializer: LexicalScope, delegateExpression: KtExpression, dataFlowInfo: DataFlowInfo, trace: BindingTrace ): KotlinType { val type = delegatedPropertyResolver.resolveDelegateExpression( - delegateExpression, property, propertyDescriptor, scopeForInitializer, trace, dataFlowInfo) + delegateExpression, property, variableDescriptor, scopeForInitializer, trace, dataFlowInfo) - val delegateFunctionsScope = ScopeUtils.makeScopeForDelegateConventionFunctions(scopeForInitializer, propertyDescriptor) + val delegateFunctionsScope = ScopeUtils.makeScopeForDelegateConventionFunctions(scopeForInitializer, variableDescriptor) val getterReturnType = delegatedPropertyResolver.getDelegatedPropertyGetMethodReturnType( - propertyDescriptor, delegateExpression, type, trace, delegateFunctionsScope, dataFlowInfo + variableDescriptor, delegateExpression, type, trace, delegateFunctionsScope, dataFlowInfo ) if (getterReturnType != null) { return getterReturnType diff --git a/compiler/testData/codegen/box/delegatedProperty/local/kt12891.kt b/compiler/testData/codegen/box/delegatedProperty/local/kt12891.kt new file mode 100644 index 00000000000..683c4fca841 --- /dev/null +++ b/compiler/testData/codegen/box/delegatedProperty/local/kt12891.kt @@ -0,0 +1,5 @@ +//WITH_RUNTIME +fun box(): String { + val x by lazy { "OK" } + return x +} \ No newline at end of file diff --git a/compiler/testData/codegen/box/delegatedProperty/local/localValNoExplicitType.kt b/compiler/testData/codegen/box/delegatedProperty/local/localValNoExplicitType.kt new file mode 100644 index 00000000000..20e86275732 --- /dev/null +++ b/compiler/testData/codegen/box/delegatedProperty/local/localValNoExplicitType.kt @@ -0,0 +1,12 @@ +package foo + +import kotlin.reflect.KProperty + +class Delegate { + operator fun getValue(t: Any?, p: KProperty<*>): Int = 1 +} + +fun box(): String { + val prop by Delegate() + return if (prop == 1) "OK" else "fail" +} diff --git a/compiler/testData/codegen/box/delegatedProperty/local/localVarNoExplicitType.kt b/compiler/testData/codegen/box/delegatedProperty/local/localVarNoExplicitType.kt new file mode 100644 index 00000000000..b9e51893775 --- /dev/null +++ b/compiler/testData/codegen/box/delegatedProperty/local/localVarNoExplicitType.kt @@ -0,0 +1,19 @@ +package foo + +import kotlin.reflect.KProperty + +class Delegate { + var inner = 1 + operator fun getValue(t: Any?, p: KProperty<*>): Int = inner + operator fun setValue(t: Any?, p: KProperty<*>, i: Int) { + inner = i + } +} + +fun box(): String { + var prop by Delegate() + if (prop != 1) return "fail get" + prop = 2 + if (prop != 2) return "fail set" + return "OK" +} diff --git a/compiler/testData/codegen/script/localDelegatedPropertyNoExplicitType.kts b/compiler/testData/codegen/script/localDelegatedPropertyNoExplicitType.kts new file mode 100644 index 00000000000..b763dbf7447 --- /dev/null +++ b/compiler/testData/codegen/script/localDelegatedPropertyNoExplicitType.kts @@ -0,0 +1,14 @@ +import kotlin.reflect.KProperty + +class Delegate { + operator fun getValue(t: Any?, p: KProperty<*>): Int = 3 +} + +fun foo(): Int { + val prop by Delegate() + return prop +} + +val x = foo() + +// expected: x: 3 diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java index fbf9108c8b4..5551da1b80a 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java @@ -5322,17 +5322,35 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { doTest(fileName); } + @TestMetadata("kt12891.kt") + public void testKt12891() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/delegatedProperty/local/kt12891.kt"); + doTest(fileName); + } + @TestMetadata("localVal.kt") public void testLocalVal() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/delegatedProperty/local/localVal.kt"); doTest(fileName); } + @TestMetadata("localValNoExplicitType.kt") + public void testLocalValNoExplicitType() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/delegatedProperty/local/localValNoExplicitType.kt"); + doTest(fileName); + } + @TestMetadata("localVar.kt") public void testLocalVar() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/delegatedProperty/local/localVar.kt"); doTest(fileName); } + + @TestMetadata("localVarNoExplicitType.kt") + public void testLocalVarNoExplicitType() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/delegatedProperty/local/localVarNoExplicitType.kt"); + doTest(fileName); + } } } diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/ScriptCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/ScriptCodegenTestGenerated.java index f8faa794531..b76f423a5a6 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/ScriptCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/ScriptCodegenTestGenerated.java @@ -71,6 +71,12 @@ public class ScriptCodegenTestGenerated extends AbstractScriptCodegenTest { doTest(fileName); } + @TestMetadata("localDelegatedPropertyNoExplicitType.kts") + public void testLocalDelegatedPropertyNoExplicitType() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/script/localDelegatedPropertyNoExplicitType.kts"); + doTest(fileName); + } + @TestMetadata("localFunction.kts") public void testLocalFunction() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/script/localFunction.kts");