diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/types/expressions/DelegatedPropertyResolver.java b/compiler/frontend/src/org/jetbrains/jet/lang/types/expressions/DelegatedPropertyResolver.java index 4679bf4a860..e986721598e 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/types/expressions/DelegatedPropertyResolver.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/types/expressions/DelegatedPropertyResolver.java @@ -53,6 +53,7 @@ import static org.jetbrains.jet.lang.psi.JetPsiFactory.createExpression; import static org.jetbrains.jet.lang.psi.JetPsiFactory.createSimpleName; import static org.jetbrains.jet.lang.resolve.BindingContext.*; import static org.jetbrains.jet.lang.types.TypeUtils.NO_EXPECTED_TYPE; +import static org.jetbrains.jet.lang.types.TypeUtils.noExpectedType; import static org.jetbrains.jet.lang.types.expressions.ExpressionTypingUtils.createFakeExpressionOfType; public class DelegatedPropertyResolver { @@ -270,6 +271,11 @@ public class DelegatedPropertyResolver { } if (!propertyDescriptor.isVar()) return; + // For the case: 'val v by d' (no declared type). + // When we add a constraint for 'set' method for delegated expression 'd' we use a type of the declared variable 'v'. + // But if the type isn't known yet, the constraint shouldn't be added (we try to infer the type of 'v' here as well). + if (propertyDescriptor.getReturnType() instanceof DeferredType) return; + OverloadResolutionResults setMethodResults = getDelegatedPropertyConventionMethod( propertyDescriptor, delegateExpression, returnType, traceToResolveConventionMethods, accessorScope, false); @@ -280,8 +286,10 @@ public class DelegatedPropertyResolver { if (valueParameters.size() == 3) { ValueParameterDescriptor valueParameterForThis = valueParameters.get(2); - constraintSystem - .addSubtypeConstraint(expectedType, valueParameterForThis.getType(), ConstraintPosition.FROM_COMPLETER); + if (!noExpectedType(expectedType)) { + constraintSystem.addSubtypeConstraint( + expectedType, valueParameterForThis.getType(), ConstraintPosition.FROM_COMPLETER); + } addConstraintForThisValue(constraintSystem, descriptor); } } diff --git a/compiler/testData/diagnostics/tests/delegatedProperty/inference/noExpectedTypeForSupertypeConstraint.kt b/compiler/testData/diagnostics/tests/delegatedProperty/inference/noExpectedTypeForSupertypeConstraint.kt new file mode 100644 index 00000000000..ef49de08bae --- /dev/null +++ b/compiler/testData/diagnostics/tests/delegatedProperty/inference/noExpectedTypeForSupertypeConstraint.kt @@ -0,0 +1,14 @@ +class A { + var a by MyProperty() +} + +class MyProperty { + + public fun get(thisRef: R, desc: PropertyMetadata): T { + throw Exception("$thisRef $desc") + } + + public fun set(thisRef: R, desc: PropertyMetadata, t: T) { + throw Exception("$thisRef $desc $t") + } +} diff --git a/compiler/testData/loadKotlin/prop/VarWithDelegated.kt b/compiler/testData/loadKotlin/prop/VarWithDelegated.kt new file mode 100644 index 00000000000..8169f5bfb52 --- /dev/null +++ b/compiler/testData/loadKotlin/prop/VarWithDelegated.kt @@ -0,0 +1,10 @@ +package test + +class A { + var a by MyProperty() +} + +class MyProperty { + fun get(t: T, p: PropertyMetadata): Int = 42 + fun set(t: T, p: PropertyMetadata, i: Int) {} +} \ No newline at end of file diff --git a/compiler/testData/loadKotlin/prop/VarWithDelegated.txt b/compiler/testData/loadKotlin/prop/VarWithDelegated.txt new file mode 100644 index 00000000000..b6cf9cc5980 --- /dev/null +++ b/compiler/testData/loadKotlin/prop/VarWithDelegated.txt @@ -0,0 +1,14 @@ +package test + +internal final class A { + /*primary*/ public constructor A() + internal final var a: jet.Int + internal final fun (): jet.Int + internal final fun (/*0*/ : jet.Int): jet.Unit +} + +internal final class MyProperty { + /*primary*/ public constructor MyProperty() + internal final fun get(/*0*/ t: T, /*1*/ p: jet.PropertyMetadata): jet.Int + internal final fun set(/*0*/ t: T, /*1*/ p: jet.PropertyMetadata, /*2*/ i: jet.Int): jet.Unit +} diff --git a/compiler/tests/org/jetbrains/jet/checkers/JetDiagnosticsTestGenerated.java b/compiler/tests/org/jetbrains/jet/checkers/JetDiagnosticsTestGenerated.java index 232d5f161f7..871dfabfad3 100644 --- a/compiler/tests/org/jetbrains/jet/checkers/JetDiagnosticsTestGenerated.java +++ b/compiler/tests/org/jetbrains/jet/checkers/JetDiagnosticsTestGenerated.java @@ -2444,6 +2444,11 @@ public class JetDiagnosticsTestGenerated extends AbstractDiagnosticsTestWithEage doTest("compiler/testData/diagnostics/tests/delegatedProperty/inference/noErrorsForImplicitConstraints.kt"); } + @TestMetadata("noExpectedTypeForSupertypeConstraint.kt") + public void testNoExpectedTypeForSupertypeConstraint() throws Exception { + doTest("compiler/testData/diagnostics/tests/delegatedProperty/inference/noExpectedTypeForSupertypeConstraint.kt"); + } + @TestMetadata("useCompleterWithoutExpectedType.kt") public void testUseCompleterWithoutExpectedType() throws Exception { doTest("compiler/testData/diagnostics/tests/delegatedProperty/inference/useCompleterWithoutExpectedType.kt"); diff --git a/compiler/tests/org/jetbrains/jet/descriptors/serialization/DescriptorSerializationTestGenerated.java b/compiler/tests/org/jetbrains/jet/descriptors/serialization/DescriptorSerializationTestGenerated.java index df7a8b150ff..caa0584f4e9 100644 --- a/compiler/tests/org/jetbrains/jet/descriptors/serialization/DescriptorSerializationTestGenerated.java +++ b/compiler/tests/org/jetbrains/jet/descriptors/serialization/DescriptorSerializationTestGenerated.java @@ -816,6 +816,11 @@ public class DescriptorSerializationTestGenerated extends AbstractDescriptorSeri doTest("compiler/testData/loadKotlin/prop/VarDelegationToTraitImpl.kt"); } + @TestMetadata("VarWithDelegated.kt") + public void testVarWithDelegated() throws Exception { + doTest("compiler/testData/loadKotlin/prop/VarWithDelegated.kt"); + } + @TestMetadata("compiler/testData/loadKotlin/prop/defaultAccessors") public static class DefaultAccessors extends AbstractDescriptorSerializationTest { public void testAllFilesPresentInDefaultAccessors() throws Exception { diff --git a/compiler/tests/org/jetbrains/jet/jvm/compiler/LoadCompiledKotlinTestGenerated.java b/compiler/tests/org/jetbrains/jet/jvm/compiler/LoadCompiledKotlinTestGenerated.java index e18bf6ec87f..d1d2d893d00 100644 --- a/compiler/tests/org/jetbrains/jet/jvm/compiler/LoadCompiledKotlinTestGenerated.java +++ b/compiler/tests/org/jetbrains/jet/jvm/compiler/LoadCompiledKotlinTestGenerated.java @@ -1054,6 +1054,11 @@ public class LoadCompiledKotlinTestGenerated extends AbstractLoadCompiledKotlinT doTestWithAccessors("compiler/testData/loadKotlin/prop/VarDelegationToTraitImpl.kt"); } + @TestMetadata("VarWithDelegated.kt") + public void testVarWithDelegated() throws Exception { + doTestWithAccessors("compiler/testData/loadKotlin/prop/VarWithDelegated.kt"); + } + @TestMetadata("compiler/testData/loadKotlin/prop/defaultAccessors") public static class DefaultAccessors extends AbstractLoadCompiledKotlinTest { public void testAllFilesPresentInDefaultAccessors() throws Exception { diff --git a/compiler/tests/org/jetbrains/jet/lang/resolve/lazy/LazyResolveNamespaceComparingTestGenerated.java b/compiler/tests/org/jetbrains/jet/lang/resolve/lazy/LazyResolveNamespaceComparingTestGenerated.java index 990e9161d12..5852fbbf7b3 100644 --- a/compiler/tests/org/jetbrains/jet/lang/resolve/lazy/LazyResolveNamespaceComparingTestGenerated.java +++ b/compiler/tests/org/jetbrains/jet/lang/resolve/lazy/LazyResolveNamespaceComparingTestGenerated.java @@ -1056,6 +1056,11 @@ public class LazyResolveNamespaceComparingTestGenerated extends AbstractLazyReso doTestCheckingPrimaryConstructorsAndAccessors("compiler/testData/loadKotlin/prop/VarDelegationToTraitImpl.kt"); } + @TestMetadata("VarWithDelegated.kt") + public void testVarWithDelegated() throws Exception { + doTestCheckingPrimaryConstructorsAndAccessors("compiler/testData/loadKotlin/prop/VarWithDelegated.kt"); + } + @TestMetadata("compiler/testData/loadKotlin/prop/defaultAccessors") public static class DefaultAccessors extends AbstractLazyResolveNamespaceComparingTest { public void testAllFilesPresentInDefaultAccessors() throws Exception {