Prohibit functions (and constructors) with multiple vararg parameters.

This commit is contained in:
Dmitry Petrov
2015-12-18 19:50:21 +03:00
parent ed2ad8e8e3
commit 142e85be04
11 changed files with 111 additions and 20 deletions
@@ -420,6 +420,8 @@ public interface Errors {
DiagnosticFactory0<KtParameter> USELESS_VARARG_ON_PARAMETER = DiagnosticFactory0.create(WARNING);
DiagnosticFactory0<KtParameter> MULTIPLE_VARARG_PARAMETERS = DiagnosticFactory0.create(ERROR, PARAMETER_VARARG_MODIFIER);
// Named parameters
DiagnosticFactory0<KtParameter> DEFAULT_VALUE_NOT_ALLOWED_IN_OVERRIDE = DiagnosticFactory0.create(ERROR, PARAMETER_DEFAULT_VALUE);
@@ -285,6 +285,13 @@ public object PositioningStrategies {
}
}
@JvmField public val PARAMETER_VARARG_MODIFIER: PositioningStrategy<KtParameter> = object : PositioningStrategy<KtParameter>() {
override fun mark(element: KtParameter): List<TextRange> {
val varargModifier = element.modifierList!!.getModifier(KtTokens.VARARG_KEYWORD)!!
return markNode(varargModifier.node)
}
}
@JvmField public val CALL_ELEMENT: PositioningStrategy<PsiElement> = object : PositioningStrategy<PsiElement>() {
override fun mark(element: PsiElement): List<TextRange> {
return markElement((element as? KtCallElement)?.getCalleeExpression() ?: element)
@@ -234,6 +234,7 @@ public class DefaultErrorMessages {
MAP.put(ANONYMOUS_FUNCTION_PARAMETER_WITH_DEFAULT_VALUE, "An anonymous function is not allowed to specify default values for its parameters");
MAP.put(USELESS_VARARG_ON_PARAMETER, "Vararg on this parameter is useless");
MAP.put(MULTIPLE_VARARG_PARAMETERS, "Multiple vararg-parameters are prohibited");
MAP.put(PROJECTION_ON_NON_CLASS_TYPE_ARGUMENT, "Projections are not allowed on type arguments of functions and properties");
MAP.put(SUPERTYPE_NOT_INITIALIZED, "This type has a constructor, and thus must be initialized here");
@@ -130,6 +130,7 @@ class DeclarationsChecker(
declaration.checkTypeReferences()
modifiersChecker.checkModifiersForDeclaration(declaration, constructorDescriptor)
identifierChecker.checkDeclaration(declaration, trace)
checkVarargParameters(trace, constructorDescriptor)
}
private fun checkModifiersAndAnnotationsInPackageDirective(file: KtFile) {
@@ -323,11 +324,13 @@ class DeclarationsChecker(
else if (aClass is KtEnumEntry) {
checkEnumEntry(aClass, classDescriptor)
}
for (memberDescriptor in classDescriptor.declaredCallableMembers) {
if (memberDescriptor.kind != CallableMemberDescriptor.Kind.DECLARATION) continue
val member = DescriptorToSourceUtils.descriptorToDeclaration(memberDescriptor) as? KtFunction
if (member != null && memberDescriptor is FunctionDescriptor) {
checkFunctionExposedType(member, memberDescriptor)
checkVarargParameters(trace, memberDescriptor)
}
}
}
@@ -650,7 +653,9 @@ class DeclarationsChecker(
trace.report(IMPLICIT_NOTHING_RETURN_TYPE.on(nameIdentifier ?: function))
}
}
checkFunctionExposedType(function, functionDescriptor)
checkVarargParameters(trace, functionDescriptor)
}
private fun checkFunctionExposedType(function: KtFunction, functionDescriptor: FunctionDescriptor) {
@@ -740,6 +745,19 @@ class DeclarationsChecker(
}
companion object {
internal fun checkVarargParameters(trace: BindingTrace, callableDescriptor: CallableDescriptor) {
val numberOfVarargParameters = callableDescriptor.valueParameters.count { it.varargElementType != null }
if (numberOfVarargParameters > 1) {
for (parameter in callableDescriptor.valueParameters) {
if (parameter.varargElementType != null) {
val parameterDeclaration = DescriptorToSourceUtils.descriptorToDeclaration(parameter)
if (parameterDeclaration is KtParameter) {
trace.report(MULTIPLE_VARARG_PARAMETERS.on(parameterDeclaration))
}
}
}
}
}
private fun removeDuplicateTypes(conflictingTypes: MutableSet<KotlinType>) {
val iterator = conflictingTypes.iterator()
@@ -109,6 +109,7 @@ internal class FunctionsTypingVisitor(facade: ExpressionTypingInternals) : Expre
if (!function.hasBody() && !function.hasModifier(KtTokens.EXTERNAL_KEYWORD)) {
context.trace.report(NON_MEMBER_FUNCTION_NO_BODY.on(function, functionDescriptor))
}
DeclarationsChecker.checkVarargParameters(context.trace, functionDescriptor)
if (isStatement) {
return createTypeInfo(components.dataFlowAnalyzer.checkStatementType(function, context), context)
@@ -33,11 +33,6 @@ annotation class Ann8(val p1: Array<String>,
val p3: Array<MyEnum>,
val p4: Array<Ann1>)
annotation class Ann9(vararg val p1: String,
vararg val p2: <!INVALID_TYPE_OF_ANNOTATION_MEMBER!>Class<*><!>,
vararg val p3: MyEnum,
vararg val p4: Ann1,
vararg val p5: Int)
// INCORRECT
annotation class InAnn1(val p1: <!NULLABLE_TYPE_OF_ANNOTATION_MEMBER!>Int?<!>,
@@ -64,6 +59,11 @@ annotation class InAnn10(val p1: <!NULLABLE_TYPE_OF_ANNOTATION_MEMBER!>String?<!
annotation class InAnn11(val p1: <!NULLABLE_TYPE_OF_ANNOTATION_MEMBER!>Ann1?<!>)
annotation class InAnn12(val p1: <!NULLABLE_TYPE_OF_ANNOTATION_MEMBER!>MyEnum?<!>)
annotation class InAnn13(<!MULTIPLE_VARARG_PARAMETERS!>vararg<!> val p1: String,
<!MULTIPLE_VARARG_PARAMETERS!>vararg<!> val p2: <!INVALID_TYPE_OF_ANNOTATION_MEMBER!>Class<*><!>,
<!MULTIPLE_VARARG_PARAMETERS!>vararg<!> val p3: MyEnum,
<!MULTIPLE_VARARG_PARAMETERS!>vararg<!> val p4: Ann1,
<!MULTIPLE_VARARG_PARAMETERS!>vararg<!> val p5: Int)
enum class MyEnum {
A
@@ -83,18 +83,6 @@ package test {
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
public final annotation class Ann9 : kotlin.Annotation {
public constructor Ann9(/*0*/ vararg p1: kotlin.String /*kotlin.Array<out kotlin.String>*/, /*1*/ vararg p2: java.lang.Class<*> /*kotlin.Array<out java.lang.Class<*>>*/, /*2*/ vararg p3: test.MyEnum /*kotlin.Array<out test.MyEnum>*/, /*3*/ vararg p4: test.Ann1 /*kotlin.Array<out test.Ann1>*/, /*4*/ vararg p5: kotlin.Int /*kotlin.IntArray*/)
public final val p1: kotlin.Array<out kotlin.String>
public final val p2: kotlin.Array<out java.lang.Class<*>>
public final val p3: kotlin.Array<out test.MyEnum>
public final val p4: kotlin.Array<out test.Ann1>
public final val p5: kotlin.IntArray
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
}
public final annotation class InAnn1 : kotlin.Annotation {
public constructor InAnn1(/*0*/ p1: kotlin.Int?, /*1*/ p3: kotlin.Short?, /*2*/ p4: kotlin.Long?, /*3*/ p5: kotlin.Double?, /*4*/ p6: kotlin.Float?, /*5*/ p7: kotlin.Char?, /*6*/ p8: kotlin.Boolean?)
public final val p1: kotlin.Int?
@@ -133,6 +121,18 @@ package test {
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
public final annotation class InAnn13 : kotlin.Annotation {
public constructor InAnn13(/*0*/ vararg p1: kotlin.String /*kotlin.Array<out kotlin.String>*/, /*1*/ vararg p2: java.lang.Class<*> /*kotlin.Array<out java.lang.Class<*>>*/, /*2*/ vararg p3: test.MyEnum /*kotlin.Array<out test.MyEnum>*/, /*3*/ vararg p4: test.Ann1 /*kotlin.Array<out test.Ann1>*/, /*4*/ vararg p5: kotlin.Int /*kotlin.IntArray*/)
public final val p1: kotlin.Array<out kotlin.String>
public final val p2: kotlin.Array<out java.lang.Class<*>>
public final val p3: kotlin.Array<out test.MyEnum>
public final val p4: kotlin.Array<out test.Ann1>
public final val p5: kotlin.IntArray
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
}
public final annotation class InAnn4 : kotlin.Annotation {
public constructor InAnn4(/*0*/ p1: kotlin.Array<kotlin.Int>, /*1*/ p2: kotlin.Array<kotlin.Int>?)
public final val p1: kotlin.Array<kotlin.Int>
@@ -0,0 +1,27 @@
// !DIAGNOSTICS: -UNUSED_PARAMETER -PRIMARY_CONSTRUCTOR_DELEGATION_CALL_EXPECTED
fun test(<!MULTIPLE_VARARG_PARAMETERS!>vararg<!> x1: Int, <!MULTIPLE_VARARG_PARAMETERS!>vararg<!> x2: Int) {
fun test2(<!MULTIPLE_VARARG_PARAMETERS!>vararg<!> x1: Int, <!MULTIPLE_VARARG_PARAMETERS!>vararg<!> x2: Int) {
class LocalClass(<!MULTIPLE_VARARG_PARAMETERS!>vararg<!> x1: Int, <!MULTIPLE_VARARG_PARAMETERS!>vararg<!> x2: Int) {
constructor(<!MULTIPLE_VARARG_PARAMETERS!>vararg<!> x1: Int, <!MULTIPLE_VARARG_PARAMETERS!>vararg<!> x2: Int, xx: Int) {}
}
fun test3(<!MULTIPLE_VARARG_PARAMETERS!>vararg<!> x1: Int, <!MULTIPLE_VARARG_PARAMETERS!>vararg<!> x2: Int) {}
}
}
fun Any.test(<!MULTIPLE_VARARG_PARAMETERS!>vararg<!> x1: Int, <!MULTIPLE_VARARG_PARAMETERS!>vararg<!> x2: Int, <!MULTIPLE_VARARG_PARAMETERS!>vararg<!> x3: Int) {}
interface I {
fun test(<!MULTIPLE_VARARG_PARAMETERS!>vararg<!> x1: Int, <!MULTIPLE_VARARG_PARAMETERS!>vararg<!> x2: Int)
}
abstract class C(<!MULTIPLE_VARARG_PARAMETERS!>vararg<!> x1: Int, <!MULTIPLE_VARARG_PARAMETERS!>vararg<!> x2: Int, b: Boolean) {
fun test(<!MULTIPLE_VARARG_PARAMETERS!>vararg<!> x1: Int, <!MULTIPLE_VARARG_PARAMETERS!>vararg<!> x2: Int) {}
abstract fun test2(<!MULTIPLE_VARARG_PARAMETERS!>vararg<!> x1: Int, <!MULTIPLE_VARARG_PARAMETERS!>vararg<!> x2: Int)
class CC(<!MULTIPLE_VARARG_PARAMETERS!>vararg<!> x1: Int, <!MULTIPLE_VARARG_PARAMETERS!>vararg<!> x2: Int, b: Boolean) {
constructor(<!MULTIPLE_VARARG_PARAMETERS!>vararg<!> x1: Int, <!MULTIPLE_VARARG_PARAMETERS!>vararg<!> x2: Int) {}
fun test(<!MULTIPLE_VARARG_PARAMETERS!>vararg<!> x1: Int, <!MULTIPLE_VARARG_PARAMETERS!>vararg<!> x2: Int) {}
}
}
@@ -0,0 +1,29 @@
package
public fun test(/*0*/ vararg x1: kotlin.Int /*kotlin.IntArray*/, /*1*/ vararg x2: kotlin.Int /*kotlin.IntArray*/): kotlin.Unit
public fun kotlin.Any.test(/*0*/ vararg x1: kotlin.Int /*kotlin.IntArray*/, /*1*/ vararg x2: kotlin.Int /*kotlin.IntArray*/, /*2*/ vararg x3: kotlin.Int /*kotlin.IntArray*/): kotlin.Unit
public abstract class C {
public constructor C(/*0*/ vararg x1: kotlin.Int /*kotlin.IntArray*/, /*1*/ vararg x2: kotlin.Int /*kotlin.IntArray*/, /*2*/ b: kotlin.Boolean)
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 final fun test(/*0*/ vararg x1: kotlin.Int /*kotlin.IntArray*/, /*1*/ vararg x2: kotlin.Int /*kotlin.IntArray*/): kotlin.Unit
public abstract fun test2(/*0*/ vararg x1: kotlin.Int /*kotlin.IntArray*/, /*1*/ vararg x2: kotlin.Int /*kotlin.IntArray*/): kotlin.Unit
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
public final class CC {
public constructor CC(/*0*/ vararg x1: kotlin.Int /*kotlin.IntArray*/, /*1*/ vararg x2: kotlin.Int /*kotlin.IntArray*/)
public constructor CC(/*0*/ vararg x1: kotlin.Int /*kotlin.IntArray*/, /*1*/ vararg x2: kotlin.Int /*kotlin.IntArray*/, /*2*/ b: kotlin.Boolean)
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 final fun test(/*0*/ vararg x1: kotlin.Int /*kotlin.IntArray*/, /*1*/ vararg x2: kotlin.Int /*kotlin.IntArray*/): kotlin.Unit
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
}
public interface I {
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 abstract fun test(/*0*/ vararg x1: kotlin.Int /*kotlin.IntArray*/, /*1*/ vararg x2: kotlin.Int /*kotlin.IntArray*/): kotlin.Unit
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
@@ -2,9 +2,9 @@ fun f(
<!VAL_OR_VAR_ON_FUN_PARAMETER!>val<!> a: Int,
<!VAL_OR_VAR_ON_FUN_PARAMETER!>var<!> b: Int,
c: Int,
vararg <!VAL_OR_VAR_ON_FUN_PARAMETER!>var<!> d: Int,
vararg <!VAL_OR_VAR_ON_FUN_PARAMETER!>val<!> e: Int,
vararg f: Int
<!MULTIPLE_VARARG_PARAMETERS!>vararg<!> <!VAL_OR_VAR_ON_FUN_PARAMETER!>var<!> d: Int,
<!MULTIPLE_VARARG_PARAMETERS!>vararg<!> <!VAL_OR_VAR_ON_FUN_PARAMETER!>val<!> e: Int,
<!MULTIPLE_VARARG_PARAMETERS!>vararg<!> f: Int
) {
@@ -4224,6 +4224,12 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTest {
doTest(fileName);
}
@TestMetadata("mulitpleVarargParameters.kt")
public void testMulitpleVarargParameters() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/declarationChecks/mulitpleVarargParameters.kt");
doTest(fileName);
}
@TestMetadata("MultiDeclarationErrors.kt")
public void testMultiDeclarationErrors() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/declarationChecks/MultiDeclarationErrors.kt");