fix KT-7042 Providing return type for property setter is not reported as error

#KT-7042 Fixed
This commit is contained in:
Michael Nedzelsky
2015-10-30 21:26:35 +03:00
parent e6a2bd05d4
commit 3dbb117598
6 changed files with 49 additions and 0 deletions
@@ -340,6 +340,7 @@ public interface Errors {
DiagnosticFactory0<PsiElement> PRIVATE_SETTER_ON_NON_PRIVATE_LATE_INIT_VAR = DiagnosticFactory0.create(ERROR);
DiagnosticFactory0<PsiElement> ACCESSOR_VISIBILITY_FOR_ABSTRACT_PROPERTY = DiagnosticFactory0.create(ERROR);
DiagnosticFactory2<KtTypeReference, KotlinType, KotlinType> WRONG_GETTER_RETURN_TYPE = DiagnosticFactory2.create(ERROR);
DiagnosticFactory0<KtTypeReference> WRONG_SETTER_RETURN_TYPE = DiagnosticFactory0.create(ERROR);
DiagnosticFactory0<KtModifierListOwner>
ABSTRACT_PROPERTY_IN_PRIMARY_CONSTRUCTOR_PARAMETERS = DiagnosticFactory0.create(ERROR, ABSTRACT_MODIFIER);
@@ -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);
@@ -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
@@ -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): <!WRONG_SETTER_RETURN_TYPE!>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): <!WRONG_SETTER_RETURN_TYPE!>String<!> {
field = value
}
@@ -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
@@ -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")