diff --git a/compiler/fir/analysis-tests/tests/org/jetbrains/kotlin/fir/FirOldFrontendDiagnosticsTestGenerated.java b/compiler/fir/analysis-tests/tests/org/jetbrains/kotlin/fir/FirOldFrontendDiagnosticsTestGenerated.java index 7d903835c63..c56c9ab9e59 100644 --- a/compiler/fir/analysis-tests/tests/org/jetbrains/kotlin/fir/FirOldFrontendDiagnosticsTestGenerated.java +++ b/compiler/fir/analysis-tests/tests/org/jetbrains/kotlin/fir/FirOldFrontendDiagnosticsTestGenerated.java @@ -1884,6 +1884,11 @@ public class FirOldFrontendDiagnosticsTestGenerated extends AbstractFirOldFronte runTest("compiler/testData/diagnostics/tests/callableReference/kt35959.kt"); } + @TestMetadata("kt37530.kt") + public void testKt37530() throws Exception { + runTest("compiler/testData/diagnostics/tests/callableReference/kt37530.kt"); + } + @TestMetadata("kt7430_wrongClassOnLHS.kt") public void testKt7430_wrongClassOnLHS() throws Exception { runTest("compiler/testData/diagnostics/tests/callableReference/kt7430_wrongClassOnLHS.kt"); @@ -15860,6 +15865,11 @@ public class FirOldFrontendDiagnosticsTestGenerated extends AbstractFirOldFronte runTest("compiler/testData/diagnostics/tests/overload/defaultParameters.kt"); } + @TestMetadata("disambiguateByFailedAbstractClassCheck.kt") + public void testDisambiguateByFailedAbstractClassCheck() throws Exception { + runTest("compiler/testData/diagnostics/tests/overload/disambiguateByFailedAbstractClassCheck.kt"); + } + @TestMetadata("EmptyArgumentListInLambda.kt") public void testEmptyArgumentListInLambda() throws Exception { runTest("compiler/testData/diagnostics/tests/overload/EmptyArgumentListInLambda.kt"); diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/PlatformConfiguratorBase.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/PlatformConfiguratorBase.kt index 6293c9cbbc1..f31fdad063a 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/PlatformConfiguratorBase.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/PlatformConfiguratorBase.kt @@ -49,7 +49,7 @@ private val DEFAULT_CALL_CHECKERS = listOf( PrimitiveNumericComparisonCallChecker, LambdaWithSuspendModifierCallChecker, UselessElvisCallChecker(), ResultTypeWithNullableOperatorsChecker(), NullableVarargArgumentCallChecker, NamedFunAsExpressionChecker, ContractNotAllowedCallChecker, ReifiedTypeParameterSubstitutionChecker(), TypeOfChecker, - MissingDependencySupertypeChecker.ForCalls + MissingDependencySupertypeChecker.ForCalls, AbstractClassInstantiationChecker, ) private val DEFAULT_TYPE_CHECKERS = emptyList() private val DEFAULT_CLASSIFIER_USAGE_CHECKERS = listOf( diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/checkers/AbstractClassInstantiationChecker.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/checkers/AbstractClassInstantiationChecker.kt new file mode 100644 index 00000000000..a01a6736dfa --- /dev/null +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/checkers/AbstractClassInstantiationChecker.kt @@ -0,0 +1,29 @@ +/* + * 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.descriptors.ConstructorDescriptor +import org.jetbrains.kotlin.descriptors.Modality +import org.jetbrains.kotlin.diagnostics.Errors.CREATING_AN_INSTANCE_OF_ABSTRACT_CLASS +import org.jetbrains.kotlin.diagnostics.reportDiagnosticOnce +import org.jetbrains.kotlin.resolve.calls.callResolverUtil.isSuperOrDelegatingConstructorCall +import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall + +object AbstractClassInstantiationChecker : CallChecker { + override fun check(resolvedCall: ResolvedCall<*>, reportOn: PsiElement, context: CallCheckerContext) { + val candidateDescriptor = resolvedCall.candidateDescriptor + val call = resolvedCall.call + + if (candidateDescriptor is ConstructorDescriptor && + !isSuperOrDelegatingConstructorCall(call) + ) { + if (candidateDescriptor.constructedClass.modality == Modality.ABSTRACT) { + context.trace.reportDiagnosticOnce(CREATING_AN_INSTANCE_OF_ABSTRACT_CLASS.on(call.callElement)) + } + } + } +} \ No newline at end of file diff --git a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/components/ResolutionParts.kt b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/components/ResolutionParts.kt index d5a94044b95..a34d91c6bb8 100644 --- a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/components/ResolutionParts.kt +++ b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/components/ResolutionParts.kt @@ -12,7 +12,6 @@ import org.jetbrains.kotlin.config.LanguageFeature import org.jetbrains.kotlin.descriptors.* import org.jetbrains.kotlin.descriptors.impl.TypeAliasConstructorDescriptor import org.jetbrains.kotlin.incremental.record -import org.jetbrains.kotlin.resolve.calls.components.CollectionTypeVariableUsagesInfo.getTypeParameterByVariable import org.jetbrains.kotlin.resolve.calls.components.TypeArgumentsToParametersMapper.TypeArgumentsMapping.NoExplicitArguments import org.jetbrains.kotlin.resolve.calls.inference.ConstraintSystemOperation import org.jetbrains.kotlin.resolve.calls.inference.NewConstraintSystem @@ -36,20 +35,6 @@ import org.jetbrains.kotlin.types.typeUtil.makeNotNullable import org.jetbrains.kotlin.types.typeUtil.makeNullable import org.jetbrains.kotlin.utils.addToStdlib.safeAs -internal object CheckInstantiationOfAbstractClass : ResolutionPart() { - override fun KotlinResolutionCandidate.process(workIndex: Int) { - val candidateDescriptor = resolvedCall.candidateDescriptor - - if (candidateDescriptor is ConstructorDescriptor && - !callComponents.statelessCallbacks.isSuperOrDelegatingConstructorCall(resolvedCall.atom) - ) { - if (candidateDescriptor.constructedClass.modality == Modality.ABSTRACT) { - addDiagnostic(InstantiationOfAbstractClass) - } - } - } -} - internal object CheckVisibility : ResolutionPart() { override fun KotlinResolutionCandidate.process(workIndex: Int) { val containingDescriptor = scopeTower.lexicalScope.ownerDescriptor diff --git a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/model/KotlinResolverContext.kt b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/model/KotlinResolverContext.kt index bf1ca960c1c..da81e6be649 100644 --- a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/model/KotlinResolverContext.kt +++ b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/model/KotlinResolverContext.kt @@ -212,7 +212,6 @@ enum class KotlinCallKind(vararg resolutionPart: ResolutionPart) { PostponedVariablesInitializerResolutionPart ), FUNCTION( - CheckInstantiationOfAbstractClass, CheckVisibility, CheckInfixResolutionPart, CheckOperatorResolutionPart, diff --git a/compiler/testData/diagnostics/tests/callableReference/kt37530.fir.kt b/compiler/testData/diagnostics/tests/callableReference/kt37530.fir.kt new file mode 100644 index 00000000000..2b262f583e1 --- /dev/null +++ b/compiler/testData/diagnostics/tests/callableReference/kt37530.fir.kt @@ -0,0 +1,11 @@ +// !WITH_NEW_INFERENCE + +abstract class Abstract + +fun create(fn: () -> D): D { + return fn() +} + +fun main() { + create(::Abstract) +} diff --git a/compiler/testData/diagnostics/tests/callableReference/kt37530.kt b/compiler/testData/diagnostics/tests/callableReference/kt37530.kt new file mode 100644 index 00000000000..1a1b22881f0 --- /dev/null +++ b/compiler/testData/diagnostics/tests/callableReference/kt37530.kt @@ -0,0 +1,11 @@ +// !WITH_NEW_INFERENCE + +abstract class Abstract + +fun create(fn: () -> D): D { + return fn() +} + +fun main() { + create(::Abstract) +} diff --git a/compiler/testData/diagnostics/tests/callableReference/kt37530.txt b/compiler/testData/diagnostics/tests/callableReference/kt37530.txt new file mode 100644 index 00000000000..eaa5984ee17 --- /dev/null +++ b/compiler/testData/diagnostics/tests/callableReference/kt37530.txt @@ -0,0 +1,11 @@ +package + +public fun create(/*0*/ fn: () -> D): D +public fun main(): kotlin.Unit + +public abstract class Abstract { + public constructor Abstract() + 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 +} diff --git a/compiler/testData/diagnostics/tests/overload/disambiguateByFailedAbstractClassCheck.fir.kt b/compiler/testData/diagnostics/tests/overload/disambiguateByFailedAbstractClassCheck.fir.kt new file mode 100644 index 00000000000..65841f1af65 --- /dev/null +++ b/compiler/testData/diagnostics/tests/overload/disambiguateByFailedAbstractClassCheck.fir.kt @@ -0,0 +1,34 @@ +// !WITH_NEW_IFERENCE +// !LANGUAGE: +NewInference +// !DIAGNOSTICS: -UNUSED_PARAMETER + +// FILE: packageA.kt + +package a + +abstract class Cls +abstract class Cls2 + +// FILE: packageB.kt + +package b + +fun Cls() {} +class Cls2 + +// FILE: test.kt + +package c + +import a.* +import b.* + +fun take(arg: Any) {} + +fun test() { + Cls() + take(Cls()) + + Cls2() + take(Cls2()) +} diff --git a/compiler/testData/diagnostics/tests/overload/disambiguateByFailedAbstractClassCheck.kt b/compiler/testData/diagnostics/tests/overload/disambiguateByFailedAbstractClassCheck.kt new file mode 100644 index 00000000000..b3c036c0cce --- /dev/null +++ b/compiler/testData/diagnostics/tests/overload/disambiguateByFailedAbstractClassCheck.kt @@ -0,0 +1,34 @@ +// !WITH_NEW_IFERENCE +// !LANGUAGE: +NewInference +// !DIAGNOSTICS: -UNUSED_PARAMETER + +// FILE: packageA.kt + +package a + +abstract class Cls +abstract class Cls2 + +// FILE: packageB.kt + +package b + +fun Cls() {} +class Cls2 + +// FILE: test.kt + +package c + +import a.* +import b.* + +fun take(arg: Any) {} + +fun test() { + Cls() + take(Cls()) + + Cls2() + take(Cls2()) +} diff --git a/compiler/testData/diagnostics/tests/overload/disambiguateByFailedAbstractClassCheck.txt b/compiler/testData/diagnostics/tests/overload/disambiguateByFailedAbstractClassCheck.txt new file mode 100644 index 00000000000..f2d4e436295 --- /dev/null +++ b/compiler/testData/diagnostics/tests/overload/disambiguateByFailedAbstractClassCheck.txt @@ -0,0 +1,34 @@ +package + +package a { + + public abstract class Cls { + public constructor Cls() + 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 abstract class Cls2 { + public constructor Cls2() + 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 + } +} + +package b { + public fun Cls(): kotlin.Unit + + public final class Cls2 { + public constructor Cls2() + 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 + } +} + +package c { + public fun take(/*0*/ arg: kotlin.Any): kotlin.Unit + public fun test(): kotlin.Unit +} diff --git a/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java index 86b6c0bce8d..97130b51f7c 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java @@ -1891,6 +1891,11 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTestWithFirVali runTest("compiler/testData/diagnostics/tests/callableReference/kt35959.kt"); } + @TestMetadata("kt37530.kt") + public void testKt37530() throws Exception { + runTest("compiler/testData/diagnostics/tests/callableReference/kt37530.kt"); + } + @TestMetadata("kt7430_wrongClassOnLHS.kt") public void testKt7430_wrongClassOnLHS() throws Exception { runTest("compiler/testData/diagnostics/tests/callableReference/kt7430_wrongClassOnLHS.kt"); @@ -15867,6 +15872,11 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTestWithFirVali runTest("compiler/testData/diagnostics/tests/overload/defaultParameters.kt"); } + @TestMetadata("disambiguateByFailedAbstractClassCheck.kt") + public void testDisambiguateByFailedAbstractClassCheck() throws Exception { + runTest("compiler/testData/diagnostics/tests/overload/disambiguateByFailedAbstractClassCheck.kt"); + } + @TestMetadata("EmptyArgumentListInLambda.kt") public void testEmptyArgumentListInLambda() throws Exception { runTest("compiler/testData/diagnostics/tests/overload/EmptyArgumentListInLambda.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsUsingJavacTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsUsingJavacTestGenerated.java index 32ac1ea7023..4bc2644b652 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsUsingJavacTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsUsingJavacTestGenerated.java @@ -1886,6 +1886,11 @@ public class DiagnosticsUsingJavacTestGenerated extends AbstractDiagnosticsUsing runTest("compiler/testData/diagnostics/tests/callableReference/kt35959.kt"); } + @TestMetadata("kt37530.kt") + public void testKt37530() throws Exception { + runTest("compiler/testData/diagnostics/tests/callableReference/kt37530.kt"); + } + @TestMetadata("kt7430_wrongClassOnLHS.kt") public void testKt7430_wrongClassOnLHS() throws Exception { runTest("compiler/testData/diagnostics/tests/callableReference/kt7430_wrongClassOnLHS.kt"); @@ -15862,6 +15867,11 @@ public class DiagnosticsUsingJavacTestGenerated extends AbstractDiagnosticsUsing runTest("compiler/testData/diagnostics/tests/overload/defaultParameters.kt"); } + @TestMetadata("disambiguateByFailedAbstractClassCheck.kt") + public void testDisambiguateByFailedAbstractClassCheck() throws Exception { + runTest("compiler/testData/diagnostics/tests/overload/disambiguateByFailedAbstractClassCheck.kt"); + } + @TestMetadata("EmptyArgumentListInLambda.kt") public void testEmptyArgumentListInLambda() throws Exception { runTest("compiler/testData/diagnostics/tests/overload/EmptyArgumentListInLambda.kt");