Additional checks for type alias constructors.

This commit is contained in:
Dmitry Petrov
2016-09-15 18:20:39 +03:00
committed by Dmitry Petrov
parent 796d11c860
commit dac9d8b845
10 changed files with 258 additions and 5 deletions
@@ -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))
}
}
@@ -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<VariableDescriptor> {
@@ -0,0 +1,14 @@
open class MyBase protected constructor() {
protected constructor(<!UNUSED_PARAMETER!>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(<!DEBUG_INFO_CONSTANT!>x<!>)
}
@@ -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
@@ -0,0 +1,32 @@
abstract class AbstractClass
typealias Test1 = AbstractClass
val test1 = <!CREATING_AN_INSTANCE_OF_ABSTRACT_CLASS!>Test1()<!>
val test1a = <!CREATING_AN_INSTANCE_OF_ABSTRACT_CLASS!>AbstractClass()<!>
annotation class AnnotationClass
typealias Test2 = AnnotationClass
val test2 = <!ANNOTATION_CLASS_CONSTRUCTOR_CALL!>Test2()<!>
val test2a = <!ANNOTATION_CLASS_CONSTRUCTOR_CALL!>AnnotationClass()<!>
enum class EnumClass { VALUE1, VALUE2 }
typealias Test3 = EnumClass
val test3 = <!ENUM_CLASS_CONSTRUCTOR_CALL!><!INVISIBLE_MEMBER!>Test3<!>()<!>
val test3a = <!ENUM_CLASS_CONSTRUCTOR_CALL!><!INVISIBLE_MEMBER!>EnumClass<!>()<!>
sealed class SealedClass
typealias Test4 = SealedClass
val test4 = <!SEALED_CLASS_CONSTRUCTOR_CALL!><!INVISIBLE_MEMBER!>Test4<!>()<!>
val test4a = <!SEALED_CLASS_CONSTRUCTOR_CALL!><!INVISIBLE_MEMBER!>SealedClass<!>()<!>
class Outer {
inner class Inner
typealias TestInner = Inner
}
typealias Test5 = Outer.Inner
val test5 = <!UNRESOLVED_REFERENCE!>Test5<!>()
val test5a = Outer.<!UNRESOLVED_REFERENCE!>Inner<!>()
val test5b = Outer.<!UNRESOLVED_REFERENCE!>TestInner<!>()
val test5c = Outer().<!UNRESOLVED_REFERENCE!>TestInner<!>() // TODO extension type alias constructors?
val test5d = Outer().Inner()
val test5e = Outer().<!UNRESOLVED_REFERENCE!>Test5<!>() // TODO extension type alias constructors?
@@ -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<EnumClass> {
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<EnumClass!>!
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<EnumClass>
}
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
@@ -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 = <!NONE_APPLICABLE!>MyAlias<!>(1)
val test1a = <!NONE_APPLICABLE!>MyClass<!>(1)
val test2 = <!NONE_APPLICABLE!>MyAlias<!>("")
val test2a = <!NONE_APPLICABLE!>MyClass<!>("")
val test3 = MyAlias(1.0)
val test3a = MyClass(1.0)
class MyDerived : MyClass(1.0) {
val test4 = <!NONE_APPLICABLE!>MyAlias<!>(1)
val test4a = <!NONE_APPLICABLE!>MyClass<!>(1)
val test5 = <!NONE_APPLICABLE!>MyAlias<!>("")
val test5a = <!PROTECTED_CONSTRUCTOR_NOT_IN_SUPER_CALL!>MyClass<!>("")
val test6 = MyAlias(1.0)
val test6a = MyClass(1.0)
}
@@ -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
@@ -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");
@@ -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;
}