Skip to content

Commit 3ee918b

Browse files
author
Mikhael Bogdanov
committed
KT-4138: java.lang.VerifyError: (class: kotlin/sql/tests/h2/Foo, method: getA$b$0 signature: ()Lkotlin/sql/tests/h2/Foo;) Wrong return type in function
#KT-4138 Fixed
1 parent 12d0da8 commit 3ee918b

File tree

6 files changed

+110
-12
lines changed

6 files changed

+110
-12
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright 2010-2013 JetBrains s.r.o.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.jetbrains.jet.codegen;
18+
19+
import org.jetbrains.annotations.NotNull;
20+
import org.jetbrains.annotations.Nullable;
21+
import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor;
22+
import org.jetbrains.jet.lang.descriptors.PropertyDescriptor;
23+
import org.jetbrains.jet.lang.types.JetType;
24+
25+
public class AccessorForPropertyBackingFieldInOuterClass extends AccessorForPropertyDescriptor {
26+
27+
public AccessorForPropertyBackingFieldInOuterClass(
28+
@NotNull PropertyDescriptor pd,
29+
@NotNull DeclarationDescriptor containingDeclaration,
30+
int index,
31+
@Nullable JetType delegationType
32+
) {
33+
super(pd, delegationType != null ? delegationType : pd.getType(), null, null, containingDeclaration, index);
34+
}
35+
}

compiler/backend/src/org/jetbrains/jet/codegen/AccessorForPropertyDescriptor.java

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package org.jetbrains.jet.codegen;
1818

19+
import org.jetbrains.annotations.NotNull;
20+
import org.jetbrains.annotations.Nullable;
1921
import org.jetbrains.jet.lang.descriptors.*;
2022
import org.jetbrains.jet.lang.descriptors.annotations.AnnotationDescriptor;
2123
import org.jetbrains.jet.lang.descriptors.impl.PropertyDescriptorImpl;
@@ -29,17 +31,23 @@
2931
import java.util.Collections;
3032

3133
public class AccessorForPropertyDescriptor extends PropertyDescriptorImpl {
32-
public AccessorForPropertyDescriptor(PropertyDescriptor pd, DeclarationDescriptor containingDeclaration, int index) {
34+
public AccessorForPropertyDescriptor(@NotNull PropertyDescriptor pd, @NotNull DeclarationDescriptor containingDeclaration, int index) {
35+
this(pd, pd.getType(), DescriptorUtils.getReceiverParameterType(pd.getReceiverParameter()), pd.getExpectedThisObject(), containingDeclaration, index);
36+
}
37+
38+
protected AccessorForPropertyDescriptor(
39+
@NotNull PropertyDescriptor original,
40+
@NotNull JetType propertyType,
41+
@Nullable JetType receiverType,
42+
@Nullable ReceiverParameterDescriptor expectedThisObject,
43+
@NotNull DeclarationDescriptor containingDeclaration,
44+
int index
45+
) {
3346
super(containingDeclaration, Collections.<AnnotationDescriptor>emptyList(), Modality.FINAL, Visibilities.LOCAL,
34-
pd.isVar(), Name.identifier(pd.getName() + "$b$" + index),
47+
original.isVar(), Name.identifier(original.getName() + "$b$" + index),
3548
Kind.DECLARATION);
3649

37-
boolean isStaticProperty = AsmUtil.isPropertyWithBackingFieldInOuterClass(pd)
38-
&& !AsmUtil.isClassObjectWithBackingFieldsInOuter(containingDeclaration);
39-
JetType receiverType = !isStaticProperty ? DescriptorUtils.getReceiverParameterType(pd.getReceiverParameter()) : null;
40-
41-
setType(pd.getType(), Collections.<TypeParameterDescriptorImpl>emptyList(), isStaticProperty ? null : pd.getExpectedThisObject(),
42-
receiverType);
50+
setType(propertyType, Collections.<TypeParameterDescriptorImpl>emptyList(), expectedThisObject, receiverType);
4351
initialize(new Getter(this), new Setter(this));
4452
}
4553

compiler/backend/src/org/jetbrains/jet/codegen/ExpressionCodegen.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1811,7 +1811,7 @@ public StackValue.StackValueWithSimpleReceiver intermediateValueForProperty(
18111811
int flags = AsmUtil.getVisibilityForSpecialPropertyBackingField(propertyDescriptor, isDelegatedProperty);
18121812
skipPropertyAccessors = (flags & ACC_PRIVATE) == 0 || methodKind == MethodKind.SYNTHETIC_ACCESSOR || methodKind == MethodKind.INITIALIZER;
18131813
if (!skipPropertyAccessors) {
1814-
propertyDescriptor = (PropertyDescriptor) backingFieldContext.getAccessor(propertyDescriptor);
1814+
propertyDescriptor = (PropertyDescriptor) backingFieldContext.getAccessor(propertyDescriptor, true, delegateType);
18151815
}
18161816
}
18171817

compiler/backend/src/org/jetbrains/jet/codegen/context/CodegenContext.java

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.jetbrains.jet.lang.descriptors.impl.ConstructorDescriptorImpl;
3030
import org.jetbrains.jet.lang.resolve.BindingContext;
3131
import org.jetbrains.jet.lang.resolve.DescriptorUtils;
32+
import org.jetbrains.jet.lang.types.JetType;
3233

3334
import java.util.Collections;
3435
import java.util.HashMap;
@@ -247,21 +248,35 @@ public CodegenContext findParentContextWithDescriptor(DeclarationDescriptor desc
247248
return c;
248249
}
249250

250-
public DeclarationDescriptor getAccessor(DeclarationDescriptor descriptor) {
251+
@NotNull
252+
public DeclarationDescriptor getAccessor(@NotNull DeclarationDescriptor descriptor) {
253+
return getAccessor(descriptor, false, null);
254+
}
255+
256+
@NotNull
257+
public DeclarationDescriptor getAccessor(@NotNull DeclarationDescriptor descriptor, boolean isForBackingFieldInOuterClass, @Nullable JetType delegateType) {
251258
if (accessors == null) {
252259
accessors = new HashMap<DeclarationDescriptor, DeclarationDescriptor>();
253260
}
254261
descriptor = descriptor.getOriginal();
255262
DeclarationDescriptor accessor = accessors.get(descriptor);
256263
if (accessor != null) {
264+
assert !isForBackingFieldInOuterClass ||
265+
accessor instanceof AccessorForPropertyBackingFieldInOuterClass : "There is already exists accessor with isForBackingFieldInOuterClass = false in this context";
257266
return accessor;
258267
}
259268

269+
int accessorIndex = accessors.size();
260270
if (descriptor instanceof SimpleFunctionDescriptor || descriptor instanceof ConstructorDescriptor) {
261-
accessor = new AccessorForFunctionDescriptor((FunctionDescriptor) descriptor, contextDescriptor, accessors.size());
271+
accessor = new AccessorForFunctionDescriptor((FunctionDescriptor) descriptor, contextDescriptor, accessorIndex);
262272
}
263273
else if (descriptor instanceof PropertyDescriptor) {
264-
accessor = new AccessorForPropertyDescriptor((PropertyDescriptor) descriptor, contextDescriptor, accessors.size());
274+
if (isForBackingFieldInOuterClass) {
275+
accessor = new AccessorForPropertyBackingFieldInOuterClass((PropertyDescriptor) descriptor, contextDescriptor,
276+
accessorIndex, delegateType);
277+
} else {
278+
accessor = new AccessorForPropertyDescriptor((PropertyDescriptor) descriptor, contextDescriptor, accessorIndex);
279+
}
265280
}
266281
else {
267282
throw new UnsupportedOperationException("Do not know how to create accessor for descriptor " + descriptor);
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
class Delegate<T>(var inner: T) {
2+
fun get(t: Any?, p: PropertyMetadata): T = inner
3+
fun set(t: Any?, p: PropertyMetadata, i: T) { inner = i }
4+
}
5+
6+
7+
class Foo (val f: Int) {
8+
class object {
9+
val A: Foo by Delegate(Foo(11))
10+
var B: Foo by Delegate(Foo(11))
11+
}
12+
}
13+
14+
trait FooTrait {
15+
class object {
16+
val A: Foo by Delegate(Foo(11))
17+
var B: Foo by Delegate(Foo(11))
18+
}
19+
}
20+
21+
fun box() : String {
22+
if (Foo.A.f != 11) return "fail 1"
23+
if (Foo.B.f != 11) return "fail 2"
24+
25+
Foo.B = Foo(12)
26+
if (Foo.B.f != 12) return "fail 3"
27+
28+
if (FooTrait.A.f != 11) return "fail 4"
29+
if (FooTrait.B.f != 11) return "fail 5"
30+
31+
FooTrait.B = Foo(12)
32+
if (FooTrait.B.f != 12) return "fail 6"
33+
34+
return "OK"
35+
}

compiler/tests/org/jetbrains/jet/codegen/generated/BlackBoxCodegenTestGenerated.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1877,6 +1877,11 @@ public void testInferredPropertyType() throws Exception {
18771877
doTest("compiler/testData/codegen/box/delegatedProperty/inferredPropertyType.kt");
18781878
}
18791879

1880+
@TestMetadata("kt4138.kt")
1881+
public void testKt4138() throws Exception {
1882+
doTest("compiler/testData/codegen/box/delegatedProperty/kt4138.kt");
1883+
}
1884+
18801885
@TestMetadata("privateVar.kt")
18811886
public void testPrivateVar() throws Exception {
18821887
doTest("compiler/testData/codegen/box/delegatedProperty/privateVar.kt");

0 commit comments

Comments
 (0)