[FE 1.0] Fix reporting of uninitialized parameter in default values of parameters

^KT-25694
^KT-50723 Fixed
This commit is contained in:
Dmitriy Novozhilov
2022-01-11 15:39:07 +03:00
committed by teamcity
parent 92e893bebe
commit ecc890efe8
16 changed files with 459 additions and 4 deletions
@@ -1032,6 +1032,7 @@ public interface Errors {
DiagnosticFactory1<KtSimpleNameExpression, VariableDescriptor> UNINITIALIZED_VARIABLE = DiagnosticFactory1.create(ERROR);
DiagnosticFactory1<KtSimpleNameExpression, ValueParameterDescriptor> UNINITIALIZED_PARAMETER = DiagnosticFactory1.create(ERROR);
DiagnosticFactory1<KtSimpleNameExpression, ValueParameterDescriptor> UNINITIALIZED_PARAMETER_WARNING = DiagnosticFactory1.create(WARNING);
DiagnosticFactory1<KtSimpleNameExpression, ClassDescriptor> UNINITIALIZED_ENUM_ENTRY = DiagnosticFactory1.create(ERROR);
DiagnosticFactory1<KtExpression, ClassDescriptor> UNINITIALIZED_ENUM_COMPANION = DiagnosticFactory1.create(ERROR);
DiagnosticFactory1<KtExpression, ClassDescriptor> UNINITIALIZED_ENUM_COMPANION_WARNING = DiagnosticFactory1.create(WARNING);
@@ -345,6 +345,7 @@ public class DefaultErrorMessages {
MAP.put(UNINITIALIZED_VARIABLE, "Variable ''{0}'' must be initialized", NAME);
MAP.put(UNINITIALIZED_PARAMETER, "Parameter ''{0}'' is uninitialized here", NAME);
MAP.put(UNINITIALIZED_PARAMETER_WARNING, "Parameter ''{0}'' is uninitialized here. This warning will be an error in future releases", NAME);
MAP.put(UNINITIALIZED_ENUM_ENTRY, "Enum entry ''{0}'' is uninitialized here", NAME);
MAP.put(UNINITIALIZED_ENUM_COMPANION, "Companion object of enum class ''{0}'' is uninitialized here", NAME);
MAP.put(UNINITIALIZED_ENUM_COMPANION_WARNING, "Companion object of enum class ''{0}'' is uninitialized here. This warning will become an error in future releases", NAME);
@@ -48,6 +48,7 @@ private val DEFAULT_DECLARATION_CHECKERS = listOf(
SuspendFunctionAsSupertypeChecker,
EnumCompanionInEnumConstructorCallChecker,
ContextualDeclarationChecker,
ValueParameterUsageInDefaultArgumentChecker,
)
private val DEFAULT_CALL_CHECKERS = listOf(
@@ -0,0 +1,78 @@
/*
* Copyright 2010-2022 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.checkers
import com.intellij.psi.PsiElement
import org.jetbrains.kotlin.config.LanguageFeature
import org.jetbrains.kotlin.config.LanguageFeature.ProhibitIllegalValueParameterUsageInDefaultArguments
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
import org.jetbrains.kotlin.descriptors.FunctionDescriptor
import org.jetbrains.kotlin.descriptors.ValueParameterDescriptor
import org.jetbrains.kotlin.diagnostics.Errors
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.resolve.calls.tower.NewVariableAsFunctionResolvedCallImpl
import org.jetbrains.kotlin.resolve.calls.util.getResolvedCall
object ValueParameterUsageInDefaultArgumentChecker : DeclarationChecker {
override fun check(declaration: KtDeclaration, descriptor: DeclarationDescriptor, context: DeclarationCheckerContext) {
if (declaration !is KtFunction || descriptor !is FunctionDescriptor) return
val allParameters = descriptor.valueParameters
val declaredParameters = mutableListOf<ValueParameterDescriptor>()
// We can don't check last parameter, because all other parameters already declared
for ((parameter, parameterDescriptor) in declaration.valueParameters.zip(allParameters).dropLast(1)) {
checkParameter(parameter, allParameters, declaredParameters, context)
declaredParameters += parameterDescriptor
}
}
private fun checkParameter(
parameter: KtParameter,
allParameters: List<ValueParameterDescriptor>,
declaredParameters: List<ValueParameterDescriptor>,
context: DeclarationCheckerContext
) {
val defaultValue = parameter.defaultValue ?: return
val bindingContext = context.trace.bindingContext
val visitor = object : KtVisitorVoid() {
override fun visitElement(element: PsiElement) {
element.acceptChildren(this)
}
override fun visitSimpleNameExpression(expression: KtSimpleNameExpression) {
val resolvedDescriptor = expression.getResolvedCall(bindingContext)
?.resultingDescriptor as? ValueParameterDescriptor
?: return
checkParameter(expression, resolvedDescriptor)
}
override fun visitCallExpression(expression: KtCallExpression) {
val resolvedCall = expression.getResolvedCall(bindingContext)
if (resolvedCall is NewVariableAsFunctionResolvedCallImpl) {
val calleeExpression = expression.calleeExpression as? KtSimpleNameExpression
val descriptor = resolvedCall.variableCall.resultingDescriptor as? ValueParameterDescriptor
if (calleeExpression != null && descriptor != null) {
checkParameter(calleeExpression, descriptor)
}
}
expression.acceptChildren(this)
}
private fun checkParameter(expression: KtSimpleNameExpression, descriptor: ValueParameterDescriptor) {
if (descriptor in allParameters && descriptor !in declaredParameters) {
val factory =
when (context.languageVersionSettings.supportsFeature(ProhibitIllegalValueParameterUsageInDefaultArguments)) {
true -> Errors.UNINITIALIZED_PARAMETER
false -> Errors.UNINITIALIZED_PARAMETER_WARNING
}
context.trace.report(factory.on(expression, descriptor))
}
}
}
defaultValue.acceptChildren(visitor)
}
}