From 729de536de35a5668b8cfa086f7f73e137447db5 Mon Sep 17 00:00:00 2001 From: Mikhail Glukhikh Date: Mon, 8 Aug 2016 14:32:59 +0300 Subject: [PATCH] Rewrite slice error removed for LEAKING_THIS #KT-13371 Fixed Also EA-86478 fixed (cherry picked from commit 5ac3126) --- .../kotlin/cfg/ConstructorConsistencyChecker.kt | 12 +++++++----- .../twoSecondaryConstructors.kt | 12 ++++++++++++ .../twoSecondaryConstructors.txt | 12 ++++++++++++ .../kotlin/checkers/DiagnosticsTestGenerated.java | 6 ++++++ .../kotlin/idea/inspections/LeakingThisInspection.kt | 2 +- 5 files changed, 38 insertions(+), 6 deletions(-) create mode 100644 compiler/testData/diagnostics/tests/constructorConsistency/twoSecondaryConstructors.kt create mode 100644 compiler/testData/diagnostics/tests/constructorConsistency/twoSecondaryConstructors.txt diff --git a/compiler/frontend/src/org/jetbrains/kotlin/cfg/ConstructorConsistencyChecker.kt b/compiler/frontend/src/org/jetbrains/kotlin/cfg/ConstructorConsistencyChecker.kt index 9b2f5ad818c..dcb8f7b7f45 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/cfg/ConstructorConsistencyChecker.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/cfg/ConstructorConsistencyChecker.kt @@ -33,14 +33,16 @@ import org.jetbrains.kotlin.resolve.BindingContext import org.jetbrains.kotlin.resolve.BindingTrace import org.jetbrains.kotlin.types.expressions.OperatorConventions -sealed class LeakingThisDescriptor(val classOrObject: KtClassOrObject) { - class PropertyIsNull(val property: PropertyDescriptor, classOrObject: KtClassOrObject) : LeakingThisDescriptor(classOrObject) +interface LeakingThisDescriptor { + val classOrObject: KtClassOrObject - class NonFinalClass(val klass: ClassDescriptor, classOrObject: KtClassOrObject): LeakingThisDescriptor(classOrObject) + data class PropertyIsNull(val property: PropertyDescriptor, override val classOrObject: KtClassOrObject) : LeakingThisDescriptor - class NonFinalProperty(val property: PropertyDescriptor, classOrObject: KtClassOrObject): LeakingThisDescriptor(classOrObject) + data class NonFinalClass(val klass: ClassDescriptor, override val classOrObject: KtClassOrObject): LeakingThisDescriptor - class NonFinalFunction(val function: FunctionDescriptor, classOrObject: KtClassOrObject): LeakingThisDescriptor(classOrObject) + data class NonFinalProperty(val property: PropertyDescriptor, override val classOrObject: KtClassOrObject): LeakingThisDescriptor + + data class NonFinalFunction(val function: FunctionDescriptor, override val classOrObject: KtClassOrObject): LeakingThisDescriptor } class ConstructorConsistencyChecker private constructor( diff --git a/compiler/testData/diagnostics/tests/constructorConsistency/twoSecondaryConstructors.kt b/compiler/testData/diagnostics/tests/constructorConsistency/twoSecondaryConstructors.kt new file mode 100644 index 00000000000..3bf926680dc --- /dev/null +++ b/compiler/testData/diagnostics/tests/constructorConsistency/twoSecondaryConstructors.kt @@ -0,0 +1,12 @@ +fun use(x: Any?) = x + +class Eap { + private val foo = toString() + + constructor(foo: Int) { + use(foo) + } + constructor(foo: String) { + use(foo) + } +} \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/constructorConsistency/twoSecondaryConstructors.txt b/compiler/testData/diagnostics/tests/constructorConsistency/twoSecondaryConstructors.txt new file mode 100644 index 00000000000..3ca679f62b7 --- /dev/null +++ b/compiler/testData/diagnostics/tests/constructorConsistency/twoSecondaryConstructors.txt @@ -0,0 +1,12 @@ +package + +public fun use(/*0*/ x: kotlin.Any?): kotlin.Any? + +public final class Eap { + public constructor Eap(/*0*/ foo: kotlin.Int) + public constructor Eap(/*0*/ foo: kotlin.String) + private final val foo: kotlin.String + 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/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java index b76ae7cf147..3d8d3ab8eae 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java @@ -3212,6 +3212,12 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTest { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/constructorConsistency/propertyAccess.kt"); doTest(fileName); } + + @TestMetadata("twoSecondaryConstructors.kt") + public void testTwoSecondaryConstructors() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/constructorConsistency/twoSecondaryConstructors.kt"); + doTest(fileName); + } } @TestMetadata("compiler/testData/diagnostics/tests/controlFlowAnalysis") diff --git a/idea/src/org/jetbrains/kotlin/idea/inspections/LeakingThisInspection.kt b/idea/src/org/jetbrains/kotlin/idea/inspections/LeakingThisInspection.kt index 06ad8f88557..47b4f6e26d9 100644 --- a/idea/src/org/jetbrains/kotlin/idea/inspections/LeakingThisInspection.kt +++ b/idea/src/org/jetbrains/kotlin/idea/inspections/LeakingThisInspection.kt @@ -43,7 +43,6 @@ class LeakingThisInspection : AbstractKotlinInspection() { val context = expression.analyzeFully() val leakingThisDescriptor = context.get(LEAKING_THIS, expression) ?: return val description = when (leakingThisDescriptor) { - is PropertyIsNull -> return // Not supported yet is NonFinalClass -> if (expression is KtThisExpression) "Leaking 'this' in constructor of non-final class ${leakingThisDescriptor.klass.name}" @@ -53,6 +52,7 @@ class LeakingThisInspection : AbstractKotlinInspection() { "Accessing non-final property ${leakingThisDescriptor.property.name} in constructor" is NonFinalFunction -> "Calling non-final function ${leakingThisDescriptor.function.name} in constructor" + else -> return // Not supported yet } val memberDescriptorToFix = when (leakingThisDescriptor) { is NonFinalProperty -> leakingThisDescriptor.property