From 9f2bf066b96d92926cbdaeaf8d22268365a17a37 Mon Sep 17 00:00:00 2001 From: Mikhail Zarechenskiy Date: Mon, 21 Aug 2017 00:26:00 +0300 Subject: [PATCH] [NI] Transform anonymous types of expected type for delegation --- .../resolve/DelegatedPropertyResolver.kt | 18 ++++++++++++- .../box/regressions/objectInsideDelegation.kt | 23 +++++++++++++++++ .../typeOfLazyDelegatedPropertyWithObject.kt | 23 +++++++++++++++++ .../typeOfLazyDelegatedPropertyWithObject.txt | 25 +++++++++++++++++++ .../ir/IrBlackBoxCodegenTestGenerated.java | 6 +++++ .../checkers/DiagnosticsTestGenerated.java | 6 +++++ .../codegen/BlackBoxCodegenTestGenerated.java | 6 +++++ .../LightAnalysisModeTestGenerated.java | 6 +++++ ...otlinConverterForWebDemoTestGenerated.java | 12 ++++----- ...otlinConverterSingleFileTestGenerated.java | 12 ++++----- .../semantics/JsCodegenBoxTestGenerated.java | 6 +++++ 11 files changed, 130 insertions(+), 13 deletions(-) create mode 100644 compiler/testData/codegen/box/regressions/objectInsideDelegation.kt create mode 100644 compiler/testData/diagnostics/tests/delegatedProperty/inference/typeOfLazyDelegatedPropertyWithObject.kt create mode 100644 compiler/testData/diagnostics/tests/delegatedProperty/inference/typeOfLazyDelegatedPropertyWithObject.txt diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/DelegatedPropertyResolver.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/DelegatedPropertyResolver.kt index c9910595cc6..400f04aec82 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/DelegatedPropertyResolver.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/DelegatedPropertyResolver.kt @@ -20,6 +20,7 @@ import com.google.common.collect.Lists import org.jetbrains.kotlin.builtins.KotlinBuiltIns import org.jetbrains.kotlin.config.LanguageFeature import org.jetbrains.kotlin.config.LanguageVersionSettings +import org.jetbrains.kotlin.descriptors.ClassifierDescriptor import org.jetbrains.kotlin.descriptors.FunctionDescriptor import org.jetbrains.kotlin.descriptors.PropertyDescriptor import org.jetbrains.kotlin.descriptors.VariableDescriptorWithAccessors @@ -35,6 +36,7 @@ import org.jetbrains.kotlin.resolve.calls.checkers.OperatorCallChecker import org.jetbrains.kotlin.resolve.calls.context.ContextDependency import org.jetbrains.kotlin.resolve.calls.inference.ConstraintSystem import org.jetbrains.kotlin.resolve.calls.inference.ConstraintSystemCompleter +import org.jetbrains.kotlin.resolve.calls.inference.components.NewTypeSubstitutor import org.jetbrains.kotlin.resolve.calls.inference.constraintPosition.ConstraintPositionKind.FROM_COMPLETER import org.jetbrains.kotlin.resolve.calls.inference.model.TypeVariableTypeConstructor import org.jetbrains.kotlin.resolve.calls.inference.toHandle @@ -566,7 +568,21 @@ class DelegatedPropertyResolver( val call = delegateExpression.getCall(traceToResolveConventionMethods.bindingContext) val pretendReturnType = call.getResolvedCall(traceToResolveConventionMethods.bindingContext)?.resultingDescriptor?.returnType - return pretendReturnType?.takeUnless { it.contains { it.constructor is TypeVariableTypeConstructor } } + val expectedType = pretendReturnType?.takeUnless { it.contains { it.constructor is TypeVariableTypeConstructor } } + return expectedType?.let { AnonymousTypeSubstitutor.safeSubstitute(it.unwrap()) } } } } + +private object AnonymousTypeSubstitutor : NewTypeSubstitutor { + override val isEmpty get() = true + + override fun substituteNotNullTypeWithConstructor(constructor: TypeConstructor): UnwrappedType? { + val declarationDescriptor = constructor.declarationDescriptor + if (declarationDescriptor is ClassifierDescriptor && DescriptorUtils.isAnonymousObject(declarationDescriptor)) { + return constructor.supertypes.firstOrNull()?.unwrap() + } + + return null + } +} diff --git a/compiler/testData/codegen/box/regressions/objectInsideDelegation.kt b/compiler/testData/codegen/box/regressions/objectInsideDelegation.kt new file mode 100644 index 00000000000..f2f9caf960d --- /dev/null +++ b/compiler/testData/codegen/box/regressions/objectInsideDelegation.kt @@ -0,0 +1,23 @@ +// WITH_RUNTIME + +val b: First by lazy { + object : First { } +} + +private val withoutType by lazy { + object : First { } +} + +private val withTwoSupertypes by lazy { + object : First, Second { } +} + +interface First +interface Second + +fun box(): String { + b + withoutType + withTwoSupertypes + return "OK" +} \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/delegatedProperty/inference/typeOfLazyDelegatedPropertyWithObject.kt b/compiler/testData/diagnostics/tests/delegatedProperty/inference/typeOfLazyDelegatedPropertyWithObject.kt new file mode 100644 index 00000000000..3a3b62a9352 --- /dev/null +++ b/compiler/testData/diagnostics/tests/delegatedProperty/inference/typeOfLazyDelegatedPropertyWithObject.kt @@ -0,0 +1,23 @@ +// !DIAGNOSTICS: -UNUSED_PARAMETER + +import kotlin.reflect.KProperty + +val b: First by lazy { + object : First { } +} + +private val withoutType by lazy { + object : First { } +} + +private val withTwoSupertypes by lazy { + object : First, Second { } +} + +interface First +interface Second + +fun lazy(initializer: () -> T): Lazy = TODO() +interface Lazy { + operator fun getValue(thisRef: Any?, property: KProperty<*>): T = TODO() +} \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/delegatedProperty/inference/typeOfLazyDelegatedPropertyWithObject.txt b/compiler/testData/diagnostics/tests/delegatedProperty/inference/typeOfLazyDelegatedPropertyWithObject.txt new file mode 100644 index 00000000000..93a4d38f52f --- /dev/null +++ b/compiler/testData/diagnostics/tests/delegatedProperty/inference/typeOfLazyDelegatedPropertyWithObject.txt @@ -0,0 +1,25 @@ +package + +public val b: First +private val withTwoSupertypes: withTwoSupertypes.. +private val withoutType: withoutType.. +public fun lazy(/*0*/ initializer: () -> T): Lazy + +public interface First { + public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean + public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int + public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String +} + +public interface Lazy { + public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean + public open operator fun getValue(/*0*/ thisRef: kotlin.Any?, /*1*/ property: kotlin.reflect.KProperty<*>): T + public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int + public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String +} + +public interface Second { + public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean + public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int + public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String +} diff --git a/compiler/tests-ir-jvm/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java b/compiler/tests-ir-jvm/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java index 259ace5ab46..ee5ff11081f 100644 --- a/compiler/tests-ir-jvm/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java +++ b/compiler/tests-ir-jvm/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java @@ -17036,6 +17036,12 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes doTest(fileName); } + @TestMetadata("objectInsideDelegation.kt") + public void testObjectInsideDelegation() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/regressions/objectInsideDelegation.kt"); + doTest(fileName); + } + @TestMetadata("referenceToSelfInLocal.kt") public void testReferenceToSelfInLocal() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/regressions/referenceToSelfInLocal.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java index ec0b1480d60..ae0f7acd0bf 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java @@ -6099,6 +6099,12 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTest { doTest(fileName); } + @TestMetadata("typeOfLazyDelegatedPropertyWithObject.kt") + public void testTypeOfLazyDelegatedPropertyWithObject() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/delegatedProperty/inference/typeOfLazyDelegatedPropertyWithObject.kt"); + doTest(fileName); + } + @TestMetadata("useCompleterWithoutExpectedType.kt") public void testUseCompleterWithoutExpectedType() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/delegatedProperty/inference/useCompleterWithoutExpectedType.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java index 07b5c25d63d..31b22549de4 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java @@ -17036,6 +17036,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { doTest(fileName); } + @TestMetadata("objectInsideDelegation.kt") + public void testObjectInsideDelegation() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/regressions/objectInsideDelegation.kt"); + doTest(fileName); + } + @TestMetadata("referenceToSelfInLocal.kt") public void testReferenceToSelfInLocal() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/regressions/referenceToSelfInLocal.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java index eae3a9185ba..6f018a5259e 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java @@ -17036,6 +17036,12 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes doTest(fileName); } + @TestMetadata("objectInsideDelegation.kt") + public void testObjectInsideDelegation() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/regressions/objectInsideDelegation.kt"); + doTest(fileName); + } + @TestMetadata("referenceToSelfInLocal.kt") public void testReferenceToSelfInLocal() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/regressions/referenceToSelfInLocal.kt"); diff --git a/j2k/tests/org/jetbrains/kotlin/j2k/JavaToKotlinConverterForWebDemoTestGenerated.java b/j2k/tests/org/jetbrains/kotlin/j2k/JavaToKotlinConverterForWebDemoTestGenerated.java index 8a97a9221ed..5f71c936a44 100644 --- a/j2k/tests/org/jetbrains/kotlin/j2k/JavaToKotlinConverterForWebDemoTestGenerated.java +++ b/j2k/tests/org/jetbrains/kotlin/j2k/JavaToKotlinConverterForWebDemoTestGenerated.java @@ -2939,18 +2939,18 @@ public class JavaToKotlinConverterForWebDemoTestGenerated extends AbstractJavaTo doTest(fileName); } - @TestMetadata("kt-19634.java") - public void testKt_19634() throws Exception { - String fileName = KotlinTestUtils.navigationMetadata("j2k/testData/fileOrElement/issues/kt-19634.java"); - doTest(fileName); - } - @TestMetadata("kt-17379.java") public void testKt_17379() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("j2k/testData/fileOrElement/issues/kt-17379.java"); doTest(fileName); } + @TestMetadata("kt-19634.java") + public void testKt_19634() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("j2k/testData/fileOrElement/issues/kt-19634.java"); + doTest(fileName); + } + @TestMetadata("kt-5294.java") public void testKt_5294() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("j2k/testData/fileOrElement/issues/kt-5294.java"); diff --git a/j2k/tests/org/jetbrains/kotlin/j2k/JavaToKotlinConverterSingleFileTestGenerated.java b/j2k/tests/org/jetbrains/kotlin/j2k/JavaToKotlinConverterSingleFileTestGenerated.java index 03a3f5d0960..6cdfdda2d3e 100644 --- a/j2k/tests/org/jetbrains/kotlin/j2k/JavaToKotlinConverterSingleFileTestGenerated.java +++ b/j2k/tests/org/jetbrains/kotlin/j2k/JavaToKotlinConverterSingleFileTestGenerated.java @@ -2939,18 +2939,18 @@ public class JavaToKotlinConverterSingleFileTestGenerated extends AbstractJavaTo doTest(fileName); } - @TestMetadata("kt-19634.java") - public void testKt_19634() throws Exception { - String fileName = KotlinTestUtils.navigationMetadata("j2k/testData/fileOrElement/issues/kt-19634.java"); - doTest(fileName); - } - @TestMetadata("kt-17379.java") public void testKt_17379() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("j2k/testData/fileOrElement/issues/kt-17379.java"); doTest(fileName); } + @TestMetadata("kt-19634.java") + public void testKt_19634() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("j2k/testData/fileOrElement/issues/kt-19634.java"); + doTest(fileName); + } + @TestMetadata("kt-5294.java") public void testKt_5294() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("j2k/testData/fileOrElement/issues/kt-5294.java"); diff --git a/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java b/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java index a5db30ec6ea..4ce24ae6fac 100644 --- a/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java +++ b/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java @@ -20810,6 +20810,12 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest { doTest(fileName); } + @TestMetadata("objectInsideDelegation.kt") + public void testObjectInsideDelegation() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/regressions/objectInsideDelegation.kt"); + doTest(fileName); + } + @TestMetadata("referenceToSelfInLocal.kt") public void testReferenceToSelfInLocal() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/regressions/referenceToSelfInLocal.kt");