Don't generate component function in lazy resolve for parameter if it has no val/var

This commit is contained in:
Nikolay Krasko
2014-09-08 14:59:09 +04:00
parent caee19cdc7
commit 65d358a066
9 changed files with 68 additions and 5 deletions
@@ -806,7 +806,7 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen {
FunctionCodegen.endVisit(mv, "toString", myClass);
}
private Type genPropertyOnStack(InstructionAdapter iv, MethodContext context, PropertyDescriptor propertyDescriptor, int index) {
private Type genPropertyOnStack(InstructionAdapter iv, MethodContext context, @NotNull PropertyDescriptor propertyDescriptor, int index) {
iv.load(index, classAsmType);
if (couldUseDirectAccessToProperty(propertyDescriptor, /* forGetter = */ true, /* isDelegated = */ false, context)) {
Type type = typeMapper.mapType(propertyDescriptor.getType());
@@ -839,6 +839,8 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen {
if (!componentType.equals(Type.VOID_TYPE)) {
PropertyDescriptor property =
bindingContext.get(BindingContext.PRIMARY_CONSTRUCTOR_PARAMETER, descriptorToDeclaration(parameter));
assert property != null : "Property descriptor is not found for primary constructor parameter: " + parameter;
genPropertyOnStack(iv, context, property, 0);
}
iv.areturn(componentType);
@@ -157,19 +157,26 @@ public class LazyClassMemberScope extends AbstractLazyMemberScope<LazyClassDescr
ConstructorDescriptor constructor = getPrimaryConstructor();
if (constructor == null) return;
int parameterIndex = 0;
List<? extends JetParameter> primaryConstructorParameters = declarationProvider.getOwnerInfo().getPrimaryConstructorParameters();
assert constructor.getValueParameters().size() == primaryConstructorParameters.size()
: "From descriptor: " + constructor.getValueParameters().size() + " but from PSI: " + primaryConstructorParameters.size();
int componentIndex = 0;
for (ValueParameterDescriptor parameter : constructor.getValueParameters()) {
if (parameter.getType().isError()) continue;
if (!primaryConstructorParameters.get(parameter.getIndex()).hasValOrVarNode()) continue;
Set<VariableDescriptor> properties = getProperties(parameter.getName());
if (properties.isEmpty()) continue;
assert properties.size() == 1 : "A constructor parameter is resolved to more than one (" + properties.size() + ") property: " + parameter;
PropertyDescriptor property = (PropertyDescriptor) properties.iterator().next();
if (property == null) continue;
++parameterIndex;
if (name.equals(Name.identifier(DescriptorResolver.COMPONENT_FUNCTION_NAME_PREFIX + parameterIndex))) {
++componentIndex;
if (name.equals(Name.identifier(DescriptorResolver.COMPONENT_FUNCTION_NAME_PREFIX + componentIndex))) {
SimpleFunctionDescriptor functionDescriptor =
DescriptorResolver.createComponentFunctionDescriptor(parameterIndex, property,
DescriptorResolver.createComponentFunctionDescriptor(componentIndex, property,
parameter, thisDescriptor, trace);
result.add(functionDescriptor);
break;
@@ -0,0 +1,10 @@
data class A(foo: String, val bar: Int, other: Long) {
val foo = foo
val other = other
}
fun test(a: A) {
a.component1()
a.<!UNRESOLVED_REFERENCE!>component2<!>()
a.<!UNRESOLVED_REFERENCE!>component3<!>()
}
@@ -0,0 +1,7 @@
//ALLOW_AST_ACCESS
package test
data class A(foo: String, val bar: Int, other: Long) {
val foo = foo
val other = other
}
@@ -0,0 +1,13 @@
package test
kotlin.data() internal final class A {
/*primary*/ public constructor A(/*0*/ foo: kotlin.String, /*1*/ bar: kotlin.Int, /*2*/ other: kotlin.Long)
internal final val bar: kotlin.Int
internal final fun <get-bar>(): kotlin.Int
internal final val foo: kotlin.String
internal final fun <get-foo>(): kotlin.String
internal final val other: kotlin.Long
internal final fun <get-other>(): kotlin.Long
internal final /*synthesized*/ fun component1(): kotlin.Int
internal final /*synthesized*/ fun copy(/*0*/ foo: kotlin.String, /*1*/ bar: kotlin.Int = ..., /*2*/ other: kotlin.Long): test.A
}
@@ -2151,6 +2151,12 @@ public class JetDiagnosticsTestGenerated extends AbstractJetDiagnosticsTest {
doTest(fileName);
}
@TestMetadata("paramNameSameToField.kt")
public void testParamNameSameToField() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/dataClasses/paramNameSameToField.kt");
doTest(fileName);
}
@TestMetadata("secondParamIsVal.kt")
public void testSecondParamIsVal() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/dataClasses/secondParamIsVal.kt");
@@ -2717,6 +2717,12 @@ public class LoadJavaTestGenerated extends AbstractLoadJavaTest {
doTestCompiledKotlin(fileName);
}
@TestMetadata("ParamNameSameToField.kt")
public void testParamNameSameToField() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/loadJava/compiledKotlin/dataClass/ParamNameSameToField.kt");
doTestCompiledKotlin(fileName);
}
@TestMetadata("TwoVals.kt")
public void testTwoVals() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/loadJava/compiledKotlin/dataClass/TwoVals.kt");
@@ -959,6 +959,12 @@ public class LazyResolveRecursiveComparingTestGenerated extends AbstractLazyReso
doTest(fileName);
}
@TestMetadata("ParamNameSameToField.kt")
public void testParamNameSameToField() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/loadJava/compiledKotlin/dataClass/ParamNameSameToField.kt");
doTest(fileName);
}
@TestMetadata("TwoVals.kt")
public void testTwoVals() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/loadJava/compiledKotlin/dataClass/TwoVals.kt");
@@ -956,6 +956,12 @@ public class LazyResolveByStubTestGenerated extends AbstractLazyResolveByStubTes
doTest(fileName);
}
@TestMetadata("ParamNameSameToField.kt")
public void testParamNameSameToField() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/loadJava/compiledKotlin/dataClass/ParamNameSameToField.kt");
doTest(fileName);
}
@TestMetadata("TwoVals.kt")
public void testTwoVals() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/loadJava/compiledKotlin/dataClass/TwoVals.kt");