diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/CallExpressionResolver.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/CallExpressionResolver.kt index 95a4ab750dc..0af50270e97 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/CallExpressionResolver.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/CallExpressionResolver.kt @@ -210,14 +210,14 @@ class CallExpressionResolver( return noTypeInfo(context) } if (functionDescriptor is ConstructorDescriptor) { - val containingDescriptor = functionDescriptor.containingDeclaration - if (DescriptorUtils.isAnnotationClass(containingDescriptor) && !canInstantiateAnnotationClass(callExpression, context.trace)) { + val constructedClass = functionDescriptor.constructedClass + if (DescriptorUtils.isAnnotationClass(constructedClass) && !canInstantiateAnnotationClass(callExpression, context.trace)) { context.trace.report(ANNOTATION_CLASS_CONSTRUCTOR_CALL.on(callExpression)) } - if (DescriptorUtils.isEnumClass(containingDescriptor)) { + if (DescriptorUtils.isEnumClass(constructedClass)) { context.trace.report(ENUM_CLASS_CONSTRUCTOR_CALL.on(callExpression)) } - if (DescriptorUtils.isSealedClass(containingDescriptor)) { + if (DescriptorUtils.isSealedClass(constructedClass)) { context.trace.report(SEALED_CLASS_CONSTRUCTOR_CALL.on(callExpression)) } } diff --git a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/tower/TowerLevels.kt b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/tower/TowerLevels.kt index 94825e895de..71918e57b0b 100644 --- a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/tower/TowerLevels.kt +++ b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/tower/TowerLevels.kt @@ -264,7 +264,7 @@ private fun ResolutionScope.getContributedFunctionsAndConstructors(name: Name, l val classifier = getContributedClassifier(name, location) return getContributedFunctions(name, location) + (getClassWithConstructors(classifier)?.constructors?.filter { it.dispatchReceiverParameter == null } ?: emptyList()) + - (classifier?.getTypeAliasConstructors() ?: emptyList()) + (classifier?.getTypeAliasConstructors()?.filter { it.dispatchReceiverParameter == null } ?: emptyList()) } private fun ResolutionScope.getContributedObjectVariables(name: Name, location: LookupLocation): Collection { diff --git a/compiler/testData/diagnostics/tests/typealias/typeAliasConstructorInSuperCall.kt b/compiler/testData/diagnostics/tests/typealias/typeAliasConstructorInSuperCall.kt new file mode 100644 index 00000000000..72b7900f92d --- /dev/null +++ b/compiler/testData/diagnostics/tests/typealias/typeAliasConstructorInSuperCall.kt @@ -0,0 +1,14 @@ +open class MyBase protected constructor() { + protected constructor(x: Nothing?): this() +} +typealias MyAlias = MyBase + +class MyDerived1 : MyAlias() +class MyDerived1a : MyBase() + +class MyDerived2 : MyAlias(null) +class MyDerived2a : MyBase(null) + +class MyDerived3 : MyAlias { + constructor(x: Nothing?) : super(x) +} \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/typealias/typeAliasConstructorInSuperCall.txt b/compiler/testData/diagnostics/tests/typealias/typeAliasConstructorInSuperCall.txt new file mode 100644 index 00000000000..00dc40813e1 --- /dev/null +++ b/compiler/testData/diagnostics/tests/typealias/typeAliasConstructorInSuperCall.txt @@ -0,0 +1,45 @@ +package + +public open class MyBase { + protected constructor MyBase() + protected constructor MyBase(/*0*/ x: kotlin.Nothing?) + 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 MyDerived1 : MyAlias /* = MyBase */ { + public constructor MyDerived1() + 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 MyDerived1a : MyBase { + public constructor MyDerived1a() + 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 MyDerived2 : MyAlias /* = MyBase */ { + public constructor MyDerived2() + 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 MyDerived2a : MyBase { + public constructor MyDerived2a() + 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 MyDerived3 : MyAlias /* = MyBase */ { + public constructor MyDerived3(/*0*/ x: kotlin.Nothing?) + 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 typealias MyAlias = MyBase diff --git a/compiler/testData/diagnostics/tests/typealias/typeAliasConstructorWrongClass.kt b/compiler/testData/diagnostics/tests/typealias/typeAliasConstructorWrongClass.kt new file mode 100644 index 00000000000..6dcfef457ed --- /dev/null +++ b/compiler/testData/diagnostics/tests/typealias/typeAliasConstructorWrongClass.kt @@ -0,0 +1,32 @@ +abstract class AbstractClass +typealias Test1 = AbstractClass +val test1 = Test1() +val test1a = AbstractClass() + +annotation class AnnotationClass +typealias Test2 = AnnotationClass +val test2 = Test2() +val test2a = AnnotationClass() + +enum class EnumClass { VALUE1, VALUE2 } +typealias Test3 = EnumClass +val test3 = Test3() +val test3a = EnumClass() + +sealed class SealedClass +typealias Test4 = SealedClass +val test4 = Test4() +val test4a = SealedClass() + +class Outer { + inner class Inner + typealias TestInner = Inner +} +typealias Test5 = Outer.Inner + +val test5 = Test5() +val test5a = Outer.Inner() +val test5b = Outer.TestInner() +val test5c = Outer().TestInner() // TODO extension type alias constructors? +val test5d = Outer().Inner() +val test5e = Outer().Test5() // TODO extension type alias constructors? \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/typealias/typeAliasConstructorWrongClass.txt b/compiler/testData/diagnostics/tests/typealias/typeAliasConstructorWrongClass.txt new file mode 100644 index 00000000000..c51cd13371f --- /dev/null +++ b/compiler/testData/diagnostics/tests/typealias/typeAliasConstructorWrongClass.txt @@ -0,0 +1,78 @@ +package + +public val test1: AbstractClass +public val test1a: AbstractClass +public val test2: AnnotationClass +public val test2a: AnnotationClass +public val test3: EnumClass +public val test3a: EnumClass +public val test4: SealedClass +public val test4a: SealedClass +public val test5: [ERROR : Type for Test5()] +public val test5a: [ERROR : Type for Outer.Inner()] +public val test5b: [ERROR : Type for Outer.TestInner()] +public val test5c: [ERROR : Type for Outer().TestInner()] +public val test5d: Outer.Inner +public val test5e: [ERROR : Type for Outer().Test5()] + +public abstract class AbstractClass { + public constructor AbstractClass() + 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 annotation class AnnotationClass : kotlin.Annotation { + public constructor AnnotationClass() + 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 enum class EnumClass : kotlin.Enum { + enum entry VALUE1 + + enum entry VALUE2 + + private constructor EnumClass() + public final override /*1*/ /*fake_override*/ val name: kotlin.String + public final override /*1*/ /*fake_override*/ val ordinal: kotlin.Int + protected final override /*1*/ /*fake_override*/ fun clone(): kotlin.Any + public final override /*1*/ /*fake_override*/ fun compareTo(/*0*/ other: EnumClass): kotlin.Int + public final override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean + protected/*protected and package*/ final override /*1*/ /*fake_override*/ /*isHiddenForResolutionEverywhereBesideSupercalls*/ fun finalize(): kotlin.Unit + public final override /*1*/ /*fake_override*/ /*isHiddenForResolutionEverywhereBesideSupercalls*/ fun getDeclaringClass(): java.lang.Class! + public final override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int + public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String + + // Static members + public final /*synthesized*/ fun valueOf(/*0*/ value: kotlin.String): EnumClass + public final /*synthesized*/ fun values(): kotlin.Array +} + +public final class Outer { + 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 + + public final inner class Inner { + public constructor Inner() + 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 typealias TestInner = Outer.Inner +} + +public sealed class SealedClass { + private constructor SealedClass() + 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 typealias Test1 = AbstractClass +public typealias Test2 = AnnotationClass +public typealias Test3 = EnumClass +public typealias Test4 = SealedClass +public typealias Test5 = Outer.Inner diff --git a/compiler/testData/diagnostics/tests/typealias/typeAliasConstructorWrongVisibility.kt b/compiler/testData/diagnostics/tests/typealias/typeAliasConstructorWrongVisibility.kt new file mode 100644 index 00000000000..69aaa4e1793 --- /dev/null +++ b/compiler/testData/diagnostics/tests/typealias/typeAliasConstructorWrongVisibility.kt @@ -0,0 +1,26 @@ +open class MyClass private constructor(val x: Int) { + + protected constructor(x: String) : this(x.length) + + constructor(x: Double) : this(x.toInt()) +} + +typealias MyAlias = MyClass + +val test1 = MyAlias(1) +val test1a = MyClass(1) + +val test2 = MyAlias("") +val test2a = MyClass("") + +val test3 = MyAlias(1.0) +val test3a = MyClass(1.0) + +class MyDerived : MyClass(1.0) { + val test4 = MyAlias(1) + val test4a = MyClass(1) + val test5 = MyAlias("") + val test5a = MyClass("") + val test6 = MyAlias(1.0) + val test6a = MyClass(1.0) +} \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/typealias/typeAliasConstructorWrongVisibility.txt b/compiler/testData/diagnostics/tests/typealias/typeAliasConstructorWrongVisibility.txt new file mode 100644 index 00000000000..5228945270b --- /dev/null +++ b/compiler/testData/diagnostics/tests/typealias/typeAliasConstructorWrongVisibility.txt @@ -0,0 +1,33 @@ +package + +public val test1: [ERROR : Type for MyAlias(1)] +public val test1a: [ERROR : Type for MyClass(1)] +public val test2: [ERROR : Type for MyAlias("")] +public val test2a: [ERROR : Type for MyClass("")] +public val test3: MyClass +public val test3a: MyClass + +public open class MyClass { + public constructor MyClass(/*0*/ x: kotlin.Double) + private constructor MyClass(/*0*/ x: kotlin.Int) + protected constructor MyClass(/*0*/ x: kotlin.String) + public final val x: kotlin.Int + 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 MyDerived : MyClass { + public constructor MyDerived() + public final val test4: [ERROR : Type for MyAlias(1)] + public final val test4a: [ERROR : Type for MyClass(1)] + public final val test5: [ERROR : Type for MyAlias("")] + public final val test5a: MyClass + public final val test6: MyClass + public final val test6a: MyClass + public final override /*1*/ /*fake_override*/ val x: kotlin.Int + 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 typealias MyAlias = MyClass diff --git a/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java index fb02ce60a9a..dd6a6dcc256 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java @@ -20466,6 +20466,24 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTest { doTest(fileName); } + @TestMetadata("typeAliasConstructorInSuperCall.kt") + public void testTypeAliasConstructorInSuperCall() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/typealias/typeAliasConstructorInSuperCall.kt"); + doTest(fileName); + } + + @TestMetadata("typeAliasConstructorWrongClass.kt") + public void testTypeAliasConstructorWrongClass() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/typealias/typeAliasConstructorWrongClass.kt"); + doTest(fileName); + } + + @TestMetadata("typeAliasConstructorWrongVisibility.kt") + public void testTypeAliasConstructorWrongVisibility() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/typealias/typeAliasConstructorWrongVisibility.kt"); + doTest(fileName); + } + @TestMetadata("typeAliasObject.kt") public void testTypeAliasObject() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/typealias/typeAliasObject.kt"); diff --git a/core/descriptors/src/org/jetbrains/kotlin/descriptors/Visibilities.java b/core/descriptors/src/org/jetbrains/kotlin/descriptors/Visibilities.java index 623778c4c24..f3fb2c338f8 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/descriptors/Visibilities.java +++ b/core/descriptors/src/org/jetbrains/kotlin/descriptors/Visibilities.java @@ -19,6 +19,7 @@ package org.jetbrains.kotlin.descriptors; import kotlin.collections.SetsKt; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import org.jetbrains.kotlin.descriptors.impl.TypeAliasConstructorDescriptor; import org.jetbrains.kotlin.resolve.DescriptorUtils; import org.jetbrains.kotlin.resolve.scopes.receivers.ReceiverValue; import org.jetbrains.kotlin.resolve.scopes.receivers.SuperCallReceiverValue; @@ -316,6 +317,12 @@ public class Visibilities { parent = DescriptorUtils.getParentOfType(parent, DeclarationDescriptorWithVisibility.class); } + if (what instanceof TypeAliasConstructorDescriptor) { + DeclarationDescriptorWithVisibility invisibleUnderlying = + findInvisibleMember(receiver, ((TypeAliasConstructorDescriptor) what).getUnderlyingConstructorDescriptor(), from); + if (invisibleUnderlying != null) return invisibleUnderlying; + } + return null; }