From 9c74f279679dd866bef3a469462b3162311e83a9 Mon Sep 17 00:00:00 2001 From: Dmitry Petrov Date: Tue, 7 Jun 2016 15:44:58 +0300 Subject: [PATCH] Type alias can't expand to 'dynamic' --- .../jetbrains/kotlin/resolve/DeclarationsChecker.kt | 11 ++++------- .../org/jetbrains/kotlin/resolve/TypeAliasExpander.kt | 5 ++++- .../dynamicTypes/typealiasExpandingToDynamic.kt | 6 ++++++ .../dynamicTypes/typealiasExpandingToDynamic.txt | 7 +++++++ .../DiagnosticsTestWithJsStdLibGenerated.java | 6 ++++++ 5 files changed, 27 insertions(+), 8 deletions(-) create mode 100644 compiler/testData/diagnostics/testsWithJsStdLib/dynamicTypes/typealiasExpandingToDynamic.kt create mode 100644 compiler/testData/diagnostics/testsWithJsStdLib/dynamicTypes/typealiasExpandingToDynamic.txt diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/DeclarationsChecker.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/DeclarationsChecker.kt index 52bf1befd49..cb022a0a6d4 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/DeclarationsChecker.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/DeclarationsChecker.kt @@ -31,10 +31,7 @@ import org.jetbrains.kotlin.resolve.BindingContext.TYPE import org.jetbrains.kotlin.resolve.BindingContext.TYPE_PARAMETER import org.jetbrains.kotlin.resolve.DescriptorUtils.classCanHaveAbstractMembers import org.jetbrains.kotlin.resolve.DescriptorUtils.classCanHaveOpenMembers -import org.jetbrains.kotlin.types.IntersectionTypeConstructor -import org.jetbrains.kotlin.types.KotlinType -import org.jetbrains.kotlin.types.SubstitutionUtils -import org.jetbrains.kotlin.types.TypeUtils +import org.jetbrains.kotlin.types.* import org.jetbrains.kotlin.types.checker.KotlinTypeChecker import org.jetbrains.kotlin.types.typeUtil.contains import org.jetbrains.kotlin.types.typeUtil.isNothing @@ -149,14 +146,14 @@ class DeclarationsChecker( } private fun checkTypeAliasDeclaration(typeAliasDescriptor: TypeAliasDescriptor, declaration: KtTypeAlias) { - val typeReference = declaration.getTypeReference() - if (typeReference == null) return + val typeReference = declaration.getTypeReference() ?: return val expandedType = typeAliasDescriptor.expandedType // TODO refactor type alias expansion if (expandedType.isError) return val expandedClassifier = expandedType.constructor.declarationDescriptor - if (expandedClassifier is TypeParameterDescriptor) { + + if (expandedType.isDynamic() || expandedClassifier is TypeParameterDescriptor) { trace.report(TYPEALIAS_SHOULD_EXPAND_TO_CLASS.on(typeReference, expandedType)) } } diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/TypeAliasExpander.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/TypeAliasExpander.kt index 443d44c41d2..a52e32bfa55 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/TypeAliasExpander.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/TypeAliasExpander.kt @@ -138,7 +138,10 @@ class TypeAliasExpander( val expandedType = expandRecursively(nestedExpansion, type.annotations, recursionDepth + 1, false) - return TypeProjectionImpl(originalProjection.projectionKind, expandedType.withAbbreviatedType(type)) + // 'dynamic' type can't be abbreviated - will be reported separately + val typeWithAbbreviation = if (expandedType.isDynamic()) expandedType else expandedType.withAbbreviatedType(type) + + return TypeProjectionImpl(originalProjection.projectionKind, typeWithAbbreviation) } else -> { val substitutedArguments = type.arguments.mapIndexed { i, originalArgument -> diff --git a/compiler/testData/diagnostics/testsWithJsStdLib/dynamicTypes/typealiasExpandingToDynamic.kt b/compiler/testData/diagnostics/testsWithJsStdLib/dynamicTypes/typealiasExpandingToDynamic.kt new file mode 100644 index 00000000000..53fae665c78 --- /dev/null +++ b/compiler/testData/diagnostics/testsWithJsStdLib/dynamicTypes/typealiasExpandingToDynamic.kt @@ -0,0 +1,6 @@ +typealias Dyn = dynamic +typealias Dyn2 = Dyn +typealias Dyn3 = Dyn2 + +typealias Type = T +typealias Dyn4 = Type diff --git a/compiler/testData/diagnostics/testsWithJsStdLib/dynamicTypes/typealiasExpandingToDynamic.txt b/compiler/testData/diagnostics/testsWithJsStdLib/dynamicTypes/typealiasExpandingToDynamic.txt new file mode 100644 index 00000000000..4b10ad99292 --- /dev/null +++ b/compiler/testData/diagnostics/testsWithJsStdLib/dynamicTypes/typealiasExpandingToDynamic.txt @@ -0,0 +1,7 @@ +package + +public typealias Dyn = dynamic +public typealias Dyn2 = Dyn +public typealias Dyn3 = Dyn2 +public typealias Dyn4 = Type +public typealias Type = T diff --git a/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestWithJsStdLibGenerated.java b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestWithJsStdLibGenerated.java index a804810449c..ff98c9468e5 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestWithJsStdLibGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestWithJsStdLibGenerated.java @@ -235,6 +235,12 @@ public class DiagnosticsTestWithJsStdLibGenerated extends AbstractDiagnosticsTes doTest(fileName); } + @TestMetadata("typealiasExpandingToDynamic.kt") + public void testTypealiasExpandingToDynamic() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/testsWithJsStdLib/dynamicTypes/typealiasExpandingToDynamic.kt"); + doTest(fileName); + } + @TestMetadata("varargs.kt") public void testVarargs() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/testsWithJsStdLib/dynamicTypes/varargs.kt");