Report illegal modifiers and annotations on primary constructor
#KT-7057 Fixed #KT-6772 Fixed
This commit is contained in:
@@ -108,13 +108,17 @@ public class DeclarationsChecker {
|
|||||||
for (Map.Entry<JetSecondaryConstructor, ConstructorDescriptor> entry : bodiesResolveContext.getSecondaryConstructors().entrySet()) {
|
for (Map.Entry<JetSecondaryConstructor, ConstructorDescriptor> entry : bodiesResolveContext.getSecondaryConstructors().entrySet()) {
|
||||||
ConstructorDescriptor constructorDescriptor = entry.getValue();
|
ConstructorDescriptor constructorDescriptor = entry.getValue();
|
||||||
JetSecondaryConstructor declaration = entry.getKey();
|
JetSecondaryConstructor declaration = entry.getKey();
|
||||||
modifiersChecker.reportIllegalModalityModifiers(declaration);
|
checkConstructorDeclaration(constructorDescriptor, declaration);
|
||||||
reportErrorIfHasIllegalModifier(declaration);
|
|
||||||
modifiersChecker.checkModifiersForDeclaration(declaration, constructorDescriptor);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void checkConstructorDeclaration(ConstructorDescriptor constructorDescriptor, JetDeclaration declaration) {
|
||||||
|
modifiersChecker.reportIllegalModalityModifiers(declaration);
|
||||||
|
reportErrorIfHasIllegalModifier(declaration);
|
||||||
|
modifiersChecker.checkModifiersForDeclaration(declaration, constructorDescriptor);
|
||||||
|
}
|
||||||
|
|
||||||
private void reportErrorIfHasIllegalModifier(JetModifierListOwner declaration) {
|
private void reportErrorIfHasIllegalModifier(JetModifierListOwner declaration) {
|
||||||
if (declaration.hasModifier(JetTokens.ENUM_KEYWORD)) {
|
if (declaration.hasModifier(JetTokens.ENUM_KEYWORD)) {
|
||||||
trace.report(ILLEGAL_ENUM_ANNOTATION.on(declaration));
|
trace.report(ILLEGAL_ENUM_ANNOTATION.on(declaration));
|
||||||
@@ -279,7 +283,7 @@ public class DeclarationsChecker {
|
|||||||
|
|
||||||
private void checkClass(BodiesResolveContext c, JetClass aClass, ClassDescriptorWithResolutionScopes classDescriptor) {
|
private void checkClass(BodiesResolveContext c, JetClass aClass, ClassDescriptorWithResolutionScopes classDescriptor) {
|
||||||
checkOpenMembers(classDescriptor);
|
checkOpenMembers(classDescriptor);
|
||||||
checkConstructorParameters(aClass);
|
checkPrimaryConstructor(aClass, classDescriptor);
|
||||||
checkTypeParameters(aClass);
|
checkTypeParameters(aClass);
|
||||||
|
|
||||||
if (aClass.isTrait()) {
|
if (aClass.isTrait()) {
|
||||||
@@ -301,13 +305,19 @@ public class DeclarationsChecker {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void checkConstructorParameters(JetClass aClass) {
|
private void checkPrimaryConstructor(JetClass aClass, ClassDescriptor classDescriptor) {
|
||||||
for (JetParameter parameter : aClass.getPrimaryConstructorParameters()) {
|
ConstructorDescriptor primaryConstructor = classDescriptor.getUnsubstitutedPrimaryConstructor();
|
||||||
|
JetPrimaryConstructor declaration = aClass.getPrimaryConstructor();
|
||||||
|
if (primaryConstructor == null || declaration == null) return;
|
||||||
|
|
||||||
|
for (JetParameter parameter : declaration.getValueParameters()) {
|
||||||
PropertyDescriptor propertyDescriptor = trace.get(BindingContext.PRIMARY_CONSTRUCTOR_PARAMETER, parameter);
|
PropertyDescriptor propertyDescriptor = trace.get(BindingContext.PRIMARY_CONSTRUCTOR_PARAMETER, parameter);
|
||||||
if (propertyDescriptor != null) {
|
if (propertyDescriptor != null) {
|
||||||
modifiersChecker.checkModifiersForDeclaration(parameter, propertyDescriptor);
|
modifiersChecker.checkModifiersForDeclaration(parameter, propertyDescriptor);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
checkConstructorDeclaration(primaryConstructor, declaration);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void checkTypeParameters(JetTypeParameterListOwner typeParameterListOwner) {
|
private void checkTypeParameters(JetTypeParameterListOwner typeParameterListOwner) {
|
||||||
|
|||||||
@@ -135,3 +135,20 @@ class IllegalModifiers9 {
|
|||||||
<!INCOMPATIBLE_MODIFIERS!>private<!> <!INCOMPATIBLE_MODIFIERS!>protected<!> constructor() {}
|
<!INCOMPATIBLE_MODIFIERS!>private<!> <!INCOMPATIBLE_MODIFIERS!>protected<!> constructor() {}
|
||||||
<!INCOMPATIBLE_MODIFIERS!>private<!> <!INCOMPATIBLE_MODIFIERS!>internal<!> constructor(<!UNUSED_PARAMETER!>x<!>: Int) {}
|
<!INCOMPATIBLE_MODIFIERS!>private<!> <!INCOMPATIBLE_MODIFIERS!>internal<!> constructor(<!UNUSED_PARAMETER!>x<!>: Int) {}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Illegal modifiers on primary constructor
|
||||||
|
|
||||||
|
class IllegalModifiers10
|
||||||
|
<!ILLEGAL_MODIFIER, INCOMPATIBLE_MODIFIERS!>abstract<!>
|
||||||
|
<!ILLEGAL_ENUM_ANNOTATION!>enum<!>
|
||||||
|
<!ILLEGAL_MODIFIER, REDUNDANT_MODIFIER, REDUNDANT_MODIFIER, INCOMPATIBLE_MODIFIERS!>open<!>
|
||||||
|
<!ILLEGAL_MODIFIER!>inner<!>
|
||||||
|
<!ILLEGAL_ANNOTATION_KEYWORD!>annotation<!>
|
||||||
|
<!ILLEGAL_MODIFIER!>override<!>
|
||||||
|
<!ILLEGAL_MODIFIER!>out<!>
|
||||||
|
<!ILLEGAL_MODIFIER!>in<!>
|
||||||
|
<!ILLEGAL_MODIFIER, INCOMPATIBLE_MODIFIERS!>final<!>
|
||||||
|
<!ILLEGAL_MODIFIER!>vararg<!>
|
||||||
|
<!ILLEGAL_MODIFIER!>reified<!> ()
|
||||||
|
|
||||||
|
class IllegalModifiers11 <!INCOMPATIBLE_MODIFIERS!>private<!> <!INCOMPATIBLE_MODIFIERS!>protected<!> ()
|
||||||
|
|||||||
@@ -44,6 +44,20 @@ package illegal_modifiers {
|
|||||||
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
|
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
|
||||||
}
|
}
|
||||||
|
|
||||||
|
internal final class IllegalModifiers10 {
|
||||||
|
public constructor IllegalModifiers10()
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
internal final class IllegalModifiers11 {
|
||||||
|
private constructor IllegalModifiers11()
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
internal final class IllegalModifiers2 {
|
internal final class IllegalModifiers2 {
|
||||||
public constructor IllegalModifiers2(/*0*/ a: kotlin.Int)
|
public constructor IllegalModifiers2(/*0*/ a: kotlin.Int)
|
||||||
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
|
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
|
||||||
|
|||||||
+1
-1
@@ -32,7 +32,7 @@ var vardef: Int = 1
|
|||||||
set
|
set
|
||||||
|
|
||||||
[<!INAPPLICABLE_ANNOTATION!>platformName("C")<!>]
|
[<!INAPPLICABLE_ANNOTATION!>platformName("C")<!>]
|
||||||
class C [platformName("primary")]() { // TODO: modifiers check on primary constructor KT-7057
|
class C [<!INAPPLICABLE_ANNOTATION!>platformName("primary")<!>]() {
|
||||||
<!INAPPLICABLE_ANNOTATION!>platformName("ctr")<!> constructor(x: Int): this() {}
|
<!INAPPLICABLE_ANNOTATION!>platformName("ctr")<!> constructor(x: Int): this() {}
|
||||||
[<!INAPPLICABLE_ANNOTATION!>platformName("a")<!>]
|
[<!INAPPLICABLE_ANNOTATION!>platformName("a")<!>]
|
||||||
fun foo() {}
|
fun foo() {}
|
||||||
|
|||||||
+1
-1
@@ -6,4 +6,4 @@ class A {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class C [platformStatic] () // TODO KT-7057
|
class C <!PLATFORM_STATIC_ILLEGAL_USAGE!>[platformStatic] ()<!>
|
||||||
|
|||||||
@@ -7,4 +7,4 @@ class A {
|
|||||||
<!INAPPLICABLE_ANNOTATION!>native constructor(<!UNUSED_PARAMETER!>x<!>: Int)
|
<!INAPPLICABLE_ANNOTATION!>native constructor(<!UNUSED_PARAMETER!>x<!>: Int)
|
||||||
<!>}
|
<!>}
|
||||||
|
|
||||||
class C [native] () // TODO KT-7057
|
class C <!INAPPLICABLE_ANNOTATION!>[native]()<!>
|
||||||
|
|||||||
@@ -0,0 +1,5 @@
|
|||||||
|
// "Make 'abstract()' not abstract" "true"
|
||||||
|
|
||||||
|
class A() {
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
// "Remove 'protected' modifier" "true"
|
||||||
|
|
||||||
|
class A private () {
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
// "Make 'abstract()' not abstract" "true"
|
||||||
|
|
||||||
|
class A <caret>abstract() {
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
// "Remove 'protected' modifier" "true"
|
||||||
|
|
||||||
|
class A private <caret>protected () {
|
||||||
|
|
||||||
|
}
|
||||||
@@ -3250,6 +3250,12 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
|
|||||||
doTest(fileName);
|
doTest(fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("beforeRemoveAbstractModifier.kt")
|
||||||
|
public void testRemoveAbstractModifier() throws Exception {
|
||||||
|
String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/modifiers/beforeRemoveAbstractModifier.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("beforeRemoveIncompatibleModifier.kt")
|
@TestMetadata("beforeRemoveIncompatibleModifier.kt")
|
||||||
public void testRemoveIncompatibleModifier() throws Exception {
|
public void testRemoveIncompatibleModifier() throws Exception {
|
||||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/modifiers/beforeRemoveIncompatibleModifier.kt");
|
String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/modifiers/beforeRemoveIncompatibleModifier.kt");
|
||||||
@@ -3262,6 +3268,12 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
|
|||||||
doTest(fileName);
|
doTest(fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("beforeRemoveProtectedModifier.kt")
|
||||||
|
public void testRemoveProtectedModifier() throws Exception {
|
||||||
|
String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/modifiers/beforeRemoveProtectedModifier.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("beforeRemoveRedundantModifier1.kt")
|
@TestMetadata("beforeRemoveRedundantModifier1.kt")
|
||||||
public void testRemoveRedundantModifier1() throws Exception {
|
public void testRemoveRedundantModifier1() throws Exception {
|
||||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/modifiers/beforeRemoveRedundantModifier1.kt");
|
String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/modifiers/beforeRemoveRedundantModifier1.kt");
|
||||||
|
|||||||
Reference in New Issue
Block a user