K2 resolve: prefer derived class property to base class field

#KT-50082 Fixed
This commit is contained in:
Mikhail Glukhikh
2022-11-16 17:14:56 +01:00
committed by teamcity
parent 642bbd38ba
commit 59bafedd8a
15 changed files with 407 additions and 41 deletions
@@ -3005,6 +3005,11 @@ public class LazyBodyIsNotTouchedTilContractsPhaseTestGenerated extends Abstract
runTest("compiler/fir/analysis-tests/testData/resolve/problems/emptySelectorInQualifiedExpression.kt");
}
@TestMetadata("enumEntryFieldShadow.kt")
public void testEnumEntryFieldShadow() throws Exception {
runTest("compiler/fir/analysis-tests/testData/resolve/problems/enumEntryFieldShadow.kt");
}
@TestMetadata("expectConstructor.kt")
public void testExpectConstructor() throws Exception {
runTest("compiler/fir/analysis-tests/testData/resolve/problems/expectConstructor.kt");
@@ -3144,6 +3149,34 @@ public class LazyBodyIsNotTouchedTilContractsPhaseTestGenerated extends Abstract
}
}
@TestMetadata("compiler/fir/analysis-tests/testData/resolve/propertyVsField")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class PropertyVsField extends AbstractLazyBodyIsNotTouchedTilContractsPhaseTest {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest(this::doTest, this, testDataFilePath);
}
public void testAllFilesPresentInPropertyVsField() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/fir/analysis-tests/testData/resolve/propertyVsField"), Pattern.compile("^([^.]+)\\.kt$"), null, true);
}
@TestMetadata("fieldPropertyShadow.kt")
public void testFieldPropertyShadow() throws Exception {
runTest("compiler/fir/analysis-tests/testData/resolve/propertyVsField/fieldPropertyShadow.kt");
}
@TestMetadata("propertyAndTwoFields.kt")
public void testPropertyAndTwoFields() throws Exception {
runTest("compiler/fir/analysis-tests/testData/resolve/propertyVsField/propertyAndTwoFields.kt");
}
@TestMetadata("propertyFieldShadow.kt")
public void testPropertyFieldShadow() throws Exception {
runTest("compiler/fir/analysis-tests/testData/resolve/propertyVsField/propertyFieldShadow.kt");
}
}
@TestMetadata("compiler/fir/analysis-tests/testData/resolve/references")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
@@ -0,0 +1,28 @@
FILE: DerivedEnum.kt
public final enum class DerivedEnum : R|BaseJava|, R|kotlin/Enum<DerivedEnum>| {
private constructor(): R|DerivedEnum| {
super<R|kotlin/Enum<DerivedEnum>|>()
}
public final static enum entry x: R|DerivedEnum|
public final fun foo(): R|kotlin/Unit| {
R|/bar|(R|/DerivedEnum.x|)
R|/baz|(<Unresolved name: y>#)
R|/baz|(Q|BaseJava|.R|/BaseJava.y|)
}
public final static fun values(): R|kotlin/Array<DerivedEnum>| {
}
public final static fun valueOf(value: R|kotlin/String|): R|DerivedEnum| {
}
public final static val entries: R|kotlin/enums/EnumEntries<DerivedEnum>|
public get(): R|kotlin/enums/EnumEntries<DerivedEnum>|
}
public final fun bar(e: R|DerivedEnum|): R|kotlin/Unit| {
}
public final fun baz(s: R|kotlin/String|): R|kotlin/Unit| {
Q|DerivedEnum|.R|/DerivedEnum.x|
}
@@ -0,0 +1,24 @@
// FILE: BaseJava.java
public interface BaseJava {
String x = "";
String y = "";
}
// FILE: DerivedEnum.kt
enum class DerivedEnum : BaseJava {
x;
fun foo() {
bar(x)
baz(<!UNRESOLVED_REFERENCE!>y<!>)
baz(BaseJava.y)
}
}
fun bar(e: DerivedEnum) {}
fun baz(s: String) {
DerivedEnum.x
}
@@ -0,0 +1,30 @@
FILE: KotlinBase.kt
public open class KotlinBase : R|kotlin/Any| {
public constructor(): R|KotlinBase| {
super<R|kotlin/Any|>()
}
public final val abcd: R|kotlin/String| = String(abcd)
public get(): R|kotlin/String|
}
FILE: KotlinProxy.kt
public abstract interface KotlinProxy : R|kotlin/Any| {
public open val zyxw: R|kotlin/String|
public get(): R|kotlin/String| {
^ String(zyxw)
}
}
FILE: test.kt
public final class Derived : R|Test|, R|KotlinProxy| {
public constructor(): R|Derived| {
super<R|Test|>()
}
public final fun test(): R|kotlin/Unit| {
this@R|/Derived|.R|/Test.abcd|
this@R|/Derived|.R|/Test.zyxw|
}
}
@@ -0,0 +1,28 @@
// FILE: Test.java
public class Test extends KotlinBase {
public final String abcd = "ABCD";
public final String zyxw = "ZYXW";
}
// FILE: KotlinBase.kt
open class KotlinBase {
val abcd = "abcd"
}
// FILE: KotlinProxy.kt
interface KotlinProxy {
val zyxw get() = "zyxw"
}
// FILE: test.kt
class Derived : Test(), KotlinProxy {
fun test() {
abcd // field!
zyxw // field!
}
}
@@ -0,0 +1,14 @@
FILE: KotlinBase.kt
public open class KotlinBase : R|kotlin/Any| {
public constructor(): R|KotlinBase| {
super<R|kotlin/Any|>()
}
public final val abcd: R|kotlin/String| = String(abcd)
public get(): R|kotlin/String|
}
FILE: test.kt
public final fun test(d: R|JavaDerived|): R|kotlin/Unit| {
R|<local>/d|.R|/JavaDerived.abcd|
}
@@ -0,0 +1,23 @@
// FILE: Test.java
public class Test extends KotlinBase {
public final String abcd = "ABCD";
}
// FILE: KotlinBase.kt
open class KotlinBase {
val abcd = "abcd"
}
// FILE: JavaDerived.java
public class JavaDerived extends Test {
public final String abcd = "AaBbCcDd"
}
// FILE: test.kt
fun test(d: JavaDerived) {
d.abcd // JavaDerived
}
@@ -0,0 +1,20 @@
FILE: test.kt
public final class Test2 : R|Test| {
public constructor(): R|Test2| {
super<R|Test|>()
}
private final val text: R|kotlin/String| = String(BCDE)
private get(): R|kotlin/String|
private final val publicPrivateText: R|kotlin/String| = String(YXWV)
private get(): R|kotlin/String|
public final fun check(): R|kotlin/String| {
^check this@R|/Test2|.R|/Test2.text|
}
}
public final fun check(): R|kotlin/String!| {
^check R|/Test2.Test2|().R|/Test.publicPrivateText|
}
@@ -0,0 +1,20 @@
// FILE: Test.java
public class Test {
protected final String text = "ABCD";
public final String publicPrivateText = "ZYXW";
}
// FILE: test.kt
class Test2 : Test() {
private val text = "BCDE"
private val publicPrivateText = "YXWV"
fun check() = text // Should be resolved to Test2.text, not to Test.text
}
fun check() = Test2().publicPrivateText // Should be resolved to Test.publicPrivateText (Test2 member is private)
@@ -3407,6 +3407,12 @@ public class FirDiagnosticTestGenerated extends AbstractFirDiagnosticTest {
runTest("compiler/fir/analysis-tests/testData/resolve/problems/emptySelectorInQualifiedExpression.kt");
}
@Test
@TestMetadata("enumEntryFieldShadow.kt")
public void testEnumEntryFieldShadow() throws Exception {
runTest("compiler/fir/analysis-tests/testData/resolve/problems/enumEntryFieldShadow.kt");
}
@Test
@TestMetadata("expectConstructor.kt")
public void testExpectConstructor() throws Exception {
@@ -3568,6 +3574,34 @@ public class FirDiagnosticTestGenerated extends AbstractFirDiagnosticTest {
}
}
@Nested
@TestMetadata("compiler/fir/analysis-tests/testData/resolve/propertyVsField")
@TestDataPath("$PROJECT_ROOT")
public class PropertyVsField {
@Test
public void testAllFilesPresentInPropertyVsField() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/fir/analysis-tests/testData/resolve/propertyVsField"), Pattern.compile("^([^.]+)\\.kt$"), null, true);
}
@Test
@TestMetadata("fieldPropertyShadow.kt")
public void testFieldPropertyShadow() throws Exception {
runTest("compiler/fir/analysis-tests/testData/resolve/propertyVsField/fieldPropertyShadow.kt");
}
@Test
@TestMetadata("propertyAndTwoFields.kt")
public void testPropertyAndTwoFields() throws Exception {
runTest("compiler/fir/analysis-tests/testData/resolve/propertyVsField/propertyAndTwoFields.kt");
}
@Test
@TestMetadata("propertyFieldShadow.kt")
public void testPropertyFieldShadow() throws Exception {
runTest("compiler/fir/analysis-tests/testData/resolve/propertyVsField/propertyFieldShadow.kt");
}
}
@Nested
@TestMetadata("compiler/fir/analysis-tests/testData/resolve/references")
@TestDataPath("$PROJECT_ROOT")
@@ -3407,6 +3407,12 @@ public class FirDiagnosticsWithLightTreeTestGenerated extends AbstractFirDiagnos
runTest("compiler/fir/analysis-tests/testData/resolve/problems/emptySelectorInQualifiedExpression.kt");
}
@Test
@TestMetadata("enumEntryFieldShadow.kt")
public void testEnumEntryFieldShadow() throws Exception {
runTest("compiler/fir/analysis-tests/testData/resolve/problems/enumEntryFieldShadow.kt");
}
@Test
@TestMetadata("expectConstructor.kt")
public void testExpectConstructor() throws Exception {
@@ -3568,6 +3574,34 @@ public class FirDiagnosticsWithLightTreeTestGenerated extends AbstractFirDiagnos
}
}
@Nested
@TestMetadata("compiler/fir/analysis-tests/testData/resolve/propertyVsField")
@TestDataPath("$PROJECT_ROOT")
public class PropertyVsField {
@Test
public void testAllFilesPresentInPropertyVsField() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/fir/analysis-tests/testData/resolve/propertyVsField"), Pattern.compile("^([^.]+)\\.kt$"), null, true);
}
@Test
@TestMetadata("fieldPropertyShadow.kt")
public void testFieldPropertyShadow() throws Exception {
runTest("compiler/fir/analysis-tests/testData/resolve/propertyVsField/fieldPropertyShadow.kt");
}
@Test
@TestMetadata("propertyAndTwoFields.kt")
public void testPropertyAndTwoFields() throws Exception {
runTest("compiler/fir/analysis-tests/testData/resolve/propertyVsField/propertyAndTwoFields.kt");
}
@Test
@TestMetadata("propertyFieldShadow.kt")
public void testPropertyFieldShadow() throws Exception {
runTest("compiler/fir/analysis-tests/testData/resolve/propertyVsField/propertyFieldShadow.kt");
}
}
@Nested
@TestMetadata("compiler/fir/analysis-tests/testData/resolve/references")
@TestDataPath("$PROJECT_ROOT")