From 70cd1cfcdfd41b2751225b39d45ab11b8fe6d49d Mon Sep 17 00:00:00 2001 From: Mikhail Zarechenskiy Date: Tue, 6 Feb 2018 14:33:14 +0300 Subject: [PATCH] Fix generation of companion object inside inline classes --- .../kotlin/codegen/ClassBodyCodegen.java | 2 +- .../jetbrains/kotlin/codegen/FieldInfo.java | 17 +++--------- .../companionObjectInsideInlineClass.kt | 11 ++++++++ .../companionObjectInsideInlineClass.txt | 26 +++++++++++++++++++ .../codegen/BytecodeListingTestGenerated.java | 6 +++++ 5 files changed, 47 insertions(+), 15 deletions(-) create mode 100644 compiler/testData/codegen/bytecodeListing/inlineClasses/companionObjectInsideInlineClass.kt create mode 100644 compiler/testData/codegen/bytecodeListing/inlineClasses/companionObjectInsideInlineClass.txt diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/ClassBodyCodegen.java b/compiler/backend/src/org/jetbrains/kotlin/codegen/ClassBodyCodegen.java index d562522ceaa..fa7006fb790 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/ClassBodyCodegen.java +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/ClassBodyCodegen.java @@ -54,7 +54,7 @@ public abstract class ClassBodyCodegen extends MemberCodegen companions = new ArrayList<>(); - if (kind != OwnerKind.DEFAULT_IMPLS) { + if (kind != OwnerKind.DEFAULT_IMPLS && kind != OwnerKind.ERASED_INLINE_CLASS) { //generate nested classes first and only then generate class body. It necessary to access to nested CodegenContexts for (KtDeclaration declaration : myClass.getDeclarations()) { if (shouldProcessFirst(declaration)) { diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/FieldInfo.java b/compiler/backend/src/org/jetbrains/kotlin/codegen/FieldInfo.java index adc32f8b538..2ba7c53e374 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/FieldInfo.java +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/FieldInfo.java @@ -1,17 +1,6 @@ /* - * 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. + * Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license + * that can be found in the license/LICENSE.txt file. */ package org.jetbrains.kotlin.codegen; @@ -39,7 +28,7 @@ public class FieldInfo { ClassDescriptor ownerDescriptor = DescriptorUtils.getParentOfType(classDescriptor, ClassDescriptor.class); assert ownerDescriptor != null : "Owner not found for class: " + classDescriptor; - Type ownerType = typeMapper.mapType(ownerDescriptor); + Type ownerType = typeMapper.mapClass(ownerDescriptor); return new FieldInfo(ownerType, typeMapper.mapType(classDescriptor), classDescriptor.getName().asString(), true); } diff --git a/compiler/testData/codegen/bytecodeListing/inlineClasses/companionObjectInsideInlineClass.kt b/compiler/testData/codegen/bytecodeListing/inlineClasses/companionObjectInsideInlineClass.kt new file mode 100644 index 00000000000..5f482095528 --- /dev/null +++ b/compiler/testData/codegen/bytecodeListing/inlineClasses/companionObjectInsideInlineClass.kt @@ -0,0 +1,11 @@ +// !LANGUAGE: +InlineClasses + +inline class Foo(val x: Int) { + companion object { + fun funInCompanion() {} + + private const val constValInCompanion = 1 + } + + fun inInlineClass() {} +} \ No newline at end of file diff --git a/compiler/testData/codegen/bytecodeListing/inlineClasses/companionObjectInsideInlineClass.txt b/compiler/testData/codegen/bytecodeListing/inlineClasses/companionObjectInsideInlineClass.txt new file mode 100644 index 00000000000..416e384f13c --- /dev/null +++ b/compiler/testData/codegen/bytecodeListing/inlineClasses/companionObjectInsideInlineClass.txt @@ -0,0 +1,26 @@ +@kotlin.Metadata +public final class Foo$Companion { + inner class Foo$Companion + private method (): void + public synthetic method (p0: kotlin.jvm.internal.DefaultConstructorMarker): void + public final method funInCompanion(): void +} + +@kotlin.Metadata +public final static class Foo$Erased { + public final static @org.jetbrains.annotations.NotNull method box(p0: int): Foo + public final static method inInlineClass(p0: int): void +} + +@kotlin.Metadata +public final class Foo { + public final static field Companion: Foo$Companion + private final static field constValInCompanion: int + private final field x: int + inner class Foo$Companion + static method (): void + public method (p0: int): void + public final method getX(): int + public final method inInlineClass(): void + public final method unbox(): int +} diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/BytecodeListingTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/BytecodeListingTestGenerated.java index f64db0faf34..f974bb05ad4 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/BytecodeListingTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/BytecodeListingTestGenerated.java @@ -267,6 +267,12 @@ public class BytecodeListingTestGenerated extends AbstractBytecodeListingTest { KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/bytecodeListing/inlineClasses"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.ANY, true); } + @TestMetadata("companionObjectInsideInlineClass.kt") + public void testCompanionObjectInsideInlineClass() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/bytecodeListing/inlineClasses/companionObjectInsideInlineClass.kt"); + doTest(fileName); + } + @TestMetadata("noBridgesForErasedInlineClass.kt") public void testNoBridgesForErasedInlineClass() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/bytecodeListing/inlineClasses/noBridgesForErasedInlineClass.kt");