[kotlin] Error/warning message for @SubclassOptInRequired-annotated class should provide more context

^KT-70407 Fixed

GitOrigin-RevId: 1a45214c9c4ffc983e1e16affc6828733b45f8e3
This commit is contained in:
Anastasia Pikalova
2024-09-23 11:33:44 +02:00
committed by intellij-monorepo-bot
parent f2484b780b
commit b47b9c2ae1
16 changed files with 38 additions and 7 deletions
@@ -15,6 +15,7 @@ class K1KotlinCompilerIdeAllowedErrorFilter : KotlinCompilerIdeAllowedErrorFilte
Errors.DIVISION_BY_ZERO,
Errors.OPT_IN_USAGE_ERROR,
Errors.OPT_IN_OVERRIDE_ERROR,
Errors.OPT_IN_TO_INHERITANCE_ERROR,
Errors.UNSAFE_CALL,
Errors.UNSAFE_OPERATOR_CALL,
Errors.ITERATOR_ON_NULLABLE,
@@ -13,6 +13,7 @@ internal class K2KotlinCompilerIdeAllowedErrorFilter : KotlinCompilerIdeAllowedE
FirErrors.DEPRECATION_ERROR,
FirErrors.DIVISION_BY_ZERO,
FirErrors.OPT_IN_USAGE_ERROR,
FirErrors.OPT_IN_TO_INHERITANCE_ERROR,
FirErrors.OPT_IN_OVERRIDE_ERROR,
FirErrors.UNSAFE_CALL,
FirErrors.UNSAFE_IMPLICIT_INVOKE_CALL,
@@ -518,10 +518,14 @@ class KotlinK2QuickFixRegistrar : KotlinQuickFixRegistrar() {
registerFactory(OptInModuleLevelFixFactories.optInIsNotEnabledFactory)
registerFactory(OptInFileLevelFixFactories.optInUsageFactory)
registerFactory(OptInFileLevelFixFactories.optInUsageErrorFactory)
registerFactory(OptInFileLevelFixFactories.optInUsageInheritanceFactory)
registerFactory(OptInFileLevelFixFactories.optInUsageInheritanceErrorFactory)
registerFactory(OptInFileLevelFixFactories.optInOverrideFactory)
registerFactory(OptInFileLevelFixFactories.optInOverrideErrorFactory)
registerFactory(OptInFixFactories.optInUsageFactory)
registerFactory(OptInFixFactories.optInToInheritanceFactory)
registerFactory(OptInFixFactories.optInUsageErrorFactory)
registerFactory(OptInFixFactories.optInToInheritanceErrorFactory)
registerFactory(OptInFixFactories.optInOverrideFactory)
registerFactory(OptInFixFactories.optInOverrideErrorFactory)
}
@@ -28,6 +28,14 @@ internal object OptInFileLevelFixFactories {
createQuickFix(diagnostic)
}
val optInUsageInheritanceFactory = KotlinQuickFixFactory.IntentionBased { diagnostic: KaFirDiagnostic.OptInToInheritance ->
createQuickFix(diagnostic)
}
val optInUsageInheritanceErrorFactory = KotlinQuickFixFactory.IntentionBased { diagnostic: KaFirDiagnostic.OptInToInheritanceError ->
createQuickFix(diagnostic)
}
val optInOverrideFactory = KotlinQuickFixFactory.IntentionBased { diagnostic: KaFirDiagnostic.OptInOverride ->
createQuickFix(diagnostic)
}
@@ -36,6 +36,14 @@ internal object OptInFixFactories {
createQuickFix(diagnostic)
}
val optInToInheritanceFactory = KotlinQuickFixFactory.IntentionBased { diagnostic: KaFirDiagnostic.OptInToInheritance ->
createQuickFix(diagnostic)
}
val optInToInheritanceErrorFactory = KotlinQuickFixFactory.IntentionBased { diagnostic: KaFirDiagnostic.OptInToInheritanceError ->
createQuickFix(diagnostic)
}
val optInOverrideFactory = KotlinQuickFixFactory.IntentionBased { diagnostic: KaFirDiagnostic.OptInOverride ->
createQuickFix(diagnostic)
}
@@ -16,6 +16,8 @@ internal object OptInFixUtils {
fun optInMarkerClassId(diagnostic: KaFirDiagnostic<PsiElement>): ClassId? = when (diagnostic) {
is KaFirDiagnostic.OptInUsage -> diagnostic.optInMarkerClassId
is KaFirDiagnostic.OptInUsageError -> diagnostic.optInMarkerClassId
is KaFirDiagnostic.OptInToInheritance -> diagnostic.optInMarkerClassId
is KaFirDiagnostic.OptInToInheritanceError -> diagnostic.optInMarkerClassId
is KaFirDiagnostic.OptInOverride -> diagnostic.optInMarkerClassId
is KaFirDiagnostic.OptInOverrideError -> diagnostic.optInMarkerClassId
else -> null
@@ -22,6 +22,8 @@ internal object OptInFixesUtils {
fun annotationFqName(diagnostic: Diagnostic) = when (diagnostic.factory) {
OPT_IN_USAGE -> OPT_IN_USAGE.cast(diagnostic).a
OPT_IN_USAGE_ERROR -> OPT_IN_USAGE_ERROR.cast(diagnostic).a
OPT_IN_TO_INHERITANCE -> OPT_IN_TO_INHERITANCE.cast(diagnostic).a
OPT_IN_TO_INHERITANCE_ERROR -> OPT_IN_TO_INHERITANCE_ERROR.cast(diagnostic).a
OPT_IN_OVERRIDE -> OPT_IN_OVERRIDE.cast(diagnostic).a
OPT_IN_OVERRIDE_ERROR -> OPT_IN_OVERRIDE_ERROR.cast(diagnostic).a
else -> null
@@ -677,6 +677,10 @@ class QuickFixRegistrar : QuickFixContributor {
OPT_IN_USAGE.registerFactory(OptInFileLevelFixesFactory)
OPT_IN_USAGE_ERROR.registerFactory(OptInFixesFactory)
OPT_IN_USAGE_ERROR.registerFactory(OptInFileLevelFixesFactory)
OPT_IN_TO_INHERITANCE.registerFactory(OptInFixesFactory)
OPT_IN_TO_INHERITANCE.registerFactory(OptInFileLevelFixesFactory)
OPT_IN_TO_INHERITANCE_ERROR.registerFactory(OptInFixesFactory)
OPT_IN_TO_INHERITANCE_ERROR.registerFactory(OptInFileLevelFixesFactory)
OPT_IN_OVERRIDE.registerFactory(OptInFixesFactory)
OPT_IN_OVERRIDE.registerFactory(OptInFileLevelFixesFactory)
OPT_IN_OVERRIDE_ERROR.registerFactory(OptInFixesFactory)
@@ -1,6 +1,6 @@
// "Propagate 'SubclassOptInRequired(UnstableApi::class)' opt-in requirement to 'SomeImplementation'" "false"
// IGNORE_K2
// ERROR: This declaration needs opt-in. Its usage must be marked with '@UnstableApi' or '@OptIn(UnstableApi::class)'
// ERROR: This class or interface requires opt-in to be implemented. Its usage must be marked with '@UnstableApi', '@OptIn(UnstableApi::class)' or '@SubclassOptInRequired(UnstableApi::class)'
// ACTION: Introduce import alias
// ACTION: Opt in for 'UnstableApi' in containing file 'propagateOptIn8.kt'
// ACTION: Opt in for 'UnstableApi' in module 'light_idea_test_case'
@@ -1,6 +1,6 @@
// "Propagate 'SubclassOptInRequired(UnstableApi::class)' opt-in requirement to 'SomeImplementation'" "false"
// IGNORE_K1
// ERROR: This declaration needs opt-in. Its usage must be marked with '@UnstableApi' or '@OptIn(UnstableApi::class)'
// ERROR: This class or interface requires opt-in to be implemented. Its usage must be marked with '@UnstableApi', '@OptIn(UnstableApi::class)' or '@SubclassOptInRequired(UnstableApi::class)'
// ACTION: Opt in for 'UnstableApi' in containing file 'propagateOptIn8Fir.kt'
// ACTION: Opt in for 'UnstableApi' in module 'light_idea_test_case'
// ACTION: Opt in for 'UnstableApi' on 'foo'
@@ -1,5 +1,5 @@
// "Propagate 'SubclassOptInRequired(A::class)' opt-in requirement to 'SomeImplementation'" "true"
// ERROR: This declaration needs opt-in. Its usage must be marked with '@B' or '@OptIn(B::class)'
// ERROR: This class or interface requires opt-in to be implemented. Its usage must be marked with '@B', '@OptIn(B::class)' or '@SubclassOptInRequired(B::class)'
@file:OptIn(ExperimentalSubclassOptIn::class)
@RequiresOptIn
@@ -1,5 +1,5 @@
// "Propagate 'SubclassOptInRequired(A::class)' opt-in requirement to 'SomeImplementation'" "true"
// ERROR: This declaration needs opt-in. Its usage must be marked with '@B' or '@OptIn(B::class)'
// ERROR: This class or interface requires opt-in to be implemented. Its usage must be marked with '@B', '@OptIn(B::class)' or '@SubclassOptInRequired(B::class)'
@file:OptIn(ExperimentalSubclassOptIn::class)
@RequiresOptIn
@@ -1,5 +1,5 @@
// "Propagate 'SubclassOptInRequired(UnstableApi::class)' opt-in requirement to 'LocalClass'" "false"
// ERROR: This declaration needs opt-in. Its usage must be marked with '@PropagateOptIn8.UnstableApi' or '@OptIn(PropagateOptIn8.UnstableApi::class)'
// ERROR: This class or interface requires opt-in to be implemented. Its usage must be marked with '@PropagateOptIn8.UnstableApi', '@OptIn(PropagateOptIn8.UnstableApi::class)' or '@SubclassOptInRequired(PropagateOptIn8.UnstableApi::class)'
// ACTION: Add full qualifier
// ACTION: Introduce import alias
// ACTION: Opt in for 'UnstableApi' in containing file 'propagateOptIn8.kts'
@@ -7,7 +7,7 @@
// ACTION: Opt in for 'A' in module 'light_idea_test_case'
// ACTION: Opt in for 'A' on 'SomeImplementation'
// ACTION: Propagate 'SubclassOptInRequired(A::class)' opt-in requirement to 'SomeImplementation'
// ERROR: This declaration needs opt-in. Its usage must be marked with '@PropagateSubclassOptInRequired2.B' or '@OptIn(PropagateSubclassOptInRequired2.B::class)'
// ERROR: This class or interface requires opt-in to be implemented. Its usage must be marked with '@PropagateSubclassOptInRequired2.B', '@OptIn(PropagateSubclassOptInRequired2.B::class)' or '@SubclassOptInRequired(PropagateSubclassOptInRequired2.B::class)'
// RUNTIME_WITH_SCRIPT_RUNTIME
@file:OptIn(ExperimentalSubclassOptIn::class)
@@ -7,7 +7,7 @@
// ACTION: Opt in for 'A' in module 'light_idea_test_case'
// ACTION: Opt in for 'A' on 'SomeImplementation'
// ACTION: Propagate 'SubclassOptInRequired(A::class)' opt-in requirement to 'SomeImplementation'
// ERROR: This declaration needs opt-in. Its usage must be marked with '@PropagateSubclassOptInRequired2.B' or '@OptIn(PropagateSubclassOptInRequired2.B::class)'
// ERROR: This class or interface requires opt-in to be implemented. Its usage must be marked with '@PropagateSubclassOptInRequired2.B', '@OptIn(PropagateSubclassOptInRequired2.B::class)' or '@SubclassOptInRequired(PropagateSubclassOptInRequired2.B::class)'
// RUNTIME_WITH_SCRIPT_RUNTIME
@file:OptIn(ExperimentalSubclassOptIn::class)
@@ -117,6 +117,7 @@ class K1KotlinCodeFragmentCompiler : KotlinCodeFragmentCompiler {
private val IGNORED_DIAGNOSTICS: Set<DiagnosticFactory<*>> = Errors.INVISIBLE_REFERENCE_DIAGNOSTICS +
setOf(
Errors.OPT_IN_USAGE_ERROR,
Errors.OPT_IN_TO_INHERITANCE_ERROR,
Errors.MISSING_DEPENDENCY_SUPERCLASS,
Errors.IR_WITH_UNSTABLE_ABI_COMPILED_CLASS,
Errors.FIR_COMPILED_CLASS,