Type aliases: do not reparse RHS of type alias declaration in expanded type resolution
This commit is contained in:
@@ -735,9 +735,7 @@ public class DescriptorResolver {
|
||||
DeferredType.create(storageManager, trace, new Function0<KotlinType>() {
|
||||
@Override
|
||||
public KotlinType invoke() {
|
||||
// TODO do not reparse type alias RHS, just expand it instead
|
||||
// NB this messes up with diagnostics
|
||||
return typeResolver.resolveType(scopeWithTypeParameters, typeReference, trace, true);
|
||||
return typeResolver.resolveExpandedTypeForTypeAlias(typeAliasDescriptor);
|
||||
}
|
||||
}));
|
||||
|
||||
|
||||
@@ -71,6 +71,13 @@ class TypeResolver(
|
||||
return resolveType(TypeResolutionContext(scope, trace, checkBounds, false, typeReference.suppressDiagnosticsInDebugMode(), true), typeReference)
|
||||
}
|
||||
|
||||
fun resolveExpandedTypeForTypeAlias(typeAliasDescriptor: TypeAliasDescriptor): KotlinType {
|
||||
val typeAliasExpansion = createTypeAliasExpansion(null, typeAliasDescriptor, emptyList())
|
||||
val expandedType = expandTypeAlias(typeAliasExpansion, TypeAliasExpansionReportStrategy.DEFAULT, Annotations.EMPTY, 0,
|
||||
withAbbreviatedType = false)
|
||||
return expandedType
|
||||
}
|
||||
|
||||
private fun resolveType(c: TypeResolutionContext, typeReference: KtTypeReference): KotlinType {
|
||||
assert(!c.allowBareTypes) { "Use resolvePossiblyBareType() when bare types are allowed" }
|
||||
return resolvePossiblyBareType(c, typeReference).getActualType()
|
||||
@@ -550,7 +557,8 @@ class TypeResolver(
|
||||
typeAliasExpansion: TypeAliasExpansion,
|
||||
reportStrategy: TypeAliasExpansionReportStrategy,
|
||||
annotations: Annotations,
|
||||
recursionDepth: Int
|
||||
recursionDepth: Int,
|
||||
withAbbreviatedType: Boolean = true
|
||||
): KotlinType {
|
||||
val originalProjection = TypeProjectionImpl(Variance.INVARIANT, typeAliasExpansion.descriptor.underlyingType)
|
||||
val expandedProjection = expandTypeProjectionForTypeAlias(originalProjection, typeAliasExpansion, null, reportStrategy, recursionDepth)
|
||||
@@ -562,13 +570,18 @@ class TypeResolver(
|
||||
"Type alias expansion: result for ${typeAliasExpansion.descriptor} is ${expandedProjection.projectionKind}, should be invariant"
|
||||
}
|
||||
|
||||
val abbreviatedType = KotlinTypeImpl.create(annotations,
|
||||
typeAliasExpansion.descriptor.typeConstructor,
|
||||
originalProjection.type.isMarkedNullable,
|
||||
typeAliasExpansion.arguments,
|
||||
MemberScope.Empty)
|
||||
return if (withAbbreviatedType) {
|
||||
val abbreviatedType = KotlinTypeImpl.create(annotations,
|
||||
typeAliasExpansion.descriptor.typeConstructor,
|
||||
originalProjection.type.isMarkedNullable,
|
||||
typeAliasExpansion.arguments,
|
||||
MemberScope.Empty)
|
||||
|
||||
return expandedType.withAbbreviatedType(abbreviatedType)
|
||||
expandedType.withAbbreviatedType(abbreviatedType)
|
||||
}
|
||||
else {
|
||||
expandedType
|
||||
}
|
||||
}
|
||||
|
||||
private fun expandTypeProjectionForTypeAlias(
|
||||
|
||||
+4
-2
@@ -1,5 +1,7 @@
|
||||
typealias S = String
|
||||
|
||||
val OK: S = "OK"
|
||||
typealias SF<T> = (T) -> S
|
||||
|
||||
fun box(): S = OK
|
||||
val f: SF<S> = { it }
|
||||
|
||||
fun box(): S = f("OK")
|
||||
|
||||
+2
-2
@@ -2,8 +2,8 @@
|
||||
|
||||
class TColl<T, C : Collection<T>>
|
||||
|
||||
typealias TCErr = TColl<String, <!UPPER_BOUND_VIOLATED, UPPER_BOUND_VIOLATED!>Any<!>>
|
||||
typealias TCErr2 = <!UPPER_BOUND_VIOLATED_IN_TYPEALIAS_EXPANSION!>TCErr<!>
|
||||
typealias TCErr = TColl<String, <!UPPER_BOUND_VIOLATED!>Any<!>>
|
||||
typealias TCErr2 = TCErr
|
||||
|
||||
fun testType1(x: <!UPPER_BOUND_VIOLATED_IN_TYPEALIAS_EXPANSION!>TCErr<!>) {}
|
||||
val testCtor1 = <!UPPER_BOUND_VIOLATED_IN_TYPEALIAS_EXPANSION!>TCErr()<!>
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
val f: () -> Unit = {}
|
||||
|
||||
typealias F = () -> Unit
|
||||
@@ -0,0 +1,4 @@
|
||||
package
|
||||
|
||||
public typealias F = () -> kotlin.Unit
|
||||
public val f: () -> kotlin.Unit
|
||||
@@ -1,4 +1,4 @@
|
||||
typealias A = <!RECURSIVE_TYPEALIAS_EXPANSION!>B<!>
|
||||
typealias B = <!RECURSIVE_TYPEALIAS_EXPANSION!>A<!>
|
||||
typealias A = B
|
||||
typealias B = A
|
||||
|
||||
val x: <!RECURSIVE_TYPEALIAS_EXPANSION!>A<!> = TODO()
|
||||
@@ -3,13 +3,13 @@ class Out<out T>
|
||||
class Inv<T>
|
||||
|
||||
typealias In1<T> = In<T>
|
||||
typealias In2<T> = In<<!REDUNDANT_PROJECTION, REDUNDANT_PROJECTION!>in<!> T>
|
||||
typealias In3<T> = In<<!CONFLICTING_PROJECTION, CONFLICTING_PROJECTION!>out<!> T>
|
||||
typealias In2<T> = In<<!REDUNDANT_PROJECTION!>in<!> T>
|
||||
typealias In3<T> = In<<!CONFLICTING_PROJECTION!>out<!> T>
|
||||
typealias In4<T> = In<*>
|
||||
|
||||
typealias Out1<T> = Out<T>
|
||||
typealias Out2<T> = Out<<!CONFLICTING_PROJECTION, CONFLICTING_PROJECTION!>in<!> T>
|
||||
typealias Out3<T> = Out<<!REDUNDANT_PROJECTION, REDUNDANT_PROJECTION!>out<!> T>
|
||||
typealias Out2<T> = Out<<!CONFLICTING_PROJECTION!>in<!> T>
|
||||
typealias Out3<T> = Out<<!REDUNDANT_PROJECTION!>out<!> T>
|
||||
typealias Out4<T> = Out<*>
|
||||
|
||||
typealias Inv1<T> = Inv<T>
|
||||
|
||||
@@ -19296,6 +19296,12 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTest {
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("functionTypeInTypeAlias.kt")
|
||||
public void testFunctionTypeInTypeAlias() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/typealias/functionTypeInTypeAlias.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("genericTypeAliasConstructor.kt")
|
||||
public void testGenericTypeAliasConstructor() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/typealias/genericTypeAliasConstructor.kt");
|
||||
|
||||
+2
@@ -88,6 +88,8 @@ abstract class AbstractTypeAliasDescriptor(
|
||||
|
||||
override val annotations: Annotations
|
||||
get() = declarationDescriptor.annotations
|
||||
|
||||
override fun toString(): String = "[typealias ${declarationDescriptor.name.asString()}]"
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user