[FIR] Correctly determine primary constructor parameters scope for class initialization section

^KT-58135 Fixed
This commit is contained in:
Dmitriy Novozhilov
2023-04-26 17:35:59 +03:00
committed by Space Team
parent 7ee648a4f5
commit 913b55174d
7 changed files with 65 additions and 9 deletions
@@ -5770,6 +5770,12 @@ public class DiagnosticCompilerTestFE10TestdataTestGenerated extends AbstractDia
runTest("compiler/testData/diagnostics/tests/constructorConsistency/outer.kt");
}
@Test
@TestMetadata("parametersVsPropertiesFromPrimaryConstructor.kt")
public void testParametersVsPropertiesFromPrimaryConstructor() throws Exception {
runTest("compiler/testData/diagnostics/tests/constructorConsistency/parametersVsPropertiesFromPrimaryConstructor.kt");
}
@Test
@TestMetadata("property.kt")
public void testProperty() throws Exception {
@@ -5770,6 +5770,12 @@ public class LLFirPreresolvedReversedDiagnosticCompilerFE10TestDataTestGenerated
runTest("compiler/testData/diagnostics/tests/constructorConsistency/outer.kt");
}
@Test
@TestMetadata("parametersVsPropertiesFromPrimaryConstructor.kt")
public void testParametersVsPropertiesFromPrimaryConstructor() throws Exception {
runTest("compiler/testData/diagnostics/tests/constructorConsistency/parametersVsPropertiesFromPrimaryConstructor.kt");
}
@Test
@TestMetadata("property.kt")
public void testProperty() throws Exception {
@@ -5770,6 +5770,12 @@ public class FirLightTreeOldFrontendDiagnosticsTestGenerated extends AbstractFir
runTest("compiler/testData/diagnostics/tests/constructorConsistency/outer.kt");
}
@Test
@TestMetadata("parametersVsPropertiesFromPrimaryConstructor.kt")
public void testParametersVsPropertiesFromPrimaryConstructor() throws Exception {
runTest("compiler/testData/diagnostics/tests/constructorConsistency/parametersVsPropertiesFromPrimaryConstructor.kt");
}
@Test
@TestMetadata("property.kt")
public void testProperty() throws Exception {
@@ -5776,6 +5776,12 @@ public class FirPsiOldFrontendDiagnosticsTestGenerated extends AbstractFirPsiDia
runTest("compiler/testData/diagnostics/tests/constructorConsistency/outer.kt");
}
@Test
@TestMetadata("parametersVsPropertiesFromPrimaryConstructor.kt")
public void testParametersVsPropertiesFromPrimaryConstructor() throws Exception {
runTest("compiler/testData/diagnostics/tests/constructorConsistency/parametersVsPropertiesFromPrimaryConstructor.kt");
}
@Test
@TestMetadata("property.kt")
public void testProperty() throws Exception {
@@ -5,10 +5,10 @@
package org.jetbrains.kotlin.fir.resolve.transformers.body.resolve
import org.jetbrains.kotlin.KtFakeSourceElementKind
import org.jetbrains.kotlin.descriptors.ClassKind
import org.jetbrains.kotlin.fir.FirSession
import org.jetbrains.kotlin.fir.PrivateForInline
import org.jetbrains.kotlin.fir.correspondingProperty
import org.jetbrains.kotlin.fir.declarations.*
import org.jetbrains.kotlin.fir.declarations.impl.FirDefaultPropertyAccessor
import org.jetbrains.kotlin.fir.declarations.utils.isCompanion
@@ -464,7 +464,7 @@ class BodyResolveContext(
val constructor = (owner as? FirRegularClass)?.declarations?.firstOrNull { it is FirConstructor } as? FirConstructor
val (primaryConstructorPureParametersScope, primaryConstructorAllParametersScope) =
if (constructor?.isPrimary == true) {
constructor.scopesWithPrimaryConstructorParameters(owner, holder.session)
constructor.scopesWithPrimaryConstructorParameters(holder.session)
} else {
null to null
}
@@ -551,17 +551,12 @@ class BodyResolveContext(
}
}
private fun FirConstructor.scopesWithPrimaryConstructorParameters(
ownerClass: FirClass,
session: FirSession
): Pair<FirLocalScope, FirLocalScope> {
private fun FirConstructor.scopesWithPrimaryConstructorParameters(session: FirSession): Pair<FirLocalScope, FirLocalScope> {
var parameterScope = FirLocalScope(session)
var allScope = FirLocalScope(session)
val properties = ownerClass.declarations.filterIsInstance<FirProperty>().associateBy { it.name }
for (parameter in valueParameters) {
allScope = allScope.storeVariable(parameter, session)
val property = properties[parameter.name]
if (property?.source?.kind != KtFakeSourceElementKind.PropertyFromParameter) {
if (parameter.correspondingProperty == null) {
parameterScope = parameterScope.storeVariable(parameter, session)
}
}
@@ -0,0 +1,31 @@
// FIR_IDENTICAL
// ISSUE: KT-58135
class Test(
val x: Int, // (1)
y: Int // (2)
) {
val String.x: String get() = this // (3)
val String.y: String get() = this // (4)
val y: Int = y // (5)
fun test(s: String) {
with(s) {
x.length // (3)
y.length // (4)
}
}
val test = with("hello") {
x.length // (3)
y.inc() // (2)
}
init {
with("hello") {
x.length // (3)
y.inc() // (2)
}
}
}
@@ -5776,6 +5776,12 @@ public class DiagnosticTestGenerated extends AbstractDiagnosticTest {
runTest("compiler/testData/diagnostics/tests/constructorConsistency/outer.kt");
}
@Test
@TestMetadata("parametersVsPropertiesFromPrimaryConstructor.kt")
public void testParametersVsPropertiesFromPrimaryConstructor() throws Exception {
runTest("compiler/testData/diagnostics/tests/constructorConsistency/parametersVsPropertiesFromPrimaryConstructor.kt");
}
@Test
@TestMetadata("property.kt")
public void testProperty() throws Exception {