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
@@ -885,6 +885,8 @@ public interface Errors {
DiagnosticFactory1<KtElement, KtElement> ILLEGAL_ESCAPE = DiagnosticFactory1.create(ERROR, CUT_CHAR_QUOTES);
DiagnosticFactory1<KtConstantExpression, KotlinType> NULL_FOR_NONNULL_TYPE = DiagnosticFactory1.create(ERROR);
DiagnosticFactory0<KtEscapeStringTemplateEntry> ILLEGAL_ESCAPE_SEQUENCE = DiagnosticFactory0.create(ERROR);
DiagnosticFactory0<KtConstantExpression> UNSIGNED_LITERAL_WITHOUT_DECLARATIONS_ON_CLASSPATH = DiagnosticFactory0.create(ERROR);
// Casts and is-checks
@@ -382,6 +382,7 @@ public class DefaultErrorMessages {
MAP.put(RESOLUTION_TO_CLASSIFIER, "{2}", NAME, TO_STRING, STRING);
MAP.put(NOT_A_CLASS, "Not a class");
MAP.put(ILLEGAL_ESCAPE_SEQUENCE, "Illegal escape sequence");
MAP.put(UNSIGNED_LITERAL_WITHOUT_DECLARATIONS_ON_CLASSPATH, "Type of the constant expression cannot be resolved. Please make sure you have the required dependencies for unsigned types in the classpath");
MAP.put(RESERVED_SYNTAX_IN_CALLABLE_REFERENCE_LHS, "Left-hand side of callable reference matches expression syntax reserved for future releases");
@@ -935,7 +935,7 @@ private class ConstantExpressionEvaluatorVisitor(
expectedType: KotlinType
): CompileTimeConstant<*>? {
if (TypeUtils.noExpectedType(expectedType) || expectedType.isError) {
return IntegerValueTypeConstant(value, constantExpressionEvaluator.module, parameters)
return createIntegerValueTypeConstant(value, constantExpressionEvaluator.module, parameters)
}
val integerValue = ConstantValueFactory.createIntegerConstantValue(value, expectedType, parameters.isUnsigned)
if (integerValue != null) {
@@ -206,7 +206,13 @@ public class BasicExpressionTypingVisitor extends ExpressionTypingVisitor {
expression, context.trace, context.expectedType
);
if (!(compileTimeConstant instanceof IntegerValueTypeConstant)) {
if (compileTimeConstant instanceof UnsignedErrorValueTypeConstant) {
ErrorValue.ErrorValueWithMessage value = ((UnsignedErrorValueTypeConstant) compileTimeConstant).getErrorValue();
context.trace.report(Errors.UNSIGNED_LITERAL_WITHOUT_DECLARATIONS_ON_CLASSPATH.on(expression));
return TypeInfoFactoryKt.createTypeInfo(value.getType(components.moduleDescriptor), context);
}
else if (!(compileTimeConstant instanceof IntegerValueTypeConstant)) {
CompileTimeConstantChecker constantChecker = new CompileTimeConstantChecker(context, components.moduleDescriptor, false);
ConstantValue constantValue =
compileTimeConstant != null ? ((TypedCompileTimeConstant) compileTimeConstant).getConstantValue() : null;
@@ -0,0 +1,3 @@
val u1 = <!UNSIGNED_LITERAL_WITHOUT_DECLARATIONS_ON_CLASSPATH!>1u<!>
val u2 = <!UNSIGNED_LITERAL_WITHOUT_DECLARATIONS_ON_CLASSPATH!>0xFu<!>
val u3 = <!UNSIGNED_LITERAL_WITHOUT_DECLARATIONS_ON_CLASSPATH!>0b1u<!>
@@ -0,0 +1,5 @@
package
public val u1: [ERROR : Type cannot be resolved. Please make sure you have the required dependencies for unsigned types in the classpath]
public val u2: [ERROR : Type cannot be resolved. Please make sure you have the required dependencies for unsigned types in the classpath]
public val u3: [ERROR : Type cannot be resolved. Please make sure you have the required dependencies for unsigned types in the classpath]
@@ -10721,6 +10721,11 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTest {
runTest("compiler/testData/diagnostics/tests/inlineClasses/propertiesWithBackingFieldsInsideInlineClass.kt");
}
@TestMetadata("unsignedLiteralsWithoutArtifactOnClasspath.kt")
public void testUnsignedLiteralsWithoutArtifactOnClasspath() throws Exception {
runTest("compiler/testData/diagnostics/tests/inlineClasses/unsignedLiteralsWithoutArtifactOnClasspath.kt");
}
@TestMetadata("varargsOnParametersOfInlineClassType.kt")
public void testVarargsOnParametersOfInlineClassType() throws Exception {
runTest("compiler/testData/diagnostics/tests/inlineClasses/varargsOnParametersOfInlineClassType.kt");
@@ -10721,6 +10721,11 @@ public class DiagnosticsUsingJavacTestGenerated extends AbstractDiagnosticsUsing
runTest("compiler/testData/diagnostics/tests/inlineClasses/propertiesWithBackingFieldsInsideInlineClass.kt");
}
@TestMetadata("unsignedLiteralsWithoutArtifactOnClasspath.kt")
public void testUnsignedLiteralsWithoutArtifactOnClasspath() throws Exception {
runTest("compiler/testData/diagnostics/tests/inlineClasses/unsignedLiteralsWithoutArtifactOnClasspath.kt");
}
@TestMetadata("varargsOnParametersOfInlineClassType.kt")
public void testVarargsOnParametersOfInlineClassType() throws Exception {
runTest("compiler/testData/diagnostics/tests/inlineClasses/varargsOnParametersOfInlineClassType.kt");
@@ -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