Fix for KT-12200: initial property assignment ignored
#KT-12200 Fixed
This commit is contained in:
@@ -409,7 +409,8 @@ public abstract class MemberCodegen<T extends KtElement/* TODO: & JetDeclaration
|
|||||||
|
|
||||||
KtExpression initializer = property.getInitializer();
|
KtExpression initializer = property.getInitializer();
|
||||||
|
|
||||||
ConstantValue<?> initializerValue = computeInitializerValue(property, propertyDescriptor, initializer);
|
ConstantValue<?> initializerValue =
|
||||||
|
initializer != null ? ExpressionCodegen.getCompileTimeConstant(initializer, bindingContext) : null;
|
||||||
// we must write constant values for fields in light classes,
|
// we must write constant values for fields in light classes,
|
||||||
// because Java's completion for annotation arguments uses this information
|
// because Java's completion for annotation arguments uses this information
|
||||||
if (initializerValue == null) return state.getClassBuilderMode() == ClassBuilderMode.FULL;
|
if (initializerValue == null) return state.getClassBuilderMode() == ClassBuilderMode.FULL;
|
||||||
@@ -420,19 +421,6 @@ public abstract class MemberCodegen<T extends KtElement/* TODO: & JetDeclaration
|
|||||||
return !skipDefaultValue(propertyDescriptor, initializerValue.getValue(), type);
|
return !skipDefaultValue(propertyDescriptor, initializerValue.getValue(), type);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nullable
|
|
||||||
private ConstantValue<?> computeInitializerValue(
|
|
||||||
@NotNull KtProperty property,
|
|
||||||
@NotNull PropertyDescriptor propertyDescriptor,
|
|
||||||
@Nullable KtExpression initializer
|
|
||||||
) {
|
|
||||||
if (property.isVar() && initializer != null) {
|
|
||||||
BindingTrace tempTrace = TemporaryBindingTrace.create(state.getBindingTrace(), "property initializer");
|
|
||||||
return constantExpressionEvaluator.evaluateToConstantValue(initializer, tempTrace, propertyDescriptor.getType());
|
|
||||||
}
|
|
||||||
return propertyDescriptor.getCompileTimeInitializer();
|
|
||||||
}
|
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
private KotlinType getPropertyOrDelegateType(@NotNull KtProperty property, @NotNull PropertyDescriptor descriptor) {
|
private KotlinType getPropertyOrDelegateType(@NotNull KtProperty property, @NotNull PropertyDescriptor descriptor) {
|
||||||
KtExpression delegateExpression = property.getDelegateExpression();
|
KtExpression delegateExpression = property.getDelegateExpression();
|
||||||
|
|||||||
@@ -0,0 +1,30 @@
|
|||||||
|
//WITH_RUNTIME
|
||||||
|
|
||||||
|
class ThingTemplate {
|
||||||
|
val prop = 0
|
||||||
|
}
|
||||||
|
|
||||||
|
class ThingVal(template: ThingTemplate) {
|
||||||
|
val prop = template.prop
|
||||||
|
}
|
||||||
|
|
||||||
|
class ThingVar(template: ThingTemplate) {
|
||||||
|
var prop = template.prop
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
fun box() : String {
|
||||||
|
val template = ThingTemplate();
|
||||||
|
val javaClass = ThingTemplate::class.java
|
||||||
|
val field = javaClass.getDeclaredField("prop")!!
|
||||||
|
field.isAccessible = true
|
||||||
|
field.set(template, 1)
|
||||||
|
|
||||||
|
val thingVal = ThingVal(template)
|
||||||
|
if (thingVal.prop != 1) return "fail 1"
|
||||||
|
|
||||||
|
val thingVar = ThingVar(template)
|
||||||
|
if (thingVar.prop != 1) return "fail 2"
|
||||||
|
|
||||||
|
return "OK"
|
||||||
|
}
|
||||||
@@ -0,0 +1,40 @@
|
|||||||
|
//WITH_RUNTIME
|
||||||
|
//FULL_JDK
|
||||||
|
//NOTE this test should be removed if Kotlin const properties will became inlined
|
||||||
|
|
||||||
|
import java.lang.reflect.Field
|
||||||
|
import java.lang.reflect.Modifier
|
||||||
|
|
||||||
|
object ThingTemplate {
|
||||||
|
const val prop = 0
|
||||||
|
}
|
||||||
|
|
||||||
|
class ThingVal {
|
||||||
|
val prop = ThingTemplate.prop
|
||||||
|
}
|
||||||
|
|
||||||
|
class ThingVar {
|
||||||
|
var prop = ThingTemplate.prop
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
fun box() : String {
|
||||||
|
val template = ThingTemplate;
|
||||||
|
val javaClass = ThingTemplate::class.java
|
||||||
|
val field = javaClass.getDeclaredField("prop")!!
|
||||||
|
field.isAccessible = true
|
||||||
|
|
||||||
|
val modifiersField = Field::class.java!!.getDeclaredField("modifiers")
|
||||||
|
modifiersField.isAccessible = true
|
||||||
|
modifiersField.setInt(field, field.modifiers and Modifier.FINAL.inv())
|
||||||
|
|
||||||
|
field.set(null, 1)
|
||||||
|
|
||||||
|
val thingVal = ThingVal()
|
||||||
|
if (thingVal.prop != 1) return "fail 1"
|
||||||
|
|
||||||
|
val thingVar = ThingVar()
|
||||||
|
if (thingVar.prop != 1) return "fail 2"
|
||||||
|
|
||||||
|
return "OK"
|
||||||
|
}
|
||||||
@@ -9964,6 +9964,18 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
|
|||||||
doTest(fileName);
|
doTest(fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("kt12200.kt")
|
||||||
|
public void testKt12200() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/properties/kt12200.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("kt12200Const.kt")
|
||||||
|
public void testKt12200Const() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/properties/kt12200Const.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("kt1398.kt")
|
@TestMetadata("kt1398.kt")
|
||||||
public void testKt1398() throws Exception {
|
public void testKt1398() throws Exception {
|
||||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/properties/kt1398.kt");
|
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/properties/kt1398.kt");
|
||||||
|
|||||||
Reference in New Issue
Block a user