Introduce warning about forbidden referencing to underscore named parameter of the catch block in a future release (KT-31567)

This commit is contained in:
Victor Petukhov
2020-09-28 13:40:36 +03:00
parent 09f1764f82
commit 8f333aef3a
18 changed files with 334 additions and 2 deletions
@@ -19197,6 +19197,16 @@ public class FirOldFrontendDiagnosticsTestGenerated extends AbstractFirOldFronte
runTest("compiler/testData/diagnostics/tests/resolve/typeParameterInDefaultValueInLocalFunction.kt");
}
@TestMetadata("underscoreInCatchBlock.kt")
public void testUnderscoreInCatchBlock() throws Exception {
runTest("compiler/testData/diagnostics/tests/resolve/underscoreInCatchBlock.kt");
}
@TestMetadata("underscoreInCatchBlockWithEnabledFeature.kt")
public void testUnderscoreInCatchBlockWithEnabledFeature() throws Exception {
runTest("compiler/testData/diagnostics/tests/resolve/underscoreInCatchBlockWithEnabledFeature.kt");
}
@TestMetadata("wrongNumberOfTypeArguments.kt")
public void testWrongNumberOfTypeArguments() throws Exception {
runTest("compiler/testData/diagnostics/tests/resolve/wrongNumberOfTypeArguments.kt");
@@ -889,6 +889,7 @@ public interface Errors {
DiagnosticFactory1<PsiElement, String> YIELD_IS_RESERVED = DiagnosticFactory1.create(ERROR);
DiagnosticFactory0<PsiElement> UNDERSCORE_IS_RESERVED = DiagnosticFactory0.create(ERROR);
DiagnosticFactory0<PsiElement> UNDERSCORE_USAGE_WITHOUT_BACKTICKS = DiagnosticFactory0.create(ERROR);
DiagnosticFactory0<PsiElement> RESOLVED_TO_UNDERSCORE_NAMED_CATCH_PARAMETER = DiagnosticFactory0.create(WARNING);
DiagnosticFactory1<PsiElement, String> INVALID_CHARACTERS = DiagnosticFactory1.create(ERROR);
DiagnosticFactory1<PsiElement, String> INAPPLICABLE_OPERATOR_MODIFIER = DiagnosticFactory1.create(ERROR);
@@ -509,6 +509,7 @@ public class DefaultErrorMessages {
MAP.put(UNDERSCORE_IS_RESERVED, "Names _, __, ___, ..., are reserved in Kotlin");
MAP.put(UNDERSCORE_USAGE_WITHOUT_BACKTICKS, "Names _, __, ___, ... can be used only in back-ticks (`_`, `__`, `___`, ...)");
MAP.put(RESOLVED_TO_UNDERSCORE_NAMED_CATCH_PARAMETER, "Referencing to an underscore-named parameter is deprecated. It will be an error in a future release.");
MAP.put(YIELD_IS_RESERVED, "{0}", STRING);
MAP.put(INVALID_CHARACTERS, "Name {0}", STRING);
@@ -54,7 +54,8 @@ private val DEFAULT_CALL_CHECKERS = listOf(
UselessElvisCallChecker(), ResultTypeWithNullableOperatorsChecker(), NullableVarargArgumentCallChecker,
NamedFunAsExpressionChecker, ContractNotAllowedCallChecker, ReifiedTypeParameterSubstitutionChecker(),
MissingDependencySupertypeChecker.ForCalls, AbstractClassInstantiationChecker, SuspendConversionCallChecker,
UnitConversionCallChecker, FunInterfaceConstructorReferenceChecker, NullableExtensionOperatorWithSafeCallChecker
UnitConversionCallChecker, FunInterfaceConstructorReferenceChecker, NullableExtensionOperatorWithSafeCallChecker,
ReferencingToUnderscoreNamedParameterOfCatchBlockChecker
)
private val DEFAULT_TYPE_CHECKERS = emptyList<AdditionalTypeChecker>()
private val DEFAULT_CLASSIFIER_USAGE_CHECKERS = listOf(
@@ -0,0 +1,49 @@
/*
* Copyright 2010-2019 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.builtins.isBuiltinFunctionalType
import org.jetbrains.kotlin.builtins.isFunctionOrSuspendFunctionType
import org.jetbrains.kotlin.config.LanguageFeature
import org.jetbrains.kotlin.descriptors.impl.LocalVariableDescriptor
import org.jetbrains.kotlin.diagnostics.Errors
import org.jetbrains.kotlin.diagnostics.reportDiagnosticOnce
import org.jetbrains.kotlin.diagnostics.reportDiagnosticOnceWrtDiagnosticFactoryList
import org.jetbrains.kotlin.psi.KtNameReferenceExpression
import org.jetbrains.kotlin.psi.KtParameter
import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.calls.SPECIAL_FUNCTION_NAMES
import org.jetbrains.kotlin.resolve.calls.callUtil.getParameterForArgument
import org.jetbrains.kotlin.resolve.calls.callUtil.getResolvedCall
import org.jetbrains.kotlin.resolve.calls.model.*
import org.jetbrains.kotlin.resolve.calls.tower.NewResolvedCallImpl
import org.jetbrains.kotlin.resolve.calls.tower.psiCallArgument
import org.jetbrains.kotlin.resolve.calls.tower.psiExpression
import org.jetbrains.kotlin.resolve.calls.tower.psiKotlinCall
import org.jetbrains.kotlin.resolve.descriptorUtil.isUnderscoreNamed
import org.jetbrains.kotlin.resolve.source.KotlinSourceElement
import org.jetbrains.kotlin.types.DeferredType
import org.jetbrains.kotlin.types.KotlinType
import org.jetbrains.kotlin.types.TypeUtils
import org.jetbrains.kotlin.types.typeUtil.isNothing
import org.jetbrains.kotlin.types.typeUtil.isNothingOrNullableNothing
import org.jetbrains.kotlin.types.typeUtil.isTypeParameter
object ReferencingToUnderscoreNamedParameterOfCatchBlockChecker : CallChecker {
override fun check(resolvedCall: ResolvedCall<*>, reportOn: PsiElement, context: CallCheckerContext) {
val descriptor = resolvedCall.resultingDescriptor
if (descriptor !is LocalVariableDescriptor || !descriptor.isUnderscoreNamed) return
val sourceElement = descriptor.source as? KotlinSourceElement ?: return
val ktParameter = sourceElement.psi as? KtParameter ?: return
if (ktParameter.isCatchParameter) {
context.trace.reportDiagnosticOnce(Errors.RESOLVED_TO_UNDERSCORE_NAMED_CATCH_PARAMETER.on(reportOn))
}
}
}
@@ -40,6 +40,7 @@ import org.jetbrains.kotlin.resolve.calls.util.CallMaker
import org.jetbrains.kotlin.resolve.constants.evaluate.ConstantExpressionEvaluator
import org.jetbrains.kotlin.resolve.deprecation.DeprecationResolver
import org.jetbrains.kotlin.resolve.descriptorUtil.builtIns
import org.jetbrains.kotlin.resolve.descriptorUtil.isUnderscoreNamed
import org.jetbrains.kotlin.resolve.scopes.*
import org.jetbrains.kotlin.resolve.scopes.receivers.*
import org.jetbrains.kotlin.types.*
@@ -837,7 +838,11 @@ class PSICallResolver(
val catchScope = with(scope) {
LexicalWritableScope(this, ownerDescriptor, false, redeclarationChecker, LexicalScopeKind.CATCH)
}
catchScope.addVariableDescriptor(variableDescriptor)
val isReferencingToUnderscoreNamedParameterForbidden =
languageVersionSettings.getFeatureSupport(LanguageFeature.ForbidReferencingToUnderscoreNamedParameterOfCatchBlock) == LanguageFeature.State.ENABLED
if (!variableDescriptor.isUnderscoreNamed || !isReferencingToUnderscoreNamedParameterForbidden) {
catchScope.addVariableDescriptor(variableDescriptor)
}
return replaceScope(catchScope)
}
}
@@ -140,6 +140,10 @@ public class KtParameter extends KtNamedDeclarationStub<KotlinParameterStub> imp
return getParent() instanceof KtForExpression;
}
public boolean isCatchParameter() {
return getParent().getParent() instanceof KtCatchClause;
}
@Nullable
@Override
public KtParameterList getValueParameterList() {
@@ -673,3 +673,6 @@ fun getTrailingCommaByElementsList(elementList: PsiElement?): PsiElement? {
val lastChild = elementList?.lastChild?.let { if (it !is PsiComment) it else it.getPrevSiblingIgnoringWhitespaceAndComments() }
return lastChild?.takeIf { it.node.elementType == KtTokens.COMMA }
}
val KtNameReferenceExpression.isUnderscoreInBackticks
get() = getReferencedName() == "`_`"
@@ -0,0 +1,57 @@
// !LANGUAGE: -ForbidReferencingToUnderscoreNamedParameterOfCatchBlock
// !DIAGNOSTICS: -UNUSED_PARAMETER -UNUSED_VARIABLE -UNUSED_ANONYMOUS_PARAMETER -UNUSED_EXPRESSION
fun foo() {
try {
TODO()
} catch (_: Exception) {
`_`.<!UNRESOLVED_REFERENCE!>stackTrace<!>
}
try {
TODO()
} catch (_: Exception) {
val x = {
val x2 = {
val x3 = { y: Int ->
val x4 = { _: Int ->
`_`
}
`_`
}
`_`
10
}
fun bar(x: Exception = `_`) {}
class Bar(`_`: Exception = `_`) {
inner class Bar2(x: Exception = `_`) { }
}
}
} catch (_: Exception) {
`_`.<!UNRESOLVED_REFERENCE!>stackTrace<!>
val y1 = _
val y2 = (`_`)
}
try {
TODO()
} catch (_: Exception) {
try {
TODO()
} catch (x: Exception) {
`_`.<!UNRESOLVED_REFERENCE!>stackTrace<!>
}
}
val boo1 = { `_`: Exception ->
try {
TODO()
} catch (x: Exception) {
`_`.<!UNRESOLVED_REFERENCE!>stackTrace<!>
}
}
val boo2 = { _: Exception ->
try {
TODO()
} catch (x: Exception) {
`_`.<!UNRESOLVED_REFERENCE!>stackTrace<!>
}
}
}
@@ -0,0 +1,57 @@
// !LANGUAGE: -ForbidReferencingToUnderscoreNamedParameterOfCatchBlock
// !DIAGNOSTICS: -UNUSED_PARAMETER -UNUSED_VARIABLE -UNUSED_ANONYMOUS_PARAMETER -UNUSED_EXPRESSION
fun foo() {
try {
TODO()
} catch (_: Exception) {
<!RESOLVED_TO_UNDERSCORE_NAMED_CATCH_PARAMETER!>`_`<!>.stackTrace
}
try {
TODO()
} catch (_: Exception) {
val x = {
val x2 = {
val x3 = { y: Int ->
val x4 = { _: Int ->
<!RESOLVED_TO_UNDERSCORE_NAMED_CATCH_PARAMETER!>`_`<!>
}
<!RESOLVED_TO_UNDERSCORE_NAMED_CATCH_PARAMETER!>`_`<!>
}
<!RESOLVED_TO_UNDERSCORE_NAMED_CATCH_PARAMETER!>`_`<!>
10
}
fun bar(x: Exception = <!RESOLVED_TO_UNDERSCORE_NAMED_CATCH_PARAMETER!>`_`<!>) {}
class Bar(`_`: Exception = <!UNINITIALIZED_PARAMETER!>`_`<!>) {
inner class Bar2(x: Exception = <!RESOLVED_TO_UNDERSCORE_NAMED_CATCH_PARAMETER!>`_`<!>) { }
}
}
} catch (_: Exception) {
<!RESOLVED_TO_UNDERSCORE_NAMED_CATCH_PARAMETER!>`_`<!>.stackTrace
val y1 = <!RESOLVED_TO_UNDERSCORE_NAMED_CATCH_PARAMETER, UNDERSCORE_USAGE_WITHOUT_BACKTICKS!>_<!>
val y2 = (<!RESOLVED_TO_UNDERSCORE_NAMED_CATCH_PARAMETER!>`_`<!>)
}
try {
TODO()
} catch (_: Exception) {
try {
TODO()
} catch (x: Exception) {
<!RESOLVED_TO_UNDERSCORE_NAMED_CATCH_PARAMETER!>`_`<!>.stackTrace
}
}
val boo1 = { `_`: Exception ->
try {
TODO()
} catch (x: Exception) {
`_`.stackTrace
}
}
val boo2 = { _: Exception ->
try {
TODO()
} catch (x: Exception) {
<!UNRESOLVED_REFERENCE!>`_`<!>.<!DEBUG_INFO_MISSING_UNRESOLVED!>stackTrace<!>
}
}
}
@@ -0,0 +1,3 @@
package
public fun foo(): kotlin.Unit
@@ -0,0 +1,57 @@
// !LANGUAGE: +ForbidReferencingToUnderscoreNamedParameterOfCatchBlock
// !DIAGNOSTICS: -UNUSED_PARAMETER -UNUSED_VARIABLE -UNUSED_ANONYMOUS_PARAMETER -UNUSED_EXPRESSION
fun foo() {
try {
TODO()
} catch (_: Exception) {
`_`.<!UNRESOLVED_REFERENCE!>stackTrace<!>
}
try {
TODO()
} catch (_: Exception) {
val x = {
val x2 = {
val x3 = { y: Int ->
val x4 = { _: Int ->
`_`
}
`_`
}
`_`
10
}
fun bar(x: Exception = `_`) {}
class Bar(`_`: Exception = `_`) {
inner class Bar2(x: Exception = `_`) { }
}
}
} catch (_: Exception) {
`_`.<!UNRESOLVED_REFERENCE!>stackTrace<!>
val y1 = _
val y2 = (`_`)
}
try {
TODO()
} catch (_: Exception) {
try {
TODO()
} catch (x: Exception) {
`_`.<!UNRESOLVED_REFERENCE!>stackTrace<!>
}
}
val boo1 = { `_`: Exception ->
try {
TODO()
} catch (x: Exception) {
`_`.<!UNRESOLVED_REFERENCE!>stackTrace<!>
}
}
val boo2 = { _: Exception ->
try {
TODO()
} catch (x: Exception) {
`_`.<!UNRESOLVED_REFERENCE!>stackTrace<!>
}
}
}
@@ -0,0 +1,57 @@
// !LANGUAGE: +ForbidReferencingToUnderscoreNamedParameterOfCatchBlock
// !DIAGNOSTICS: -UNUSED_PARAMETER -UNUSED_VARIABLE -UNUSED_ANONYMOUS_PARAMETER -UNUSED_EXPRESSION
fun foo() {
try {
TODO()
} catch (_: Exception) {
<!UNRESOLVED_REFERENCE!>`_`<!>.<!DEBUG_INFO_MISSING_UNRESOLVED!>stackTrace<!>
}
try {
TODO()
} catch (_: Exception) {
val x = {
val x2 = {
val x3 = { y: Int ->
val x4 = { _: Int ->
<!UNRESOLVED_REFERENCE!>`_`<!>
}
<!UNRESOLVED_REFERENCE!>`_`<!>
}
<!UNRESOLVED_REFERENCE!>`_`<!>
10
}
fun bar(x: Exception = <!UNRESOLVED_REFERENCE!>`_`<!>) {}
class Bar(`_`: Exception = <!UNINITIALIZED_PARAMETER!>`_`<!>) {
inner class Bar2(x: Exception = <!UNRESOLVED_REFERENCE!>`_`<!>) { }
}
}
} catch (_: Exception) {
<!UNRESOLVED_REFERENCE!>`_`<!>.<!DEBUG_INFO_MISSING_UNRESOLVED!>stackTrace<!>
val y1 = <!UNRESOLVED_REFERENCE!>_<!>
val y2 = (<!UNRESOLVED_REFERENCE!>`_`<!>)
}
try {
TODO()
} catch (_: Exception) {
try {
TODO()
} catch (x: Exception) {
<!UNRESOLVED_REFERENCE!>`_`<!>.<!DEBUG_INFO_MISSING_UNRESOLVED!>stackTrace<!>
}
}
val boo1 = { `_`: Exception ->
try {
TODO()
} catch (x: Exception) {
`_`.stackTrace
}
}
val boo2 = { _: Exception ->
try {
TODO()
} catch (x: Exception) {
<!UNRESOLVED_REFERENCE!>`_`<!>.<!DEBUG_INFO_MISSING_UNRESOLVED!>stackTrace<!>
}
}
}
@@ -0,0 +1,3 @@
package
public fun foo(): kotlin.Unit
@@ -19209,6 +19209,16 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTestWithFirVali
runTest("compiler/testData/diagnostics/tests/resolve/typeParameterInDefaultValueInLocalFunction.kt");
}
@TestMetadata("underscoreInCatchBlock.kt")
public void testUnderscoreInCatchBlock() throws Exception {
runTest("compiler/testData/diagnostics/tests/resolve/underscoreInCatchBlock.kt");
}
@TestMetadata("underscoreInCatchBlockWithEnabledFeature.kt")
public void testUnderscoreInCatchBlockWithEnabledFeature() throws Exception {
runTest("compiler/testData/diagnostics/tests/resolve/underscoreInCatchBlockWithEnabledFeature.kt");
}
@TestMetadata("wrongNumberOfTypeArguments.kt")
public void testWrongNumberOfTypeArguments() throws Exception {
runTest("compiler/testData/diagnostics/tests/resolve/wrongNumberOfTypeArguments.kt");
@@ -19199,6 +19199,16 @@ public class DiagnosticsUsingJavacTestGenerated extends AbstractDiagnosticsUsing
runTest("compiler/testData/diagnostics/tests/resolve/typeParameterInDefaultValueInLocalFunction.kt");
}
@TestMetadata("underscoreInCatchBlock.kt")
public void testUnderscoreInCatchBlock() throws Exception {
runTest("compiler/testData/diagnostics/tests/resolve/underscoreInCatchBlock.kt");
}
@TestMetadata("underscoreInCatchBlockWithEnabledFeature.kt")
public void testUnderscoreInCatchBlockWithEnabledFeature() throws Exception {
runTest("compiler/testData/diagnostics/tests/resolve/underscoreInCatchBlockWithEnabledFeature.kt");
}
@TestMetadata("wrongNumberOfTypeArguments.kt")
public void testWrongNumberOfTypeArguments() throws Exception {
runTest("compiler/testData/diagnostics/tests/resolve/wrongNumberOfTypeArguments.kt");
@@ -138,6 +138,7 @@ enum class LanguageFeature(
InferenceCompatibility(KOTLIN_1_5, kind = BUG_FIX),
RequiredPrimaryConstructorDelegationCallInEnums(KOTLIN_1_5, kind = BUG_FIX),
ForbidAnonymousReturnTypesInPrivateInlineFunctions(KOTLIN_1_5, kind = BUG_FIX),
ForbidReferencingToUnderscoreNamedParameterOfCatchBlock(KOTLIN_1_5, kind = BUG_FIX),
// Temporarily disabled, see KT-27084/KT-22379
SoundSmartcastFromLoopConditionForLoopAssignedVariables(sinceVersion = null, kind = BUG_FIX),
@@ -452,3 +452,6 @@ fun ModuleDescriptor.getKotlinTypeRefiner(): KotlinTypeRefiner = getCapability(R
@OptIn(TypeRefinement::class)
fun ModuleDescriptor.isTypeRefinementEnabled(): Boolean = getCapability(REFINER_CAPABILITY)?.value != null
val VariableDescriptor.isUnderscoreNamed
get() = name.identifier == "_"