diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/PlatformConfiguratorBase.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/PlatformConfiguratorBase.kt index 98605b29532..dc8207b54f3 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/PlatformConfiguratorBase.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/PlatformConfiguratorBase.kt @@ -73,7 +73,7 @@ private val DEFAULT_CALL_CHECKERS = listOf( NewSchemeOfIntegerOperatorResolutionChecker, EnumEntryVsCompanionPriorityCallChecker, CompanionInParenthesesLHSCallChecker, ResolutionToPrivateConstructorOfSealedClassChecker, EqualityCallChecker, UnsupportedUntilOperatorChecker, BuilderInferenceAssignmentChecker, IncorrectCapturedApproximationCallChecker, CompanionIncorrectlyUnboundedWhenUsedAsLHSCallChecker, - CustomEnumEntriesMigrationCallChecker, + CustomEnumEntriesMigrationCallChecker, EnumEntriesUnsupportedChecker ) private val DEFAULT_TYPE_CHECKERS = emptyList() private val DEFAULT_CLASSIFIER_USAGE_CHECKERS = listOf( diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/checkers/EnumEntriesUnsupportedChecker.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/checkers/EnumEntriesUnsupportedChecker.kt new file mode 100644 index 00000000000..b05fd19335d --- /dev/null +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/checkers/EnumEntriesUnsupportedChecker.kt @@ -0,0 +1,29 @@ +/* + * Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. + */ + +package org.jetbrains.kotlin.resolve.calls.checkers + +import com.intellij.psi.PsiElement +import org.jetbrains.kotlin.config.LanguageFeature +import org.jetbrains.kotlin.descriptors.PropertyDescriptor +import org.jetbrains.kotlin.diagnostics.Errors +import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall +import org.jetbrains.kotlin.resolve.calls.model.isSyntheticEnumEntries + +object EnumEntriesUnsupportedChecker : CallChecker { + override fun check(resolvedCall: ResolvedCall<*>, reportOn: PsiElement, context: CallCheckerContext) { + val languageVersionSettings = context.languageVersionSettings + if (languageVersionSettings.supportsFeature(LanguageFeature.EnumEntries)) return + val propertyDescriptor = resolvedCall.resultingDescriptor as? PropertyDescriptor ?: return + if (propertyDescriptor.isSyntheticEnumEntries()) { + context.trace.report( + Errors.UNSUPPORTED_FEATURE.on( + reportOn, + LanguageFeature.EnumEntries to languageVersionSettings + ) + ) + } + } +} \ No newline at end of file diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/PSICallResolver.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/PSICallResolver.kt index 3f027f63d00..09a416b238e 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/PSICallResolver.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/PSICallResolver.kt @@ -470,7 +470,7 @@ class PSICallResolver( dispatchReceiver: ReceiverValueWithSmartCastInfo?, extensionReceiver: ReceiverValueWithSmartCastInfo? ): Collection { - val result = candidateInterceptor.interceptVariableCandidates( + return candidateInterceptor.interceptVariableCandidates( initialResults, this, context, @@ -481,14 +481,6 @@ class PSICallResolver( dispatchReceiver, extensionReceiver ) - if (name != StandardNames.ENUM_ENTRIES || languageVersionSettings.supportsFeature(LanguageFeature.EnumEntries)) { - return result - } - return result.filterNot { - it is PropertyDescriptor && it.isSynthesized && - it.dispatchReceiverParameter == null && it.extensionReceiverParameter == null && - (it.containingDeclaration as? ClassDescriptor)?.kind == ClassKind.ENUM_CLASS - } } } diff --git a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/model/CallableReferencesCandidateFactory.kt b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/model/CallableReferencesCandidateFactory.kt index 92a83736f13..3f46df17b0e 100644 --- a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/model/CallableReferencesCandidateFactory.kt +++ b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/model/CallableReferencesCandidateFactory.kt @@ -15,6 +15,7 @@ import org.jetbrains.kotlin.descriptors.annotations.Annotations import org.jetbrains.kotlin.resolve.calls.components.* import org.jetbrains.kotlin.resolve.calls.components.candidate.CallableReferenceResolutionCandidate import org.jetbrains.kotlin.resolve.calls.inference.model.ConstraintStorage +import org.jetbrains.kotlin.resolve.calls.inference.model.LowerPriorityToPreserveCompatibility import org.jetbrains.kotlin.resolve.calls.inference.model.TypeVariableTypeConstructor import org.jetbrains.kotlin.resolve.calls.tasks.ExplicitReceiverKind import org.jetbrains.kotlin.resolve.calls.tower.* @@ -117,6 +118,10 @@ class CallableReferencesCandidateFactory( return createCallableReferenceCallCandidate(listOf(NotCallableMemberReference(kotlinCall, candidateDescriptor))) } + if (candidateDescriptor is PropertyDescriptor && candidateDescriptor.isSyntheticEnumEntries()) { + diagnostics.add(LowerPriorityToPreserveCompatibility(needToReportWarning = false).asDiagnostic()) + } + diagnostics.addAll(towerCandidate.diagnostics) // todo smartcast on receiver diagnostic and CheckInstantiationOfAbstractClass diff --git a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/model/ResolvedCallAtoms.kt b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/model/ResolvedCallAtoms.kt index 404736c101e..abb983799dc 100644 --- a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/model/ResolvedCallAtoms.kt +++ b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/model/ResolvedCallAtoms.kt @@ -27,6 +27,7 @@ import org.jetbrains.kotlin.resolve.calls.inference.components.NewTypeSubstituto import org.jetbrains.kotlin.resolve.calls.inference.model.ConstraintSystemError import org.jetbrains.kotlin.resolve.calls.inference.model.LowerPriorityToPreserveCompatibility import org.jetbrains.kotlin.resolve.calls.tasks.ExplicitReceiverKind +import org.jetbrains.kotlin.resolve.calls.tower.CandidateFactory import org.jetbrains.kotlin.resolve.constants.IntegerValueTypeConstant import org.jetbrains.kotlin.types.UnwrappedType import org.jetbrains.kotlin.utils.SmartList diff --git a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/model/SimpleCandidateFactory.kt b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/model/SimpleCandidateFactory.kt index aa18c5858cf..477df5af4db 100644 --- a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/model/SimpleCandidateFactory.kt +++ b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/model/SimpleCandidateFactory.kt @@ -6,6 +6,9 @@ package org.jetbrains.kotlin.resolve.calls.model import org.jetbrains.kotlin.descriptors.CallableDescriptor +import org.jetbrains.kotlin.descriptors.ClassDescriptor +import org.jetbrains.kotlin.descriptors.ClassKind +import org.jetbrains.kotlin.descriptors.PropertyDescriptor import org.jetbrains.kotlin.resolve.calls.components.InferenceSession import org.jetbrains.kotlin.resolve.calls.components.KotlinResolutionCallbacks import org.jetbrains.kotlin.resolve.calls.components.NewConstraintSystemImpl @@ -13,13 +16,14 @@ import org.jetbrains.kotlin.resolve.calls.components.candidate.SimpleErrorResolu import org.jetbrains.kotlin.resolve.calls.components.candidate.SimpleResolutionCandidate import org.jetbrains.kotlin.resolve.calls.inference.addSubsystemFromArgument import org.jetbrains.kotlin.resolve.calls.inference.model.ConstraintStorage +import org.jetbrains.kotlin.resolve.calls.inference.model.LowerPriorityToPreserveCompatibility import org.jetbrains.kotlin.resolve.calls.tasks.ExplicitReceiverKind import org.jetbrains.kotlin.resolve.calls.tower.* import org.jetbrains.kotlin.resolve.descriptorUtil.hasDynamicExtensionAnnotation import org.jetbrains.kotlin.resolve.scopes.receivers.ReceiverValueWithSmartCastInfo -import org.jetbrains.kotlin.types.error.ErrorUtils import org.jetbrains.kotlin.types.TypeSubstitutor import org.jetbrains.kotlin.types.error.ErrorScopeKind +import org.jetbrains.kotlin.types.error.ErrorUtils import org.jetbrains.kotlin.types.isDynamic class SimpleCandidateFactory( @@ -97,10 +101,15 @@ class SimpleCandidateFactory( ) val extensionArgumentReceiver = createReceiverArgument(kotlinCall.getExplicitExtensionReceiver(explicitReceiverKind), extensionReceiver) + val descriptor = towerCandidate.descriptor + var diagnostics: List = towerCandidate.diagnostics + if (descriptor is PropertyDescriptor && descriptor.isSyntheticEnumEntries()) { + diagnostics = diagnostics + LowerPriorityToPreserveCompatibility(needToReportWarning = false).asDiagnostic() + } return createCandidate( - towerCandidate.descriptor, explicitReceiverKind, dispatchArgumentReceiver, - extensionArgumentReceiver, null, towerCandidate.diagnostics, knownSubstitutor = null + descriptor, explicitReceiverKind, dispatchArgumentReceiver, + extensionArgumentReceiver, extensionArgumentReceiverCandidates = null, diagnostics, knownSubstitutor = null ) } @@ -180,4 +189,9 @@ class SimpleCandidateFactory( extensionArgumentReceiverCandidates = null, initialDiagnostics = listOf(), knownSubstitutor = null ) } +} + +fun PropertyDescriptor.isSyntheticEnumEntries(): Boolean { + return isSynthesized && dispatchReceiverParameter == null && extensionReceiverParameter == null && + (containingDeclaration as? ClassDescriptor)?.kind == ClassKind.ENUM_CLASS } \ No newline at end of file diff --git a/compiler/testData/cli/jvm/enumEntriesForJavaNotEnabled.args b/compiler/testData/cli/jvm/enumEntriesForJavaNotEnabled.args index 1ea8467ccf1..f71be949ef9 100644 --- a/compiler/testData/cli/jvm/enumEntriesForJavaNotEnabled.args +++ b/compiler/testData/cli/jvm/enumEntriesForJavaNotEnabled.args @@ -1,3 +1,4 @@ $TESTDATA_DIR$/enumEntriesForJavaNotEnabled.kt +-XXLanguage\:-EnumEntries -d $TEMP_DIR$ diff --git a/compiler/testData/cli/jvm/enumEntriesForJavaNotEnabled.out b/compiler/testData/cli/jvm/enumEntriesForJavaNotEnabled.out index 8c954db9195..57f855e95a1 100644 --- a/compiler/testData/cli/jvm/enumEntriesForJavaNotEnabled.out +++ b/compiler/testData/cli/jvm/enumEntriesForJavaNotEnabled.out @@ -1,4 +1,13 @@ -compiler/testData/cli/jvm/enumEntriesForJavaNotEnabled.kt:4:49: error: unresolved reference: entries +warning: ATTENTION! +This build uses unsafe internal compiler arguments: + +-XXLanguage:-EnumEntries + +This mode is not recommended for production use, +as no stability/compatibility guarantees are given on +compiler or generated code. Use it at your own risk! + +compiler/testData/cli/jvm/enumEntriesForJavaNotEnabled.kt:4:49: error: the feature "enum entries" is only available since language version 1.9 val entries = java.util.concurrent.TimeUnit.entries ^ COMPILATION_ERROR diff --git a/compiler/testData/cli/jvm/enumEntriesNotEnabled.args b/compiler/testData/cli/jvm/enumEntriesNotEnabled.args index 8e809193105..5ce4f03ab43 100644 --- a/compiler/testData/cli/jvm/enumEntriesNotEnabled.args +++ b/compiler/testData/cli/jvm/enumEntriesNotEnabled.args @@ -1,3 +1,4 @@ $TESTDATA_DIR$/enumEntriesNotEnabled.kt +-XXLanguage\:-EnumEntries -d $TEMP_DIR$ diff --git a/compiler/testData/cli/jvm/enumEntriesNotEnabled.out b/compiler/testData/cli/jvm/enumEntriesNotEnabled.out index edac9a3e7ef..03bc1b2d0b8 100644 --- a/compiler/testData/cli/jvm/enumEntriesNotEnabled.out +++ b/compiler/testData/cli/jvm/enumEntriesNotEnabled.out @@ -1,4 +1,13 @@ -compiler/testData/cli/jvm/enumEntriesNotEnabled.kt:7:26: error: unresolved reference: entries +warning: ATTENTION! +This build uses unsafe internal compiler arguments: + +-XXLanguage:-EnumEntries + +This mode is not recommended for production use, +as no stability/compatibility guarantees are given on +compiler or generated code. Use it at your own risk! + +compiler/testData/cli/jvm/enumEntriesNotEnabled.kt:7:26: error: the feature "enum entries" is only available since language version 1.9 val entries = MyEnum.entries ^ COMPILATION_ERROR diff --git a/compiler/testData/diagnostics/tests/enum/entries/entriesUnsupported.kt b/compiler/testData/diagnostics/tests/enum/entries/entriesUnsupported.kt index 836c7e0f3a3..8263ea4c1f0 100644 --- a/compiler/testData/diagnostics/tests/enum/entries/entriesUnsupported.kt +++ b/compiler/testData/diagnostics/tests/enum/entries/entriesUnsupported.kt @@ -7,5 +7,5 @@ enum class Foo { } fun main() { - Foo.entries + Foo.entries }