Report illegal modifiers and annotations on primary constructor

#KT-7057 Fixed
 #KT-6772 Fixed
This commit is contained in:
Denis Zharkov
2015-03-30 18:34:02 +03:00
parent 717f850470
commit 99f1ab333e
11 changed files with 82 additions and 9 deletions
@@ -108,13 +108,17 @@ public class DeclarationsChecker {
for (Map.Entry<JetSecondaryConstructor, ConstructorDescriptor> entry : bodiesResolveContext.getSecondaryConstructors().entrySet()) {
ConstructorDescriptor constructorDescriptor = entry.getValue();
JetSecondaryConstructor declaration = entry.getKey();
modifiersChecker.reportIllegalModalityModifiers(declaration);
reportErrorIfHasIllegalModifier(declaration);
modifiersChecker.checkModifiersForDeclaration(declaration, constructorDescriptor);
checkConstructorDeclaration(constructorDescriptor, declaration);
}
}
private void checkConstructorDeclaration(ConstructorDescriptor constructorDescriptor, JetDeclaration declaration) {
modifiersChecker.reportIllegalModalityModifiers(declaration);
reportErrorIfHasIllegalModifier(declaration);
modifiersChecker.checkModifiersForDeclaration(declaration, constructorDescriptor);
}
private void reportErrorIfHasIllegalModifier(JetModifierListOwner declaration) {
if (declaration.hasModifier(JetTokens.ENUM_KEYWORD)) {
trace.report(ILLEGAL_ENUM_ANNOTATION.on(declaration));
@@ -279,7 +283,7 @@ public class DeclarationsChecker {
private void checkClass(BodiesResolveContext c, JetClass aClass, ClassDescriptorWithResolutionScopes classDescriptor) {
checkOpenMembers(classDescriptor);
checkConstructorParameters(aClass);
checkPrimaryConstructor(aClass, classDescriptor);
checkTypeParameters(aClass);
if (aClass.isTrait()) {
@@ -301,13 +305,19 @@ public class DeclarationsChecker {
}
}
private void checkConstructorParameters(JetClass aClass) {
for (JetParameter parameter : aClass.getPrimaryConstructorParameters()) {
private void checkPrimaryConstructor(JetClass aClass, ClassDescriptor classDescriptor) {
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);
if (propertyDescriptor != null) {
modifiersChecker.checkModifiersForDeclaration(parameter, propertyDescriptor);
}
}
checkConstructorDeclaration(primaryConstructor, declaration);
}
private void checkTypeParameters(JetTypeParameterListOwner typeParameterListOwner) {
@@ -135,3 +135,20 @@ class IllegalModifiers9 {
<!INCOMPATIBLE_MODIFIERS!>private<!> <!INCOMPATIBLE_MODIFIERS!>protected<!> constructor() {}
<!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
}
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 {
public constructor IllegalModifiers2(/*0*/ a: kotlin.Int)
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
@@ -32,7 +32,7 @@ var vardef: Int = 1
set
[<!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("a")<!>]
fun foo() {}
@@ -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)
<!>}
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);
}
@TestMetadata("beforeRemoveAbstractModifier.kt")
public void testRemoveAbstractModifier() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/modifiers/beforeRemoveAbstractModifier.kt");
doTest(fileName);
}
@TestMetadata("beforeRemoveIncompatibleModifier.kt")
public void testRemoveIncompatibleModifier() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/modifiers/beforeRemoveIncompatibleModifier.kt");
@@ -3262,6 +3268,12 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
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")
public void testRemoveRedundantModifier1() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/modifiers/beforeRemoveRedundantModifier1.kt");