KT-13544: support typealiases in JS backend

This commit is contained in:
Alexey Andreev
2016-09-06 15:04:56 +03:00
parent 7510b7f080
commit b91ed1e02a
8 changed files with 119 additions and 2 deletions
@@ -0,0 +1,9 @@
class A {
companion object {
val result = "OK"
}
}
typealias Alias = A.Companion
fun box(): String = Alias.result
@@ -15550,6 +15550,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
doTest(fileName);
}
@TestMetadata("typeAliasCompanion.kt")
public void testTypeAliasCompanion() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/typealias/typeAliasCompanion.kt");
doTest(fileName);
}
@TestMetadata("typeAliasConstructor.kt")
public void testTypeAliasConstructor() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/typealias/typeAliasConstructor.kt");
@@ -1205,6 +1205,10 @@ fun main(args: Array<String>) {
testClass<AbstractNonLocalReturnsTest>() {
model("codegen/boxInline/nonLocalReturns/", targetBackend = TargetBackend.JS)
}
testClass<AbstractTypeAliasesTests>() {
model("codegen/box/typealias/", targetBackend = TargetBackend.JS)
}
}
}
@@ -0,0 +1,67 @@
/*
* 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.js.test.semantics;
import com.intellij.testFramework.TestDataPath;
import org.jetbrains.kotlin.test.JUnit3RunnerWithInners;
import org.jetbrains.kotlin.test.KotlinTestUtils;
import org.jetbrains.kotlin.test.TestMetadata;
import org.junit.runner.RunWith;
import java.io.File;
import java.util.regex.Pattern;
/** This class is generated by {@link org.jetbrains.kotlin.generators.tests.TestsPackage}. DO NOT MODIFY MANUALLY */
@SuppressWarnings("all")
@TestMetadata("compiler/testData/codegen/box/typealias")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public class TypeAliasesTestsGenerated extends AbstractTypeAliasesTests {
public void testAllFilesPresentInTypealias() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/box/typealias"), Pattern.compile("^(.+)\\.kt$"), true);
}
@TestMetadata("localTypeAlias.kt")
public void testLocalTypeAlias() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/typealias/localTypeAlias.kt");
doTest(fileName);
}
@TestMetadata("simple.kt")
public void testSimple() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/typealias/simple.kt");
doTest(fileName);
}
@TestMetadata("typeAliasCompanion.kt")
public void testTypeAliasCompanion() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/typealias/typeAliasCompanion.kt");
doTest(fileName);
}
@TestMetadata("typeAliasConstructor.kt")
public void testTypeAliasConstructor() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/typealias/typeAliasConstructor.kt");
doTest(fileName);
}
@TestMetadata("typeAliasObject.kt")
public void testTypeAliasObject() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/typealias/typeAliasObject.kt");
doTest(fileName);
}
}
@@ -68,3 +68,5 @@ abstract class AbstractRttiTest : SingleFileTranslationTest("rtti/")
abstract class AbstractCastTest : SingleFileTranslationTest("expression/cast/")
abstract class AbstractLightReflectionTest : SingleFileTranslationTest("reflection/light/")
abstract class AbstractTypeAliasesTests : AbstractBlackBoxTest("typealiases/")
@@ -142,4 +142,10 @@ public class DeclarationBodyVisitor extends TranslatorVisitor<Void> {
}
initializerStatements.add(statement);
}
@Override
public Void visitTypeAlias(@NotNull KtTypeAlias typeAlias, TranslationContext data) {
// Resolved by front-end, not used by backend
return null;
}
}
@@ -590,6 +590,12 @@ public final class ExpressionVisitor extends TranslatorVisitor<JsNode> {
return JsEmpty.INSTANCE;
}
@Override
public JsNode visitTypeAlias(@NotNull KtTypeAlias typeAlias, TranslationContext data) {
// Resolved by front-end, not used by backend
return JsEmpty.INSTANCE;
}
private static ClassTranslator.TranslationResult translateClassOrObject(
@NotNull KtClassOrObject declaration,
@NotNull ClassDescriptor descriptor,
@@ -106,7 +106,7 @@ public final class BindingUtils {
public static DeclarationDescriptor getDescriptorForReferenceExpression(@NotNull BindingContext context,
@NotNull KtReferenceExpression reference) {
if (BindingContextUtils.isExpressionWithValidReference(reference, context)) {
return BindingContextUtils.getNotNull(context, BindingContext.REFERENCE_TARGET, reference);
return resolveObjectViaTypeAlias(BindingContextUtils.getNotNull(context, BindingContext.REFERENCE_TARGET, reference));
}
return null;
}
@@ -114,7 +114,24 @@ public final class BindingUtils {
@Nullable
private static DeclarationDescriptor getNullableDescriptorForReferenceExpression(@NotNull BindingContext context,
@NotNull KtReferenceExpression reference) {
return context.get(BindingContext.REFERENCE_TARGET, reference);
DeclarationDescriptor descriptor = context.get(BindingContext.REFERENCE_TARGET, reference);
return descriptor != null ? resolveObjectViaTypeAlias(descriptor) : null;
}
@NotNull
private static DeclarationDescriptor resolveObjectViaTypeAlias(@NotNull DeclarationDescriptor descriptor) {
if (descriptor instanceof TypeAliasDescriptor) {
ClassDescriptor classDescriptor = ((TypeAliasDescriptor) descriptor).getClassDescriptor();
assert classDescriptor != null : "Class descriptor must be non-null in resolved typealias: " + descriptor;
if (classDescriptor.getKind() != ClassKind.OBJECT) {
classDescriptor = classDescriptor.getCompanionObjectDescriptor();
assert classDescriptor != null : "Resolved typealias must have non-null class descriptor: " + descriptor;
}
return classDescriptor;
}
else {
return descriptor;
}
}
public static boolean isVariableReassignment(@NotNull BindingContext context, @NotNull KtExpression expression) {