Stable accessors names

This commit is contained in:
Michael Bogdanov
2015-10-15 17:25:49 +03:00
committed by Max Kammerer
parent 7851abd535
commit 01864801f3
18 changed files with 263 additions and 57 deletions
@@ -31,11 +31,11 @@ public class AccessorForFunctionDescriptor extends AbstractAccessorForFunctionDe
public AccessorForFunctionDescriptor(
@NotNull FunctionDescriptor descriptor,
@NotNull DeclarationDescriptor containingDeclaration,
int index,
@Nullable JetSuperExpression superCallExpression
@Nullable JetSuperExpression superCallExpression,
@NotNull String nameSuffix
) {
super(containingDeclaration,
Name.identifier("access$" + (descriptor instanceof ConstructorDescriptor ? "init" : descriptor.getName()) + "$" + index));
Name.identifier("access$" + nameSuffix));
this.calleeDescriptor = descriptor;
this.superCallExpression = superCallExpression;
@@ -26,9 +26,9 @@ public class AccessorForPropertyBackingFieldInOuterClass extends AccessorForProp
public AccessorForPropertyBackingFieldInOuterClass(
@NotNull PropertyDescriptor property,
@NotNull DeclarationDescriptor containingDeclaration,
int index,
@Nullable JetType delegationType
@Nullable JetType delegationType,
@NotNull String nameSuffix
) {
super(property, delegationType != null ? delegationType : property.getType(), null, null, containingDeclaration, index, null);
super(property, delegationType != null ? delegationType : property.getType(), null, null, containingDeclaration, null, nameSuffix);
}
}
@@ -33,17 +33,17 @@ import java.util.Collections;
public class AccessorForPropertyDescriptor extends PropertyDescriptorImpl implements AccessorForCallableDescriptor<PropertyDescriptor> {
private final PropertyDescriptor calleeDescriptor;
private final int accessorIndex;
private final JetSuperExpression superCallExpression;
@NotNull private final String nameSuffix;
public AccessorForPropertyDescriptor(
@NotNull PropertyDescriptor property,
@NotNull DeclarationDescriptor containingDeclaration,
int index,
@Nullable JetSuperExpression superCallExpression
@Nullable JetSuperExpression superCallExpression,
@NotNull String nameSuffix
) {
this(property, property.getType(), DescriptorUtils.getReceiverParameterType(property.getExtensionReceiverParameter()),
property.getDispatchReceiverParameter(), containingDeclaration, index, superCallExpression);
property.getDispatchReceiverParameter(), containingDeclaration, superCallExpression, nameSuffix);
}
protected AccessorForPropertyDescriptor(
@@ -52,16 +52,16 @@ public class AccessorForPropertyDescriptor extends PropertyDescriptorImpl implem
@Nullable JetType receiverType,
@Nullable ReceiverParameterDescriptor dispatchReceiverParameter,
@NotNull DeclarationDescriptor containingDeclaration,
int index,
@Nullable JetSuperExpression superCallExpression
@Nullable JetSuperExpression superCallExpression,
@NotNull String nameSuffix
) {
super(containingDeclaration, null, Annotations.Companion.getEMPTY(), Modality.FINAL, Visibilities.LOCAL,
original.isVar(), Name.identifier("access$" + getIndexedAccessorSuffix(original, index)),
original.isVar(), Name.identifier("access$" + nameSuffix),
Kind.DECLARATION, SourceElement.NO_SOURCE, /* lateInit = */ false, /* isConst = */ false);
this.calleeDescriptor = original;
this.accessorIndex = index;
this.superCallExpression = superCallExpression;
this.nameSuffix = nameSuffix;
setType(propertyType, Collections.<TypeParameterDescriptorImpl>emptyList(), dispatchReceiverParameter, receiverType);
initialize(new Getter(this), new Setter(this));
}
@@ -85,6 +85,7 @@ public class AccessorForPropertyDescriptor extends PropertyDescriptorImpl implem
public JetSuperExpression getSuperCallExpression() {
return ((AccessorForPropertyDescriptor) getCorrespondingProperty()).getSuperCallExpression();
}
}
public static class Setter extends PropertySetterDescriptorImpl implements AccessorForCallableDescriptor<PropertySetterDescriptor>{
@@ -120,12 +121,7 @@ public class AccessorForPropertyDescriptor extends PropertyDescriptorImpl implem
}
@NotNull
public String getIndexedAccessorSuffix() {
return getIndexedAccessorSuffix(calleeDescriptor, accessorIndex);
}
@NotNull
private static String getIndexedAccessorSuffix(@NotNull PropertyDescriptor original, int index) {
return original.getName() + "$" + index;
public String getAccessorSuffix() {
return nameSuffix;
}
}
@@ -303,11 +303,10 @@ public abstract class CodegenContext<T extends DeclarationDescriptor> {
accessor instanceof AccessorForPropertyBackingFieldInOuterClass : "There is already exists accessor with isForBackingFieldInOuterClass = false in this context";
return (D) accessor;
}
int accessorIndex = accessors.size();
String nameSuffix = SyntheticAccessorUtilKt.getAccessorNameSuffix(descriptor, key.superCallLabelTarget, isForBackingFieldInOuterClass);
if (descriptor instanceof SimpleFunctionDescriptor) {
accessor = new AccessorForFunctionDescriptor(
(FunctionDescriptor) descriptor, contextDescriptor, accessorIndex, superCallExpression
(FunctionDescriptor) descriptor, contextDescriptor, superCallExpression, nameSuffix
);
}
else if (descriptor instanceof ConstructorDescriptor) {
@@ -316,11 +315,11 @@ public abstract class CodegenContext<T extends DeclarationDescriptor> {
else if (descriptor instanceof PropertyDescriptor) {
if (isForBackingFieldInOuterClass) {
accessor = new AccessorForPropertyBackingFieldInOuterClass((PropertyDescriptor) descriptor, contextDescriptor,
accessorIndex, delegateType);
delegateType, nameSuffix);
}
else {
accessor = new AccessorForPropertyDescriptor((PropertyDescriptor) descriptor, contextDescriptor,
accessorIndex, superCallExpression);
superCallExpression, nameSuffix);
}
}
else {
@@ -858,7 +858,7 @@ public class JetTypeMapper {
boolean isAccessor = property instanceof AccessorForPropertyDescriptor;
String propertyName = isAccessor
? ((AccessorForPropertyDescriptor) property).getIndexedAccessorSuffix()
? ((AccessorForPropertyDescriptor) property).getAccessorSuffix()
: property.getName().asString();
String accessorName = descriptor instanceof PropertyGetterDescriptor
@@ -0,0 +1,34 @@
/*
* Copyright 2010-2015 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.kotlin.codegen
import org.jetbrains.kotlin.descriptors.*
fun getAccessorNameSuffix(descriptor: CallableMemberDescriptor, superCallDescriptor: ClassDescriptor?, isForBackingFieldInOuterClass: Boolean): String {
val suffix = when (descriptor) {
is ConstructorDescriptor ->
return "will be ignored"
is SimpleFunctionDescriptor ->
descriptor.name.asString()
is PropertyDescriptor ->
descriptor.name.asString() + "$" + (if (isForBackingFieldInOuterClass) "cp" else "p")
else ->
throw UnsupportedOperationException("Do not know how to create accessor for descriptor " + descriptor)
}
return if (superCallDescriptor == null) suffix else "$suffix\$s${superCallDescriptor.name.asString().hashCode()}"
}
+1 -1
View File
@@ -23,7 +23,7 @@ class SubTr : Tr {
// Clashing synthetic accessors are only reported in compiler, IDE doesn't see them
class C {
private fun f() {}
fun `access$f$0`(c: C) {}
fun `access$f`(c: C) {}
class Nested {
+6 -6
View File
@@ -33,14 +33,14 @@ compiler/testData/cli/jvm/signatureClash.kt:20:5: error: platform declaration cl
fun getTr(): kotlin.Int
val tr = 1
^
compiler/testData/cli/jvm/signatureClash.kt:24:7: error: platform declaration clash: The following declarations have the same JVM signature (access$f$0(LC;)V):
fun `access$f$0`(c: C): kotlin.Unit
compiler/testData/cli/jvm/signatureClash.kt:24:7: error: platform declaration clash: The following declarations have the same JVM signature (access$f(LC;)V):
fun `access$f`(c: C): kotlin.Unit
fun f(): kotlin.Unit
class C {
^
compiler/testData/cli/jvm/signatureClash.kt:26:5: error: platform declaration clash: The following declarations have the same JVM signature (access$f$0(LC;)V):
fun `access$f$0`(c: C): kotlin.Unit
compiler/testData/cli/jvm/signatureClash.kt:26:5: error: platform declaration clash: The following declarations have the same JVM signature (access$f(LC;)V):
fun `access$f`(c: C): kotlin.Unit
fun f(): kotlin.Unit
fun `access$f$0`(c: C) {}
fun `access$f`(c: C) {}
^
COMPILATION_ERROR
COMPILATION_ERROR
@@ -0,0 +1,3 @@
$TESTDATA_DIR$/syntheticAccessorForPropertiesSignatureClash.kt
-d
$TEMP_DIR$
@@ -0,0 +1,32 @@
class A {
private var foo = 1;
fun `access$getFoo$p`(a: A): Int = 1
fun `access$setFoo$p`(a: A, d: Int) {}
//companion backing field accessors
fun `access$getFoo$cp`(): Int = 1
fun `access$setFoo$cp`(d: Int) {}
companion object {
private var foo = 1;
fun test() {
{
foo = 2;
foo
}()
}
fun `access$getFoo$p`(p: A.Companion): Int = 1
fun `access$setFoo$p`(p: A.Companion, d: Int) {}
}
fun test() {
{
foo = 2;
foo
}()
}
}
@@ -0,0 +1,61 @@
compiler/testData/cli/jvm/syntheticAccessorForPropertiesSignatureClash.kt:1:7: error: platform declaration clash: The following declarations have the same JVM signature (access$setFoo$p(LA;I)V):
fun `access$setFoo$p`(a: A, d: kotlin.Int): kotlin.Unit
fun <set-foo>(<set-?>: kotlin.Int): kotlin.Unit
class A {
^
compiler/testData/cli/jvm/syntheticAccessorForPropertiesSignatureClash.kt:1:7: error: platform declaration clash: The following declarations have the same JVM signature (access$getFoo$p(LA;)I):
fun `access$getFoo$p`(a: A): kotlin.Int
fun <get-foo>(): kotlin.Int
class A {
^
compiler/testData/cli/jvm/syntheticAccessorForPropertiesSignatureClash.kt:1:7: error: platform declaration clash: The following declarations have the same JVM signature (access$setFoo$cp(I)V):
fun `access$setFoo$cp`(d: kotlin.Int): kotlin.Unit
fun <set-foo>(<set-?>: kotlin.Int): kotlin.Unit
class A {
^
compiler/testData/cli/jvm/syntheticAccessorForPropertiesSignatureClash.kt:1:7: error: platform declaration clash: The following declarations have the same JVM signature (access$getFoo$cp()I):
fun `access$getFoo$cp`(): kotlin.Int
fun <get-foo>(): kotlin.Int
class A {
^
compiler/testData/cli/jvm/syntheticAccessorForPropertiesSignatureClash.kt:5:5: error: platform declaration clash: The following declarations have the same JVM signature (access$getFoo$p(LA;)I):
fun `access$getFoo$p`(a: A): kotlin.Int
fun <get-foo>(): kotlin.Int
fun `access$getFoo$p`(a: A): Int = 1
^
compiler/testData/cli/jvm/syntheticAccessorForPropertiesSignatureClash.kt:6:5: error: platform declaration clash: The following declarations have the same JVM signature (access$setFoo$p(LA;I)V):
fun `access$setFoo$p`(a: A, d: kotlin.Int): kotlin.Unit
fun <set-foo>(<set-?>: kotlin.Int): kotlin.Unit
fun `access$setFoo$p`(a: A, d: Int) {}
^
compiler/testData/cli/jvm/syntheticAccessorForPropertiesSignatureClash.kt:9:5: error: platform declaration clash: The following declarations have the same JVM signature (access$getFoo$cp()I):
fun `access$getFoo$cp`(): kotlin.Int
fun <get-foo>(): kotlin.Int
fun `access$getFoo$cp`(): Int = 1
^
compiler/testData/cli/jvm/syntheticAccessorForPropertiesSignatureClash.kt:10:5: error: platform declaration clash: The following declarations have the same JVM signature (access$setFoo$cp(I)V):
fun `access$setFoo$cp`(d: kotlin.Int): kotlin.Unit
fun <set-foo>(<set-?>: kotlin.Int): kotlin.Unit
fun `access$setFoo$cp`(d: Int) {}
^
compiler/testData/cli/jvm/syntheticAccessorForPropertiesSignatureClash.kt:12:15: error: platform declaration clash: The following declarations have the same JVM signature (access$setFoo$p(LA$Companion;I)V):
fun `access$setFoo$p`(p: A.Companion, d: kotlin.Int): kotlin.Unit
fun <set-foo>(<set-?>: kotlin.Int): kotlin.Unit
companion object {
^
compiler/testData/cli/jvm/syntheticAccessorForPropertiesSignatureClash.kt:12:15: error: platform declaration clash: The following declarations have the same JVM signature (access$getFoo$p(LA$Companion;)I):
fun `access$getFoo$p`(p: A.Companion): kotlin.Int
fun <get-foo>(): kotlin.Int
companion object {
^
compiler/testData/cli/jvm/syntheticAccessorForPropertiesSignatureClash.kt:22:9: error: platform declaration clash: The following declarations have the same JVM signature (access$getFoo$p(LA$Companion;)I):
fun `access$getFoo$p`(p: A.Companion): kotlin.Int
fun <get-foo>(): kotlin.Int
fun `access$getFoo$p`(p: A.Companion): Int = 1
^
compiler/testData/cli/jvm/syntheticAccessorForPropertiesSignatureClash.kt:23:9: error: platform declaration clash: The following declarations have the same JVM signature (access$setFoo$p(LA$Companion;I)V):
fun `access$setFoo$p`(p: A.Companion, d: kotlin.Int): kotlin.Unit
fun <set-foo>(<set-?>: kotlin.Int): kotlin.Unit
fun `access$setFoo$p`(p: A.Companion, d: Int) {}
^
COMPILATION_ERROR
@@ -0,0 +1,3 @@
$TESTDATA_DIR$/syntheticAccessorPropertyAndFunSignatureClash.kt
-d
$TEMP_DIR$
@@ -0,0 +1,25 @@
class A {
private var foo = 1;
fun `access$getFoo$p`(a: A): Int = 1
fun `access$setFoo$p`(a: A, d: Int) {}
private fun getFoo() = 1;
private fun setFoo(i: Int) {}
fun `access$getFoo`(a: A): Int = 1
fun `access$setFoo`(a: A, d: Int) {}
fun test() {
{
foo = 2;
foo
}();
{
setFoo(2)
getFoo();
}()
}
}
@@ -0,0 +1,41 @@
compiler/testData/cli/jvm/syntheticAccessorPropertyAndFunSignatureClash.kt:1:7: error: platform declaration clash: The following declarations have the same JVM signature (access$getFoo(LA;)I):
fun `access$getFoo`(a: A): kotlin.Int
fun getFoo(): kotlin.Int
class A {
^
compiler/testData/cli/jvm/syntheticAccessorPropertyAndFunSignatureClash.kt:1:7: error: platform declaration clash: The following declarations have the same JVM signature (access$setFoo(LA;I)V):
fun `access$setFoo`(a: A, d: kotlin.Int): kotlin.Unit
fun setFoo(i: kotlin.Int): kotlin.Unit
class A {
^
compiler/testData/cli/jvm/syntheticAccessorPropertyAndFunSignatureClash.kt:1:7: error: platform declaration clash: The following declarations have the same JVM signature (access$getFoo$p(LA;)I):
fun `access$getFoo$p`(a: A): kotlin.Int
fun <get-foo>(): kotlin.Int
class A {
^
compiler/testData/cli/jvm/syntheticAccessorPropertyAndFunSignatureClash.kt:1:7: error: platform declaration clash: The following declarations have the same JVM signature (access$setFoo$p(LA;I)V):
fun `access$setFoo$p`(a: A, d: kotlin.Int): kotlin.Unit
fun <set-foo>(<set-?>: kotlin.Int): kotlin.Unit
class A {
^
compiler/testData/cli/jvm/syntheticAccessorPropertyAndFunSignatureClash.kt:5:5: error: platform declaration clash: The following declarations have the same JVM signature (access$getFoo$p(LA;)I):
fun `access$getFoo$p`(a: A): kotlin.Int
fun <get-foo>(): kotlin.Int
fun `access$getFoo$p`(a: A): Int = 1
^
compiler/testData/cli/jvm/syntheticAccessorPropertyAndFunSignatureClash.kt:6:5: error: platform declaration clash: The following declarations have the same JVM signature (access$setFoo$p(LA;I)V):
fun `access$setFoo$p`(a: A, d: kotlin.Int): kotlin.Unit
fun <set-foo>(<set-?>: kotlin.Int): kotlin.Unit
fun `access$setFoo$p`(a: A, d: Int) {}
^
compiler/testData/cli/jvm/syntheticAccessorPropertyAndFunSignatureClash.kt:11:5: error: platform declaration clash: The following declarations have the same JVM signature (access$getFoo(LA;)I):
fun `access$getFoo`(a: A): kotlin.Int
fun getFoo(): kotlin.Int
fun `access$getFoo`(a: A): Int = 1
^
compiler/testData/cli/jvm/syntheticAccessorPropertyAndFunSignatureClash.kt:12:5: error: platform declaration clash: The following declarations have the same JVM signature (access$setFoo(LA;I)V):
fun `access$setFoo`(a: A, d: kotlin.Int): kotlin.Unit
fun setFoo(i: kotlin.Int): kotlin.Unit
fun `access$setFoo`(a: A, d: Int) {}
^
COMPILATION_ERROR
@@ -1,14 +1,14 @@
open class Base {
open fun `access$foo$0`(d: Derived) {}
open fun `access$foo`(d: Derived) {}
open fun `access$getBar$1`(d: Derived): Int = 1
open fun `access$setBar$1`(d: Derived, i: Int) {}
open fun `access$getBar$p`(d: Derived): Int = 1
open fun `access$setBar$p`(d: Derived, i: Int) {}
open fun `access$getBaz$2`(d: Derived): Int = 1
open fun `access$getBaz$p`(d: Derived): Int = 1
open fun `access$getBoo$3`(d: Derived): Int = 1
open fun `access$getBoo$p`(d: Derived): Int = 1
open fun `access$setBar1$4`(d: Derived, i: Int) {}
open fun `access$setBar1$p`(d: Derived, i: Int) {}
}
class Derived : Base() {
+13 -13
View File
@@ -1,31 +1,31 @@
compiler/testData/cli/jvm/syntheticAccessorSignatureClash.kt:15:5: error: accidental override: The following declarations have the same JVM signature (access$foo$0(LDerived;)V):
fun `access$foo$0`(d: Derived): kotlin.Unit
compiler/testData/cli/jvm/syntheticAccessorSignatureClash.kt:15:5: error: accidental override: The following declarations have the same JVM signature (access$foo(LDerived;)V):
fun `access$foo`(d: Derived): kotlin.Unit
fun foo(): kotlin.Unit
private fun foo() {}
^
compiler/testData/cli/jvm/syntheticAccessorSignatureClash.kt:18:9: error: accidental override: The following declarations have the same JVM signature (access$getBar$1(LDerived;)I):
fun `access$getBar$1`(d: Derived): kotlin.Int
compiler/testData/cli/jvm/syntheticAccessorSignatureClash.kt:18:9: error: accidental override: The following declarations have the same JVM signature (access$getBar$p(LDerived;)I):
fun `access$getBar$p`(d: Derived): kotlin.Int
fun <get-bar>(): kotlin.Int
get
^
compiler/testData/cli/jvm/syntheticAccessorSignatureClash.kt:19:9: error: accidental override: The following declarations have the same JVM signature (access$setBar$1(LDerived;I)V):
fun `access$setBar$1`(d: Derived, i: kotlin.Int): kotlin.Unit
compiler/testData/cli/jvm/syntheticAccessorSignatureClash.kt:19:9: error: accidental override: The following declarations have the same JVM signature (access$setBar$p(LDerived;I)V):
fun `access$setBar$p`(d: Derived, i: kotlin.Int): kotlin.Unit
fun <set-bar>(<set-?>: kotlin.Int): kotlin.Unit
set
^
compiler/testData/cli/jvm/syntheticAccessorSignatureClash.kt:21:5: error: accidental override: The following declarations have the same JVM signature (access$getBaz$2(LDerived;)I):
fun `access$getBaz$2`(d: Derived): kotlin.Int
compiler/testData/cli/jvm/syntheticAccessorSignatureClash.kt:21:5: error: accidental override: The following declarations have the same JVM signature (access$getBaz$p(LDerived;)I):
fun `access$getBaz$p`(d: Derived): kotlin.Int
fun <get-baz>(): kotlin.Int
private var baz = 1
^
compiler/testData/cli/jvm/syntheticAccessorSignatureClash.kt:23:5: error: accidental override: The following declarations have the same JVM signature (access$getBoo$3(LDerived;)I):
fun `access$getBoo$3`(d: Derived): kotlin.Int
compiler/testData/cli/jvm/syntheticAccessorSignatureClash.kt:23:5: error: accidental override: The following declarations have the same JVM signature (access$getBoo$p(LDerived;)I):
fun `access$getBoo$p`(d: Derived): kotlin.Int
fun <get-boo>(): kotlin.Int
private val boo = 1
^
compiler/testData/cli/jvm/syntheticAccessorSignatureClash.kt:27:9: error: accidental override: The following declarations have the same JVM signature (access$setBar1$4(LDerived;I)V):
fun `access$setBar1$4`(d: Derived, i: kotlin.Int): kotlin.Unit
compiler/testData/cli/jvm/syntheticAccessorSignatureClash.kt:27:9: error: accidental override: The following declarations have the same JVM signature (access$setBar1$p(LDerived;I)V):
fun `access$setBar1$p`(d: Derived, i: kotlin.Int): kotlin.Unit
fun <set-bar1>(<set-?>: kotlin.Int): kotlin.Unit
set
^
COMPILATION_ERROR
COMPILATION_ERROR
@@ -217,6 +217,18 @@ public class KotlincExecutableTestGenerated extends AbstractKotlincExecutableTes
doJvmTest(fileName);
}
@TestMetadata("syntheticAccessorForPropertiesSignatureClash.args")
public void testSyntheticAccessorForPropertiesSignatureClash() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/cli/jvm/syntheticAccessorForPropertiesSignatureClash.args");
doJvmTest(fileName);
}
@TestMetadata("syntheticAccessorPropertyAndFunSignatureClash.args")
public void testSyntheticAccessorPropertyAndFunSignatureClash() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/cli/jvm/syntheticAccessorPropertyAndFunSignatureClash.args");
doJvmTest(fileName);
}
@TestMetadata("syntheticAccessorSignatureClash.args")
public void testSyntheticAccessorSignatureClash() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/cli/jvm/syntheticAccessorSignatureClash.args");
@@ -99,10 +99,10 @@ public class MethodOrderTest: CodegenTestCase() {
listOf(
"c()V",
"<init>(ILjava/lang/String;)V",
"access\$getB$0(LOuter;)Ljava/lang/String;",
"access\$setB$0(LOuter;Ljava/lang/String;)V",
"access\$getA$1(LOuter;)I",
"access\$c$2(LOuter;)V"
"access\$getB\$p(LOuter;)Ljava/lang/String;",
"access\$setB\$p(LOuter;Ljava/lang/String;)V",
"access\$getA\$p(LOuter;)I",
"access\$c(LOuter;)V"
)
)
}