KT-12846 Forbid vararg of type 'Nothing' and 'Nothing?'
This commit is contained in:
@@ -500,6 +500,8 @@ public interface Errors {
|
||||
|
||||
DiagnosticFactory0<KtParameter> MULTIPLE_VARARG_PARAMETERS = DiagnosticFactory0.create(ERROR, PARAMETER_VARARG_MODIFIER);
|
||||
|
||||
DiagnosticFactory1<KtParameter, KotlinType> FORBIDDEN_VARARG_PARAMETER_TYPE = DiagnosticFactory1.create(ERROR, PARAMETER_VARARG_MODIFIER);
|
||||
|
||||
// Named parameters
|
||||
|
||||
DiagnosticFactory0<KtParameter> DEFAULT_VALUE_NOT_ALLOWED_IN_OVERRIDE = DiagnosticFactory0.create(ERROR, PARAMETER_DEFAULT_VALUE);
|
||||
|
||||
+1
@@ -267,6 +267,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(FORBIDDEN_VARARG_PARAMETER_TYPE, "Forbidden vararg parameter type: {0}", RENDER_TYPE);
|
||||
|
||||
MAP.put(HEADER_DECLARATION_WITH_BODY, "Header declaration must not have a body");
|
||||
MAP.put(HEADER_DECLARATION_WITH_DEFAULT_PARAMETER, "Header declaration cannot have parameters with default values");
|
||||
|
||||
@@ -34,6 +34,7 @@ import org.jetbrains.kotlin.resolve.BindingContext.*
|
||||
import org.jetbrains.kotlin.resolve.DescriptorUtils.classCanHaveAbstractMembers
|
||||
import org.jetbrains.kotlin.resolve.DescriptorUtils.classCanHaveOpenMembers
|
||||
import org.jetbrains.kotlin.resolve.calls.results.TypeSpecificityComparator
|
||||
import org.jetbrains.kotlin.resolve.descriptorUtil.builtIns
|
||||
import org.jetbrains.kotlin.resolve.source.KotlinSourceElement
|
||||
import org.jetbrains.kotlin.types.*
|
||||
import org.jetbrains.kotlin.types.checker.KotlinTypeChecker
|
||||
@@ -891,15 +892,21 @@ class DeclarationsChecker(
|
||||
}
|
||||
|
||||
private 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))
|
||||
}
|
||||
}
|
||||
val varargParameters = callableDescriptor.valueParameters.filter { it.varargElementType != null }
|
||||
|
||||
if (varargParameters.size > 1) {
|
||||
for (parameter in varargParameters) {
|
||||
val parameterDeclaration = DescriptorToSourceUtils.descriptorToDeclaration(parameter) as? KtParameter ?: continue
|
||||
trace.report(MULTIPLE_VARARG_PARAMETERS.on(parameterDeclaration))
|
||||
}
|
||||
}
|
||||
|
||||
val nullableNothing = callableDescriptor.builtIns.nullableNothingType
|
||||
for (parameter in varargParameters) {
|
||||
val varargElementType = parameter.varargElementType!!.upperIfFlexible()
|
||||
if (KotlinTypeChecker.DEFAULT.isSubtypeOf(varargElementType, nullableNothing)) {
|
||||
val parameterDeclaration = DescriptorToSourceUtils.descriptorToDeclaration(parameter) as? KtParameter ?: continue
|
||||
trace.report(FORBIDDEN_VARARG_PARAMETER_TYPE.on(parameterDeclaration, varargElementType))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
// !DIAGNOSTICS: -UNUSED_PARAMETER
|
||||
|
||||
fun testVarargOfNothing(<!FORBIDDEN_VARARG_PARAMETER_TYPE!>vararg<!> v: Nothing) {}
|
||||
|
||||
fun testVarargOfNNothing(<!FORBIDDEN_VARARG_PARAMETER_TYPE!>vararg<!> v: Nothing?) {}
|
||||
|
||||
fun <T : Nothing?> testVarargOfT(<!FORBIDDEN_VARARG_PARAMETER_TYPE!>vararg<!> v: T) {}
|
||||
|
||||
fun outer() {
|
||||
fun testVarargOfNothing(<!FORBIDDEN_VARARG_PARAMETER_TYPE!>vararg<!> v: Nothing) {}
|
||||
|
||||
fun testVarargOfNNothing(<!FORBIDDEN_VARARG_PARAMETER_TYPE!>vararg<!> v: Nothing?) {}
|
||||
|
||||
fun <T : Nothing?> testVarargOfT(<!FORBIDDEN_VARARG_PARAMETER_TYPE!>vararg<!> v: T) {}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package
|
||||
|
||||
public fun outer(): kotlin.Unit
|
||||
public fun testVarargOfNNothing(/*0*/ vararg v: kotlin.Nothing? /*kotlin.Array<out kotlin.Nothing?>*/): kotlin.Unit
|
||||
public fun testVarargOfNothing(/*0*/ vararg v: kotlin.Nothing /*kotlin.Array<out kotlin.Nothing>*/): kotlin.Unit
|
||||
public fun </*0*/ T : kotlin.Nothing?> testVarargOfT(/*0*/ vararg v: T /*kotlin.Array<out T>*/): kotlin.Unit
|
||||
@@ -22627,6 +22627,12 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTest {
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("varargOfNothing.kt")
|
||||
public void testVarargOfNothing() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/varargs/varargOfNothing.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("varargsAndFunctionLiterals.kt")
|
||||
public void testVarargsAndFunctionLiterals() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/varargs/varargsAndFunctionLiterals.kt");
|
||||
|
||||
Reference in New Issue
Block a user