From 872b3e8a52952868eb56f6c0ab732e83fb364891 Mon Sep 17 00:00:00 2001 From: Dmitry Petrov Date: Mon, 6 Jun 2016 10:14:59 +0300 Subject: [PATCH] DeclarationsChecker runs checks on type alias declarations. TYPEALIAS_SHOULD_EXPAND_TO_CLASS diagnostics (implemented for type aliases expanded to type parameters). --- .../jetbrains/kotlin/diagnostics/Errors.java | 1 + .../rendering/DefaultErrorMessages.java | 1 + .../kotlin/resolve/DeclarationsChecker.kt | 19 +++++++++++++++++++ .../typealias/typeAliasShouldExpandToClass.kt | 14 ++++++++++++++ .../typeAliasShouldExpandToClass.txt | 19 +++++++++++++++++++ .../checkers/DiagnosticsTestGenerated.java | 6 ++++++ 6 files changed, 60 insertions(+) create mode 100644 compiler/testData/diagnostics/tests/typealias/typeAliasShouldExpandToClass.kt create mode 100644 compiler/testData/diagnostics/tests/typealias/typeAliasShouldExpandToClass.txt diff --git a/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/Errors.java b/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/Errors.java index 5cbf760b55d..0e6e044bb3f 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/Errors.java +++ b/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/Errors.java @@ -120,6 +120,7 @@ public interface Errors { DiagnosticFactory3 UPPER_BOUND_VIOLATED_IN_TYPEALIAS_EXPANSION = DiagnosticFactory3.create(ERROR); DiagnosticFactory1 CONFLICTING_PROJECTION_IN_TYPEALIAS_EXPANSION = DiagnosticFactory1.create(ERROR); + DiagnosticFactory1 TYPEALIAS_SHOULD_EXPAND_TO_CLASS = DiagnosticFactory1.create(ERROR); //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/rendering/DefaultErrorMessages.java b/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/rendering/DefaultErrorMessages.java index 9b90f79bd43..32ae168ddc0 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/rendering/DefaultErrorMessages.java +++ b/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/rendering/DefaultErrorMessages.java @@ -424,6 +424,7 @@ public class DefaultErrorMessages { "should be subtype of ''{0}'', substituted type is ''{1}''", RENDER_TYPE, RENDER_TYPE, NAME); MAP.put(CONFLICTING_PROJECTION_IN_TYPEALIAS_EXPANSION, "Conflicting projection in type alias expansion in intermediate type ''{0}''", RENDER_TYPE); + MAP.put(TYPEALIAS_SHOULD_EXPAND_TO_CLASS, "Type alias expands to {0}, which is not a class, an interface, or an object", RENDER_TYPE); MAP.put(TOO_MANY_ARGUMENTS, "Too many arguments for {0}", FQ_NAMES_IN_TYPES); diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/DeclarationsChecker.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/DeclarationsChecker.kt index 67d4d3e422d..52bf1befd49 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/DeclarationsChecker.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/DeclarationsChecker.kt @@ -140,6 +140,25 @@ class DeclarationsChecker( checkConstructorDeclaration(constructorDescriptor, declaration) exposedChecker.checkFunction(declaration, constructorDescriptor) } + + for ((declaration, typeAliasDescriptor) in bodiesResolveContext.typeAliases.entries) { + checkTypeAliasDeclaration(typeAliasDescriptor, declaration) + modifiersChecker.checkModifiersForDeclaration(declaration, typeAliasDescriptor) + // TODO exposedChecker.checkTypeAlias(declaration, typeAliasDescriptor) + } + } + + private fun checkTypeAliasDeclaration(typeAliasDescriptor: TypeAliasDescriptor, declaration: KtTypeAlias) { + val typeReference = declaration.getTypeReference() + if (typeReference == null) return + + val expandedType = typeAliasDescriptor.expandedType // TODO refactor type alias expansion + if (expandedType.isError) return + + val expandedClassifier = expandedType.constructor.declarationDescriptor + if (expandedClassifier is TypeParameterDescriptor) { + trace.report(TYPEALIAS_SHOULD_EXPAND_TO_CLASS.on(typeReference, expandedType)) + } } private fun checkConstructorDeclaration(constructorDescriptor: ConstructorDescriptor, declaration: KtDeclaration) { diff --git a/compiler/testData/diagnostics/tests/typealias/typeAliasShouldExpandToClass.kt b/compiler/testData/diagnostics/tests/typealias/typeAliasShouldExpandToClass.kt new file mode 100644 index 00000000000..3e34b052738 --- /dev/null +++ b/compiler/testData/diagnostics/tests/typealias/typeAliasShouldExpandToClass.kt @@ -0,0 +1,14 @@ +typealias ToTypeParam1 = T +typealias ToTypeParam2 = ToTypeParam1 +typealias ToTypeParam3 = ToTypeParam2 +typealias ToTypeParam4 = ToTypeParam1 + +typealias ToFun1 = () -> Unit +typealias ToFun2 = (T) -> Unit + +class Outer { + typealias ToTypeParam1 = T + typealias ToTypeParam2 = ToTypeParam1 + typealias ToTypeParam3 = ToTypeParam2 + typealias ToTypeParam4 = ToTypeParam1 +} diff --git a/compiler/testData/diagnostics/tests/typealias/typeAliasShouldExpandToClass.txt b/compiler/testData/diagnostics/tests/typealias/typeAliasShouldExpandToClass.txt new file mode 100644 index 00000000000..51c73fefa46 --- /dev/null +++ b/compiler/testData/diagnostics/tests/typealias/typeAliasShouldExpandToClass.txt @@ -0,0 +1,19 @@ +package + +public typealias ToFun1 = () -> kotlin.Unit +public typealias ToFun2 = (T) -> kotlin.Unit +public typealias ToTypeParam1 = T +public typealias ToTypeParam2 = ToTypeParam1 +public typealias ToTypeParam3 = ToTypeParam2 +public typealias ToTypeParam4 = ToTypeParam1 + +public final class Outer { + public typealias ToTypeParam1 = T + public typealias ToTypeParam2 = Outer.ToTypeParam1 + public typealias ToTypeParam3 = Outer.ToTypeParam2 + public typealias ToTypeParam4 = Outer.ToTypeParam1 + public constructor Outer() + 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 +} diff --git a/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java index 75d6d798013..5009fa5bdde 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java @@ -19410,6 +19410,12 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTest { doTest(fileName); } + @TestMetadata("typeAliasShouldExpandToClass.kt") + public void testTypeAliasShouldExpandToClass() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/typealias/typeAliasShouldExpandToClass.kt"); + doTest(fileName); + } + @TestMetadata("unsupportedTypeAlias.kt") public void testUnsupportedTypeAlias() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/typealias/unsupportedTypeAlias.kt");