diff --git a/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/Errors.java b/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/Errors.java index 8b1edda73d9..2e7aef978e6 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/Errors.java +++ b/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/Errors.java @@ -340,6 +340,7 @@ public interface Errors { DiagnosticFactory0 PRIVATE_SETTER_ON_NON_PRIVATE_LATE_INIT_VAR = DiagnosticFactory0.create(ERROR); DiagnosticFactory0 ACCESSOR_VISIBILITY_FOR_ABSTRACT_PROPERTY = DiagnosticFactory0.create(ERROR); DiagnosticFactory2 WRONG_GETTER_RETURN_TYPE = DiagnosticFactory2.create(ERROR); + DiagnosticFactory0 WRONG_SETTER_RETURN_TYPE = DiagnosticFactory0.create(ERROR); DiagnosticFactory0 ABSTRACT_PROPERTY_IN_PRIMARY_CONSTRUCTOR_PARAMETERS = DiagnosticFactory0.create(ERROR, ABSTRACT_MODIFIER); 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 c2913af74b2..44cb34c12c8 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/rendering/DefaultErrorMessages.java +++ b/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/rendering/DefaultErrorMessages.java @@ -323,6 +323,7 @@ public class DefaultErrorMessages { MAP.put(USELESS_NULLABLE_CHECK, "Non-null type is checked for instance of nullable type"); MAP.put(WRONG_SETTER_PARAMETER_TYPE, "Setter parameter type must be equal to the type of the property, i.e. ''{0}''", RENDER_TYPE, RENDER_TYPE); MAP.put(WRONG_GETTER_RETURN_TYPE, "Getter return type must be equal to the type of the property, i.e. ''{0}''", RENDER_TYPE, RENDER_TYPE); + MAP.put(WRONG_SETTER_RETURN_TYPE, "Setter return type must be Unit"); MAP.put(NO_COMPANION_OBJECT, "Please specify constructor invocation; classifier ''{0}'' does not have a companion object", NAME); MAP.put(TYPE_PARAMETER_IS_NOT_AN_EXPRESSION, "Type parameter ''{0}'' is not an expression", NAME); MAP.put(TYPE_PARAMETER_ON_LHS_OF_DOT, "Type parameter ''{0}'' cannot have or inherit a companion object, so it cannot be on the left hand side of dot", NAME); diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/DescriptorResolver.java b/compiler/frontend/src/org/jetbrains/kotlin/resolve/DescriptorResolver.java index 536f17eefbe..38edba88a01 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/DescriptorResolver.java +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/DescriptorResolver.java @@ -1010,6 +1010,14 @@ public class DescriptorResolver { setter.hasBody(), false, setter.hasModifier(EXTERNAL_KEYWORD), CallableMemberDescriptor.Kind.DECLARATION, null, KotlinSourceElementKt .toSourceElement(setter)); + KtTypeReference returnTypeReference = setter.getReturnTypeReference(); + if (returnTypeReference != null) { + KotlinType returnType = typeResolver.resolveType(scope, returnTypeReference, trace, true); + if (!KotlinBuiltIns.isUnit(returnType)) { + trace.report(WRONG_SETTER_RETURN_TYPE.on(returnTypeReference)); + } + } + if (parameter != null) { // This check is redundant: the parser does not allow a default value, but we'll keep it just in case diff --git a/compiler/testData/diagnostics/tests/backingField/SetterWithExplicitType.kt b/compiler/testData/diagnostics/tests/backingField/SetterWithExplicitType.kt new file mode 100644 index 00000000000..0502a9bf943 --- /dev/null +++ b/compiler/testData/diagnostics/tests/backingField/SetterWithExplicitType.kt @@ -0,0 +1,26 @@ +// KT-7042 Providing return type for property setter is not reported as error + +var x: Int = 1 + +// No backing field! +var y: Int + get() = x + set(value): Any { + x = value + } + +var z: Int + get() = x + set(value): Unit { + x = value + } + +var u: String = "" + set(value): Unit { + field = value + } + +var v: String = "" + set(value): String { + field = value + } \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/backingField/SetterWithExplicitType.txt b/compiler/testData/diagnostics/tests/backingField/SetterWithExplicitType.txt new file mode 100644 index 00000000000..f094d84a1e0 --- /dev/null +++ b/compiler/testData/diagnostics/tests/backingField/SetterWithExplicitType.txt @@ -0,0 +1,7 @@ +package + +public var u: kotlin.String +public var v: kotlin.String +public var x: kotlin.Int +public var y: kotlin.Int +public var z: kotlin.Int diff --git a/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java index ff624d68dd4..4fa390f3754 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java @@ -1553,6 +1553,12 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTest { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/backingField/kt782packageLevel.kt"); doTest(fileName); } + + @TestMetadata("SetterWithExplicitType.kt") + public void testSetterWithExplicitType() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/backingField/SetterWithExplicitType.kt"); + doTest(fileName); + } } @TestMetadata("compiler/testData/diagnostics/tests/callableReference")