Fix reference resolver bug for companion object whose name is the same as class

FirReferenceResolveHelper internally checks whether the referenced class
id matches the qualifed access or not. If they do not match, it reports
an error. When the companion object has the same name as the class,
resolving a qualified expression access to a member of the companion
object causes an error because of the mismatch e.g.,

```
package my.sample

class Test {
    fun a() {
        my.sample.<caret>Test.say()
    }

    companion object Test {
        fun say() {}
    }
}
```

This commit fixes the issue.

TODO: When the companion object has a name difference from class, it
does not report an error but the resolution result is wrong in FIR. See
KT-56167.

---

Commentary from rebaser: the issue mentioned in this code is
fixed in 71a368e06e, so the actual
fix is omitted, and only test data is preserved
This commit is contained in:
Jaebaek Seo
2023-01-25 01:34:46 +01:00
committed by teamcity
parent 633d182846
commit 860dee2cb1
10 changed files with 90 additions and 4 deletions
@@ -112,6 +112,18 @@ public class Fe10IdeNormalAnalysisSourceModuleReferenceResolveTestGenerated exte
runTest("analysis/analysis-api/testData/referenceResolve/ClassReferenceInImport.kt");
}
@Test
@TestMetadata("CompanionObjectWithName1.kt")
public void testCompanionObjectWithName1() throws Exception {
runTest("analysis/analysis-api/testData/referenceResolve/CompanionObjectWithName1.kt");
}
@Test
@TestMetadata("CompanionObjectWithName2.kt")
public void testCompanionObjectWithName2() throws Exception {
runTest("analysis/analysis-api/testData/referenceResolve/CompanionObjectWithName2.kt");
}
@Test
@TestMetadata("CtrlClickResolve.kt")
public void testCtrlClickResolve() throws Exception {
@@ -517,12 +517,12 @@ internal object FirReferenceResolveHelper {
// If we're looking for the deepest qualifier, then just resolve to the companion
if (expression === deepestQualifier) return referencedSymbolsByFir
if (deepestQualifier?.getReferencedName() != referencedClass.classId.shortClassName.asString()) {
// Remove the last companion name part if the qualified access does not contain it.
// This is needed because the companion name part is optional.
if (fir.resolvedToCompanionObject) {
// this flag is true only when companion object is resolved through its containing class name,
// so we want to drop companion object own name from the classId
referencedClass.classId.outerClassId ?: return referencedSymbolsByFir
} else {
referencedClass.classId
referencedClass.classId // ?: return referencedSymbolsByFir
}
} else {
referencedClass.classId
@@ -112,6 +112,18 @@ public class FirIdeDependentAnalysisSourceModuleReferenceResolveTestGenerated ex
runTest("analysis/analysis-api/testData/referenceResolve/ClassReferenceInImport.kt");
}
@Test
@TestMetadata("CompanionObjectWithName1.kt")
public void testCompanionObjectWithName1() throws Exception {
runTest("analysis/analysis-api/testData/referenceResolve/CompanionObjectWithName1.kt");
}
@Test
@TestMetadata("CompanionObjectWithName2.kt")
public void testCompanionObjectWithName2() throws Exception {
runTest("analysis/analysis-api/testData/referenceResolve/CompanionObjectWithName2.kt");
}
@Test
@TestMetadata("CtrlClickResolve.kt")
public void testCtrlClickResolve() throws Exception {
@@ -112,6 +112,18 @@ public class FirIdeNormalAnalysisLibrarySourceModuleReferenceResolveTestGenerate
runTest("analysis/analysis-api/testData/referenceResolve/ClassReferenceInImport.kt");
}
@Test
@TestMetadata("CompanionObjectWithName1.kt")
public void testCompanionObjectWithName1() throws Exception {
runTest("analysis/analysis-api/testData/referenceResolve/CompanionObjectWithName1.kt");
}
@Test
@TestMetadata("CompanionObjectWithName2.kt")
public void testCompanionObjectWithName2() throws Exception {
runTest("analysis/analysis-api/testData/referenceResolve/CompanionObjectWithName2.kt");
}
@Test
@TestMetadata("CtrlClickResolve.kt")
public void testCtrlClickResolve() throws Exception {
@@ -112,6 +112,18 @@ public class FirIdeNormalAnalysisSourceModuleReferenceResolveTestGenerated exten
runTest("analysis/analysis-api/testData/referenceResolve/ClassReferenceInImport.kt");
}
@Test
@TestMetadata("CompanionObjectWithName1.kt")
public void testCompanionObjectWithName1() throws Exception {
runTest("analysis/analysis-api/testData/referenceResolve/CompanionObjectWithName1.kt");
}
@Test
@TestMetadata("CompanionObjectWithName2.kt")
public void testCompanionObjectWithName2() throws Exception {
runTest("analysis/analysis-api/testData/referenceResolve/CompanionObjectWithName2.kt");
}
@Test
@TestMetadata("CtrlClickResolve.kt")
public void testCtrlClickResolve() throws Exception {
@@ -112,6 +112,18 @@ public class FirStandaloneNormalAnalysisSourceModuleReferenceResolveTestGenerate
runTest("analysis/analysis-api/testData/referenceResolve/ClassReferenceInImport.kt");
}
@Test
@TestMetadata("CompanionObjectWithName1.kt")
public void testCompanionObjectWithName1() throws Exception {
runTest("analysis/analysis-api/testData/referenceResolve/CompanionObjectWithName1.kt");
}
@Test
@TestMetadata("CompanionObjectWithName2.kt")
public void testCompanionObjectWithName2() throws Exception {
runTest("analysis/analysis-api/testData/referenceResolve/CompanionObjectWithName2.kt");
}
@Test
@TestMetadata("CtrlClickResolve.kt")
public void testCtrlClickResolve() throws Exception {
@@ -0,0 +1,11 @@
package my.sample
class Inner {
fun a() {
my.sample.<caret>Inner.say()
}
companion object Inner {
fun say() {}
}
}
@@ -0,0 +1,2 @@
Resolved to:
0: (in my.sample.Inner) companion object Inner
@@ -0,0 +1,11 @@
package my.sample
class Inner {
fun a() {
my.<caret>sample.Inner.say()
}
companion object Inner {
fun say() {}
}
}
@@ -0,0 +1,2 @@
Resolved to:
0: package my.sample