Make FIR/FE1.0 report similar OptIn messages

This commit is contained in:
Mikhail Glukhikh
2022-02-21 18:26:13 +03:00
committed by Space
parent b42c236fb3
commit d823020c07
7 changed files with 23 additions and 14 deletions
@@ -206,8 +206,10 @@ object FirOptInUsageBaseChecker {
Experimentality.Severity.WARNING -> FirErrors.OPT_IN_USAGE to "should" Experimentality.Severity.WARNING -> FirErrors.OPT_IN_USAGE to "should"
Experimentality.Severity.ERROR -> FirErrors.OPT_IN_USAGE_ERROR to "must" Experimentality.Severity.ERROR -> FirErrors.OPT_IN_USAGE_ERROR to "must"
} }
val reportedMessage = message?.takeIf { it.isNotBlank() } ?: "This declaration is experimental and its usage $verb be marked" val fqName = annotationClassId.asSingleFqName()
reporter.reportOn(element.source, diagnostic, annotationClassId.asSingleFqName(), reportedMessage, context) val reportedMessage = message?.takeIf { it.isNotBlank() }
?: OptInNames.buildDefaultDiagnosticMessage(OptInNames.buildMessagePrefix(verb), fqName)
reporter.reportOn(element.source, diagnostic, fqName, reportedMessage, context)
} }
} }
} }
@@ -118,18 +118,18 @@ class ExperimentalUsageChecker(project: Project) : CallChecker {
private val WARNING_LEVEL = Name.identifier("WARNING") private val WARNING_LEVEL = Name.identifier("WARNING")
private val ERROR_LEVEL = Name.identifier("ERROR") private val ERROR_LEVEL = Name.identifier("ERROR")
internal fun getDefaultDiagnosticMessage(prefix: String) = fun(fqName: FqName): String { internal fun getDefaultDiagnosticMessage(prefix: String): (FqName) -> String = { fqName: FqName ->
return "$prefix with '@${fqName.asString()}' or '@OptIn(${fqName.asString()}::class)'" OptInNames.buildDefaultDiagnosticMessage(prefix, fqName)
} }
private val USAGE_DIAGNOSTICS = ExperimentalityDiagnostics( private val USAGE_DIAGNOSTICS = ExperimentalityDiagnostics(
warning = ExperimentalityDiagnostic2( warning = ExperimentalityDiagnostic2(
Errors.OPT_IN_USAGE, Errors.OPT_IN_USAGE,
getDefaultDiagnosticMessage("This declaration is experimental and its usage should be marked") getDefaultDiagnosticMessage(OptInNames.buildMessagePrefix("should"))
), ),
error = ExperimentalityDiagnostic2( error = ExperimentalityDiagnostic2(
Errors.OPT_IN_USAGE_ERROR, Errors.OPT_IN_USAGE_ERROR,
getDefaultDiagnosticMessage("This declaration is experimental and its usage must be marked") getDefaultDiagnosticMessage(OptInNames.buildMessagePrefix("must"))
), ),
futureError = ExperimentalityDiagnostic2( futureError = ExperimentalityDiagnostic2(
Errors.OPT_IN_USAGE_FUTURE_ERROR, Errors.OPT_IN_USAGE_FUTURE_ERROR,
+1 -1
View File
@@ -1,4 +1,4 @@
compiler/testData/cli/jvm/optInEmptyMessage.kt:8:5: error: this declaration is experimental and its usage must be marked with '@EmptyMarker' or '@OptIn(EmptyMarker::class)' compiler/testData/cli/jvm/optInEmptyMessage.kt:8:5: error: this declaration needs OptIn. Its usage must be marked with '@EmptyMarker' or '@OptIn(EmptyMarker::class)'
foo() foo()
^ ^
COMPILATION_ERROR COMPILATION_ERROR
+1 -1
View File
@@ -1,4 +1,4 @@
$TESTDATA_DIR$/optInEmptyMessage.kt $TESTDATA_DIR$/optInEmptyMessageFir.kt
-d -d
$TEMP_DIR$ $TEMP_DIR$
-opt-in=kotlin.RequiresOptIn -opt-in=kotlin.RequiresOptIn
+1 -1
View File
@@ -1,7 +1,7 @@
warning: ATTENTION! warning: ATTENTION!
This build uses in-dev FIR: This build uses in-dev FIR:
-Xuse-fir -Xuse-fir
compiler/testData/cli/jvm/optInEmptyMessage.kt:8:5: error: this declaration is experimental and its usage must be marked compiler/testData/cli/jvm/optInEmptyMessage.kt:8:5: error: this declaration needs OptIn. Its usage must be marked with '@EmptyMarker' or '@OptIn(EmptyMarker::class)'
foo() foo()
^ ^
COMPILATION_ERROR COMPILATION_ERROR
@@ -1,17 +1,17 @@
warning: language version 1.4 is deprecated and its support will be removed in a future version of Kotlin warning: language version 1.4 is deprecated and its support will be removed in a future version of Kotlin
compiler/testData/cli/jvm/useDeclarationThatWasExperimentalWithoutMarker2.kt:2:13: warning: this declaration is experimental and its usage should be marked with '@kotlin.ExperimentalUnsignedTypes' or '@OptIn(kotlin.ExperimentalUnsignedTypes::class)' compiler/testData/cli/jvm/useDeclarationThatWasExperimentalWithoutMarker2.kt:2:13: warning: this declaration needs OptIn. Its usage should be marked with '@kotlin.ExperimentalUnsignedTypes' or '@OptIn(kotlin.ExperimentalUnsignedTypes::class)'
fun test(p: ULong) { fun test(p: ULong) {
^ ^
compiler/testData/cli/jvm/useDeclarationThatWasExperimentalWithoutMarker2.kt:3:12: warning: this declaration is experimental and its usage should be marked with '@kotlin.ExperimentalUnsignedTypes' or '@OptIn(kotlin.ExperimentalUnsignedTypes::class)' compiler/testData/cli/jvm/useDeclarationThatWasExperimentalWithoutMarker2.kt:3:12: warning: this declaration needs OptIn. Its usage should be marked with '@kotlin.ExperimentalUnsignedTypes' or '@OptIn(kotlin.ExperimentalUnsignedTypes::class)'
val z: ULong = p val z: ULong = p
^ ^
compiler/testData/cli/jvm/useDeclarationThatWasExperimentalWithoutMarker2.kt:3:20: warning: this declaration is experimental and its usage should be marked with '@kotlin.ExperimentalUnsignedTypes' or '@OptIn(kotlin.ExperimentalUnsignedTypes::class)' compiler/testData/cli/jvm/useDeclarationThatWasExperimentalWithoutMarker2.kt:3:20: warning: this declaration needs OptIn. Its usage should be marked with '@kotlin.ExperimentalUnsignedTypes' or '@OptIn(kotlin.ExperimentalUnsignedTypes::class)'
val z: ULong = p val z: ULong = p
^ ^
compiler/testData/cli/jvm/useDeclarationThatWasExperimentalWithoutMarker2.kt:4:5: warning: this declaration is experimental and its usage should be marked with '@kotlin.ExperimentalUnsignedTypes' or '@OptIn(kotlin.ExperimentalUnsignedTypes::class)' compiler/testData/cli/jvm/useDeclarationThatWasExperimentalWithoutMarker2.kt:4:5: warning: this declaration needs OptIn. Its usage should be marked with '@kotlin.ExperimentalUnsignedTypes' or '@OptIn(kotlin.ExperimentalUnsignedTypes::class)'
z.inv() z.inv()
^ ^
compiler/testData/cli/jvm/useDeclarationThatWasExperimentalWithoutMarker2.kt:4:7: warning: this declaration is experimental and its usage should be marked with '@kotlin.ExperimentalUnsignedTypes' or '@OptIn(kotlin.ExperimentalUnsignedTypes::class)' compiler/testData/cli/jvm/useDeclarationThatWasExperimentalWithoutMarker2.kt:4:7: warning: this declaration needs OptIn. Its usage should be marked with '@kotlin.ExperimentalUnsignedTypes' or '@OptIn(kotlin.ExperimentalUnsignedTypes::class)'
z.inv() z.inv()
^ ^
OK OK
@@ -38,4 +38,11 @@ object OptInNames {
) )
@Suppress("unused") @Suppress("unused")
val USE_EXPERIMENTAL_FQ_NAMES = OPT_IN_FQ_NAMES val USE_EXPERIMENTAL_FQ_NAMES = OPT_IN_FQ_NAMES
fun buildDefaultDiagnosticMessage(prefix: String, fqName: FqName): String {
return "$prefix with '@${fqName.asString()}' or '@OptIn(${fqName.asString()}::class)'"
}
fun buildMessagePrefix(verb: String): String =
"This declaration needs OptIn. Its usage $verb be marked"
} }