Handle case when u-literals are using without unsigned declarations

This commit is contained in:
Mikhail Zarechenskiy
2018-05-28 17:30:42 +03:00
parent 8cd2d2e44c
commit 0da3ae328e
10 changed files with 71 additions and 9 deletions
@@ -19,10 +19,8 @@ package org.jetbrains.kotlin.resolve.constants
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
import org.jetbrains.kotlin.descriptors.ModuleDescriptor
import org.jetbrains.kotlin.descriptors.annotations.Annotations
import org.jetbrains.kotlin.types.ErrorUtils
import org.jetbrains.kotlin.types.KotlinType
import org.jetbrains.kotlin.types.KotlinTypeFactory
import org.jetbrains.kotlin.types.TypeUtils
import org.jetbrains.kotlin.descriptors.findClassAcrossModuleDependencies
import org.jetbrains.kotlin.types.*
interface CompileTimeConstant<out T> {
val isError: Boolean
@@ -85,6 +83,39 @@ class TypedCompileTimeConstant<out T>(
}
}
fun createIntegerValueTypeConstant(
value: Number,
module: ModuleDescriptor,
parameters: CompileTimeConstant.Parameters
): CompileTimeConstant<*> {
return if (parameters.isUnsigned && !hasUnsignedTypesInModuleDependencies(module)) {
UnsignedErrorValueTypeConstant(value, parameters)
} else {
IntegerValueTypeConstant(value, module, parameters)
}
}
fun hasUnsignedTypesInModuleDependencies(module: ModuleDescriptor): Boolean {
return module.findClassAcrossModuleDependencies(KotlinBuiltIns.FQ_NAMES.uInt) != null
}
class UnsignedErrorValueTypeConstant(
private val value: Number,
override val parameters: CompileTimeConstant.Parameters
) : CompileTimeConstant<Unit> {
val errorValue = ErrorValue.ErrorValueWithMessage(
"Type cannot be resolved. Please make sure you have the required dependencies for unsigned types in the classpath"
)
override fun toConstantValue(expectedType: KotlinType): ConstantValue<Unit> {
return errorValue
}
override fun equals(other: Any?) = other is UnsignedErrorValueTypeConstant && value == other.value
override fun hashCode() = value.hashCode()
}
class IntegerValueTypeConstant(
private val value: Number,
module: ModuleDescriptor,
@@ -21,7 +21,6 @@ import org.jetbrains.kotlin.descriptors.ModuleDescriptor
import org.jetbrains.kotlin.descriptors.TypeParameterDescriptor
import org.jetbrains.kotlin.descriptors.findClassAcrossModuleDependencies
import org.jetbrains.kotlin.name.ClassId
import org.jetbrains.kotlin.types.ErrorUtils
import org.jetbrains.kotlin.types.KotlinType
import org.jetbrains.kotlin.types.SimpleType
import org.jetbrains.kotlin.types.TypeConstructor
@@ -40,6 +39,12 @@ class IntegerValueTypeConstructor(
// for expected type 'Any' result type 'Int' should be returned
val isUnsigned = parameters.isUnsigned
if (isUnsigned) {
assert(hasUnsignedTypesInModuleDependencies(module)) {
"Unsigned types should be on classpath to create an unsigned type constructor"
}
}
// TODO: fix value ranges for unsigned types
checkBoundsAndAddSuperType(
value,
@@ -68,8 +73,7 @@ class IntegerValueTypeConstructor(
}
}
private fun unsignedType(classId: ClassId): SimpleType =
module.findClassAcrossModuleDependencies(classId)?.defaultType ?: ErrorUtils.createErrorType("Error type for $classId")
private fun unsignedType(classId: ClassId): SimpleType = module.findClassAcrossModuleDependencies(classId)!!.defaultType
override fun getSupertypes(): Collection<KotlinType> = supertypes