From 6428a7655dbd57aa3084cea201bb7d23d840591b Mon Sep 17 00:00:00 2001 From: Dmitry Petrov Date: Thu, 1 Sep 2016 15:02:05 +0300 Subject: [PATCH] KT-13422 Type alias for Java exception class can be used as an object in 'throw' expression Report NO_COMPANION_OBJECT for type aliases. --- .../kotlin/resolve/QualifiedExpressionResolveUtil.kt | 7 +++++++ .../diagnostics/tests/typealias/throwJLException.kt | 7 +++++++ .../diagnostics/tests/typealias/throwJLException.txt | 4 ++++ .../kotlin/checkers/DiagnosticsTestGenerated.java | 6 ++++++ 4 files changed, 24 insertions(+) create mode 100644 compiler/testData/diagnostics/tests/typealias/throwJLException.kt create mode 100644 compiler/testData/diagnostics/tests/typealias/throwJLException.txt diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/QualifiedExpressionResolveUtil.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/QualifiedExpressionResolveUtil.kt index 831105fe162..3407f59762f 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/QualifiedExpressionResolveUtil.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/QualifiedExpressionResolveUtil.kt @@ -47,6 +47,13 @@ fun resolveQualifierAsStandaloneExpression( val referenceTarget = resolveQualifierReferenceTarget(qualifier, null, context, classifierUsageCheckers) when (referenceTarget) { + is TypeAliasDescriptor -> { + referenceTarget.classDescriptor?.let { classDescriptor -> + if (!classDescriptor.kind.isSingleton) { + context.trace.report(Errors.NO_COMPANION_OBJECT.on(qualifier.referenceExpression, referenceTarget)) + } + } + } is TypeParameterDescriptor -> { context.trace.report(Errors.TYPE_PARAMETER_IS_NOT_AN_EXPRESSION.on(qualifier.referenceExpression, referenceTarget)) } diff --git a/compiler/testData/diagnostics/tests/typealias/throwJLException.kt b/compiler/testData/diagnostics/tests/typealias/throwJLException.kt new file mode 100644 index 00000000000..8448c0ad67b --- /dev/null +++ b/compiler/testData/diagnostics/tests/typealias/throwJLException.kt @@ -0,0 +1,7 @@ +// +JDK + +typealias Exn = java.lang.Exception + +fun test() { + throw Exn +} \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/typealias/throwJLException.txt b/compiler/testData/diagnostics/tests/typealias/throwJLException.txt new file mode 100644 index 00000000000..e21513a1ff2 --- /dev/null +++ b/compiler/testData/diagnostics/tests/typealias/throwJLException.txt @@ -0,0 +1,4 @@ +package + +public typealias Exn = java.lang.Exception +public fun test(): kotlin.Unit diff --git a/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java index c7d2bcb1462..5601666aae2 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java @@ -20244,6 +20244,12 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTest { doTest(fileName); } + @TestMetadata("throwJLException.kt") + public void testThrowJLException() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/typealias/throwJLException.kt"); + doTest(fileName); + } + @TestMetadata("typeAliasArgumentsInCompanionObject.kt") public void testTypeAliasArgumentsInCompanionObject() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/typealias/typeAliasArgumentsInCompanionObject.kt");