[FIR] Do not include transitive friend dependencies in symbol provider

When flattening a dependency FirSymbolProvider, make sure transitive
dependency FirSymbolProviders are not included. This requires checking
that nested symbol provider sessions match the composite symbol provider
session when they are both source sessions.

^KT-60614 Fixed
This commit is contained in:
Brian Norman
2023-10-26 11:32:26 -05:00
committed by Space Team
parent 33ab1871c7
commit c1f6fe1e76
25 changed files with 285 additions and 132 deletions
@@ -23266,6 +23266,12 @@ public class DiagnosticCompilerTestFE10TestdataTestGenerated extends AbstractDia
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/diagnostics/tests/multimodule"), Pattern.compile("^(.+)\\.(kt|kts)$"), Pattern.compile("^(.+)\\.(reversed|fir|ll)\\.kts?$"), true);
}
@Test
@TestMetadata("dependencyModule.kt")
public void testDependencyModule() throws Exception {
runTest("compiler/testData/diagnostics/tests/multimodule/dependencyModule.kt");
}
@Test
@TestMetadata("dependsOnModule.kt")
public void testDependsOnModule() throws Exception {
@@ -23266,6 +23266,12 @@ public class LLFirPreresolvedReversedDiagnosticCompilerFE10TestDataTestGenerated
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/diagnostics/tests/multimodule"), Pattern.compile("^(.+)\\.(kt|kts)$"), Pattern.compile("^(.+)\\.(reversed|fir|ll)\\.kts?$"), true);
}
@Test
@TestMetadata("dependencyModule.kt")
public void testDependencyModule() throws Exception {
runTest("compiler/testData/diagnostics/tests/multimodule/dependencyModule.kt");
}
@Test
@TestMetadata("dependsOnModule.kt")
public void testDependsOnModule() throws Exception {
@@ -23260,6 +23260,12 @@ public class FirLightTreeOldFrontendDiagnosticsTestGenerated extends AbstractFir
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/diagnostics/tests/multimodule"), Pattern.compile("^(.+)\\.kt$"), Pattern.compile("^(.+)\\.(reversed|fir|ll)\\.kts?$"), true, "multiplatform");
}
@Test
@TestMetadata("dependencyModule.kt")
public void testDependencyModule() throws Exception {
runTest("compiler/testData/diagnostics/tests/multimodule/dependencyModule.kt");
}
@Test
@TestMetadata("dependsOnModule.kt")
public void testDependsOnModule() throws Exception {
@@ -23266,6 +23266,12 @@ public class FirPsiOldFrontendDiagnosticsTestGenerated extends AbstractFirPsiDia
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/diagnostics/tests/multimodule"), Pattern.compile("^(.*)\\.kts?$"), Pattern.compile("^(.+)\\.(reversed|fir|ll)\\.kts?$"), true, "multiplatform");
}
@Test
@TestMetadata("dependencyModule.kt")
public void testDependencyModule() throws Exception {
runTest("compiler/testData/diagnostics/tests/multimodule/dependencyModule.kt");
}
@Test
@TestMetadata("dependsOnModule.kt")
public void testDependsOnModule() throws Exception {
@@ -6,7 +6,6 @@
package org.jetbrains.kotlin.fir.session
import org.jetbrains.kotlin.config.LanguageVersionSettings
import org.jetbrains.kotlin.container.topologicalSort
import org.jetbrains.kotlin.fir.*
import org.jetbrains.kotlin.fir.checkers.registerCommonCheckers
import org.jetbrains.kotlin.fir.deserialization.ModuleDataProvider
@@ -146,37 +145,37 @@ abstract class FirAbstractSessionFactory {
}
private fun FirSession.computeDependencyProviderList(moduleData: FirModuleData): List<FirSymbolProvider> {
val visited = mutableSetOf<FirSymbolProvider>()
// dependsOnDependencies can actualize declarations from their dependencies. Because actual declarations can be more specific
// (e.g. have additional supertypes), the modules must be ordered from most specific (i.e. actual) to most generic (i.e. expect)
// to prevent false positive resolution errors (see KT-57369 for an example).
return (moduleData.dependencies + moduleData.friendDependencies + moduleData.allDependsOnDependencies)
.mapNotNull { sessionProvider?.getSession(it) }
.map { it.symbolProvider }
.flatMap { it.flatten(visited, collectSourceProviders = it.session.kind == FirSession.Kind.Source) }
.flatMap { it.symbolProvider.flatten() }
.distinct()
.sortedBy { it.session.kind }
}
/* It eliminates dependency and composite providers since the current dependency provider is composite in fact.
* To prevent duplications and resolving errors, library or source providers from other modules should be filtered out during flattening.
* It depends on the session's kind of the top-level provider */
private fun FirSymbolProvider.flatten(
visited: MutableSet<FirSymbolProvider>,
collectSourceProviders: Boolean
): List<FirSymbolProvider> {
private fun FirSymbolProvider.flatten(): List<FirSymbolProvider> {
val originalSession = session.takeIf { it.kind == FirSession.Kind.Source }
val result = mutableListOf<FirSymbolProvider>()
fun FirSymbolProvider.collectProviders() {
if (!visited.add(this)) return
when {
// When provider is composite, unwrap all contained providers and recurse.
this is FirCachingCompositeSymbolProvider -> {
for (provider in providers) {
provider.collectProviders()
}
}
collectSourceProviders && session.kind == FirSession.Kind.Source ||
!collectSourceProviders && session.kind == FirSession.Kind.Library -> {
// Make sure only source symbol providers from the same session as the original symbol provider are flattened. A composite
// symbol provider can contain source symbol providers from multiple sessions that may represent dependency symbol providers
// which should not be propagated transitively.
originalSession != null && session.kind == FirSession.Kind.Source && session == originalSession ||
originalSession == null && session.kind == FirSession.Kind.Library -> {
result.add(this)
}
}
@@ -504,12 +504,6 @@ public class FirLightTreeJvmIrTextTestGenerated extends AbstractFirLightTreeJvmI
runTest("compiler/testData/ir/irText/declarations/kt35550.kt");
}
@Test
@TestMetadata("kt45308.kt")
public void testKt45308() throws Exception {
runTest("compiler/testData/ir/irText/declarations/kt45308.kt");
}
@Test
@TestMetadata("kt47527.kt")
public void testKt47527() throws Exception {
@@ -504,12 +504,6 @@ public class FirPsiJvmIrTextTestGenerated extends AbstractFirPsiJvmIrTextTest {
runTest("compiler/testData/ir/irText/declarations/kt35550.kt");
}
@Test
@TestMetadata("kt45308.kt")
public void testKt45308() throws Exception {
runTest("compiler/testData/ir/irText/declarations/kt45308.kt");
}
@Test
@TestMetadata("kt47527.kt")
public void testKt47527() throws Exception {
@@ -9,7 +9,7 @@ FILE fqName:b fileName:/b.kt
RETURN type=kotlin.Nothing from='public final fun foo (f: kotlin.Function0<kotlin.String>): kotlin.String declared in b'
CALL 'public abstract fun invoke (): R of kotlin.Function0 declared in kotlin.Function0' type=kotlin.String origin=INVOKE
$this: GET_VAR 'f: kotlin.Function0<kotlin.String> declared in b.foo' type=kotlin.Function0<kotlin.String> origin=VARIABLE_AS_FUNCTION
Module: c
Module: main
FILE fqName:<root> fileName:/c.kt
FUN name:box visibility:public modality:FINAL <> () returnType:kotlin.String
BLOCK_BODY
+2
View File
@@ -1,3 +1,5 @@
// FIR_IDENTICAL
// DUMP_IR
// This test checks that unresolved typealias in an abbreviated type does not crash the compiler or result in a compilation error.
// Apparently, there's some demand for this behavior, see KT-45308, KT-58335.
@@ -0,0 +1,83 @@
// !DIAGNOSTICS: -UNUSED_VARIABLE
// MODULE: m1
// FILE: a.kt
package p
public class A {
public val propPublic = A()
internal val propInternal = A()
private val propPrivate = A()
public fun funPublic() = A()
internal fun funInternal() = A()
private fun funPrivate() = A()
public inner class ClassPublic
internal inner class ClassInternal
private inner class ClassPrivate
}
public val propPublic = A()
internal val propInternal = A()
private val propPrivate = A()
public fun funPublic() = A()
internal fun funInternal() = A()
private fun funPrivate() = A()
public class ClassPublic
internal class ClassInternal
private class ClassPrivate
// MODULE: m2(m1)
// FILE: b.kt
import p.*
fun test2() {
propPublic
<!INVISIBLE_REFERENCE!>propInternal<!>
<!INVISIBLE_REFERENCE!>propPrivate<!>
funPublic()
<!INVISIBLE_REFERENCE!>funInternal<!>()
<!INVISIBLE_REFERENCE!>funPrivate<!>()
ClassPublic()
<!INVISIBLE_REFERENCE!>ClassInternal<!>()
<!INVISIBLE_REFERENCE!>ClassPrivate<!>()
val inst = A()
inst.propPublic
inst.<!INVISIBLE_REFERENCE!>propInternal<!>
inst.<!INVISIBLE_REFERENCE!>propPrivate<!>
inst.funPublic()
inst.<!INVISIBLE_REFERENCE!>funInternal<!>()
inst.<!INVISIBLE_REFERENCE!>funPrivate<!>()
inst.ClassPublic()
inst.<!INVISIBLE_REFERENCE!>ClassInternal<!>()
inst.<!INVISIBLE_REFERENCE!>ClassPrivate<!>()
}
// MODULE: m3(m2)
// FILE: c.kt
import <!UNRESOLVED_IMPORT!>p<!>.*
fun test3() {
<!UNRESOLVED_REFERENCE!>propPublic<!>
<!UNRESOLVED_REFERENCE!>propInternal<!>
<!UNRESOLVED_REFERENCE!>propPrivate<!>
<!UNRESOLVED_REFERENCE!>funPublic<!>()
<!UNRESOLVED_REFERENCE!>funInternal<!>()
<!UNRESOLVED_REFERENCE!>funPrivate<!>()
<!UNRESOLVED_REFERENCE!>ClassPublic<!>()
<!UNRESOLVED_REFERENCE!>ClassInternal<!>()
<!UNRESOLVED_REFERENCE!>ClassPrivate<!>()
val inst = <!UNRESOLVED_REFERENCE!>A<!>()
inst.propPublic
inst.propInternal
inst.propPrivate
inst.funPublic()
inst.funInternal()
inst.funPrivate()
inst.ClassPublic()
inst.ClassInternal()
inst.ClassPrivate()
}
@@ -0,0 +1,83 @@
// !DIAGNOSTICS: -UNUSED_VARIABLE
// MODULE: m1
// FILE: a.kt
package p
public class A {
public val propPublic = A()
internal val propInternal = A()
private val propPrivate = A()
public fun funPublic() = A()
internal fun funInternal() = A()
private fun funPrivate() = A()
public inner class ClassPublic
internal inner class ClassInternal
private inner class ClassPrivate
}
public val propPublic = A()
internal val propInternal = A()
private val propPrivate = A()
public fun funPublic() = A()
internal fun funInternal() = A()
private fun funPrivate() = A()
public class ClassPublic
internal class ClassInternal
private class ClassPrivate
// MODULE: m2(m1)
// FILE: b.kt
import p.*
fun test2() {
propPublic
<!INVISIBLE_MEMBER!>propInternal<!>
<!INVISIBLE_MEMBER!>propPrivate<!>
funPublic()
<!INVISIBLE_MEMBER!>funInternal<!>()
<!INVISIBLE_MEMBER!>funPrivate<!>()
ClassPublic()
<!INVISIBLE_MEMBER!>ClassInternal<!>()
<!INVISIBLE_MEMBER!>ClassPrivate<!>()
val inst = A()
inst.propPublic
inst.<!INVISIBLE_MEMBER!>propInternal<!>
inst.<!INVISIBLE_MEMBER!>propPrivate<!>
inst.funPublic()
inst.<!INVISIBLE_MEMBER!>funInternal<!>()
inst.<!INVISIBLE_MEMBER!>funPrivate<!>()
inst.ClassPublic()
inst.<!INVISIBLE_MEMBER!>ClassInternal<!>()
inst.<!INVISIBLE_MEMBER!>ClassPrivate<!>()
}
// MODULE: m3(m2)
// FILE: c.kt
import <!UNRESOLVED_REFERENCE!>p<!>.*
fun test3() {
<!UNRESOLVED_REFERENCE!>propPublic<!>
<!UNRESOLVED_REFERENCE!>propInternal<!>
<!UNRESOLVED_REFERENCE!>propPrivate<!>
<!UNRESOLVED_REFERENCE!>funPublic<!>()
<!UNRESOLVED_REFERENCE!>funInternal<!>()
<!UNRESOLVED_REFERENCE!>funPrivate<!>()
<!UNRESOLVED_REFERENCE!>ClassPublic<!>()
<!UNRESOLVED_REFERENCE!>ClassInternal<!>()
<!UNRESOLVED_REFERENCE!>ClassPrivate<!>()
val inst = <!UNRESOLVED_REFERENCE!>A<!>()
<!DEBUG_INFO_ELEMENT_WITH_ERROR_TYPE!>inst<!>.<!DEBUG_INFO_MISSING_UNRESOLVED!>propPublic<!>
<!DEBUG_INFO_ELEMENT_WITH_ERROR_TYPE!>inst<!>.<!DEBUG_INFO_MISSING_UNRESOLVED!>propInternal<!>
<!DEBUG_INFO_ELEMENT_WITH_ERROR_TYPE!>inst<!>.<!DEBUG_INFO_MISSING_UNRESOLVED!>propPrivate<!>
<!DEBUG_INFO_ELEMENT_WITH_ERROR_TYPE!>inst<!>.<!DEBUG_INFO_MISSING_UNRESOLVED!>funPublic<!>()
<!DEBUG_INFO_ELEMENT_WITH_ERROR_TYPE!>inst<!>.<!DEBUG_INFO_MISSING_UNRESOLVED!>funInternal<!>()
<!DEBUG_INFO_ELEMENT_WITH_ERROR_TYPE!>inst<!>.<!DEBUG_INFO_MISSING_UNRESOLVED!>funPrivate<!>()
<!DEBUG_INFO_ELEMENT_WITH_ERROR_TYPE!>inst<!>.<!DEBUG_INFO_MISSING_UNRESOLVED!>ClassPublic<!>()
<!DEBUG_INFO_ELEMENT_WITH_ERROR_TYPE!>inst<!>.<!DEBUG_INFO_MISSING_UNRESOLVED!>ClassInternal<!>()
<!DEBUG_INFO_ELEMENT_WITH_ERROR_TYPE!>inst<!>.<!DEBUG_INFO_MISSING_UNRESOLVED!>ClassPrivate<!>()
}
@@ -0,0 +1,53 @@
// !DIAGNOSTICS: -UNUSED_VARIABLE
// MODULE: m1
// FILE: a.kt
package p
public class A {
internal val a = A()
internal var v = A()
internal fun a() = A()
internal inner class B
}
internal val a = A()
internal var v = A()
internal fun a() = A()
internal class B
// MODULE: m2()(m1)
// FILE: b.kt
import p.*
fun test() {
val _a = a
val _v = v
a()
B()
val inst = A()
val ia = inst.a
val iv = inst.v
inst.a()
inst.B()
}
// MODULE: m3()(m2)
// FILE: c.kt
import <!UNRESOLVED_IMPORT!>p<!>.*
fun test3() {
val _a = <!UNRESOLVED_REFERENCE!>a<!>
val _v = <!UNRESOLVED_REFERENCE!>v<!>
<!UNRESOLVED_REFERENCE!>a<!>()
<!UNRESOLVED_REFERENCE!>B<!>()
val inst = <!UNRESOLVED_REFERENCE!>A<!>()
val ia = inst.a
val iv = inst.v
inst.a()
inst.B()
}
@@ -1,4 +1,3 @@
// FIR_IDENTICAL
// !DIAGNOSTICS: -UNUSED_VARIABLE
// MODULE: m1
// FILE: a.kt
@@ -35,3 +34,20 @@ fun test() {
inst.B()
}
// MODULE: m3()(m2)
// FILE: c.kt
import <!UNRESOLVED_REFERENCE!>p<!>.*
fun test3() {
val _a = <!UNRESOLVED_REFERENCE!>a<!>
val _v = <!UNRESOLVED_REFERENCE!>v<!>
<!UNRESOLVED_REFERENCE!>a<!>()
<!UNRESOLVED_REFERENCE!>B<!>()
val inst = <!UNRESOLVED_REFERENCE!>A<!>()
val ia = <!DEBUG_INFO_ELEMENT_WITH_ERROR_TYPE!>inst<!>.<!DEBUG_INFO_MISSING_UNRESOLVED!>a<!>
val iv = <!DEBUG_INFO_ELEMENT_WITH_ERROR_TYPE!>inst<!>.<!DEBUG_INFO_MISSING_UNRESOLVED!>v<!>
<!DEBUG_INFO_ELEMENT_WITH_ERROR_TYPE!>inst<!>.<!DEBUG_INFO_MISSING_UNRESOLVED!>a<!>()
<!DEBUG_INFO_ELEMENT_WITH_ERROR_TYPE!>inst<!>.<!DEBUG_INFO_MISSING_UNRESOLVED!>B<!>()
}
@@ -35,3 +35,8 @@ package p {
package
public fun test(): kotlin.Unit
// -- Module: <m3> --
package
public fun test3(): kotlin.Unit
-24
View File
@@ -1,24 +0,0 @@
// FIR_IDENTICAL
// SKIP_KT_DUMP
// IGNORE_BACKEND_K1: JS_IR
// IGNORE_BACKEND_K1: JS_IR_ES6
// MODULE: a
// FILE: a.kt
package a
typealias A = String
// MODULE: b(a)
// FILE: b.kt
package b
import a.A
fun foo(f: () -> A): A = f()
// MODULE: c(b)
// FILE: c.kt
import b.foo
fun box(): String = foo { "OK" }
@@ -1,35 +0,0 @@
// MODULE: a
// FILE: a.kt
package a
// CHECK:
// Mangled name: a.A
// Public signature: a/A|null[0]
typealias A = String
// MODULE: b
// FILE: b.kt
package b
// CHECK JVM_IR:
// Mangled name: b#foo(kotlin.Function0<kotlin.String>){}kotlin.String
// Public signature: b/foo|-5020381652845254261[0]
// Public signature debug description: foo(kotlin.Function0<kotlin.String>){}kotlin.String
// CHECK JS_IR NATIVE:
// Mangled name: b#foo(kotlin.Function0<kotlin.String>){}
// Public signature: b/foo|-2695324588787180624[0]
// Public signature debug description: foo(kotlin.Function0<kotlin.String>){}
fun foo(f: Function0<String>): String
// MODULE: c
// FILE: c.kt
// CHECK JVM_IR:
// Mangled name: #box(){}kotlin.String
// Public signature: /box|-9347091776561469[0]
// Public signature debug description: box(){}kotlin.String
// CHECK JS_IR NATIVE:
// Mangled name: #box(){}
// Public signature: /box|2173511048851971368[0]
// Public signature debug description: box(){}
fun box(): String
@@ -23266,6 +23266,12 @@ public class DiagnosticTestGenerated extends AbstractDiagnosticTest {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/diagnostics/tests/multimodule"), Pattern.compile("^(.*)\\.kts?$"), Pattern.compile("^(.+)\\.(reversed|fir|ll)\\.kts?$"), true);
}
@Test
@TestMetadata("dependencyModule.kt")
public void testDependencyModule() throws Exception {
runTest("compiler/testData/diagnostics/tests/multimodule/dependencyModule.kt");
}
@Test
@TestMetadata("dependsOnModule.kt")
public void testDependsOnModule() throws Exception {
@@ -504,12 +504,6 @@ public class ClassicJvmIrTextTestGenerated extends AbstractClassicJvmIrTextTest
runTest("compiler/testData/ir/irText/declarations/kt35550.kt");
}
@Test
@TestMetadata("kt45308.kt")
public void testKt45308() throws Exception {
runTest("compiler/testData/ir/irText/declarations/kt45308.kt");
}
@Test
@TestMetadata("kt47527.kt")
public void testKt47527() throws Exception {
@@ -388,11 +388,6 @@ public class KlibIrTextTestCaseGenerated extends AbstractKlibIrTextTestCase {
runTest("compiler/testData/ir/irText/declarations/kt35550.kt");
}
@TestMetadata("kt45308.kt")
public void testKt45308() throws Exception {
runTest("compiler/testData/ir/irText/declarations/kt45308.kt");
}
@TestMetadata("kt47527.kt")
public void testKt47527() throws Exception {
runTest("compiler/testData/ir/irText/declarations/kt47527.kt");
@@ -438,12 +438,6 @@ public class FirLightTreeJsIrTextTestGenerated extends AbstractFirLightTreeJsIrT
runTest("compiler/testData/ir/irText/declarations/kt35550.kt");
}
@Test
@TestMetadata("kt45308.kt")
public void testKt45308() throws Exception {
runTest("compiler/testData/ir/irText/declarations/kt45308.kt");
}
@Test
@TestMetadata("kt47527.kt")
public void testKt47527() throws Exception {
@@ -438,12 +438,6 @@ public class FirPsiJsIrTextTestGenerated extends AbstractFirPsiJsIrTextTest {
runTest("compiler/testData/ir/irText/declarations/kt35550.kt");
}
@Test
@TestMetadata("kt45308.kt")
public void testKt45308() throws Exception {
runTest("compiler/testData/ir/irText/declarations/kt45308.kt");
}
@Test
@TestMetadata("kt47527.kt")
public void testKt47527() throws Exception {
@@ -438,12 +438,6 @@ public class ClassicJsIrTextTestGenerated extends AbstractClassicJsIrTextTest {
runTest("compiler/testData/ir/irText/declarations/kt35550.kt");
}
@Test
@TestMetadata("kt45308.kt")
public void testKt45308() throws Exception {
runTest("compiler/testData/ir/irText/declarations/kt45308.kt");
}
@Test
@TestMetadata("kt47527.kt")
public void testKt47527() throws Exception {
@@ -438,12 +438,6 @@ public class ClassicNativeIrTextTestGenerated extends AbstractClassicNativeIrTex
runTest("compiler/testData/ir/irText/declarations/kt35550.kt");
}
@Test
@TestMetadata("kt45308.kt")
public void testKt45308() throws Exception {
runTest("compiler/testData/ir/irText/declarations/kt45308.kt");
}
@Test
@TestMetadata("kt47527.kt")
public void testKt47527() throws Exception {
@@ -438,12 +438,6 @@ public class FirLightTreeNativeIrTextTestGenerated extends AbstractFirLightTreeN
runTest("compiler/testData/ir/irText/declarations/kt35550.kt");
}
@Test
@TestMetadata("kt45308.kt")
public void testKt45308() throws Exception {
runTest("compiler/testData/ir/irText/declarations/kt45308.kt");
}
@Test
@TestMetadata("kt47527.kt")
public void testKt47527() throws Exception {
@@ -438,12 +438,6 @@ public class FirPsiNativeIrTextTestGenerated extends AbstractFirPsiNativeIrTextT
runTest("compiler/testData/ir/irText/declarations/kt35550.kt");
}
@Test
@TestMetadata("kt45308.kt")
public void testKt45308() throws Exception {
runTest("compiler/testData/ir/irText/declarations/kt45308.kt");
}
@Test
@TestMetadata("kt47527.kt")
public void testKt47527() throws Exception {