Fix quickFix for PROPERTY_TYPE_MISMATCH_ON_OVERRIDE
This commit is contained in:
@@ -106,9 +106,6 @@ public class ChangeVariableTypeFix extends JetIntentionAction<JetVariableDeclara
|
||||
if (property != null) {
|
||||
BindingContext context = KotlinCacheManagerUtil.getDeclarationsBindingContext(property);
|
||||
JetType lowerBoundOfOverriddenPropertiesTypes = QuickFixUtil.findLowerBoundOfOverriddenCallablesReturnTypes(context, property);
|
||||
if (lowerBoundOfOverriddenPropertiesTypes != null) {
|
||||
actions.add(new ChangeVariableTypeFix(property, lowerBoundOfOverriddenPropertiesTypes));
|
||||
}
|
||||
|
||||
PropertyDescriptor descriptor = (PropertyDescriptor) context.get(BindingContext.DECLARATION_TO_DESCRIPTOR, property);
|
||||
assert descriptor != null : "Descriptor of property not available in binding context";
|
||||
@@ -116,18 +113,31 @@ public class ChangeVariableTypeFix extends JetIntentionAction<JetVariableDeclara
|
||||
assert propertyType != null : "Property type cannot be null if it mismatch something";
|
||||
|
||||
List<PropertyDescriptor> overriddenMismatchingProperties = new LinkedList<PropertyDescriptor>();
|
||||
boolean canChangeOverriddenPropertyType = true;
|
||||
for (PropertyDescriptor overriddenProperty: descriptor.getOverriddenDescriptors()) {
|
||||
JetType overriddenPropertyType = overriddenProperty.getReturnType();
|
||||
if (overriddenPropertyType != null && !JetTypeChecker.INSTANCE.isSubtypeOf(propertyType, overriddenPropertyType)) {
|
||||
overriddenMismatchingProperties.add(overriddenProperty);
|
||||
if (overriddenPropertyType != null) {
|
||||
if (!JetTypeChecker.INSTANCE.isSubtypeOf(propertyType, overriddenPropertyType)) {
|
||||
overriddenMismatchingProperties.add(overriddenProperty);
|
||||
}
|
||||
else if (overriddenProperty.isVar() && !JetTypeChecker.INSTANCE.equalTypes(overriddenPropertyType, propertyType)) {
|
||||
canChangeOverriddenPropertyType = false;
|
||||
}
|
||||
if (overriddenProperty.isVar() && lowerBoundOfOverriddenPropertiesTypes != null &&
|
||||
!JetTypeChecker.INSTANCE.equalTypes(lowerBoundOfOverriddenPropertiesTypes, overriddenPropertyType)) {
|
||||
lowerBoundOfOverriddenPropertiesTypes = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (overriddenMismatchingProperties.size() == 1) {
|
||||
JetProperty overriddenProperty =
|
||||
(JetProperty) BindingContextUtils.descriptorToDeclaration(context, overriddenMismatchingProperties.get(0));
|
||||
if (overriddenProperty != null) {
|
||||
actions.add(new ChangeVariableTypeFix(overriddenProperty, propertyType));
|
||||
if (lowerBoundOfOverriddenPropertiesTypes != null) {
|
||||
actions.add(new ChangeVariableTypeFix(property, lowerBoundOfOverriddenPropertiesTypes));
|
||||
}
|
||||
|
||||
if (overriddenMismatchingProperties.size() == 1 && canChangeOverriddenPropertyType) {
|
||||
PsiElement overriddenProperty = BindingContextUtils.descriptorToDeclaration(context, overriddenMismatchingProperties.get(0));
|
||||
if (overriddenProperty instanceof JetProperty) {
|
||||
actions.add(new ChangeVariableTypeFix((JetProperty) overriddenProperty, propertyType));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
// "Change 'A.x' type to 'String'" "true"
|
||||
trait A {
|
||||
var x: String
|
||||
}
|
||||
|
||||
trait B {
|
||||
var x: String
|
||||
}
|
||||
|
||||
trait C : A, B {
|
||||
override var x: String<caret>
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
// "Change 'A.x' type to 'String'" "false"
|
||||
// ERROR: <html>Var-property type is 'jet.String', which is not a type of overridden<br/><b>internal</b> <b>abstract</b> <b>var</b> x: jet.Int <i>defined in</i> A</html>
|
||||
trait A {
|
||||
var x: Int
|
||||
}
|
||||
|
||||
trait B {
|
||||
var x: Any
|
||||
}
|
||||
|
||||
trait C : A, B {
|
||||
override var x: String<caret>
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
// "Change 'C.x' type to 'String'" "false"
|
||||
// ERROR: <html>Var-property type is 'jet.Int', which is not a type of overridden<br/><b>internal</b> <b>abstract</b> <b>var</b> x: jet.String <i>defined in</i> A</html>
|
||||
trait A {
|
||||
var x: String
|
||||
}
|
||||
|
||||
trait B {
|
||||
var x: Any
|
||||
}
|
||||
|
||||
trait C : A, B {
|
||||
override var x: Int<caret>
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
// "Change 'A.x' type to 'String'" "true"
|
||||
trait A {
|
||||
var x: Int
|
||||
}
|
||||
|
||||
trait B {
|
||||
var x: String
|
||||
}
|
||||
|
||||
trait C : A, B {
|
||||
override var x: String<caret>
|
||||
}
|
||||
@@ -1023,9 +1023,24 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
|
||||
doTest("idea/testData/quickfix/override/typeMismatchOnOverride/beforeCantChangeMultipleOverriddenPropertiesTypes.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("beforeChangeOverriddenPropertyType.kt")
|
||||
public void testChangeOverriddenPropertyType() throws Exception {
|
||||
doTest("idea/testData/quickfix/override/typeMismatchOnOverride/beforeChangeOverriddenPropertyType.kt");
|
||||
@TestMetadata("beforeCantChangeOverriddenPropertyTypeToMatchOverridingProperty.kt")
|
||||
public void testCantChangeOverriddenPropertyTypeToMatchOverridingProperty() throws Exception {
|
||||
doTest("idea/testData/quickfix/override/typeMismatchOnOverride/beforeCantChangeOverriddenPropertyTypeToMatchOverridingProperty.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("beforeCantChangePropertyTypeToMatchOverridenProperties.kt")
|
||||
public void testCantChangePropertyTypeToMatchOverridenProperties() throws Exception {
|
||||
doTest("idea/testData/quickfix/override/typeMismatchOnOverride/beforeCantChangePropertyTypeToMatchOverridenProperties.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("beforeChangeOverriddenPropertyType1.kt")
|
||||
public void testChangeOverriddenPropertyType1() throws Exception {
|
||||
doTest("idea/testData/quickfix/override/typeMismatchOnOverride/beforeChangeOverriddenPropertyType1.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("beforeChangeOverriddenPropertyType2.kt")
|
||||
public void testChangeOverriddenPropertyType2() throws Exception {
|
||||
doTest("idea/testData/quickfix/override/typeMismatchOnOverride/beforeChangeOverriddenPropertyType2.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("beforeChangeOverridingPropertyTypeToFunctionType.kt")
|
||||
|
||||
Reference in New Issue
Block a user