JS: serialize type of local anonymous class as its denotable supertype. See KT-14888

This commit is contained in:
Alexey Andreev
2016-12-13 12:54:08 +03:00
parent b08afb0e93
commit e6501591fa
9 changed files with 204 additions and 3 deletions
@@ -21,7 +21,7 @@ import org.jetbrains.kotlin.resolve.constants.NullValue
import org.jetbrains.kotlin.types.KotlinType
open class KotlinSerializerExtensionBase(private val protocol: SerializerExtensionProtocol) : SerializerExtension() {
override final val stringTable = StringTableImpl()
override val stringTable = StringTableImpl()
override fun serializeClass(descriptor: ClassDescriptor, proto: ProtoBuf.Class.Builder) {
for (annotation in descriptor.annotations) {
@@ -26,7 +26,7 @@ import org.jetbrains.kotlin.types.ErrorUtils
import org.jetbrains.kotlin.utils.Interner
import java.io.OutputStream
class StringTableImpl : StringTable {
open class StringTableImpl : StringTable {
private class FqNameProto(val fqName: QualifiedName.Builder) {
override fun hashCode(): Int {
var result = 13
@@ -72,7 +72,7 @@ class StringTableImpl : StringTable {
is ClassDescriptor -> {
builder.parentQualifiedName = getFqNameIndex(containingDeclaration)
}
else -> throw IllegalStateException("Cannot get FQ name of local class: " + descriptor)
else -> return getFqNameIndexOfLocalAnonymousClass(descriptor)
}
builder.shortName = getStringIndex(descriptor.name.asString())
@@ -80,6 +80,10 @@ class StringTableImpl : StringTable {
return qualifiedNames.intern(FqNameProto(builder))
}
open fun getFqNameIndexOfLocalAnonymousClass(descriptor: ClassifierDescriptorWithTypeParameters): Int {
throw IllegalStateException("Cannot get FQ name of local class: " + descriptor)
}
fun getPackageFqNameIndex(fqName: FqName): Int {
var result = -1
for (segment in fqName.pathSegments()) {
@@ -0,0 +1,51 @@
// MODULE: lib
// FILE: lib.kt
interface I {
fun foo(): String
}
abstract class A {
abstract fun bar(): String
}
abstract class G<T> {
abstract fun baz(): T
}
class C {
private val propA = object : A() {
override fun bar() = "propA.bar"
fun x() = "OK"
}
private val propI = object : I {
override fun foo() = "propI.foo"
fun x() = "OK"
}
private val propAI = object : A(), I {
override fun foo() = "propAI.foo"
override fun bar() = "propAI.bar"
fun x() = "OK"
}
private val propG = object : G<String>() {
override fun baz() = "propG.baz"
fun x() = "OK"
}
}
// MODULE: main(lib)
// FILE: main.kt
fun test() {
println(C().<!INVISIBLE_MEMBER!>propA<!>.x())
println(C().<!INVISIBLE_MEMBER!>propI<!>.x())
println(C().<!INVISIBLE_MEMBER!>propAI<!>.x())
println(C().<!INVISIBLE_MEMBER!>propG<!>.x())
}
@@ -0,0 +1,42 @@
// -- Module: <lib> --
package
public abstract class A {
public constructor A()
public abstract fun bar(): kotlin.String
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
public final class C {
public constructor C()
private final val propA: C.propA.<no name provided>
private final val propAI: C.propAI.<no name provided>
private final val propG: C.propG.<no name provided>
private final val propI: C.propI.<no name provided>
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
public abstract class G</*0*/ T> {
public constructor G</*0*/ T>()
public abstract fun baz(): T
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
public interface I {
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public abstract fun foo(): kotlin.String
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
// -- Module: <main> --
package
public fun test(): kotlin.Unit
@@ -36,6 +36,12 @@ public class DiagnosticsTestWithJsStdLibGenerated extends AbstractDiagnosticsTes
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/diagnostics/testsWithJsStdLib"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.ANY, true);
}
@TestMetadata("localClassMetadata.kt")
public void testLocalClassMetadata() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/testsWithJsStdLib/localClassMetadata.kt");
doTest(fileName);
}
@TestMetadata("runtimeAnnotations.kt")
public void testRuntimeAnnotations() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/testsWithJsStdLib/runtimeAnnotations.kt");
@@ -0,0 +1,45 @@
/*
* Copyright 2010-2016 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.serialization.js
import org.jetbrains.kotlin.descriptors.CallableMemberDescriptor
import org.jetbrains.kotlin.descriptors.ClassifierDescriptorWithTypeParameters
import org.jetbrains.kotlin.resolve.DescriptorUtils
import org.jetbrains.kotlin.resolve.descriptorUtil.getAllSuperClassifiers
import org.jetbrains.kotlin.resolve.descriptorUtil.module
import org.jetbrains.kotlin.serialization.StringTableImpl
class JavaScriptStringTable : StringTableImpl() {
override fun getFqNameIndexOfLocalAnonymousClass(descriptor: ClassifierDescriptorWithTypeParameters): Int {
return if (descriptor.containingDeclaration is CallableMemberDescriptor) {
val superClassifiers = descriptor.getAllSuperClassifiers()
.mapNotNull { it as ClassifierDescriptorWithTypeParameters }
.filter { it != descriptor }
.toList()
if (superClassifiers.size == 1) {
getFqNameIndex(superClassifiers[0])
}
else {
val superClass = superClassifiers.find { !DescriptorUtils.isInterface(it) }
getFqNameIndex(superClass ?: descriptor.module.builtIns.any)
}
}
else {
super.getFqNameIndexOfLocalAnonymousClass(descriptor)
}
}
}
@@ -28,6 +28,8 @@ import org.jetbrains.kotlin.types.FlexibleType
class KotlinJavascriptSerializerExtension(private val fileRegistry: KotlinFileRegistry) :
KotlinSerializerExtensionBase(JsSerializerProtocol) {
override val stringTable = JavaScriptStringTable()
override fun serializeFlexibleType(flexibleType: FlexibleType, lowerProto: ProtoBuf.Type.Builder, upperProto: ProtoBuf.Type.Builder) {
lowerProto.flexibleTypeCapabilitiesId = stringTable.getStringIndex(DynamicTypeDeserializer.id)
}
@@ -5240,6 +5240,12 @@ public class BoxJsTestGenerated extends AbstractBoxJsTest {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("js/js.translator/testData/box/multiModule"), Pattern.compile("^([^_](.+))\\.kt$"), TargetBackend.JS, true);
}
@TestMetadata("localClassMetadata.kt")
public void testLocalClassMetadata() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("js/js.translator/testData/box/multiModule/localClassMetadata.kt");
doTest(fileName);
}
@TestMetadata("moduleAndVariableNameClash.kt")
public void testModuleAndVariableNameClash() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("js/js.translator/testData/box/multiModule/moduleAndVariableNameClash.kt");
@@ -0,0 +1,45 @@
// MODULE: lib
// FILE: lib.kt
interface I {
fun foo(): String
}
abstract class A {
abstract fun bar(): String
}
abstract class G<T> {
abstract fun baz(): T
}
class C {
private val propA = object : A() {
override fun bar() = "propA.bar"
}
private val propI = object : I {
override fun foo() = "propI.foo"
}
private val propAI = object : A(), I {
override fun foo() = "propAI.foo"
override fun bar() = "propAI.bar"
}
private val propG = object : G<String>() {
override fun baz() = "propG.baz"
}
fun test() = "${propA.bar()};${propI.foo()};${propAI.foo()};${propAI.bar()};${propG.baz()}"
}
// MODULE: main(lib)
// FILE: main.kt
fun box(): String {
val result = C().test()
if (result != "propA.bar;propI.foo;propAI.foo;propAI.bar;propG.baz") return "fail: $result"
return "OK"
}