From bdc0b6b308a4f871f3dba48bf32c07f31d74b3fc Mon Sep 17 00:00:00 2001 From: Mikhael Bogdanov Date: Mon, 30 May 2016 12:39:19 +0300 Subject: [PATCH] Local delegated properties moved to language feature option --- .../jetbrains/kotlin/diagnostics/Errors.java | 1 + .../rendering/DefaultErrorMessages.java | 1 + .../kotlin/resolve/LocalVariableResolver.kt | 25 +++++++++++++------ .../noLocalDelegatedProperty.kt | 14 +++++++++++ .../noLocalDelegatedProperty.txt | 11 ++++++++ .../noLocalDelegatedPropertyInScript.kt | 23 +++++++++++++++++ .../noLocalDelegatedPropertyInScript.txt | 20 +++++++++++++++ .../checkers/DiagnosticsTestGenerated.java | 12 +++++++++ 8 files changed, 99 insertions(+), 8 deletions(-) create mode 100644 compiler/testData/diagnostics/tests/sourceCompatibility/noLocalDelegatedProperty.kt create mode 100644 compiler/testData/diagnostics/tests/sourceCompatibility/noLocalDelegatedProperty.txt create mode 100644 compiler/testData/diagnostics/tests/sourceCompatibility/noLocalDelegatedPropertyInScript.kt create mode 100644 compiler/testData/diagnostics/tests/sourceCompatibility/noLocalDelegatedPropertyInScript.txt diff --git a/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/Errors.java b/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/Errors.java index d4a9221b934..2cf6f1207c6 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/Errors.java +++ b/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/Errors.java @@ -393,6 +393,7 @@ public interface Errors { DiagnosticFactory0 ABSTRACT_DELEGATED_PROPERTY = DiagnosticFactory0.create(ERROR); DiagnosticFactory0 ACCESSOR_FOR_DELEGATED_PROPERTY = DiagnosticFactory0.create(ERROR); DiagnosticFactory0 DELEGATED_PROPERTY_IN_INTERFACE = DiagnosticFactory0.create(ERROR); + DiagnosticFactory0 LOCAL_VARIABLE_WITH_DELEGATE = DiagnosticFactory0.create(ERROR); DiagnosticFactory0 PROPERTY_WITH_NO_TYPE_NO_INITIALIZER = DiagnosticFactory0.create(ERROR, DECLARATION_SIGNATURE); diff --git a/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/rendering/DefaultErrorMessages.java b/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/rendering/DefaultErrorMessages.java index da4a6403458..04acfe166e2 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/rendering/DefaultErrorMessages.java +++ b/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/rendering/DefaultErrorMessages.java @@ -217,6 +217,7 @@ public class DefaultErrorMessages { MAP.put(ABSTRACT_DELEGATED_PROPERTY, "Delegated property cannot be abstract"); MAP.put(ACCESSOR_FOR_DELEGATED_PROPERTY, "Delegated property cannot have accessors with non-default implementations"); MAP.put(DELEGATED_PROPERTY_IN_INTERFACE, "Delegated properties are not allowed in interfaces"); + MAP.put(LOCAL_VARIABLE_WITH_DELEGATE, "Local variables are not allowed to have delegates"); MAP.put(INAPPLICABLE_LATEINIT_MODIFIER, "''lateinit'' modifier {0}", STRING); diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/LocalVariableResolver.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/LocalVariableResolver.kt index 00c78995ff6..c23299e54fc 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/LocalVariableResolver.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/LocalVariableResolver.kt @@ -16,6 +16,8 @@ package org.jetbrains.kotlin.resolve +import org.jetbrains.kotlin.config.LanguageFeature +import org.jetbrains.kotlin.config.LanguageFeatureSettings import org.jetbrains.kotlin.descriptors.* import org.jetbrains.kotlin.descriptors.annotations.Annotations import org.jetbrains.kotlin.descriptors.impl.LocalVariableDescriptor @@ -40,7 +42,8 @@ class LocalVariableResolver( private val dataFlowAnalyzer: DataFlowAnalyzer, private val annotationResolver: AnnotationResolver, private val variableTypeResolver: VariableTypeResolver, - private val delegatedPropertyResolver: DelegatedPropertyResolver + private val delegatedPropertyResolver: DelegatedPropertyResolver, + private val languageFeatureSettings: LanguageFeatureSettings ) { fun process( @@ -68,13 +71,19 @@ class LocalVariableResolver( val propertyDescriptor = resolveLocalVariableDescriptor(scope, property, context.dataFlowInfo, context.trace) val delegateExpression = property.delegateExpression - if (delegateExpression != null && propertyDescriptor is VariableDescriptorWithAccessors) { - delegatedPropertyResolver.resolvePropertyDelegate(typingContext.dataFlowInfo, - property, - propertyDescriptor, - delegateExpression, - typingContext.scope, - typingContext.trace); + if (delegateExpression != null) { + if (!languageFeatureSettings.supportsFeature(LanguageFeature.LocalDelegatedProperties)) { + context.trace.report(LOCAL_VARIABLE_WITH_DELEGATE.on(property.delegate!!)) + } + + if (propertyDescriptor is VariableDescriptorWithAccessors) { + delegatedPropertyResolver.resolvePropertyDelegate(typingContext.dataFlowInfo, + property, + propertyDescriptor, + delegateExpression, + typingContext.scope, + typingContext.trace); + } } val initializer = property.initializer diff --git a/compiler/testData/diagnostics/tests/sourceCompatibility/noLocalDelegatedProperty.kt b/compiler/testData/diagnostics/tests/sourceCompatibility/noLocalDelegatedProperty.kt new file mode 100644 index 00000000000..22da64b9d2b --- /dev/null +++ b/compiler/testData/diagnostics/tests/sourceCompatibility/noLocalDelegatedProperty.kt @@ -0,0 +1,14 @@ +// !LANGUAGE: -LocalDelegatedProperties +import kotlin.reflect.KProperty + +class Delegate { + operator fun getValue(t: Any?, p: KProperty<*>): Int = 1 +} + +fun foo(): Int { + val prop: Int by Delegate() + + val prop2: Int by 123 + + return prop + prop2 +} diff --git a/compiler/testData/diagnostics/tests/sourceCompatibility/noLocalDelegatedProperty.txt b/compiler/testData/diagnostics/tests/sourceCompatibility/noLocalDelegatedProperty.txt new file mode 100644 index 00000000000..d2ba7ec8e27 --- /dev/null +++ b/compiler/testData/diagnostics/tests/sourceCompatibility/noLocalDelegatedProperty.txt @@ -0,0 +1,11 @@ +package + +public fun foo(): kotlin.Int + +public final class Delegate { + public constructor Delegate() + public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean + public final operator fun getValue(/*0*/ t: kotlin.Any?, /*1*/ p: kotlin.reflect.KProperty<*>): kotlin.Int + public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int + public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String +} diff --git a/compiler/testData/diagnostics/tests/sourceCompatibility/noLocalDelegatedPropertyInScript.kt b/compiler/testData/diagnostics/tests/sourceCompatibility/noLocalDelegatedPropertyInScript.kt new file mode 100644 index 00000000000..6c445015aa7 --- /dev/null +++ b/compiler/testData/diagnostics/tests/sourceCompatibility/noLocalDelegatedPropertyInScript.kt @@ -0,0 +1,23 @@ +// !LANGUAGE: -LocalDelegatedProperties + +// FILE: script.kts +import kotlin.reflect.KProperty + +class Delegate { + operator fun getValue(t: Any?, p: KProperty<*>): Int = 1 +} + +fun foo(): Int { + val prop: Int by Delegate() + + val prop2: Int by 123 + + return prop + prop2 +} + + +val prop: Int by Delegate() + +val prop2: Int by 123 + +prop + prop2 \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/sourceCompatibility/noLocalDelegatedPropertyInScript.txt b/compiler/testData/diagnostics/tests/sourceCompatibility/noLocalDelegatedPropertyInScript.txt new file mode 100644 index 00000000000..244401d2898 --- /dev/null +++ b/compiler/testData/diagnostics/tests/sourceCompatibility/noLocalDelegatedPropertyInScript.txt @@ -0,0 +1,20 @@ +package + +public final class Script { + public constructor Script(/*0*/ args: kotlin.Array) + public final val args: kotlin.Array + public final val prop: kotlin.Int + public final val prop2: kotlin.Int + public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean + public final fun foo(): kotlin.Int + public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int + public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String + + public final class Delegate { + public constructor Delegate() + public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean + public final operator fun getValue(/*0*/ t: kotlin.Any?, /*1*/ p: kotlin.reflect.KProperty<*>): kotlin.Int + 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/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java index 36cb9a0ce11..0245d0bc272 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java @@ -18219,6 +18219,18 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTest { KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/diagnostics/tests/sourceCompatibility"), Pattern.compile("^(.+)\\.kt$"), true); } + @TestMetadata("noLocalDelegatedProperty.kt") + public void testNoLocalDelegatedProperty() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/sourceCompatibility/noLocalDelegatedProperty.kt"); + doTest(fileName); + } + + @TestMetadata("noLocalDelegatedPropertyInScript.kt") + public void testNoLocalDelegatedPropertyInScript() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/sourceCompatibility/noLocalDelegatedPropertyInScript.kt"); + doTest(fileName); + } + @TestMetadata("noTopLevelSealedInheritance.kt") public void testNoTopLevelSealedInheritance() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/sourceCompatibility/noTopLevelSealedInheritance.kt");