Fix for KT-5495: Support method implementations in local traits
#KT-5495 Fixed
This commit is contained in:
@@ -88,6 +88,7 @@ import static org.jetbrains.jet.lang.resolve.calls.callUtil.CallUtilPackage.getR
|
||||
import static org.jetbrains.jet.lang.resolve.java.AsmTypeConstants.*;
|
||||
import static org.jetbrains.jet.lang.resolve.java.JvmAnnotationNames.KotlinSyntheticClass;
|
||||
import static org.jetbrains.jet.lang.resolve.java.diagnostics.DiagnosticsPackage.OtherOrigin;
|
||||
import static org.jetbrains.jet.lang.resolve.java.diagnostics.DiagnosticsPackage.TraitImpl;
|
||||
import static org.jetbrains.jet.lang.resolve.scopes.receivers.ReceiverValue.NO_RECEIVER;
|
||||
import static org.jetbrains.org.objectweb.asm.Opcodes.ACC_PRIVATE;
|
||||
import static org.jetbrains.org.objectweb.asm.Opcodes.GETFIELD;
|
||||
@@ -197,7 +198,7 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> implem
|
||||
);
|
||||
|
||||
|
||||
ClassContext objectContext = context.intoAnonymousClass(classDescriptor, this);
|
||||
ClassContext objectContext = context.intoAnonymousClass(classDescriptor, this, OwnerKind.IMPLEMENTATION);
|
||||
|
||||
new ImplementationBodyCodegen(objectDeclaration, objectContext, classBuilder, state, getParentCodegen()).generate();
|
||||
|
||||
@@ -285,9 +286,16 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> implem
|
||||
Type asmType = asmTypeForAnonymousClass(bindingContext, declaration);
|
||||
ClassBuilder classBuilder = state.getFactory().newVisitor(OtherOrigin(declaration, descriptor), asmType, declaration.getContainingFile());
|
||||
|
||||
ClassContext objectContext = context.intoAnonymousClass(descriptor, this);
|
||||
ClassContext objectContext = context.intoAnonymousClass(descriptor, this, OwnerKind.IMPLEMENTATION);
|
||||
new ImplementationBodyCodegen(declaration, objectContext, classBuilder, state, getParentCodegen()).generate();
|
||||
|
||||
if (declaration instanceof JetClass && ((JetClass) declaration).isTrait()) {
|
||||
Type traitImplType = state.getTypeMapper().mapTraitImpl(descriptor);
|
||||
ClassBuilder traitImplBuilder = state.getFactory().newVisitor(TraitImpl(declaration, descriptor), traitImplType, declaration.getContainingFile());
|
||||
ClassContext traitImplContext = context.intoAnonymousClass(descriptor, this, OwnerKind.TRAIT_IMPL);
|
||||
new TraitImplBodyCodegen(declaration, traitImplContext, traitImplBuilder, state, parentCodegen).generate();
|
||||
}
|
||||
|
||||
return StackValue.none();
|
||||
}
|
||||
|
||||
|
||||
@@ -160,8 +160,8 @@ public abstract class CodegenContext<T extends DeclarationDescriptor> {
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public ClassContext intoAnonymousClass(@NotNull ClassDescriptor descriptor, @NotNull ExpressionCodegen codegen) {
|
||||
return new AnonymousClassContext(codegen.getState().getTypeMapper(), descriptor, OwnerKind.IMPLEMENTATION, this, codegen);
|
||||
public ClassContext intoAnonymousClass(@NotNull ClassDescriptor descriptor, @NotNull ExpressionCodegen codegen, @NotNull OwnerKind ownerKind) {
|
||||
return new AnonymousClassContext(codegen.getState().getTypeMapper(), descriptor, ownerKind, this, codegen);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
fun box(): String {
|
||||
trait A {
|
||||
fun foo() = "OK"
|
||||
}
|
||||
|
||||
class B : A
|
||||
|
||||
return B().foo()
|
||||
}
|
||||
@@ -45,76 +45,88 @@ public class KotlinSyntheticClassAnnotationTest extends CodegenTestCase {
|
||||
public void testPackagePart() {
|
||||
doTest("fun foo() = 42",
|
||||
"$",
|
||||
PACKAGE_PART);
|
||||
PACKAGE_PART, false);
|
||||
}
|
||||
|
||||
public void testTraitImpl() {
|
||||
doTest("trait A { fun foo() = 42 }",
|
||||
JvmAbi.TRAIT_IMPL_SUFFIX,
|
||||
TRAIT_IMPL);
|
||||
TRAIT_IMPL, true);
|
||||
}
|
||||
|
||||
public void testSamWrapper() {
|
||||
doTest("val f = {}\nval foo = Thread(f)",
|
||||
"$sam",
|
||||
SAM_WRAPPER);
|
||||
SAM_WRAPPER, false);
|
||||
}
|
||||
|
||||
public void testSamLambda() {
|
||||
doTest("val foo = Thread { }",
|
||||
"$1",
|
||||
SAM_LAMBDA);
|
||||
SAM_LAMBDA, true);
|
||||
}
|
||||
|
||||
public void testCallableReferenceWrapper() {
|
||||
doTest("val f = String::get",
|
||||
"$1",
|
||||
CALLABLE_REFERENCE_WRAPPER);
|
||||
CALLABLE_REFERENCE_WRAPPER, true);
|
||||
}
|
||||
|
||||
public void testLocalFunction() {
|
||||
doTest("fun foo() { fun bar() {} }",
|
||||
"$1",
|
||||
LOCAL_FUNCTION);
|
||||
LOCAL_FUNCTION, true);
|
||||
}
|
||||
|
||||
public void testAnonymousFunction() {
|
||||
doTest("val f = {}",
|
||||
"$1",
|
||||
ANONYMOUS_FUNCTION);
|
||||
ANONYMOUS_FUNCTION, true);
|
||||
}
|
||||
|
||||
public void testLocalClass() {
|
||||
doTest("fun foo() { class Local }",
|
||||
"Local",
|
||||
LOCAL_CLASS);
|
||||
LOCAL_CLASS, true);
|
||||
}
|
||||
|
||||
public void testLocalTraitImpl() {
|
||||
doTest("fun foo() { trait Local { fun bar() = 42 } }",
|
||||
"Local$$TImpl",
|
||||
LOCAL_CLASS, true);
|
||||
}
|
||||
|
||||
public void testLocalTraitInterface() {
|
||||
doTest("fun foo() { trait Local { fun bar() = 42 } }",
|
||||
"Local",
|
||||
LOCAL_CLASS);
|
||||
LOCAL_CLASS, true);
|
||||
}
|
||||
|
||||
public void testInnerClassOfLocalClass() {
|
||||
doTest("fun foo() { class Local { inner class Inner } }",
|
||||
"Inner",
|
||||
LOCAL_CLASS);
|
||||
LOCAL_CLASS, true);
|
||||
}
|
||||
|
||||
public void testAnonymousObject() {
|
||||
doTest("val o = object {}",
|
||||
"$1",
|
||||
ANONYMOUS_OBJECT);
|
||||
ANONYMOUS_OBJECT, true);
|
||||
}
|
||||
|
||||
private void doTest(@NotNull String code, @NotNull final String classNamePart, @NotNull KotlinSyntheticClass.Kind expectedKind) {
|
||||
private void doTest(
|
||||
@NotNull String code,
|
||||
@NotNull final String classNamePart,
|
||||
@NotNull KotlinSyntheticClass.Kind expectedKind,
|
||||
final boolean endWithNotContains
|
||||
) {
|
||||
loadText("package " + PACKAGE_NAME + "\n\n" + code);
|
||||
List<OutputFile> output = generateClassesInFile().asList();
|
||||
Collection<OutputFile> files = Collections2.filter(output, new Predicate<OutputFile>() {
|
||||
@Override
|
||||
public boolean apply(OutputFile file) {
|
||||
return file.getRelativePath().contains(classNamePart);
|
||||
String path = file.getRelativePath();
|
||||
return endWithNotContains ? path.endsWith(classNamePart + ".class") : path.contains(classNamePart);
|
||||
}
|
||||
});
|
||||
assertFalse("No files with \"" + classNamePart + "\" in the name are found: " + output, files.isEmpty());
|
||||
|
||||
@@ -6283,6 +6283,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("kt5495.kt")
|
||||
public void testKt5495() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("compiler/testData/codegen/box/traits/kt5495.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("multiple.kt")
|
||||
public void testMultiple() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("compiler/testData/codegen/box/traits/multiple.kt");
|
||||
|
||||
Reference in New Issue
Block a user