K2: prefer base class sources for JVM mapped functions

This commit is a follow-up to 3cb9396b20
"K2: prefer derived class sources for callable copies (e.g. fake overrides)"

#KT-64044 Fixed
This commit is contained in:
Mikhail Glukhikh
2023-12-20 16:14:44 +01:00
committed by Space Team
parent c80854eb7c
commit f5453690a6
11 changed files with 64 additions and 7 deletions
@@ -118,6 +118,12 @@ public class Fe10IdeNormalAnalysisSourceModuleReferenceResolveTestGenerated exte
runTest("analysis/analysis-api/testData/referenceResolve/ClassReferenceInImport.kt");
}
@Test
@TestMetadata("CollectionStream.kt")
public void testCollectionStream() throws Exception {
runTest("analysis/analysis-api/testData/referenceResolve/CollectionStream.kt");
}
@Test
@TestMetadata("CompanionObjectWithName1.kt")
public void testCompanionObjectWithName1() throws Exception {
@@ -118,6 +118,12 @@ public class FirIdeDependentAnalysisSourceModuleReferenceResolveTestGenerated ex
runTest("analysis/analysis-api/testData/referenceResolve/ClassReferenceInImport.kt");
}
@Test
@TestMetadata("CollectionStream.kt")
public void testCollectionStream() throws Exception {
runTest("analysis/analysis-api/testData/referenceResolve/CollectionStream.kt");
}
@Test
@TestMetadata("CompanionObjectWithName1.kt")
public void testCompanionObjectWithName1() throws Exception {
@@ -118,6 +118,12 @@ public class FirIdeNormalAnalysisLibrarySourceModuleReferenceResolveTestGenerate
runTest("analysis/analysis-api/testData/referenceResolve/ClassReferenceInImport.kt");
}
@Test
@TestMetadata("CollectionStream.kt")
public void testCollectionStream() throws Exception {
runTest("analysis/analysis-api/testData/referenceResolve/CollectionStream.kt");
}
@Test
@TestMetadata("CompanionObjectWithName1.kt")
public void testCompanionObjectWithName1() throws Exception {
@@ -118,6 +118,12 @@ public class FirIdeNormalAnalysisSourceModuleReferenceResolveTestGenerated exten
runTest("analysis/analysis-api/testData/referenceResolve/ClassReferenceInImport.kt");
}
@Test
@TestMetadata("CollectionStream.kt")
public void testCollectionStream() throws Exception {
runTest("analysis/analysis-api/testData/referenceResolve/CollectionStream.kt");
}
@Test
@TestMetadata("CompanionObjectWithName1.kt")
public void testCompanionObjectWithName1() throws Exception {
@@ -78,8 +78,9 @@ abstract class AbstractReferenceResolveTest : AbstractAnalysisApiBasedTest() {
val resolvedTo = analyzeReferenceElement(ktReferences.first().element, mainModule) {
val symbols = ktReferences.flatMap { it.resolveToSymbols() }
checkReferenceResultForValidity(ktReferences, mainModule, testServices, symbols)
renderResolvedTo(symbols, renderingOptions) { getAdditionalSymbolInfo(it) }
}
val renderPsiClassName = Directives.RENDER_PSI_CLASS_NAME in mainModule.directives
renderResolvedTo(symbols, renderPsiClassName, renderingOptions) { getAdditionalSymbolInfo(it) }
}
if (Directives.UNRESOLVED_REFERENCE in mainModule.directives) {
return
@@ -119,6 +120,10 @@ abstract class AbstractReferenceResolveTest : AbstractAnalysisApiBasedTest() {
val UNRESOLVED_REFERENCE by directive(
"Reference should be unresolved",
)
val RENDER_PSI_CLASS_NAME by directive(
"Render also PSI class name for resolved reference"
)
}
private val renderingOptions = KtDeclarationRendererForDebug.WITH_QUALIFIED_NAMES.with {
@@ -17,16 +17,18 @@ import org.jetbrains.kotlin.types.Variance
object TestReferenceResolveResultRenderer {
fun KtAnalysisSession.renderResolvedTo(
symbols: List<KtSymbol>,
renderPsiClassName: Boolean = false,
renderer: KtDeclarationRenderer = KtDeclarationRendererForDebug.WITH_QUALIFIED_NAMES,
additionalInfo: KtAnalysisSession.(KtSymbol) -> String? = { null }
) =
symbols.map { renderResolveResult(it, renderer, additionalInfo) }
symbols.map { renderResolveResult(it, renderPsiClassName, renderer, additionalInfo) }
.sorted()
.withIndex()
.joinToString(separator = "\n") { "${it.index}: ${it.value}" }
private fun KtAnalysisSession.renderResolveResult(
symbol: KtSymbol,
renderPsiClassName: Boolean,
renderer: KtDeclarationRenderer,
additionalInfo: KtAnalysisSession.(KtSymbol) -> String?
): String {
@@ -35,7 +37,12 @@ object TestReferenceResolveResultRenderer {
append("(in $fqName) ")
}
when (symbol) {
is KtDeclarationSymbol -> append(symbol.render(renderer))
is KtDeclarationSymbol -> {
append(symbol.render(renderer))
if (renderPsiClassName) {
append(" (psi: ${symbol.psi?.let { it::class.simpleName }})")
}
}
is KtPackageSymbol -> append("package ${symbol.fqName}")
is KtReceiverParameterSymbol -> {
append("extension receiver with type ")
@@ -118,6 +118,12 @@ public class FirStandaloneNormalAnalysisSourceModuleReferenceResolveTestGenerate
runTest("analysis/analysis-api/testData/referenceResolve/ClassReferenceInImport.kt");
}
@Test
@TestMetadata("CollectionStream.kt")
public void testCollectionStream() throws Exception {
runTest("analysis/analysis-api/testData/referenceResolve/CollectionStream.kt");
}
@Test
@TestMetadata("CompanionObjectWithName1.kt")
public void testCompanionObjectWithName1() throws Exception {
@@ -0,0 +1,9 @@
// ISSUE: KT-64044
// IGNORE_FE10
// (stream call is unresolved for some reason)
// FULL_JDK
// RENDER_PSI_CLASS_NAME
fun f(collection: Collection<String>) {
collection.strea<caret>m()
}
@@ -0,0 +1,2 @@
Resolved to:
0: (in kotlin.collections.Collection) fun stream(): java.util.stream.Stream<E> (psi: ClsMethodImpl)
@@ -223,6 +223,7 @@ class JvmMappedScope(
newDispatchReceiverType = kotlinDispatchReceiverType,
newParameterTypes = oldFunction.valueParameters.map { substitutor.substituteOrSelf(it.returnTypeRef.coneType) },
newReturnType = substitutor.substituteOrSelf(oldFunction.returnTypeRef.coneType),
newSource = symbol.fir.source,
).apply {
if (jdkMemberStatus == JDKMemberStatus.HIDDEN) {
isHiddenEverywhereBesideSuperCalls = true
@@ -295,7 +296,8 @@ class JvmMappedScope(
newTypeParameters = null,
newContextReceiverTypes = emptyList(),
isExpect = false,
callableCopySubstitutionForTypeUpdater = null
callableCopySubstitutionForTypeUpdater = null,
newSource = oldConstructor.source,
)
return newSymbol
}
@@ -120,8 +120,9 @@ object FirFakeOverrideGenerator {
newModality: Modality? = null,
newVisibility: Visibility? = null,
callableCopySubstitutionForTypeUpdater: CallableCopySubstitution? = null,
newSource: KtSourceElement? = null,
): FirSimpleFunction = buildSimpleFunction {
source = derivedClassLookupTag?.toSymbol(session)?.source ?: baseFunction.source
source = newSource ?: derivedClassLookupTag?.toSymbol(session)?.source ?: baseFunction.source
moduleData = session.nullableModuleData ?: baseFunction.moduleData
this.origin = origin
name = baseFunction.name
@@ -153,11 +154,12 @@ object FirFakeOverrideGenerator {
newTypeParameters: List<FirTypeParameterRef>?,
isExpect: Boolean,
callableCopySubstitutionForTypeUpdater: CallableCopySubstitution?,
newSource: KtSourceElement? = null,
): FirConstructor = buildConstructor {
// TODO: consider using here some light-weight functions instead of pseudo-real FirMemberFunctionImpl
// As second alternative, we can invent some light-weight kind of FirRegularClass
annotations += baseConstructor.annotations
source = derivedClassLookupTag?.toSymbol(session)?.source ?: baseConstructor.source
source = newSource ?: derivedClassLookupTag?.toSymbol(session)?.source ?: baseConstructor.source
moduleData = session.nullableModuleData ?: baseConstructor.moduleData
this.origin = origin
receiverParameter = baseConstructor.receiverParameter?.let { receiverParameter ->