Reject values of DeprecatedSince.. that are not parseable as a version

This commit is contained in:
Mikhail Zarechenskiy
2020-06-18 11:21:55 +03:00
parent 5d88058928
commit 67100d5ebe
14 changed files with 131 additions and 45 deletions
@@ -6657,6 +6657,11 @@ public class FirOldFrontendDiagnosticsTestGenerated extends AbstractFirOldFronte
KotlinTestUtils.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/diagnostics/tests/deprecated/deprecatedSinceKotlin"), Pattern.compile("^(.+)\\.kt$"), Pattern.compile("^(.+)\\.fir\\.kts?$"), true);
}
@TestMetadata("checkValuesAreParseableAsVersion.kt")
public void testCheckValuesAreParseableAsVersion() throws Exception {
runTest("compiler/testData/diagnostics/tests/deprecated/deprecatedSinceKotlin/checkValuesAreParseableAsVersion.kt");
}
@TestMetadata("deprecatedSinceKotlinDeclaration.kt")
public void testDeprecatedSinceKotlinDeclaration() throws Exception {
runTest("compiler/testData/diagnostics/tests/deprecated/deprecatedSinceKotlin/deprecatedSinceKotlinDeclaration.kt");
@@ -5,15 +5,21 @@
package org.jetbrains.kotlin.resolve.checkers
import com.intellij.psi.PsiElement
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
import org.jetbrains.kotlin.config.ApiVersion
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
import org.jetbrains.kotlin.descriptors.annotations.AnnotationDescriptor
import org.jetbrains.kotlin.diagnostics.Errors
import org.jetbrains.kotlin.diagnostics.reportDiagnosticOnce
import org.jetbrains.kotlin.psi.KtAnnotationEntry
import org.jetbrains.kotlin.psi.KtDeclaration
import org.jetbrains.kotlin.resolve.RequireKotlinConstants
import org.jetbrains.kotlin.resolve.annotations.argumentValue
import org.jetbrains.kotlin.resolve.constants.StringValue
import org.jetbrains.kotlin.resolve.deprecation.getSinceVersion
import org.jetbrains.kotlin.resolve.source.getPsi
import org.jetbrains.kotlin.utils.addToStdlib.safeAs
object DeprecatedSinceKotlinAnnotationChecker : DeclarationChecker {
override fun check(declaration: KtDeclaration, descriptor: DeclarationDescriptor, context: DeclarationCheckerContext) {
@@ -42,9 +48,13 @@ object DeprecatedSinceKotlinAnnotationChecker : DeclarationChecker {
return
}
val warningSince = deprecatedSinceAnnotation.getSinceVersion("warningSince")
val errorSince = deprecatedSinceAnnotation.getSinceVersion("errorSince")
val hiddenSince = deprecatedSinceAnnotation.getSinceVersion("hiddenSince")
fun AnnotationDescriptor.getCheckedSinceVersion(name: String) =
getSinceVersion(name).also { checkVersion(it, name, context, deprecatedSinceAnnotationName) }
val warningSince = deprecatedSinceAnnotation.getCheckedSinceVersion(DeprecatedSinceKotlin::warningSince.name)
val errorSince = deprecatedSinceAnnotation.getCheckedSinceVersion(DeprecatedSinceKotlin::errorSince.name)
val hiddenSince = deprecatedSinceAnnotation.getCheckedSinceVersion(DeprecatedSinceKotlin::hiddenSince.name)
if (!lessOrNull(warningSince, errorSince) || !lessOrNull(errorSince, hiddenSince) || !lessOrNull(warningSince, hiddenSince)) {
context.trace.report(
Errors.DEPRECATED_SINCE_KOTLIN_WITH_UNORDERED_VERSIONS.on(
@@ -55,6 +65,22 @@ object DeprecatedSinceKotlinAnnotationChecker : DeclarationChecker {
}
}
private fun AnnotationDescriptor.checkVersion(
parsedVersion: ApiVersion?,
name: String,
context: DeclarationCheckerContext,
reportOn: PsiElement
) {
val argumentValue = argumentValue(name).safeAs<StringValue>()?.value
if (argumentValue != null && (parsedVersion == null || !argumentValue.matches(RequireKotlinConstants.VERSION_REGEX))) {
context.trace.reportDiagnosticOnce(
Errors.ILLEGAL_KOTLIN_VERSION_STRING_VALUE.on(
reportOn, fqName ?: return
)
)
}
}
private fun lessOrNull(a: ApiVersion?, b: ApiVersion?): Boolean =
if (a == null || b == null) true else a <= b
}
@@ -0,0 +1,19 @@
@Deprecated("")
@DeprecatedSinceKotlin("", errorSince = "1.0")
fun test1() {}
@Deprecated("")
@DeprecatedSinceKotlin("")
fun test2() {}
@Deprecated("")
@DeprecatedSinceKotlin("", "", "")
fun test3() {}
@Deprecated("")
@DeprecatedSinceKotlin("1.4-M2")
fun test4() {}
@Deprecated("")
@DeprecatedSinceKotlin("1.3.70")
fun test5() {}
@@ -0,0 +1,19 @@
@Deprecated("")
@<!ILLEGAL_KOTLIN_VERSION_STRING_VALUE!>DeprecatedSinceKotlin<!>("", errorSince = "1.0")
fun test1() {}
@Deprecated("")
@<!ILLEGAL_KOTLIN_VERSION_STRING_VALUE!>DeprecatedSinceKotlin<!>("")
fun test2() {}
@Deprecated("")
@<!ILLEGAL_KOTLIN_VERSION_STRING_VALUE!>DeprecatedSinceKotlin<!>("", "", "")
fun test3() {}
@Deprecated("")
@<!ILLEGAL_KOTLIN_VERSION_STRING_VALUE!>DeprecatedSinceKotlin<!>("1.4-M2")
fun test4() {}
@Deprecated("")
@DeprecatedSinceKotlin("1.3.70")
fun test5() {}
@@ -0,0 +1,7 @@
package
@kotlin.Deprecated(message = "") @kotlin.DeprecatedSinceKotlin(errorSince = "1.0", warningSince = "") public fun test1(): kotlin.Unit
@kotlin.Deprecated(message = "") @kotlin.DeprecatedSinceKotlin(warningSince = "") public fun test2(): kotlin.Unit
@kotlin.Deprecated(message = "") @kotlin.DeprecatedSinceKotlin(errorSince = "", hiddenSince = "", warningSince = "") public fun test3(): kotlin.Unit
@kotlin.Deprecated(message = "") @kotlin.DeprecatedSinceKotlin(warningSince = "1.4-M2") public fun test4(): kotlin.Unit
@kotlin.Deprecated(message = "") @kotlin.DeprecatedSinceKotlin(warningSince = "1.3.70") public fun test5(): kotlin.Unit
@@ -1,27 +1,27 @@
// !API_VERSION: 1.4
@Deprecated("")
@DeprecatedSinceKotlin("", errorSince = "1.4")
@DeprecatedSinceKotlin(errorSince = "1.4")
class ClassCur
@Deprecated("")
@DeprecatedSinceKotlin("", errorSince = "1.4")
@DeprecatedSinceKotlin(errorSince = "1.4")
fun funCur() {}
@Deprecated("")
@DeprecatedSinceKotlin("", errorSince = "1.4")
@DeprecatedSinceKotlin(errorSince = "1.4")
val valCur = Unit
@Deprecated("")
@DeprecatedSinceKotlin("", errorSince = "1.5")
@DeprecatedSinceKotlin(errorSince = "1.5")
class ClassNext
@Deprecated("")
@DeprecatedSinceKotlin("", errorSince = "1.5")
@DeprecatedSinceKotlin(errorSince = "1.5")
fun funNext() {}
@Deprecated("")
@DeprecatedSinceKotlin("", errorSince = "1.5")
@DeprecatedSinceKotlin(errorSince = "1.5")
val valNext = Unit
fun usage() {
@@ -1,27 +1,27 @@
// !API_VERSION: 1.4
@Deprecated("")
@DeprecatedSinceKotlin("", errorSince = "1.4")
@DeprecatedSinceKotlin(errorSince = "1.4")
class ClassCur
@Deprecated("")
@DeprecatedSinceKotlin("", errorSince = "1.4")
@DeprecatedSinceKotlin(errorSince = "1.4")
fun funCur() {}
@Deprecated("")
@DeprecatedSinceKotlin("", errorSince = "1.4")
@DeprecatedSinceKotlin(errorSince = "1.4")
val valCur = Unit
@Deprecated("")
@DeprecatedSinceKotlin("", errorSince = "1.5")
@DeprecatedSinceKotlin(errorSince = "1.5")
class ClassNext
@Deprecated("")
@DeprecatedSinceKotlin("", errorSince = "1.5")
@DeprecatedSinceKotlin(errorSince = "1.5")
fun funNext() {}
@Deprecated("")
@DeprecatedSinceKotlin("", errorSince = "1.5")
@DeprecatedSinceKotlin(errorSince = "1.5")
val valNext = Unit
fun usage() {
@@ -1,19 +1,19 @@
package
@kotlin.Deprecated(message = "") @kotlin.DeprecatedSinceKotlin(errorSince = "1.3", warningSince = "") public val valCur: kotlin.Unit
@kotlin.Deprecated(message = "") @kotlin.DeprecatedSinceKotlin(errorSince = "1.4", warningSince = "") public val valNext: kotlin.Unit
@kotlin.Deprecated(message = "") @kotlin.DeprecatedSinceKotlin(errorSince = "1.3", warningSince = "") public fun funCur(): kotlin.Unit
@kotlin.Deprecated(message = "") @kotlin.DeprecatedSinceKotlin(errorSince = "1.4", warningSince = "") public fun funNext(): kotlin.Unit
@kotlin.Deprecated(message = "") @kotlin.DeprecatedSinceKotlin(errorSince = "1.4") public val valCur: kotlin.Unit
@kotlin.Deprecated(message = "") @kotlin.DeprecatedSinceKotlin(errorSince = "1.5") public val valNext: kotlin.Unit
@kotlin.Deprecated(message = "") @kotlin.DeprecatedSinceKotlin(errorSince = "1.4") public fun funCur(): kotlin.Unit
@kotlin.Deprecated(message = "") @kotlin.DeprecatedSinceKotlin(errorSince = "1.5") public fun funNext(): kotlin.Unit
public fun usage(): kotlin.Unit
@kotlin.Deprecated(message = "") @kotlin.DeprecatedSinceKotlin(errorSince = "1.3", warningSince = "") public final class ClassCur {
@kotlin.Deprecated(message = "") @kotlin.DeprecatedSinceKotlin(errorSince = "1.4") public final class ClassCur {
public constructor ClassCur()
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
@kotlin.Deprecated(message = "") @kotlin.DeprecatedSinceKotlin(errorSince = "1.4", warningSince = "") public final class ClassNext {
@kotlin.Deprecated(message = "") @kotlin.DeprecatedSinceKotlin(errorSince = "1.5") public final class ClassNext {
public constructor ClassNext()
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
@@ -1,27 +1,27 @@
// !API_VERSION: 1.4
@Deprecated("")
@DeprecatedSinceKotlin("", hiddenSince = "1.4")
@DeprecatedSinceKotlin(hiddenSince = "1.4")
class ClassCur
@Deprecated("")
@DeprecatedSinceKotlin("", hiddenSince = "1.4")
@DeprecatedSinceKotlin(hiddenSince = "1.4")
fun funCur() {}
@Deprecated("")
@DeprecatedSinceKotlin("", hiddenSince = "1.4")
@DeprecatedSinceKotlin(hiddenSince = "1.4")
val valCur = Unit
@Deprecated("")
@DeprecatedSinceKotlin("", hiddenSince = "1.5")
@DeprecatedSinceKotlin(hiddenSince = "1.5")
class ClassNext
@Deprecated("")
@DeprecatedSinceKotlin("", hiddenSince = "1.5")
@DeprecatedSinceKotlin(hiddenSince = "1.5")
fun funNext() {}
@Deprecated("")
@DeprecatedSinceKotlin("", hiddenSince = "1.5")
@DeprecatedSinceKotlin(hiddenSince = "1.5")
val valNext = Unit
fun usage() {
@@ -1,27 +1,27 @@
// !API_VERSION: 1.4
@Deprecated("")
@DeprecatedSinceKotlin("", hiddenSince = "1.4")
@DeprecatedSinceKotlin(hiddenSince = "1.4")
class ClassCur
@Deprecated("")
@DeprecatedSinceKotlin("", hiddenSince = "1.4")
@DeprecatedSinceKotlin(hiddenSince = "1.4")
fun funCur() {}
@Deprecated("")
@DeprecatedSinceKotlin("", hiddenSince = "1.4")
@DeprecatedSinceKotlin(hiddenSince = "1.4")
val valCur = Unit
@Deprecated("")
@DeprecatedSinceKotlin("", hiddenSince = "1.5")
@DeprecatedSinceKotlin(hiddenSince = "1.5")
class ClassNext
@Deprecated("")
@DeprecatedSinceKotlin("", hiddenSince = "1.5")
@DeprecatedSinceKotlin(hiddenSince = "1.5")
fun funNext() {}
@Deprecated("")
@DeprecatedSinceKotlin("", hiddenSince = "1.5")
@DeprecatedSinceKotlin(hiddenSince = "1.5")
val valNext = Unit
fun usage() {
@@ -1,19 +1,19 @@
package
@kotlin.Deprecated(message = "") @kotlin.DeprecatedSinceKotlin(hiddenSince = "1.3", warningSince = "") public val valCur: kotlin.Unit
@kotlin.Deprecated(message = "") @kotlin.DeprecatedSinceKotlin(hiddenSince = "1.4", warningSince = "") public val valNext: kotlin.Unit
@kotlin.Deprecated(message = "") @kotlin.DeprecatedSinceKotlin(hiddenSince = "1.3", warningSince = "") public fun funCur(): kotlin.Unit
@kotlin.Deprecated(message = "") @kotlin.DeprecatedSinceKotlin(hiddenSince = "1.4", warningSince = "") public fun funNext(): kotlin.Unit
@kotlin.Deprecated(message = "") @kotlin.DeprecatedSinceKotlin(hiddenSince = "1.4") public val valCur: kotlin.Unit
@kotlin.Deprecated(message = "") @kotlin.DeprecatedSinceKotlin(hiddenSince = "1.5") public val valNext: kotlin.Unit
@kotlin.Deprecated(message = "") @kotlin.DeprecatedSinceKotlin(hiddenSince = "1.4") public fun funCur(): kotlin.Unit
@kotlin.Deprecated(message = "") @kotlin.DeprecatedSinceKotlin(hiddenSince = "1.5") public fun funNext(): kotlin.Unit
public fun usage(): kotlin.Unit
@kotlin.Deprecated(message = "") @kotlin.DeprecatedSinceKotlin(hiddenSince = "1.3", warningSince = "") public final class ClassCur {
@kotlin.Deprecated(message = "") @kotlin.DeprecatedSinceKotlin(hiddenSince = "1.4") public final class ClassCur {
public constructor ClassCur()
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
@kotlin.Deprecated(message = "") @kotlin.DeprecatedSinceKotlin(hiddenSince = "1.4", warningSince = "") public final class ClassNext {
@kotlin.Deprecated(message = "") @kotlin.DeprecatedSinceKotlin(hiddenSince = "1.5") public final class ClassNext {
public constructor ClassNext()
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
@@ -1,19 +1,19 @@
package
@kotlin.Deprecated(message = "") @kotlin.DeprecatedSinceKotlin(warningSince = "1.3") public val valCur: kotlin.Unit
@kotlin.Deprecated(message = "") @kotlin.DeprecatedSinceKotlin(warningSince = "1.4") public val valNext: kotlin.Unit
@kotlin.Deprecated(message = "") @kotlin.DeprecatedSinceKotlin(warningSince = "1.3") public fun funCur(): kotlin.Unit
@kotlin.Deprecated(message = "") @kotlin.DeprecatedSinceKotlin(warningSince = "1.4") public fun funNext(): kotlin.Unit
@kotlin.Deprecated(message = "") @kotlin.DeprecatedSinceKotlin(warningSince = "1.4") public val valCur: kotlin.Unit
@kotlin.Deprecated(message = "") @kotlin.DeprecatedSinceKotlin(warningSince = "1.5") public val valNext: kotlin.Unit
@kotlin.Deprecated(message = "") @kotlin.DeprecatedSinceKotlin(warningSince = "1.4") public fun funCur(): kotlin.Unit
@kotlin.Deprecated(message = "") @kotlin.DeprecatedSinceKotlin(warningSince = "1.5") public fun funNext(): kotlin.Unit
public fun usage(): kotlin.Unit
@kotlin.Deprecated(message = "") @kotlin.DeprecatedSinceKotlin(warningSince = "1.3") public final class ClassCur {
@kotlin.Deprecated(message = "") @kotlin.DeprecatedSinceKotlin(warningSince = "1.4") public final class ClassCur {
public constructor ClassCur()
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
@kotlin.Deprecated(message = "") @kotlin.DeprecatedSinceKotlin(warningSince = "1.4") public final class ClassNext {
@kotlin.Deprecated(message = "") @kotlin.DeprecatedSinceKotlin(warningSince = "1.5") public final class ClassNext {
public constructor ClassNext()
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
@@ -6664,6 +6664,11 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTestWithFirVali
KotlinTestUtils.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/diagnostics/tests/deprecated/deprecatedSinceKotlin"), Pattern.compile("^(.*)\\.kts?$"), Pattern.compile("^(.+)\\.fir\\.kts?$"), true);
}
@TestMetadata("checkValuesAreParseableAsVersion.kt")
public void testCheckValuesAreParseableAsVersion() throws Exception {
runTest("compiler/testData/diagnostics/tests/deprecated/deprecatedSinceKotlin/checkValuesAreParseableAsVersion.kt");
}
@TestMetadata("deprecatedSinceKotlinDeclaration.kt")
public void testDeprecatedSinceKotlinDeclaration() throws Exception {
runTest("compiler/testData/diagnostics/tests/deprecated/deprecatedSinceKotlin/deprecatedSinceKotlinDeclaration.kt");
@@ -6659,6 +6659,11 @@ public class DiagnosticsUsingJavacTestGenerated extends AbstractDiagnosticsUsing
KotlinTestUtils.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/diagnostics/tests/deprecated/deprecatedSinceKotlin"), Pattern.compile("^(.+)\\.kt$"), Pattern.compile("^(.+)\\.fir\\.kts?$"), true);
}
@TestMetadata("checkValuesAreParseableAsVersion.kt")
public void testCheckValuesAreParseableAsVersion() throws Exception {
runTest("compiler/testData/diagnostics/tests/deprecated/deprecatedSinceKotlin/checkValuesAreParseableAsVersion.kt");
}
@TestMetadata("deprecatedSinceKotlinDeclaration.kt")
public void testDeprecatedSinceKotlinDeclaration() throws Exception {
runTest("compiler/testData/diagnostics/tests/deprecated/deprecatedSinceKotlin/deprecatedSinceKotlinDeclaration.kt");