Support underscores in non local destructuring declarations

This commit is contained in:
Mikhail Zarechenskiy
2017-05-25 12:16:04 +03:00
parent 47f2386212
commit d0e26c6527
4 changed files with 21 additions and 2 deletions
@@ -73,6 +73,7 @@ import org.jetbrains.kotlin.resolve.calls.callUtil.CallUtilKt;
import org.jetbrains.kotlin.resolve.calls.model.*;
import org.jetbrains.kotlin.resolve.calls.util.CallMaker;
import org.jetbrains.kotlin.resolve.calls.util.FakeCallableDescriptorForObject;
import org.jetbrains.kotlin.resolve.calls.util.UnderscoreUtilKt;
import org.jetbrains.kotlin.resolve.constants.CompileTimeConstant;
import org.jetbrains.kotlin.resolve.constants.ConstantValue;
import org.jetbrains.kotlin.resolve.constants.evaluate.ConstantExpressionEvaluator;
@@ -3495,7 +3496,7 @@ public class ExpressionCodegen extends KtVisitor<StackValue, StackValue> impleme
VariableDescriptor variableDescriptor = getVariableDescriptorNotNull(variableDeclaration);
// Do not call `componentX` for destructuring entry called _
if (variableDescriptor.getName().isSpecial()) continue;
if (UnderscoreUtilKt.isSingleUnderscore(variableDeclaration)) continue;
if (asProperty && variableDescriptor instanceof PropertyDescriptor) {
StackValue.Property propertyValue = intermediateValueForProperty(
@@ -38,6 +38,7 @@ import org.jetbrains.kotlin.resolve.DescriptorFactory;
import org.jetbrains.kotlin.resolve.DescriptorToSourceUtils;
import org.jetbrains.kotlin.resolve.annotations.AnnotationUtilKt;
import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall;
import org.jetbrains.kotlin.resolve.calls.util.UnderscoreUtilKt;
import org.jetbrains.kotlin.resolve.constants.ConstantValue;
import org.jetbrains.kotlin.resolve.jvm.diagnostics.JvmDeclarationOriginKt;
import org.jetbrains.kotlin.resolve.jvm.jvmSignature.JvmMethodGenericSignature;
@@ -142,12 +143,14 @@ public class PropertyCodegen {
}
private void genDestructuringDeclaration(
@Nullable KtDestructuringDeclarationEntry entry,
@NotNull KtDestructuringDeclarationEntry entry,
@NotNull PropertyDescriptor descriptor
) {
assert kind == OwnerKind.PACKAGE || kind == OwnerKind.IMPLEMENTATION || kind == OwnerKind.DEFAULT_IMPLS
: "Generating property with a wrong kind (" + kind + "): " + descriptor;
if (UnderscoreUtilKt.isSingleUnderscore(entry)) return;
genBackingFieldAndAnnotations(entry, descriptor, false);
generateGetter(entry, descriptor, null);
@@ -0,0 +1,9 @@
val (_, b, _) = A()
class A {
operator fun component1(): Int = throw RuntimeException()
operator fun component2() = 2
operator fun component3(): Int = throw RuntimeException()
}
// expected: b: 2
@@ -54,6 +54,12 @@ public class ScriptCodegenTestGenerated extends AbstractScriptCodegenTest {
doTest(fileName);
}
@TestMetadata("destructuringDeclarationUnderscore.kts")
public void testDestructuringDeclarationUnderscore() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/script/destructuringDeclarationUnderscore.kts");
doTest(fileName);
}
@TestMetadata("empty.kts")
public void testEmpty() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/script/empty.kts");