Specify exactly which typevars are allowed to top level

This commit is contained in:
Zac Sweers
2020-01-18 23:11:17 -05:00
parent 76b4ec9f92
commit 1edcb77f76

View File

@@ -255,7 +255,7 @@ internal fun targetType(messager: Messager,
val supertypeProperties = declaredProperties( val supertypeProperties = declaredProperties(
constructor = constructor, constructor = constructor,
kotlinApi = supertypeApi, kotlinApi = supertypeApi,
allowTypeVars = typeVariables.isNotEmpty(), allowedTypeVars = typeVariables.toSet(),
currentClass = appliedClassName, currentClass = appliedClassName,
resolvedTypes = resolvedTypes resolvedTypes = resolvedTypes
) )
@@ -296,7 +296,7 @@ private fun resolveTypeArgs(
targetClass: ClassName, targetClass: ClassName,
propertyType: TypeName, propertyType: TypeName,
resolvedTypes: List<ResolvedTypeMapping>, resolvedTypes: List<ResolvedTypeMapping>,
allowTypeVars: Boolean, allowedTypeVars: Set<TypeVariableName>,
entryStartIndex: Int = resolvedTypes.indexOfLast { it.target == targetClass } entryStartIndex: Int = resolvedTypes.indexOfLast { it.target == targetClass }
): TypeName { ): TypeName {
val unwrappedType = propertyType.unwrapTypeAlias() val unwrappedType = propertyType.unwrapTypeAlias()
@@ -321,13 +321,14 @@ private fun resolveTypeArgs(
resolvedType !is TypeVariableName -> resolvedType resolvedType !is TypeVariableName -> resolvedType
entryStartIndex != 0 -> { entryStartIndex != 0 -> {
// We need to go deeper // We need to go deeper
resolveTypeArgs(targetClass, resolvedType, resolvedTypes, allowTypeVars, entryStartIndex - 1) resolveTypeArgs(targetClass, resolvedType, resolvedTypes, allowedTypeVars, entryStartIndex - 1)
} }
allowTypeVars -> { resolvedType.copy(nullable = false) in allowedTypeVars -> {
// TODO this is ok but it would be nice to verify the typevar here is the one defined on our target class // This is a generic type in the top-level declared class. This is fine to leave in because
// this will be handled by the `Type` array passed in at runtime.
resolvedType resolvedType
} }
else -> error("Could not find $resolvedType in $resolvedTypes") else -> error("Could not find $resolvedType in $resolvedTypes. Also not present in allowable top-level type vars $allowedTypeVars")
} }
} }
@@ -336,7 +337,7 @@ private fun resolveTypeArgs(
private fun declaredProperties( private fun declaredProperties(
constructor: TargetConstructor, constructor: TargetConstructor,
kotlinApi: TypeSpec, kotlinApi: TypeSpec,
allowTypeVars: Boolean, allowedTypeVars: Set<TypeVariableName>,
currentClass: ClassName, currentClass: ClassName,
resolvedTypes: List<ResolvedTypeMapping> resolvedTypes: List<ResolvedTypeMapping>
): Map<String, TargetProperty> { ): Map<String, TargetProperty> {
@@ -347,7 +348,7 @@ private fun declaredProperties(
targetClass = currentClass, targetClass = currentClass,
propertyType = initialProperty.type, propertyType = initialProperty.type,
resolvedTypes = resolvedTypes, resolvedTypes = resolvedTypes,
allowTypeVars = allowTypeVars allowedTypeVars = allowedTypeVars
) )
val property = initialProperty.toBuilder(type = resolvedType).build() val property = initialProperty.toBuilder(type = resolvedType).build()
val name = property.name val name = property.name