UNUSED_TYPEALIAS_PARAMETER: special diagnostic for typealias parameters
explicitly telling that such type parameters are not used in type checking. Move typealias-related utility functions on types to TypeUtils.kt.
This commit is contained in:
@@ -650,6 +650,8 @@ public interface Errors {
|
||||
|
||||
DiagnosticFactory1<KtNamedDeclaration, VariableDescriptor> UNUSED_VARIABLE = DiagnosticFactory1.create(WARNING, DECLARATION_NAME);
|
||||
DiagnosticFactory1<KtParameter, VariableDescriptor> UNUSED_PARAMETER = DiagnosticFactory1.create(WARNING, DECLARATION_NAME);
|
||||
DiagnosticFactory2<KtTypeParameter, TypeParameterDescriptor, KotlinType> UNUSED_TYPEALIAS_PARAMETER =
|
||||
DiagnosticFactory2.create(WARNING, DECLARATION_NAME);
|
||||
|
||||
DiagnosticFactory1<KtNamedDeclaration, VariableDescriptor> ASSIGNED_BUT_NEVER_ACCESSED_VARIABLE =
|
||||
DiagnosticFactory1.create(WARNING, DECLARATION_NAME);
|
||||
|
||||
+1
@@ -428,6 +428,7 @@ public class DefaultErrorMessages {
|
||||
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(TYPEALIAS_EXPANDED_TO_MALFORMED_TYPE, "Type alias expanded to malformed type {0}: {1}", RENDER_TYPE, STRING);
|
||||
MAP.put(UNUSED_TYPEALIAS_PARAMETER, "Type alias parameter {0} is not used in the expanded type {1} and does not affect type checking", NAME, RENDER_TYPE);
|
||||
|
||||
MAP.put(TOO_MANY_ARGUMENTS, "Too many arguments for {0}", FQ_NAMES_IN_TYPES);
|
||||
|
||||
|
||||
@@ -35,9 +35,8 @@ import org.jetbrains.kotlin.resolve.DescriptorUtils.classCanHaveAbstractMembers
|
||||
import org.jetbrains.kotlin.resolve.DescriptorUtils.classCanHaveOpenMembers
|
||||
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.isArrayOfNothing
|
||||
import org.jetbrains.kotlin.types.typeUtil.isNothing
|
||||
import org.jetbrains.kotlin.types.typeUtil.*
|
||||
import org.jetbrains.kotlin.utils.addToStdlib.check
|
||||
import java.util.*
|
||||
|
||||
fun KtDeclaration.checkTypeReferences(trace: BindingTrace) {
|
||||
@@ -155,7 +154,7 @@ class DeclarationsChecker(
|
||||
|
||||
checkTypeAliasExpansion(typeAliasDescriptor, declaration)
|
||||
|
||||
val expandedType = typeAliasDescriptor.expandedType // TODO refactor type alias expansion
|
||||
val expandedType = typeAliasDescriptor.expandedType
|
||||
if (expandedType.isError) return
|
||||
|
||||
val expandedClassifier = expandedType.constructor.declarationDescriptor
|
||||
@@ -167,8 +166,23 @@ class DeclarationsChecker(
|
||||
if (TypeUtils.contains(expandedType) { it.isArrayOfNothing()} ) {
|
||||
trace.report(TYPEALIAS_EXPANDED_TO_MALFORMED_TYPE.on(typeReference, expandedType, "Array<Nothing> is illegal"))
|
||||
}
|
||||
|
||||
val usedTypeAliasParameters: Set<TypeParameterDescriptor> = getUsedTypeAliasParameters(expandedType, typeAliasDescriptor)
|
||||
for (typeParameter in typeAliasDescriptor.declaredTypeParameters) {
|
||||
if (typeParameter !in usedTypeAliasParameters) {
|
||||
val source = DescriptorToSourceUtils.descriptorToDeclaration(typeParameter) as? KtTypeParameter
|
||||
?: throw AssertionError("No source element for type parameter $typeParameter of $typeAliasDescriptor")
|
||||
trace.report(UNUSED_TYPEALIAS_PARAMETER.on(source, typeParameter, expandedType))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun getUsedTypeAliasParameters(type: KotlinType, typeAlias: TypeAliasDescriptor): Set<TypeParameterDescriptor> =
|
||||
type.constituentTypes().mapNotNullTo(HashSet()) {
|
||||
val descriptor = it.constructor.declarationDescriptor as? TypeParameterDescriptor
|
||||
descriptor?.check { it.containingDeclaration == typeAlias }
|
||||
}
|
||||
|
||||
private class TypeAliasDeclarationCheckingReportStrategy(
|
||||
private val trace: BindingTrace,
|
||||
typeAliasDescriptor: TypeAliasDescriptor,
|
||||
|
||||
@@ -21,6 +21,8 @@ import org.jetbrains.kotlin.descriptors.TypeParameterDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.annotations.Annotations
|
||||
import org.jetbrains.kotlin.resolve.scopes.MemberScope
|
||||
import org.jetbrains.kotlin.types.*
|
||||
import org.jetbrains.kotlin.types.typeUtil.containsTypeAliasParameters
|
||||
import org.jetbrains.kotlin.types.typeUtil.requiresTypeAliasExpansion
|
||||
|
||||
class TypeAliasExpander(
|
||||
private val reportStrategy: TypeAliasExpansionReportStrategy
|
||||
|
||||
@@ -23,6 +23,7 @@ import org.jetbrains.kotlin.types.KotlinType
|
||||
import org.jetbrains.kotlin.types.TypeConstructor
|
||||
import org.jetbrains.kotlin.types.TypeProjection
|
||||
import org.jetbrains.kotlin.types.TypeUtils
|
||||
import org.jetbrains.kotlin.types.typeUtil.contains
|
||||
|
||||
class TypeAliasExpansion private constructor(
|
||||
val parent: TypeAliasExpansion?,
|
||||
@@ -56,23 +57,3 @@ class TypeAliasExpansion private constructor(
|
||||
create(null, typeAliasDescriptor, emptyList())
|
||||
}
|
||||
}
|
||||
|
||||
fun KotlinType.containsTypeAliasParameters(): Boolean =
|
||||
TypeUtils.contains(this) {
|
||||
it.constructor.declarationDescriptor?.isTypeAliasParameter() ?: false
|
||||
}
|
||||
|
||||
fun KotlinType.containsTypeAliases(): Boolean =
|
||||
TypeUtils.contains(this) {
|
||||
it.constructor.declarationDescriptor is TypeAliasDescriptor
|
||||
}
|
||||
|
||||
fun ClassifierDescriptor.isTypeAliasParameter(): Boolean =
|
||||
this is TypeParameterDescriptor && containingDeclaration is TypeAliasDescriptor
|
||||
|
||||
fun KotlinType.requiresTypeAliasExpansion(): Boolean =
|
||||
TypeUtils.contains(this) {
|
||||
it.constructor.declarationDescriptor?.let {
|
||||
it is TypeAliasDescriptor || it is TypeParameterDescriptor
|
||||
}
|
||||
}
|
||||
|
||||
@@ -35,8 +35,6 @@ import org.jetbrains.kotlin.psi.debugText.getDebugText
|
||||
import org.jetbrains.kotlin.resolve.PossiblyBareType.type
|
||||
import org.jetbrains.kotlin.resolve.bindingContextUtil.recordScope
|
||||
import org.jetbrains.kotlin.resolve.calls.tasks.DynamicCallableDescriptors
|
||||
import org.jetbrains.kotlin.resolve.lazy.ForceResolveUtil
|
||||
import org.jetbrains.kotlin.resolve.lazy.LazyEntity
|
||||
import org.jetbrains.kotlin.resolve.scopes.LazyScopeAdapter
|
||||
import org.jetbrains.kotlin.resolve.scopes.LexicalScope
|
||||
import org.jetbrains.kotlin.resolve.scopes.MemberScope
|
||||
@@ -44,10 +42,10 @@ import org.jetbrains.kotlin.resolve.scopes.utils.findClassifier
|
||||
import org.jetbrains.kotlin.resolve.source.toSourceElement
|
||||
import org.jetbrains.kotlin.storage.LockBasedStorageManager
|
||||
import org.jetbrains.kotlin.storage.StorageManager
|
||||
import org.jetbrains.kotlin.storage.getValue
|
||||
import org.jetbrains.kotlin.types.*
|
||||
import org.jetbrains.kotlin.types.Variance.*
|
||||
import org.jetbrains.kotlin.types.typeUtil.contains
|
||||
import org.jetbrains.kotlin.types.typeUtil.containsTypeAliasParameters
|
||||
import org.jetbrains.kotlin.types.typeUtil.containsTypeAliases
|
||||
import org.jetbrains.kotlin.types.typeUtil.isArrayOfNothing
|
||||
|
||||
class TypeResolver(
|
||||
|
||||
@@ -3,7 +3,7 @@ object AnObject {
|
||||
fun foo() = "OK"
|
||||
}
|
||||
|
||||
typealias GenericTestObject<T> = AnObject
|
||||
typealias GenericTestObject<<!UNUSED_TYPEALIAS_PARAMETER!>T<!>> = AnObject
|
||||
|
||||
val test11: AnObject = GenericTestObject
|
||||
val test12: GenericTestObject<*> = GenericTestObject
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
// !DIAGNOSTICS: -UNUSED_TYPEALIAS_PARAMETER
|
||||
|
||||
typealias WithVariance<<!VARIANCE_ON_TYPE_PARAMETER_NOT_ALLOWED!>in<!> X, <!VARIANCE_ON_TYPE_PARAMETER_NOT_ALLOWED!>out<!> Y> = Int
|
||||
typealias WithBounds1<T : <!BOUND_ON_TYPE_ALIAS_PARAMETER_NOT_ALLOWED!>T<!>> = Int
|
||||
typealias WithBounds2<X : <!BOUND_ON_TYPE_ALIAS_PARAMETER_NOT_ALLOWED!>Y<!>, Y : <!BOUND_ON_TYPE_ALIAS_PARAMETER_NOT_ALLOWED!>X<!>> = Int
|
||||
|
||||
@@ -5,16 +5,16 @@ class Inv<T>
|
||||
typealias In1<T> = In<T>
|
||||
typealias In2<T> = In<<!REDUNDANT_PROJECTION!>in<!> T>
|
||||
typealias In3<T> = In<<!CONFLICTING_PROJECTION!>out<!> T>
|
||||
typealias In4<T> = In<*>
|
||||
typealias In4<<!UNUSED_TYPEALIAS_PARAMETER!>T<!>> = In<*>
|
||||
|
||||
typealias Out1<T> = Out<T>
|
||||
typealias Out2<T> = Out<<!CONFLICTING_PROJECTION!>in<!> T>
|
||||
typealias Out3<T> = Out<<!REDUNDANT_PROJECTION!>out<!> T>
|
||||
typealias Out4<T> = Out<*>
|
||||
typealias Out4<<!UNUSED_TYPEALIAS_PARAMETER!>T<!>> = Out<*>
|
||||
|
||||
typealias Inv1<T> = Inv<T>
|
||||
typealias Inv2<T> = Inv<in T>
|
||||
typealias Inv3<T> = Inv<out T>
|
||||
typealias Inv4<T> = Inv<*>
|
||||
typealias Inv4<<!UNUSED_TYPEALIAS_PARAMETER!>T<!>> = Inv<*>
|
||||
|
||||
val inv1: Inv1<Int> = Inv<Int>()
|
||||
+2
-2
@@ -1,6 +1,6 @@
|
||||
typealias ToTypeParam1<T> = <!TYPEALIAS_SHOULD_EXPAND_TO_CLASS!>T<!>
|
||||
typealias ToTypeParam2<T> = <!TYPEALIAS_SHOULD_EXPAND_TO_CLASS!>ToTypeParam1<T><!>
|
||||
typealias ToTypeParam3<T1, T2> = <!TYPEALIAS_SHOULD_EXPAND_TO_CLASS!>ToTypeParam2<T1><!>
|
||||
typealias ToTypeParam3<T1, <!UNUSED_TYPEALIAS_PARAMETER!>T2<!>> = <!TYPEALIAS_SHOULD_EXPAND_TO_CLASS!>ToTypeParam2<T1><!>
|
||||
typealias ToTypeParam4 = ToTypeParam1<Any>
|
||||
|
||||
typealias ToFun1 = () -> Unit
|
||||
@@ -9,6 +9,6 @@ typealias ToFun2<T> = (T) -> Unit
|
||||
class Outer {
|
||||
typealias ToTypeParam1<T> = <!TYPEALIAS_SHOULD_EXPAND_TO_CLASS!>T<!>
|
||||
typealias ToTypeParam2<T> = <!TYPEALIAS_SHOULD_EXPAND_TO_CLASS!>ToTypeParam1<T><!>
|
||||
typealias ToTypeParam3<T1, T2> = <!TYPEALIAS_SHOULD_EXPAND_TO_CLASS!>ToTypeParam2<T1><!>
|
||||
typealias ToTypeParam3<T1, <!UNUSED_TYPEALIAS_PARAMETER!>T2<!>> = <!TYPEALIAS_SHOULD_EXPAND_TO_CLASS!>ToTypeParam2<T1><!>
|
||||
typealias ToTypeParam4 = ToTypeParam1<Any>
|
||||
}
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
typealias Test<T, <!UNUSED_TYPEALIAS_PARAMETER!>X<!>> = List<T>
|
||||
typealias Test2<T, <!UNUSED_TYPEALIAS_PARAMETER!>X<!>> = Test<T, X>
|
||||
@@ -0,0 +1,4 @@
|
||||
package
|
||||
|
||||
public typealias Test</*0*/ T, /*1*/ X> = kotlin.collections.List<T>
|
||||
public typealias Test2</*0*/ T, /*1*/ X> = Test<T, X>
|
||||
@@ -19647,6 +19647,12 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTest {
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("unusedTypeAliasParameter.kt")
|
||||
public void testUnusedTypeAliasParameter() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/typealias/unusedTypeAliasParameter.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("wrongNumberOfArgumentsInTypeAliasConstructor.kt")
|
||||
public void testWrongNumberOfArgumentsInTypeAliasConstructor() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/typealias/wrongNumberOfArgumentsInTypeAliasConstructor.kt");
|
||||
|
||||
@@ -17,9 +17,7 @@
|
||||
package org.jetbrains.kotlin.types.typeUtil
|
||||
|
||||
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
|
||||
import org.jetbrains.kotlin.descriptors.ClassDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.ClassKind
|
||||
import org.jetbrains.kotlin.descriptors.TypeParameterDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.*
|
||||
import org.jetbrains.kotlin.descriptors.annotations.Annotations
|
||||
import org.jetbrains.kotlin.resolve.DescriptorUtils
|
||||
import org.jetbrains.kotlin.resolve.calls.inference.isCaptured
|
||||
@@ -128,6 +126,9 @@ fun constituentTypes(types: Collection<KotlinType>): Collection<KotlinType> {
|
||||
return result
|
||||
}
|
||||
|
||||
fun KotlinType.constituentTypes(): Collection<KotlinType> =
|
||||
constituentTypes(listOf(this))
|
||||
|
||||
private fun constituentTypes(result: MutableSet<KotlinType>, types: Collection<KotlinType>) {
|
||||
result.addAll(types)
|
||||
for (type in types) {
|
||||
@@ -135,7 +136,7 @@ private fun constituentTypes(result: MutableSet<KotlinType>, types: Collection<K
|
||||
with (type.asFlexibleType()) { constituentTypes(result, setOf(lowerBound, upperBound)) }
|
||||
}
|
||||
else {
|
||||
constituentTypes(result, type.arguments.filterNot { it.isStarProjection }.map { it.type })
|
||||
constituentTypes(result, type.arguments.mapNotNull { if (!it.isStarProjection) it.type else null })
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -170,3 +171,24 @@ private fun SimpleType.replaceArgumentsWithStarProjections(): SimpleType {
|
||||
|
||||
return replace(newArguments)
|
||||
}
|
||||
|
||||
fun KotlinType.containsTypeAliasParameters(): Boolean =
|
||||
contains {
|
||||
it.constructor.declarationDescriptor?.isTypeAliasParameter() ?: false
|
||||
}
|
||||
|
||||
fun KotlinType.containsTypeAliases(): Boolean =
|
||||
contains {
|
||||
it.constructor.declarationDescriptor is TypeAliasDescriptor
|
||||
}
|
||||
|
||||
fun ClassifierDescriptor.isTypeAliasParameter(): Boolean =
|
||||
this is TypeParameterDescriptor && containingDeclaration is TypeAliasDescriptor
|
||||
|
||||
fun KotlinType.requiresTypeAliasExpansion(): Boolean =
|
||||
contains {
|
||||
it.constructor.declarationDescriptor?.let {
|
||||
it is TypeAliasDescriptor || it is TypeParameterDescriptor
|
||||
} ?: false
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user