Setter visibility must be worse or the same as property visibility
This commit is contained in:
@@ -358,6 +358,7 @@ public interface Errors {
|
||||
DiagnosticFactory0<PsiElement> REDUNDANT_MODIFIER_IN_GETTER = DiagnosticFactory0.create(WARNING);
|
||||
DiagnosticFactory0<PsiElement> GETTER_VISIBILITY_DIFFERS_FROM_PROPERTY_VISIBILITY = DiagnosticFactory0.create(ERROR);
|
||||
DiagnosticFactory0<PsiElement> SETTER_VISIBILITY_DIFFERS_FROM_LATEINIT_VISIBILITY = DiagnosticFactory0.create(ERROR);
|
||||
DiagnosticFactory0<PsiElement> SETTER_VISIBILITY_INCONSISTENT_WITH_PROPERTY_VISIBILITY = DiagnosticFactory0.create(ERROR);
|
||||
DiagnosticFactory0<PsiElement> PRIVATE_SETTER_FOR_ABSTRACT_PROPERTY = DiagnosticFactory0.create(ERROR);
|
||||
DiagnosticFactory0<PsiElement> PRIVATE_SETTER_FOR_OPEN_PROPERTY = DiagnosticFactory0.create(ERROR);
|
||||
DiagnosticFactory2<KtTypeReference, KotlinType, KotlinType> WRONG_GETTER_RETURN_TYPE = DiagnosticFactory2.create(ERROR);
|
||||
|
||||
+1
@@ -211,6 +211,7 @@ public class DefaultErrorMessages {
|
||||
|
||||
MAP.put(GETTER_VISIBILITY_DIFFERS_FROM_PROPERTY_VISIBILITY, "Getter visibility must be the same as property visibility");
|
||||
MAP.put(SETTER_VISIBILITY_DIFFERS_FROM_LATEINIT_VISIBILITY, "Setter visibility must be the same as lateinit property visibility");
|
||||
MAP.put(SETTER_VISIBILITY_INCONSISTENT_WITH_PROPERTY_VISIBILITY, "Setter visibility must be the same or less permissive than property visibility");
|
||||
MAP.put(PRIVATE_SETTER_FOR_ABSTRACT_PROPERTY, "Private setters are not allowed for abstract properties");
|
||||
MAP.put(PRIVATE_SETTER_FOR_OPEN_PROPERTY, "Private setters are deprecated for open properties");
|
||||
MAP.put(BACKING_FIELD_IN_INTERFACE, "Property in an interface cannot have a backing field");
|
||||
|
||||
@@ -718,6 +718,12 @@ class DeclarationsChecker(
|
||||
else if (propertyDescriptor.isLateInit && accessorDescriptor.visibility != propertyDescriptor.visibility) {
|
||||
reportVisibilityModifierDiagnostics(tokens.values, Errors.SETTER_VISIBILITY_DIFFERS_FROM_LATEINIT_VISIBILITY)
|
||||
}
|
||||
else {
|
||||
val compare = Visibilities.compare(accessorDescriptor.visibility, propertyDescriptor.visibility)
|
||||
if (compare == null || compare > 0) {
|
||||
reportVisibilityModifierDiagnostics(tokens.values, Errors.SETTER_VISIBILITY_INCONSISTENT_WITH_PROPERTY_VISIBILITY)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -17,7 +17,7 @@ class Test: ATest(), ITest {
|
||||
|
||||
override var prop2 : Int
|
||||
get() = 14
|
||||
<!CANNOT_CHANGE_ACCESS_PRIVILEGE!>internal<!> set(value) {}
|
||||
<!CANNOT_CHANGE_ACCESS_PRIVILEGE, SETTER_VISIBILITY_INCONSISTENT_WITH_PROPERTY_VISIBILITY!>internal<!> set(value) {}
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
class My {
|
||||
var x: Int = 0
|
||||
// Ok
|
||||
private set
|
||||
|
||||
private var y: Int = 1
|
||||
// Error: better
|
||||
<!SETTER_VISIBILITY_INCONSISTENT_WITH_PROPERTY_VISIBILITY!>public<!> set
|
||||
|
||||
protected var z: Int = 2
|
||||
// Ok
|
||||
private set
|
||||
|
||||
protected var w: Int = 3
|
||||
// Error: incompatible
|
||||
<!SETTER_VISIBILITY_INCONSISTENT_WITH_PROPERTY_VISIBILITY!>internal<!> set
|
||||
|
||||
internal var v: Int = 4
|
||||
// Error: incompatible
|
||||
<!SETTER_VISIBILITY_INCONSISTENT_WITH_PROPERTY_VISIBILITY!>protected<!> set
|
||||
|
||||
internal var t: Int = 5
|
||||
// Error: better
|
||||
<!SETTER_VISIBILITY_INCONSISTENT_WITH_PROPERTY_VISIBILITY!>public<!> set
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package
|
||||
|
||||
public final class My {
|
||||
public constructor My()
|
||||
internal final var t: kotlin.Int
|
||||
internal final var v: kotlin.Int
|
||||
protected final var w: kotlin.Int
|
||||
public final var x: kotlin.Int
|
||||
private final var y: kotlin.Int
|
||||
protected final var z: kotlin.Int
|
||||
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
|
||||
}
|
||||
-6
@@ -16,12 +16,6 @@ class ClassVal() {
|
||||
protected var property5: String = ""
|
||||
private set
|
||||
|
||||
protected var property6: String = ""
|
||||
internal set
|
||||
|
||||
protected var property7: java.util.Date = java.util.Date()
|
||||
public set
|
||||
|
||||
public var property8: Int = 1
|
||||
set
|
||||
|
||||
|
||||
-6
@@ -23,12 +23,6 @@ public final class ClassVal {
|
||||
protected final var property5: kotlin.String
|
||||
protected final fun <get-property5>(): kotlin.String
|
||||
private final fun <set-property5>(/*0*/ <set-?>: kotlin.String): kotlin.Unit
|
||||
protected final var property6: kotlin.String
|
||||
protected final fun <get-property6>(): kotlin.String
|
||||
internal final fun <set-property6>(/*0*/ <set-?>: kotlin.String): kotlin.Unit
|
||||
protected final var property7: java.util.Date
|
||||
protected final fun <get-property7>(): java.util.Date
|
||||
public final fun <set-property7>(/*0*/ <set-?>: java.util.Date): kotlin.Unit
|
||||
public final var property8: kotlin.Int
|
||||
public final fun <get-property8>(): kotlin.Int
|
||||
public final fun <set-property8>(/*0*/ <set-?>: kotlin.Int): kotlin.Unit
|
||||
|
||||
@@ -595,6 +595,12 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTest {
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("SetterVisibility.kt")
|
||||
public void testSetterVisibility() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/SetterVisibility.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("ShiftFunctionTypes.kt")
|
||||
public void testShiftFunctionTypes() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/ShiftFunctionTypes.kt");
|
||||
|
||||
@@ -115,6 +115,7 @@ public class QuickFixRegistrar : QuickFixContributor {
|
||||
val removeModifierFactory = RemoveModifierFix.createRemoveModifierFactory()
|
||||
GETTER_VISIBILITY_DIFFERS_FROM_PROPERTY_VISIBILITY.registerFactory(removeModifierFactory)
|
||||
SETTER_VISIBILITY_DIFFERS_FROM_LATEINIT_VISIBILITY.registerFactory(removeModifierFactory)
|
||||
SETTER_VISIBILITY_INCONSISTENT_WITH_PROPERTY_VISIBILITY.registerFactory(removeModifierFactory)
|
||||
PRIVATE_SETTER_FOR_ABSTRACT_PROPERTY.registerFactory(removeModifierFactory)
|
||||
PRIVATE_SETTER_FOR_OPEN_PROPERTY.registerFactory(removeModifierFactory)
|
||||
REDUNDANT_MODIFIER_IN_GETTER.registerFactory(removeRedundantModifierFactory)
|
||||
|
||||
Reference in New Issue
Block a user