Look into proper context when generating backing field access

Problem manifests when a class property name matches a companion object
property name, and class property is referenced in closure context.

 #KT-19367 Fixed Target versions 1.1.5
This commit is contained in:
Dmitry Petrov
2017-08-01 10:35:57 +03:00
parent 05eee9c173
commit c5613888eb
6 changed files with 47 additions and 1 deletions
@@ -1868,7 +1868,14 @@ public class ExpressionCodegen extends KtVisitor<StackValue, StackValue> impleme
@NotNull DeclarationDescriptor containingDeclaration
) {
switch (accessorKind) {
case NORMAL: return context.getParentContext();
case NORMAL:
if (containingDeclaration instanceof ClassDescriptor) {
CodegenContext parentWithDescriptor = context.findParentContextWithDescriptor(containingDeclaration);
if (parentWithDescriptor != null) {
return parentWithDescriptor;
}
}
return context.getParentContext();
// For companion object property, backing field lives in object containing class
// Otherwise, it lives in its containing declaration
case IN_CLASS_COMPANION: return context.findParentContextWithDescriptor(containingDeclaration.getContainingDeclaration());
@@ -0,0 +1,15 @@
// WITH_RUNTIME
class Host {
val ok = "OK"
fun foo() = run { bar(ok) }
companion object {
val ok = 0
fun bar(s: String) = s.substring(ok)
}
}
fun box() = Host().foo()
@@ -2921,6 +2921,12 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes
doTest(fileName);
}
@TestMetadata("comanionObjectFieldVsClassField.kt")
public void testComanionObjectFieldVsClassField() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/classes/comanionObjectFieldVsClassField.kt");
doTest(fileName);
}
@TestMetadata("defaultObjectSameNamesAsInOuter.kt")
public void testDefaultObjectSameNamesAsInOuter() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/classes/defaultObjectSameNamesAsInOuter.kt");
@@ -2921,6 +2921,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
doTest(fileName);
}
@TestMetadata("comanionObjectFieldVsClassField.kt")
public void testComanionObjectFieldVsClassField() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/classes/comanionObjectFieldVsClassField.kt");
doTest(fileName);
}
@TestMetadata("defaultObjectSameNamesAsInOuter.kt")
public void testDefaultObjectSameNamesAsInOuter() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/classes/defaultObjectSameNamesAsInOuter.kt");
@@ -2921,6 +2921,12 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes
doTest(fileName);
}
@TestMetadata("comanionObjectFieldVsClassField.kt")
public void testComanionObjectFieldVsClassField() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/classes/comanionObjectFieldVsClassField.kt");
doTest(fileName);
}
@TestMetadata("defaultObjectSameNamesAsInOuter.kt")
public void testDefaultObjectSameNamesAsInOuter() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/classes/defaultObjectSameNamesAsInOuter.kt");
@@ -3539,6 +3539,12 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest {
doTest(fileName);
}
@TestMetadata("comanionObjectFieldVsClassField.kt")
public void testComanionObjectFieldVsClassField() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/classes/comanionObjectFieldVsClassField.kt");
doTest(fileName);
}
@TestMetadata("defaultObjectSameNamesAsInOuter.kt")
public void testDefaultObjectSameNamesAsInOuter() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/classes/defaultObjectSameNamesAsInOuter.kt");