KT-2963 VerifyError using traits with required classes

#KT-2963 Fixed
This commit is contained in:
Alexander Udalov
2012-11-01 15:25:29 +04:00
parent f139b637f3
commit cd35a6626e
3 changed files with 27 additions and 3 deletions
@@ -339,7 +339,10 @@ public abstract class StackValue {
}
public static StackValue thisOrOuter(ExpressionCodegen codegen, ClassDescriptor descriptor, boolean isSuper) {
return new ThisOuter(codegen, descriptor, isSuper);
// Coerce this/super for traits to support traits with required classes
// Do not coerce for other classes due to the 'protected' access issues (JVMS 7, 4.9.2 Structural Constraints)
boolean coerceType = descriptor.getKind() == ClassKind.TRAIT;
return new ThisOuter(codegen, descriptor, isSuper, coerceType);
}
public static StackValue postIncrement(int index, int increment) {
@@ -1187,18 +1190,20 @@ public abstract class StackValue {
private final ExpressionCodegen codegen;
private final ClassDescriptor descriptor;
private final boolean isSuper;
private final boolean coerceType;
public ThisOuter(ExpressionCodegen codegen, ClassDescriptor descriptor, boolean isSuper) {
public ThisOuter(ExpressionCodegen codegen, ClassDescriptor descriptor, boolean isSuper, boolean coerceType) {
super(OBJECT_TYPE);
this.codegen = codegen;
this.descriptor = descriptor;
this.isSuper = isSuper;
this.coerceType = coerceType;
}
@Override
public void put(Type type, InstructionAdapter v) {
final StackValue stackValue = codegen.generateThisOrOuter(descriptor, isSuper);
stackValue.put(stackValue.type, v); // no coercion here
stackValue.put(coerceType ? type : stackValue.type, v);
}
}
@@ -0,0 +1,15 @@
open class Base
trait Derived : Base {
fun foo(): String {
return object {
fun bar() = baz(this@Derived)
}.bar()
}
}
class DerivedImpl : Derived, Base()
fun baz(b: Base) = "OK"
fun box() = DerivedImpl().foo()
@@ -84,4 +84,8 @@ public class TraitsTest extends CodegenTestCase {
public void testKt1936_2() throws Exception {
blackBoxFile("regressions/kt1936_2.kt");
}
public void testKt2963() {
blackBoxFile("regressions/kt2963.kt");
}
}