Report warning on modifier list of primary ctor unless 'constructor' keyword specified

This commit is contained in:
Denis Zharkov
2015-05-12 16:13:59 +03:00
parent 805a811d91
commit 09cae59f0a
7 changed files with 54 additions and 0 deletions
@@ -159,6 +159,8 @@ public interface Errors {
DiagnosticFactory0<JetNullableType> NULLABLE_SUPERTYPE = DiagnosticFactory0.create(ERROR, NULLABLE_TYPE);
DiagnosticFactory0<JetTypeReference> DYNAMIC_SUPERTYPE = DiagnosticFactory0.create(ERROR);
DiagnosticFactory0<JetElement> MISSING_CONSTRUCTOR_KEYWORD = DiagnosticFactory0.create(WARNING);
// Secondary constructors
DiagnosticFactory0<JetConstructorDelegationReferenceExpression> CYCLIC_CONSTRUCTOR_DELEGATION_CALL = DiagnosticFactory0.create(ERROR);
@@ -442,6 +442,8 @@ public class DefaultErrorMessages {
MAP.put(SMARTCAST_IMPOSSIBLE,
"Smart cast to ''{0}'' is impossible, because ''{1}'' could have changed since the is-check", RENDER_TYPE, STRING);
MAP.put(MISSING_CONSTRUCTOR_KEYWORD, "Use 'constructor' keyword after modifiers of primary constructor");
MAP.put(VARIANCE_ON_TYPE_PARAMETER_OF_FUNCTION_OR_PROPERTY, "Variance annotations are only allowed for type parameters of classes and interfaces");
MAP.put(TYPE_VARIANCE_CONFLICT, "Type parameter {0} is declared as ''{1}'' but occurs in ''{2}'' position in type {3}",
new MultiRenderer<VarianceConflictDiagnosticData>() {
@@ -82,4 +82,9 @@ public class JetPrimaryConstructor extends JetDeclarationStub<KotlinPlaceHolderS
assert classOrNull != null : "This method should be called when parent is JetClass";
return classOrNull;
}
public boolean hasConstructorKeyword() {
if (getStub() != null) return true;
return findChildByType(JetTokens.CONSTRUCTOR_KEYWORD) != null;
}
}
@@ -282,6 +282,10 @@ public class DeclarationsChecker {
}
}
if (declaration.getModifierList() != null && !declaration.hasConstructorKeyword()) {
trace.report(MISSING_CONSTRUCTOR_KEYWORD.on(declaration.getModifierList()));
}
checkConstructorDeclaration(primaryConstructor, declaration);
}
@@ -0,0 +1,9 @@
// !DIAGNOSTICS: -UNUSED_PARAMETER
annotation class Ann(val x: Int = 1)
class A <!MISSING_CONSTRUCTOR_KEYWORD!>private<!> (val x: Int) {
inner class B <!MISSING_CONSTRUCTOR_KEYWORD!>Ann(2)<!> (val y: Int)
fun foo() {
class C <!MISSING_CONSTRUCTOR_KEYWORD!>private @Ann(3)<!> (args: Int)
}
}
@@ -0,0 +1,26 @@
package
internal final class A {
private constructor A(/*0*/ x: kotlin.Int)
internal final val x: kotlin.Int
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
internal final fun foo(): kotlin.Unit
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
internal final inner class B {
Ann(x = IntegerValueType(2): IntegerValueType(2)) public constructor B(/*0*/ y: kotlin.Int)
internal final val y: 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
}
}
internal final annotation class Ann : kotlin.Annotation {
public constructor Ann(/*0*/ x: kotlin.Int = ...)
internal final val x: 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
}
@@ -7659,6 +7659,12 @@ public class JetDiagnosticsTestGenerated extends AbstractJetDiagnosticsTest {
doTest(fileName);
}
@TestMetadata("primaryConstructorMissingKeyword.kt")
public void testPrimaryConstructorMissingKeyword() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/modifiers/primaryConstructorMissingKeyword.kt");
doTest(fileName);
}
@TestMetadata("repeatedModifiers.kt")
public void testRepeatedModifiers() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/modifiers/repeatedModifiers.kt");