Prohibit functional interface constructor references

#KT-36706 Fixed
This commit is contained in:
Mikhail Zarechenskiy
2020-06-26 08:08:18 +03:00
parent e3050cd997
commit bc40669598
10 changed files with 69 additions and 1 deletions
@@ -8014,6 +8014,11 @@ public class FirOldFrontendDiagnosticsTestGenerated extends AbstractFirOldFronte
runTest("compiler/testData/diagnostics/tests/funInterface/noCompatibilityResolveForFunInterfaces.kt");
}
@TestMetadata("prohibitFunInterfaceConstructorReferences.kt")
public void testProhibitFunInterfaceConstructorReferences() throws Exception {
runTest("compiler/testData/diagnostics/tests/funInterface/prohibitFunInterfaceConstructorReferences.kt");
}
@TestMetadata("resolveFunInterfaceWithoutMainMethod.kt")
public void testResolveFunInterfaceWithoutMainMethod() throws Exception {
runTest("compiler/testData/diagnostics/tests/funInterface/resolveFunInterfaceWithoutMainMethod.kt");
@@ -369,6 +369,7 @@ public interface Errors {
DiagnosticFactory0<PsiElement> FUN_INTERFACE_CANNOT_HAVE_ABSTRACT_PROPERTIES = DiagnosticFactory0.create(ERROR);
DiagnosticFactory0<PsiElement> FUN_INTERFACE_ABSTRACT_METHOD_WITH_TYPE_PARAMETERS = DiagnosticFactory0.create(ERROR);
DiagnosticFactory0<PsiElement> FUN_INTERFACE_ABSTRACT_METHOD_WITH_DEFAULT_VALUE = DiagnosticFactory0.create(ERROR);
DiagnosticFactory0<PsiElement> FUN_INTERFACE_CONSTRUCTOR_REFERENCE = DiagnosticFactory0.create(ERROR);
// Secondary constructors
@@ -709,6 +709,7 @@ public class DefaultErrorMessages {
MAP.put(FUN_INTERFACE_CANNOT_HAVE_ABSTRACT_PROPERTIES, "Fun interfaces cannot have abstract properties");
MAP.put(FUN_INTERFACE_ABSTRACT_METHOD_WITH_TYPE_PARAMETERS, "Single abstract member cannot declare type parameters");
MAP.put(FUN_INTERFACE_ABSTRACT_METHOD_WITH_DEFAULT_VALUE, "Single abstract member cannot declare default values");
MAP.put(FUN_INTERFACE_CONSTRUCTOR_REFERENCE, "Functional interface constructor references are prohibited");
MAP.put(VARIANCE_ON_TYPE_PARAMETER_NOT_ALLOWED, "Variance annotations are only allowed for type parameters of classes and interfaces");
MAP.put(BOUND_ON_TYPE_ALIAS_PARAMETER_NOT_ALLOWED, "Bounds are not allowed on type alias parameters");
@@ -50,7 +50,7 @@ private val DEFAULT_CALL_CHECKERS = listOf(
UselessElvisCallChecker(), ResultTypeWithNullableOperatorsChecker(), NullableVarargArgumentCallChecker,
NamedFunAsExpressionChecker, ContractNotAllowedCallChecker, ReifiedTypeParameterSubstitutionChecker(),
MissingDependencySupertypeChecker.ForCalls, AbstractClassInstantiationChecker, SuspendConversionCallChecker,
UnitConversionCallChecker
UnitConversionCallChecker, FunInterfaceConstructorReferenceChecker
)
private val DEFAULT_TYPE_CHECKERS = emptyList<AdditionalTypeChecker>()
private val DEFAULT_CLASSIFIER_USAGE_CHECKERS = listOf(
@@ -0,0 +1,25 @@
/*
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.resolve.calls.checkers
import com.intellij.psi.PsiElement
import org.jetbrains.kotlin.diagnostics.Errors
import org.jetbrains.kotlin.resolve.calls.callUtil.isCallableReference
import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall
import org.jetbrains.kotlin.resolve.sam.SamConstructorDescriptor
object FunInterfaceConstructorReferenceChecker : CallChecker {
override fun check(resolvedCall: ResolvedCall<*>, reportOn: PsiElement, context: CallCheckerContext) {
val resultingDescriptor = resolvedCall.resultingDescriptor
if (resultingDescriptor !is SamConstructorDescriptor || !resolvedCall.call.isCallableReference()) return
if (resultingDescriptor.baseDescriptorForSynthetic.isFun) {
context.trace.report(
Errors.FUN_INTERFACE_CONSTRUCTOR_REFERENCE.on(reportOn)
)
}
}
}
@@ -0,0 +1,7 @@
fun interface Foo {
fun run()
}
val x = ::Foo
val y = Foo { }
val z = ::Runnable
@@ -0,0 +1,7 @@
fun interface Foo {
fun run()
}
val x = ::<!FUN_INTERFACE_CONSTRUCTOR_REFERENCE!>Foo<!>
val y = Foo { }
val z = ::Runnable
@@ -0,0 +1,12 @@
package
public val x: kotlin.reflect.KFunction1<() -> kotlin.Unit, Foo>
public val y: Foo
public val z: kotlin.reflect.KFunction1<() -> kotlin.Unit, java.lang.Runnable>
public fun interface Foo {
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 run(): kotlin.Unit
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
@@ -8021,6 +8021,11 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTestWithFirVali
runTest("compiler/testData/diagnostics/tests/funInterface/noCompatibilityResolveForFunInterfaces.kt");
}
@TestMetadata("prohibitFunInterfaceConstructorReferences.kt")
public void testProhibitFunInterfaceConstructorReferences() throws Exception {
runTest("compiler/testData/diagnostics/tests/funInterface/prohibitFunInterfaceConstructorReferences.kt");
}
@TestMetadata("resolveFunInterfaceWithoutMainMethod.kt")
public void testResolveFunInterfaceWithoutMainMethod() throws Exception {
runTest("compiler/testData/diagnostics/tests/funInterface/resolveFunInterfaceWithoutMainMethod.kt");
@@ -8016,6 +8016,11 @@ public class DiagnosticsUsingJavacTestGenerated extends AbstractDiagnosticsUsing
runTest("compiler/testData/diagnostics/tests/funInterface/noCompatibilityResolveForFunInterfaces.kt");
}
@TestMetadata("prohibitFunInterfaceConstructorReferences.kt")
public void testProhibitFunInterfaceConstructorReferences() throws Exception {
runTest("compiler/testData/diagnostics/tests/funInterface/prohibitFunInterfaceConstructorReferences.kt");
}
@TestMetadata("resolveFunInterfaceWithoutMainMethod.kt")
public void testResolveFunInterfaceWithoutMainMethod() throws Exception {
runTest("compiler/testData/diagnostics/tests/funInterface/resolveFunInterfaceWithoutMainMethod.kt");