diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/constants/evaluate/ConstantExpressionEvaluator.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/constants/evaluate/ConstantExpressionEvaluator.kt index 3424f0b1cef..4be229843e9 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/constants/evaluate/ConstantExpressionEvaluator.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/constants/evaluate/ConstantExpressionEvaluator.kt @@ -303,7 +303,6 @@ class ConstantExpressionEvaluator( if (!UnsignedTypes.isUnsignedType(constantType)) return - with(ExperimentalUsageChecker) { val descriptor = constantType.constructor.declarationDescriptor ?: return val experimentalities = descriptor.loadExperimentalities(moduleAnnotationsResolver, languageVersionSettings) @@ -986,6 +985,15 @@ private class ConstantExpressionEvaluatorVisitor( parameters: CompileTimeConstant.Parameters, expectedType: KotlinType ): CompileTimeConstant<*>? { + if (parameters.isUnsignedNumberLiteral && + !checkAccessibilityOfUnsignedTypes( + constantExpressionEvaluator.module, + constantExpressionEvaluator.languageVersionSettings + ) + ) { + return UnsignedErrorValueTypeConstant(value, parameters) + } + if (parameters.isUnsignedLongNumberLiteral) { return ULongValue(value).wrap(parameters) } @@ -1013,6 +1021,13 @@ private class ConstantExpressionEvaluatorVisitor( }.wrap(parameters) } + private fun checkAccessibilityOfUnsignedTypes(module: ModuleDescriptor, languageVersionSettings: LanguageVersionSettings): Boolean { + val uInt = module.findClassAcrossModuleDependencies(KotlinBuiltIns.FQ_NAMES.uInt) ?: return false + val accessibility = uInt.checkSinceKotlinVersionAccessibility(languageVersionSettings) + // Case `NotAccessibleButWasExperimental` will be checked later in `checkExperimentalityOfConstantLiteral` + return accessibility is SinceKotlinAccessibility.Accessible + } + private fun ConstantValue.wrap(parameters: CompileTimeConstant.Parameters): TypedCompileTimeConstant = TypedCompileTimeConstant(this, constantExpressionEvaluator.module, parameters) diff --git a/compiler/testData/diagnostics/testsWithUnsignedTypes/unsignedLiteralsOn1_2.kt b/compiler/testData/diagnostics/testsWithUnsignedTypes/unsignedLiteralsOn1_2.kt new file mode 100644 index 00000000000..d3996077681 --- /dev/null +++ b/compiler/testData/diagnostics/testsWithUnsignedTypes/unsignedLiteralsOn1_2.kt @@ -0,0 +1,6 @@ +// !API_VERSION: 1.2 + +val a = 0u +val b = 1uL + +fun foo(u: UInt) {} \ No newline at end of file diff --git a/compiler/testData/diagnostics/testsWithUnsignedTypes/unsignedLiteralsOn1_2.txt b/compiler/testData/diagnostics/testsWithUnsignedTypes/unsignedLiteralsOn1_2.txt new file mode 100644 index 00000000000..142c044bbd3 --- /dev/null +++ b/compiler/testData/diagnostics/testsWithUnsignedTypes/unsignedLiteralsOn1_2.txt @@ -0,0 +1,5 @@ +package + +public val a: [ERROR : Type cannot be resolved. Please make sure you have the required dependencies for unsigned types in the classpath] +public val b: [ERROR : Type cannot be resolved. Please make sure you have the required dependencies for unsigned types in the classpath] +public fun foo(/*0*/ u: [ERROR : UInt]): kotlin.Unit diff --git a/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsWithUnsignedTypesGenerated.java b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsWithUnsignedTypesGenerated.java index cd34b4f573a..2f33fd8457b 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsWithUnsignedTypesGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsWithUnsignedTypesGenerated.java @@ -64,6 +64,11 @@ public class DiagnosticsWithUnsignedTypesGenerated extends AbstractDiagnosticsWi runTest("compiler/testData/diagnostics/testsWithUnsignedTypes/unsignedLiteralsInsideConstVals.kt"); } + @TestMetadata("unsignedLiteralsOn1_2.kt") + public void testUnsignedLiteralsOn1_2() throws Exception { + runTest("compiler/testData/diagnostics/testsWithUnsignedTypes/unsignedLiteralsOn1_2.kt"); + } + @TestMetadata("unsignedLiteralsOverflowSignedBorder.kt") public void testUnsignedLiteralsOverflowSignedBorder() throws Exception { runTest("compiler/testData/diagnostics/testsWithUnsignedTypes/unsignedLiteralsOverflowSignedBorder.kt"); diff --git a/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/CompileTimeConstant.kt b/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/CompileTimeConstant.kt index 7b8271acc91..2cd9ecc7b58 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/CompileTimeConstant.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/CompileTimeConstant.kt @@ -93,11 +93,7 @@ fun createIntegerValueTypeConstant( module: ModuleDescriptor, parameters: CompileTimeConstant.Parameters ): CompileTimeConstant<*> { - return if (parameters.isUnsignedNumberLiteral && !hasUnsignedTypesInModuleDependencies(module)) { - UnsignedErrorValueTypeConstant(value, parameters) - } else { - IntegerValueTypeConstant(value, module, parameters) - } + return IntegerValueTypeConstant(value, module, parameters) } fun hasUnsignedTypesInModuleDependencies(module: ModuleDescriptor): Boolean {