Type aliases: do not reparse RHS of type alias declaration in expanded type resolution

This commit is contained in:
Dmitry Petrov
2016-06-02 14:12:10 +03:00
parent 79ce614591
commit df335b9e8d
10 changed files with 48 additions and 20 deletions
@@ -735,9 +735,7 @@ public class DescriptorResolver {
DeferredType.create(storageManager, trace, new Function0<KotlinType>() { DeferredType.create(storageManager, trace, new Function0<KotlinType>() {
@Override @Override
public KotlinType invoke() { public KotlinType invoke() {
// TODO do not reparse type alias RHS, just expand it instead return typeResolver.resolveExpandedTypeForTypeAlias(typeAliasDescriptor);
// NB this messes up with diagnostics
return typeResolver.resolveType(scopeWithTypeParameters, typeReference, trace, true);
} }
})); }));
@@ -71,6 +71,13 @@ class TypeResolver(
return resolveType(TypeResolutionContext(scope, trace, checkBounds, false, typeReference.suppressDiagnosticsInDebugMode(), true), typeReference) 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 { private fun resolveType(c: TypeResolutionContext, typeReference: KtTypeReference): KotlinType {
assert(!c.allowBareTypes) { "Use resolvePossiblyBareType() when bare types are allowed" } assert(!c.allowBareTypes) { "Use resolvePossiblyBareType() when bare types are allowed" }
return resolvePossiblyBareType(c, typeReference).getActualType() return resolvePossiblyBareType(c, typeReference).getActualType()
@@ -550,7 +557,8 @@ class TypeResolver(
typeAliasExpansion: TypeAliasExpansion, typeAliasExpansion: TypeAliasExpansion,
reportStrategy: TypeAliasExpansionReportStrategy, reportStrategy: TypeAliasExpansionReportStrategy,
annotations: Annotations, annotations: Annotations,
recursionDepth: Int recursionDepth: Int,
withAbbreviatedType: Boolean = true
): KotlinType { ): KotlinType {
val originalProjection = TypeProjectionImpl(Variance.INVARIANT, typeAliasExpansion.descriptor.underlyingType) val originalProjection = TypeProjectionImpl(Variance.INVARIANT, typeAliasExpansion.descriptor.underlyingType)
val expandedProjection = expandTypeProjectionForTypeAlias(originalProjection, typeAliasExpansion, null, reportStrategy, recursionDepth) 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" "Type alias expansion: result for ${typeAliasExpansion.descriptor} is ${expandedProjection.projectionKind}, should be invariant"
} }
val abbreviatedType = KotlinTypeImpl.create(annotations, return if (withAbbreviatedType) {
typeAliasExpansion.descriptor.typeConstructor, val abbreviatedType = KotlinTypeImpl.create(annotations,
originalProjection.type.isMarkedNullable, typeAliasExpansion.descriptor.typeConstructor,
typeAliasExpansion.arguments, originalProjection.type.isMarkedNullable,
MemberScope.Empty) typeAliasExpansion.arguments,
MemberScope.Empty)
return expandedType.withAbbreviatedType(abbreviatedType) expandedType.withAbbreviatedType(abbreviatedType)
}
else {
expandedType
}
} }
private fun expandTypeProjectionForTypeAlias( private fun expandTypeProjectionForTypeAlias(
+4 -2
View File
@@ -1,5 +1,7 @@
typealias S = String 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,8 +2,8 @@
class TColl<T, C : Collection<T>> class TColl<T, C : Collection<T>>
typealias TCErr = TColl<String, <!UPPER_BOUND_VIOLATED, UPPER_BOUND_VIOLATED!>Any<!>> typealias TCErr = TColl<String, <!UPPER_BOUND_VIOLATED!>Any<!>>
typealias TCErr2 = <!UPPER_BOUND_VIOLATED_IN_TYPEALIAS_EXPANSION!>TCErr<!> typealias TCErr2 = TCErr
fun testType1(x: <!UPPER_BOUND_VIOLATED_IN_TYPEALIAS_EXPANSION!>TCErr<!>) {} fun testType1(x: <!UPPER_BOUND_VIOLATED_IN_TYPEALIAS_EXPANSION!>TCErr<!>) {}
val testCtor1 = <!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
+2 -2
View File
@@ -1,4 +1,4 @@
typealias A = <!RECURSIVE_TYPEALIAS_EXPANSION!>B<!> typealias A = B
typealias B = <!RECURSIVE_TYPEALIAS_EXPANSION!>A<!> typealias B = A
val x: <!RECURSIVE_TYPEALIAS_EXPANSION!>A<!> = TODO() val x: <!RECURSIVE_TYPEALIAS_EXPANSION!>A<!> = TODO()
@@ -3,13 +3,13 @@ class Out<out T>
class Inv<T> class Inv<T>
typealias In1<T> = In<T> typealias In1<T> = In<T>
typealias In2<T> = In<<!REDUNDANT_PROJECTION, REDUNDANT_PROJECTION!>in<!> T> typealias In2<T> = In<<!REDUNDANT_PROJECTION!>in<!> T>
typealias In3<T> = In<<!CONFLICTING_PROJECTION, CONFLICTING_PROJECTION!>out<!> T> typealias In3<T> = In<<!CONFLICTING_PROJECTION!>out<!> T>
typealias In4<T> = In<*> typealias In4<T> = In<*>
typealias Out1<T> = Out<T> typealias Out1<T> = Out<T>
typealias Out2<T> = Out<<!CONFLICTING_PROJECTION, CONFLICTING_PROJECTION!>in<!> T> typealias Out2<T> = Out<<!CONFLICTING_PROJECTION!>in<!> T>
typealias Out3<T> = Out<<!REDUNDANT_PROJECTION, REDUNDANT_PROJECTION!>out<!> T> typealias Out3<T> = Out<<!REDUNDANT_PROJECTION!>out<!> T>
typealias Out4<T> = Out<*> typealias Out4<T> = Out<*>
typealias Inv1<T> = Inv<T> typealias Inv1<T> = Inv<T>
@@ -19296,6 +19296,12 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTest {
doTest(fileName); 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") @TestMetadata("genericTypeAliasConstructor.kt")
public void testGenericTypeAliasConstructor() throws Exception { public void testGenericTypeAliasConstructor() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/typealias/genericTypeAliasConstructor.kt"); String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/typealias/genericTypeAliasConstructor.kt");
@@ -88,6 +88,8 @@ abstract class AbstractTypeAliasDescriptor(
override val annotations: Annotations override val annotations: Annotations
get() = declarationDescriptor.annotations get() = declarationDescriptor.annotations
override fun toString(): String = "[typealias ${declarationDescriptor.name.asString()}]"
} }
} }