[FIR] Don't drop directly inherited functions from origins for intersection overrides in intersection scope

```
open class Base<T> {
    fun foo(): T = ...
}

class Derived<T> : Base<T> {
    override fun foo(): T = ...
}
```

In intersection scope of type `Base<T> & Other<R>` we should create
  intersection override based on `Base.foo(): T` and `Derived.foo(): R`
  at the same time, despite the fact that `Derived.foo` actually directly
  overrides `Base.foo`

^KT-56722 Fixed
This commit is contained in:
Dmitriy Novozhilov
2023-02-23 20:19:20 +02:00
committed by Space Team
parent e0fa1e64b9
commit 8ff4af034a
7 changed files with 71 additions and 1 deletions
@@ -4623,6 +4623,12 @@ public class DiagnosisCompilerFirTestdataTestGenerated extends AbstractDiagnosis
public void testNoSmartcastToNullableNothing() throws Exception {
runTest("compiler/fir/analysis-tests/testData/resolve/smartcasts/problems/noSmartcastToNullableNothing.kt");
}
@Test
@TestMetadata("smartcastToStarProjectedSubclass.kt")
public void testSmartcastToStarProjectedSubclass() throws Exception {
runTest("compiler/fir/analysis-tests/testData/resolve/smartcasts/problems/smartcastToStarProjectedSubclass.kt");
}
}
@Nested
@@ -4088,6 +4088,11 @@ public class LazyBodyIsNotTouchedTilContractsPhaseTestGenerated extends Abstract
public void testNoSmartcastToNullableNothing() throws Exception {
runTest("compiler/fir/analysis-tests/testData/resolve/smartcasts/problems/noSmartcastToNullableNothing.kt");
}
@TestMetadata("smartcastToStarProjectedSubclass.kt")
public void testSmartcastToStarProjectedSubclass() throws Exception {
runTest("compiler/fir/analysis-tests/testData/resolve/smartcasts/problems/smartcastToStarProjectedSubclass.kt");
}
}
@TestMetadata("compiler/fir/analysis-tests/testData/resolve/smartcasts/receivers")
@@ -0,0 +1,19 @@
FILE: test.kt
public final fun test_1(option: R|Option<kotlin/Pair<kotlin/String, kotlin/String>>?|): R|kotlin/Unit| {
when () {
(R|<local>/option| is R|Option.Some<*>|) -> {
lval x: R|kotlin/String| = R|<local>/option|.R|/Option.Some.get|().R|SubstitutionOverride<kotlin/Pair.first: R|kotlin/String|>|
R|<local>/x|.R|kotlin/String.length|
}
}
}
public final fun test_2(option: R|Option<kotlin/Pair<kotlin/String, kotlin/String>>?|): R|kotlin/Unit| {
when () {
(R|<local>/option| is R|Option.Some<kotlin/Pair<kotlin/String, kotlin/String>>|) -> {
lval x: R|kotlin/String| = R|<local>/option|.R|SubstitutionOverride</Option.Some.get: R|ft<kotlin/Pair<kotlin/String, kotlin/String>, kotlin/Pair<kotlin/String, kotlin/String>?>|>|().R|SubstitutionOverride<kotlin/Pair.first: R|kotlin/String|>|
R|<local>/x|.R|kotlin/String.length|
}
}
}
@@ -0,0 +1,28 @@
// WITH_STDLIB
// ISSUE: KT-56722
// FILE: Option.java
public interface Option<T> {
T get();
public final class Some<T> implements Option<T> {
@Override
public T get() {
return null;
}
}
}
// FILE: test.kt
fun test_1(option: Option<Pair<String, String>>?) {
if (option is Option.Some<*>) {
val x = <!DEBUG_INFO_EXPRESSION_TYPE("kotlin.Pair<kotlin.String, kotlin.String>..kotlin.Pair<kotlin.String, kotlin.String>?!")!>option.get()<!>.first
x.length
}
}
fun test_2(option: Option<Pair<String, String>>?) {
if (option is Option.Some) {
val x = <!DEBUG_INFO_EXPRESSION_TYPE("kotlin.Pair<kotlin.String, kotlin.String>..kotlin.Pair<kotlin.String, kotlin.String>?!")!>option.get()<!>.first
x.length
}
}
@@ -4623,6 +4623,12 @@ public class FirLightTreeDiagnosticsTestGenerated extends AbstractFirLightTreeDi
public void testNoSmartcastToNullableNothing() throws Exception {
runTest("compiler/fir/analysis-tests/testData/resolve/smartcasts/problems/noSmartcastToNullableNothing.kt");
}
@Test
@TestMetadata("smartcastToStarProjectedSubclass.kt")
public void testSmartcastToStarProjectedSubclass() throws Exception {
runTest("compiler/fir/analysis-tests/testData/resolve/smartcasts/problems/smartcastToStarProjectedSubclass.kt");
}
}
@Nested
@@ -4623,6 +4623,12 @@ public class FirPsiDiagnosticTestGenerated extends AbstractFirPsiDiagnosticTest
public void testNoSmartcastToNullableNothing() throws Exception {
runTest("compiler/fir/analysis-tests/testData/resolve/smartcasts/problems/noSmartcastToNullableNothing.kt");
}
@Test
@TestMetadata("smartcastToStarProjectedSubclass.kt")
public void testSmartcastToStarProjectedSubclass() throws Exception {
runTest("compiler/fir/analysis-tests/testData/resolve/smartcasts/problems/smartcastToStarProjectedSubclass.kt");
}
}
@Nested
@@ -152,7 +152,7 @@ class FirTypeIntersectionScopeContext(
val groupWithPrivate =
overrideService.extractBothWaysOverridable(allMembersWithScope.maxByVisibility(), allMembersWithScope, overrideChecker)
val group = groupWithPrivate.filter { !Visibilities.isPrivate(it.member.fir.visibility) }.ifEmpty { groupWithPrivate }
val directOverrides = group.onlyDirectlyInherited()
val directOverrides = if (forClassUseSiteScope) group.onlyDirectlyInherited() else group
val mostSpecific = overrideService.selectMostSpecificMembers(directOverrides, ReturnTypeCalculatorForFullBodyResolve)
val nonTrivial = if (forClassUseSiteScope) {
// Create a non-trivial intersection override when the base methods come from different scopes,