[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
@@ -5290,6 +5290,18 @@ public class DiagnosisCompilerTestFE10TestdataTestGenerated extends AbstractDiag
@TestMetadata("compiler/testData/diagnostics/tests/controlFlowAnalysis")
@TestDataPath("$PROJECT_ROOT")
public class ControlFlowAnalysis {
@Test
@TestMetadata("accessValueParameterInDefaultValue_after.kt")
public void testAccessValueParameterInDefaultValue_after() throws Exception {
runTest("compiler/testData/diagnostics/tests/controlFlowAnalysis/accessValueParameterInDefaultValue_after.kt");
}
@Test
@TestMetadata("accessValueParameterInDefaultValue_before.kt")
public void testAccessValueParameterInDefaultValue_before() throws Exception {
runTest("compiler/testData/diagnostics/tests/controlFlowAnalysis/accessValueParameterInDefaultValue_before.kt");
}
@Test
public void testAllFilesPresentInControlFlowAnalysis() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/diagnostics/tests/controlFlowAnalysis"), Pattern.compile("^(.+)\\.kt$"), Pattern.compile("^(.+)\\.fir\\.kts?$"), true);
@@ -5290,6 +5290,18 @@ public class FirOldFrontendDiagnosticsTestGenerated extends AbstractFirDiagnosti
@TestMetadata("compiler/testData/diagnostics/tests/controlFlowAnalysis")
@TestDataPath("$PROJECT_ROOT")
public class ControlFlowAnalysis {
@Test
@TestMetadata("accessValueParameterInDefaultValue_after.kt")
public void testAccessValueParameterInDefaultValue_after() throws Exception {
runTest("compiler/testData/diagnostics/tests/controlFlowAnalysis/accessValueParameterInDefaultValue_after.kt");
}
@Test
@TestMetadata("accessValueParameterInDefaultValue_before.kt")
public void testAccessValueParameterInDefaultValue_before() throws Exception {
runTest("compiler/testData/diagnostics/tests/controlFlowAnalysis/accessValueParameterInDefaultValue_before.kt");
}
@Test
public void testAllFilesPresentInControlFlowAnalysis() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/diagnostics/tests/controlFlowAnalysis"), Pattern.compile("^(.+)\\.kt$"), Pattern.compile("^(.+)\\.fir\\.kts?$"), true);
@@ -5290,6 +5290,18 @@ public class FirOldFrontendDiagnosticsWithLightTreeTestGenerated extends Abstrac
@TestMetadata("compiler/testData/diagnostics/tests/controlFlowAnalysis")
@TestDataPath("$PROJECT_ROOT")
public class ControlFlowAnalysis {
@Test
@TestMetadata("accessValueParameterInDefaultValue_after.kt")
public void testAccessValueParameterInDefaultValue_after() throws Exception {
runTest("compiler/testData/diagnostics/tests/controlFlowAnalysis/accessValueParameterInDefaultValue_after.kt");
}
@Test
@TestMetadata("accessValueParameterInDefaultValue_before.kt")
public void testAccessValueParameterInDefaultValue_before() throws Exception {
runTest("compiler/testData/diagnostics/tests/controlFlowAnalysis/accessValueParameterInDefaultValue_before.kt");
}
@Test
public void testAllFilesPresentInControlFlowAnalysis() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/diagnostics/tests/controlFlowAnalysis"), Pattern.compile("^(.+)\\.kt$"), Pattern.compile("^(.+)\\.fir\\.kts?$"), true);
@@ -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)
}
}
@@ -15,13 +15,13 @@ val z = 3
fun foo(x: Int = <!UNINITIALIZED_PARAMETER!>y<!>, y: Int = x, i : Int = z): Int = x + y
fun foo(x: () -> Int = { y }, y: Int = x(), i : Int = z): Int = x() + y
fun foo(x: () -> Int = { <!UNINITIALIZED_PARAMETER_WARNING!>y<!> }, y: Int = x(), i : Int = z): Int = x() + y
fun bar(x: () -> Int = { y; 1 }, y: Int) {}
fun bar(x: () -> Int = { <!UNINITIALIZED_PARAMETER_WARNING!>y<!>; 1 }, y: Int) {}
fun baz(
x: () -> Int = {
fun bar(xx: () -> Int = { y; 1 }) = xx
fun bar(xx: () -> Int = { <!UNINITIALIZED_PARAMETER_WARNING!>y<!>; 1 }) = xx
bar()()
},
y: Int
@@ -30,7 +30,7 @@ fun baz(
fun boo(
x: () -> Int = {
fun bar(): Int = y
fun bar(): Int = <!UNINITIALIZED_PARAMETER_WARNING!>y<!>
bar()
},
y: Int
@@ -0,0 +1,72 @@
// LANGUAGE: +ProhibitIllegalValueParameterUsageInDefaultArguments
// DIAGNOSTICS: -UNUSED_PARAMETER
// WITH_STDLIB
// ISSUE: KT-25694
fun test_1(
x: () -> String = { <!UNINITIALIZED_PARAMETER!>y<!> }, // Error
y: String = "OK"
) {}
fun test_2(
x: String = "OK",
y: () -> String = { x } // OK
) {}
fun test_3(
x: () -> Any = { y() to <!UNINITIALIZED_PARAMETER!>y<!>.invoke() }, // Error
y: () -> String = { "OK" }
) {}
fun test_4(
x: () -> String = { "OK" },
y: () -> Any = { x() to x.invoke() } // OK
) {}
interface Foo {
fun foo()
}
fun test_5(
x: Foo = object : Foo {
val z1 = <!UNINITIALIZED_PARAMETER!>y<!> // Error
val z2 = run { <!UNINITIALIZED_PARAMETER!>y<!> } // Error
init {
println(<!UNINITIALIZED_PARAMETER!>y<!>) // Error
}
override fun foo() {
println(<!UNINITIALIZED_PARAMETER!>y<!>) // Error
}
},
y: String = "OK"
) {}
fun test_6(
x: String = "OK",
y: Foo = object : Foo {
val z1 = x // OK
val z2 = run { x } // OK
init {
println(x) // OK
}
override fun foo() {
println(x) // OK
}
}
) {}
fun getFoo(x: String): Foo = null!!
fun test_7(
x: Foo = object : Foo by getFoo(<!UNINITIALIZED_PARAMETER!>y<!>) {}, // Error
y: String = "OK"
) {}
fun test_8(
x: String = "OK",
y: Foo = object : Foo by getFoo(x) {} // OK
) {}
@@ -0,0 +1,72 @@
// LANGUAGE: +ProhibitIllegalValueParameterUsageInDefaultArguments
// DIAGNOSTICS: -UNUSED_PARAMETER
// WITH_STDLIB
// ISSUE: KT-25694
fun test_1(
x: () -> String = { <!UNINITIALIZED_PARAMETER!>y<!> }, // Error
y: String = "OK"
) {}
fun test_2(
x: String = "OK",
y: () -> String = { x } // OK
) {}
fun test_3(
x: () -> Any = { <!UNINITIALIZED_PARAMETER!>y<!>() to <!UNINITIALIZED_PARAMETER!>y<!>.invoke() }, // Error
y: () -> String = { "OK" }
) {}
fun test_4(
x: () -> String = { "OK" },
y: () -> Any = { x() to x.invoke() } // OK
) {}
interface Foo {
fun foo()
}
fun test_5(
x: Foo = object : Foo {
val z1 = <!UNINITIALIZED_PARAMETER, UNINITIALIZED_PARAMETER!>y<!> // Error
val z2 = run { <!UNINITIALIZED_PARAMETER!>y<!> } // Error
init {
println(<!UNINITIALIZED_PARAMETER!>y<!>) // Error
}
override fun foo() {
println(<!UNINITIALIZED_PARAMETER!>y<!>) // Error
}
},
y: String = "OK"
) {}
fun test_6(
x: String = "OK",
y: Foo = object : Foo {
val z1 = x // OK
val z2 = run { x } // OK
init {
println(x) // OK
}
override fun foo() {
println(x) // OK
}
}
) {}
fun getFoo(x: String): Foo = null!!
fun test_7(
x: Foo = object : Foo by getFoo(<!UNINITIALIZED_PARAMETER, UNINITIALIZED_PARAMETER!>y<!>) {}, // Error
y: String = "OK"
) {}
fun test_8(
x: String = "OK",
y: Foo = object : Foo by getFoo(x) {} // OK
) {}
@@ -0,0 +1,18 @@
package
public fun getFoo(/*0*/ x: kotlin.String): Foo
public fun test_1(/*0*/ x: () -> kotlin.String = ..., /*1*/ y: kotlin.String = ...): kotlin.Unit
public fun test_2(/*0*/ x: kotlin.String = ..., /*1*/ y: () -> kotlin.String = ...): kotlin.Unit
public fun test_3(/*0*/ x: () -> kotlin.Any = ..., /*1*/ y: () -> kotlin.String = ...): kotlin.Unit
public fun test_4(/*0*/ x: () -> kotlin.String = ..., /*1*/ y: () -> kotlin.Any = ...): kotlin.Unit
public fun test_5(/*0*/ x: Foo = ..., /*1*/ y: kotlin.String = ...): kotlin.Unit
public fun test_6(/*0*/ x: kotlin.String = ..., /*1*/ y: Foo = ...): kotlin.Unit
public fun test_7(/*0*/ x: Foo = ..., /*1*/ y: kotlin.String = ...): kotlin.Unit
public fun test_8(/*0*/ x: kotlin.String = ..., /*1*/ y: Foo = ...): kotlin.Unit
public interface Foo {
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public abstract fun foo(): kotlin.Unit
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
@@ -0,0 +1,72 @@
// LANGUAGE: -ProhibitIllegalValueParameterUsageInDefaultArguments
// DIAGNOSTICS: -UNUSED_PARAMETER
// WITH_STDLIB
// ISSUE: KT-25694
fun test_1(
x: () -> String = { <!UNINITIALIZED_PARAMETER!>y<!> }, // Error
y: String = "OK"
) {}
fun test_2(
x: String = "OK",
y: () -> String = { x } // OK
) {}
fun test_3(
x: () -> Any = { y() to <!UNINITIALIZED_PARAMETER!>y<!>.invoke() }, // Error
y: () -> String = { "OK" }
) {}
fun test_4(
x: () -> String = { "OK" },
y: () -> Any = { x() to x.invoke() } // OK
) {}
interface Foo {
fun foo()
}
fun test_5(
x: Foo = object : Foo {
val z1 = <!UNINITIALIZED_PARAMETER!>y<!> // Error
val z2 = run { <!UNINITIALIZED_PARAMETER!>y<!> } // Error
init {
println(<!UNINITIALIZED_PARAMETER!>y<!>) // Error
}
override fun foo() {
println(<!UNINITIALIZED_PARAMETER!>y<!>) // Error
}
},
y: String = "OK"
) {}
fun test_6(
x: String = "OK",
y: Foo = object : Foo {
val z1 = x // OK
val z2 = run { x } // OK
init {
println(x) // OK
}
override fun foo() {
println(x) // OK
}
}
) {}
fun getFoo(x: String): Foo = null!!
fun test_7(
x: Foo = object : Foo by getFoo(<!UNINITIALIZED_PARAMETER!>y<!>) {}, // Error
y: String = "OK"
) {}
fun test_8(
x: String = "OK",
y: Foo = object : Foo by getFoo(x) {} // OK
) {}
@@ -0,0 +1,72 @@
// LANGUAGE: -ProhibitIllegalValueParameterUsageInDefaultArguments
// DIAGNOSTICS: -UNUSED_PARAMETER
// WITH_STDLIB
// ISSUE: KT-25694
fun test_1(
x: () -> String = { <!UNINITIALIZED_PARAMETER_WARNING!>y<!> }, // Error
y: String = "OK"
) {}
fun test_2(
x: String = "OK",
y: () -> String = { x } // OK
) {}
fun test_3(
x: () -> Any = { <!UNINITIALIZED_PARAMETER_WARNING!>y<!>() to <!UNINITIALIZED_PARAMETER_WARNING!>y<!>.invoke() }, // Error
y: () -> String = { "OK" }
) {}
fun test_4(
x: () -> String = { "OK" },
y: () -> Any = { x() to x.invoke() } // OK
) {}
interface Foo {
fun foo()
}
fun test_5(
x: Foo = object : Foo {
val z1 = <!UNINITIALIZED_PARAMETER, UNINITIALIZED_PARAMETER_WARNING!>y<!> // Error
val z2 = run { <!UNINITIALIZED_PARAMETER_WARNING!>y<!> } // Error
init {
println(<!UNINITIALIZED_PARAMETER_WARNING!>y<!>) // Error
}
override fun foo() {
println(<!UNINITIALIZED_PARAMETER_WARNING!>y<!>) // Error
}
},
y: String = "OK"
) {}
fun test_6(
x: String = "OK",
y: Foo = object : Foo {
val z1 = x // OK
val z2 = run { x } // OK
init {
println(x) // OK
}
override fun foo() {
println(x) // OK
}
}
) {}
fun getFoo(x: String): Foo = null!!
fun test_7(
x: Foo = object : Foo by getFoo(<!UNINITIALIZED_PARAMETER, UNINITIALIZED_PARAMETER_WARNING!>y<!>) {}, // Error
y: String = "OK"
) {}
fun test_8(
x: String = "OK",
y: Foo = object : Foo by getFoo(x) {} // OK
) {}
@@ -0,0 +1,19 @@
package
public fun getFoo(/*0*/ x: kotlin.String): Foo
public fun test_1(/*0*/ x: () -> kotlin.String = ..., /*1*/ y: kotlin.String = ...): kotlin.Unit
public fun test_2(/*0*/ x: kotlin.String = ..., /*1*/ y: () -> kotlin.String = ...): kotlin.Unit
public fun test_3(/*0*/ x: () -> kotlin.Any = ..., /*1*/ y: () -> kotlin.String = ...): kotlin.Unit
public fun test_4(/*0*/ x: () -> kotlin.String = ..., /*1*/ y: () -> kotlin.Any = ...): kotlin.Unit
public fun test_5(/*0*/ x: Foo = ..., /*1*/ y: kotlin.String = ...): kotlin.Unit
public fun test_6(/*0*/ x: kotlin.String = ..., /*1*/ y: Foo = ...): kotlin.Unit
public fun test_7(/*0*/ x: Foo = ..., /*1*/ y: kotlin.String = ...): kotlin.Unit
public fun test_8(/*0*/ x: kotlin.String = ..., /*1*/ y: Foo = ...): kotlin.Unit
public interface Foo {
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public abstract fun foo(): kotlin.Unit
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
@@ -5296,6 +5296,18 @@ public class DiagnosticTestGenerated extends AbstractDiagnosticTest {
@TestMetadata("compiler/testData/diagnostics/tests/controlFlowAnalysis")
@TestDataPath("$PROJECT_ROOT")
public class ControlFlowAnalysis {
@Test
@TestMetadata("accessValueParameterInDefaultValue_after.kt")
public void testAccessValueParameterInDefaultValue_after() throws Exception {
runTest("compiler/testData/diagnostics/tests/controlFlowAnalysis/accessValueParameterInDefaultValue_after.kt");
}
@Test
@TestMetadata("accessValueParameterInDefaultValue_before.kt")
public void testAccessValueParameterInDefaultValue_before() throws Exception {
runTest("compiler/testData/diagnostics/tests/controlFlowAnalysis/accessValueParameterInDefaultValue_before.kt");
}
@Test
public void testAllFilesPresentInControlFlowAnalysis() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/diagnostics/tests/controlFlowAnalysis"), Pattern.compile("^(.*)\\.kts?$"), Pattern.compile("^(.+)\\.fir\\.kts?$"), true);
@@ -257,6 +257,7 @@ enum class LanguageFeature(
// 1.9
DisableCheckingChangedProgressionsResolve(KOTLIN_1_9), // KT-49276
ProhibitIllegalValueParameterUsageInDefaultArguments(KOTLIN_1_9, kind = BUG_FIX), // KT-25694
// Temporarily disabled, see KT-27084/KT-22379
SoundSmartcastFromLoopConditionForLoopAssignedVariables(sinceVersion = null, kind = BUG_FIX),