From 3c8693758b7641a98c903dadece333a3a18b95ad Mon Sep 17 00:00:00 2001 From: Tianyu Geng Date: Wed, 9 Jun 2021 15:25:16 -0700 Subject: [PATCH] FIR: handle synthetic properties with unstable smartcast Synthetic properties from Java getter/setters need to be specially handled so that candidates from such symbols are marked with unstable. --- ...irOldFrontendDiagnosticsTestGenerated.java | 6 +++ ...DiagnosticsWithLightTreeTestGenerated.java | 6 +++ .../kotlin/fir/resolve/calls/Synthetics.kt | 8 +++- .../scopes/FirUnstableSmartcastTypeScope.kt | 8 +++- .../syntheticPropertyOnUnstableSmartcast.kt | 29 +++++++++++++++ .../syntheticPropertyOnUnstableSmartcast.txt | 25 +++++++++++++ .../unstableSmartCast.fir.kt | 37 ------------------- .../test/runners/DiagnosticTestGenerated.java | 6 +++ ...CompilerTestFE10TestdataTestGenerated.java | 5 +++ 9 files changed, 90 insertions(+), 40 deletions(-) create mode 100644 compiler/testData/diagnostics/tests/smartCasts/inference/syntheticPropertyOnUnstableSmartcast.kt create mode 100644 compiler/testData/diagnostics/tests/smartCasts/inference/syntheticPropertyOnUnstableSmartcast.txt delete mode 100644 compiler/testData/diagnostics/tests/smartCasts/intersectionScope/unstableSmartCast.fir.kt diff --git a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsTestGenerated.java b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsTestGenerated.java index fd7388e037c..bab5aaf4e18 100644 --- a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsTestGenerated.java +++ b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsTestGenerated.java @@ -26664,6 +26664,12 @@ public class FirOldFrontendDiagnosticsTestGenerated extends AbstractFirDiagnosti runTest("compiler/testData/diagnostics/tests/smartCasts/inference/stabilityOfSmartcastsAgainstGenericFunctions.kt"); } + @Test + @TestMetadata("syntheticPropertyOnUnstableSmartcast.kt") + public void testSyntheticPropertyOnUnstableSmartcast() throws Exception { + runTest("compiler/testData/diagnostics/tests/smartCasts/inference/syntheticPropertyOnUnstableSmartcast.kt"); + } + @Test @TestMetadata("unneededUnstableSmartcast.kt") public void testUnneededUnstableSmartcast() throws Exception { diff --git a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsWithLightTreeTestGenerated.java b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsWithLightTreeTestGenerated.java index e75bb33a7b0..b9795108e5a 100644 --- a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsWithLightTreeTestGenerated.java +++ b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirOldFrontendDiagnosticsWithLightTreeTestGenerated.java @@ -26664,6 +26664,12 @@ public class FirOldFrontendDiagnosticsWithLightTreeTestGenerated extends Abstrac runTest("compiler/testData/diagnostics/tests/smartCasts/inference/stabilityOfSmartcastsAgainstGenericFunctions.kt"); } + @Test + @TestMetadata("syntheticPropertyOnUnstableSmartcast.kt") + public void testSyntheticPropertyOnUnstableSmartcast() throws Exception { + runTest("compiler/testData/diagnostics/tests/smartCasts/inference/syntheticPropertyOnUnstableSmartcast.kt"); + } + @Test @TestMetadata("unneededUnstableSmartcast.kt") public void testUnneededUnstableSmartcast() throws Exception { diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/calls/Synthetics.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/calls/Synthetics.kt index eec9a42da1f..bc4b781e6b9 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/calls/Synthetics.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/calls/Synthetics.kt @@ -118,7 +118,13 @@ class FirSyntheticPropertiesScope( delegateGetter = getter delegateSetter = matchingSetter } - processor(property.symbol) + val syntheticSymbol = property.symbol + (baseScope as? FirUnstableSmartcastTypeScope)?.apply { + if (isSymbolFromUnstableSmartcast(getterSymbol)) { + markSymbolFromUnstableSmartcast(syntheticSymbol) + } + } + processor(syntheticSymbol) } private fun FirNamedFunctionSymbol.hasJavaOverridden(): Boolean { diff --git a/compiler/fir/tree/src/org/jetbrains/kotlin/fir/scopes/FirUnstableSmartcastTypeScope.kt b/compiler/fir/tree/src/org/jetbrains/kotlin/fir/scopes/FirUnstableSmartcastTypeScope.kt index 1f04a108bc3..45a2a46dc24 100644 --- a/compiler/fir/tree/src/org/jetbrains/kotlin/fir/scopes/FirUnstableSmartcastTypeScope.kt +++ b/compiler/fir/tree/src/org/jetbrains/kotlin/fir/scopes/FirUnstableSmartcastTypeScope.kt @@ -45,7 +45,7 @@ class FirUnstableSmartcastTypeScope( } smartcastScope.process(name) { if (it !in unique) { - symbolsFromUnstableSmartcast += it + markSymbolFromUnstableSmartcast(it) processor(it) } } @@ -72,7 +72,7 @@ class FirUnstableSmartcastTypeScope( smartcastScope.process(name) { symbol, firTypeScope -> if (symbol !in unique) { - symbolsFromUnstableSmartcast += symbol + markSymbolFromUnstableSmartcast(symbol) processor(symbol, firTypeScope) } else { ProcessorAction.NEXT @@ -83,6 +83,10 @@ class FirUnstableSmartcastTypeScope( fun isSymbolFromUnstableSmartcast(symbol: AbstractFirBasedSymbol<*>) = symbol in symbolsFromUnstableSmartcast + fun markSymbolFromUnstableSmartcast(symbol: FirCallableSymbol<*>) { + symbolsFromUnstableSmartcast += symbol + } + override fun processDirectOverriddenFunctionsWithBaseScope( functionSymbol: FirNamedFunctionSymbol, processor: (FirNamedFunctionSymbol, FirTypeScope) -> ProcessorAction diff --git a/compiler/testData/diagnostics/tests/smartCasts/inference/syntheticPropertyOnUnstableSmartcast.kt b/compiler/testData/diagnostics/tests/smartCasts/inference/syntheticPropertyOnUnstableSmartcast.kt new file mode 100644 index 00000000000..06651ca9fa9 --- /dev/null +++ b/compiler/testData/diagnostics/tests/smartCasts/inference/syntheticPropertyOnUnstableSmartcast.kt @@ -0,0 +1,29 @@ +// FIR_IDENTICAL +// FILE: p/Super.java + +package p + +public interface Super { + public String getName() + public void setName(String name) +} + +// FILE: p/test.kt + +package p + +class Sub : Super { + val onlyInSub: Int = 1 + override fun getName(): String = "" + override fun setName(name: String) {} +} + +var s: Super = Sub() + +fun test() { + if (s is Sub) { + s.name + s.name = "" + s.onlyInSub + } +} diff --git a/compiler/testData/diagnostics/tests/smartCasts/inference/syntheticPropertyOnUnstableSmartcast.txt b/compiler/testData/diagnostics/tests/smartCasts/inference/syntheticPropertyOnUnstableSmartcast.txt new file mode 100644 index 00000000000..17d8db9a5aa --- /dev/null +++ b/compiler/testData/diagnostics/tests/smartCasts/inference/syntheticPropertyOnUnstableSmartcast.txt @@ -0,0 +1,25 @@ +package + +package p { + public var s: p.Super + public fun test(): kotlin.Unit + + public final class Sub : p.Super { + public constructor Sub() + public final val onlyInSub: kotlin.Int = 1 + public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean + public open override /*1*/ fun getName(): kotlin.String + public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int + public open override /*1*/ fun setName(/*0*/ name: kotlin.String): kotlin.Unit + public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String + } + + public interface Super { + public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean + public abstract fun getName(): kotlin.String! + public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int + public abstract fun setName(/*0*/ name: kotlin.String!): kotlin.Unit + public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String + } +} + diff --git a/compiler/testData/diagnostics/tests/smartCasts/intersectionScope/unstableSmartCast.fir.kt b/compiler/testData/diagnostics/tests/smartCasts/intersectionScope/unstableSmartCast.fir.kt deleted file mode 100644 index 9d6aa96915a..00000000000 --- a/compiler/testData/diagnostics/tests/smartCasts/intersectionScope/unstableSmartCast.fir.kt +++ /dev/null @@ -1,37 +0,0 @@ -// !CHECK_TYPE - -interface A { - fun foo(): CharSequence? - fun baz(x: Any) {} -} - -interface B { - fun foo(): String - fun baz(x: Int): String ="" - fun baz(x: Int, y: Int) {} - - fun foobar(): CharSequence? -} - -interface C { - fun foo(): String - fun baz(x: Int): String ="" - fun baz(x: Int, y: Int) {} - - fun foobar(): String -} - -var x: A = null!! - -fun test() { - x.foo().checkType { _() } - - if (x is B && x is C) { - x.foo().checkType { _() } - x.baz("") - x.baz(1).checkType { _() } - x.baz(1, 2) - - x.foobar().checkType { _() } - } -} diff --git a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/DiagnosticTestGenerated.java b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/DiagnosticTestGenerated.java index b80421756e6..3b347177a8f 100644 --- a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/DiagnosticTestGenerated.java +++ b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/DiagnosticTestGenerated.java @@ -26754,6 +26754,12 @@ public class DiagnosticTestGenerated extends AbstractDiagnosticTest { runTest("compiler/testData/diagnostics/tests/smartCasts/inference/stabilityOfSmartcastsAgainstGenericFunctions.kt"); } + @Test + @TestMetadata("syntheticPropertyOnUnstableSmartcast.kt") + public void testSyntheticPropertyOnUnstableSmartcast() throws Exception { + runTest("compiler/testData/diagnostics/tests/smartCasts/inference/syntheticPropertyOnUnstableSmartcast.kt"); + } + @Test @TestMetadata("unneededUnstableSmartcast.kt") public void testUnneededUnstableSmartcast() throws Exception { diff --git a/idea/idea-frontend-fir/idea-fir-low-level-api/tests/org/jetbrains/kotlin/idea/fir/low/level/api/diagnostic/compiler/based/DiagnosisCompilerTestFE10TestdataTestGenerated.java b/idea/idea-frontend-fir/idea-fir-low-level-api/tests/org/jetbrains/kotlin/idea/fir/low/level/api/diagnostic/compiler/based/DiagnosisCompilerTestFE10TestdataTestGenerated.java index 1c7387b2ff3..682ef12a9de 100644 --- a/idea/idea-frontend-fir/idea-fir-low-level-api/tests/org/jetbrains/kotlin/idea/fir/low/level/api/diagnostic/compiler/based/DiagnosisCompilerTestFE10TestdataTestGenerated.java +++ b/idea/idea-frontend-fir/idea-fir-low-level-api/tests/org/jetbrains/kotlin/idea/fir/low/level/api/diagnostic/compiler/based/DiagnosisCompilerTestFE10TestdataTestGenerated.java @@ -23321,6 +23321,11 @@ public class DiagnosisCompilerTestFE10TestdataTestGenerated extends AbstractDiag runTest("compiler/testData/diagnostics/tests/smartCasts/inference/stabilityOfSmartcastsAgainstGenericFunctions.kt"); } + @TestMetadata("syntheticPropertyOnUnstableSmartcast.kt") + public void testSyntheticPropertyOnUnstableSmartcast() throws Exception { + runTest("compiler/testData/diagnostics/tests/smartCasts/inference/syntheticPropertyOnUnstableSmartcast.kt"); + } + @TestMetadata("unneededUnstableSmartcast.kt") public void testUnneededUnstableSmartcast() throws Exception { runTest("compiler/testData/diagnostics/tests/smartCasts/inference/unneededUnstableSmartcast.kt");