[FIR] KT-55747: Report error for operator fun mod

^KT-55747 Fixed

Merge-request: KT-MR-8522
Merged-by: Nikolay Lunyak <Nikolay.Lunyak@jetbrains.com>
This commit is contained in:
Nikolay Lunyak
2023-01-30 08:48:33 +00:00
committed by Space Team
parent 37a7044e74
commit e2403c801f
40 changed files with 147 additions and 173 deletions
@@ -387,6 +387,22 @@ internal val KT_DIAGNOSTIC_CONVERTER = KtDiagnosticConverterBuilder.buildConvert
token,
)
}
add(FirErrors.FORBIDDEN_BINARY_MOD) { firDiagnostic ->
ForbiddenBinaryModImpl(
firSymbolBuilder.buildSymbol(firDiagnostic.a),
firDiagnostic.b,
firDiagnostic as KtPsiDiagnostic,
token,
)
}
add(FirErrors.DEPRECATED_BINARY_MOD) { firDiagnostic ->
DeprecatedBinaryModImpl(
firSymbolBuilder.buildSymbol(firDiagnostic.a),
firDiagnostic.b,
firDiagnostic as KtPsiDiagnostic,
token,
)
}
add(FirErrors.SUPER_IS_NOT_AN_EXPRESSION) { firDiagnostic ->
SuperIsNotAnExpressionImpl(
firDiagnostic as KtPsiDiagnostic,
@@ -307,6 +307,18 @@ sealed class KtFirDiagnostic<PSI : PsiElement> : KtDiagnosticWithPsi<PSI> {
abstract val altererNames: List<String?>
}
abstract class ForbiddenBinaryMod : KtFirDiagnostic<PsiElement>() {
override val diagnosticClass get() = ForbiddenBinaryMod::class
abstract val forbiddenFunction: KtSymbol
abstract val suggestedFunction: String
}
abstract class DeprecatedBinaryMod : KtFirDiagnostic<PsiElement>() {
override val diagnosticClass get() = DeprecatedBinaryMod::class
abstract val forbiddenFunction: KtSymbol
abstract val suggestedFunction: String
}
abstract class SuperIsNotAnExpression : KtFirDiagnostic<PsiElement>() {
override val diagnosticClass get() = SuperIsNotAnExpression::class
}
@@ -355,6 +355,20 @@ internal class AmbiguousAlteredAssignImpl(
override val token: KtLifetimeToken,
) : KtFirDiagnostic.AmbiguousAlteredAssign(), KtAbstractFirDiagnostic<PsiElement>
internal class ForbiddenBinaryModImpl(
override val forbiddenFunction: KtSymbol,
override val suggestedFunction: String,
override val firDiagnostic: KtPsiDiagnostic,
override val token: KtLifetimeToken,
) : KtFirDiagnostic.ForbiddenBinaryMod(), KtAbstractFirDiagnostic<PsiElement>
internal class DeprecatedBinaryModImpl(
override val forbiddenFunction: KtSymbol,
override val suggestedFunction: String,
override val firDiagnostic: KtPsiDiagnostic,
override val token: KtLifetimeToken,
) : KtFirDiagnostic.DeprecatedBinaryMod(), KtAbstractFirDiagnostic<PsiElement>
internal class SuperIsNotAnExpressionImpl(
override val firDiagnostic: KtPsiDiagnostic,
override val token: KtLifetimeToken,
@@ -35403,6 +35403,12 @@ public class DiagnosisCompilerTestFE10TestdataTestGenerated extends AbstractDiag
runTest("compiler/testData/diagnostics/testsWithStdLib/kt54410.kt");
}
@Test
@TestMetadata("kt55747.kt")
public void testKt55747() throws Exception {
runTest("compiler/testData/diagnostics/testsWithStdLib/kt55747.kt");
}
@Test
@TestMetadata("kt8050.kt")
public void testKt8050() throws Exception {
@@ -35499,6 +35499,12 @@ public class FirOldFrontendDiagnosticsTestGenerated extends AbstractFirDiagnosti
runTest("compiler/testData/diagnostics/testsWithStdLib/kt54410.kt");
}
@Test
@TestMetadata("kt55747.kt")
public void testKt55747() throws Exception {
runTest("compiler/testData/diagnostics/testsWithStdLib/kt55747.kt");
}
@Test
@TestMetadata("kt8050.kt")
public void testKt8050() throws Exception {
@@ -35403,6 +35403,12 @@ public class FirOldFrontendDiagnosticsWithLightTreeTestGenerated extends Abstrac
runTest("compiler/testData/diagnostics/testsWithStdLib/kt54410.kt");
}
@Test
@TestMetadata("kt55747.kt")
public void testKt55747() throws Exception {
runTest("compiler/testData/diagnostics/testsWithStdLib/kt55747.kt");
}
@Test
@TestMetadata("kt8050.kt")
public void testKt8050() throws Exception {
@@ -152,6 +152,14 @@ object DIAGNOSTICS_LIST : DiagnosticList("FirErrors") {
val AMBIGUOUS_ALTERED_ASSIGN by error<PsiElement> {
parameter<List<String?>>("altererNames")
}
val FORBIDDEN_BINARY_MOD by error<PsiElement>(PositioningStrategy.OPERATOR_MODIFIER) {
parameter<FirBasedSymbol<*>>("forbiddenFunction")
parameter<String>("suggestedFunction")
}
val DEPRECATED_BINARY_MOD by error<PsiElement>(PositioningStrategy.OPERATOR_MODIFIER) {
parameter<FirBasedSymbol<*>>("forbiddenFunction")
parameter<String>("suggestedFunction")
}
}
val SUPER by object : DiagnosticGroup("Super") {
@@ -164,6 +164,8 @@ object FirErrors {
val FUNCTION_EXPECTED by error2<PsiElement, String, ConeKotlinType>(SourceElementPositioningStrategies.REFERENCED_NAME_BY_QUALIFIED)
val RESOLUTION_TO_CLASSIFIER by error1<PsiElement, FirRegularClassSymbol>()
val AMBIGUOUS_ALTERED_ASSIGN by error1<PsiElement, List<String?>>()
val FORBIDDEN_BINARY_MOD by error2<PsiElement, FirBasedSymbol<*>, String>(SourceElementPositioningStrategies.OPERATOR_MODIFIER)
val DEPRECATED_BINARY_MOD by error2<PsiElement, FirBasedSymbol<*>, String>(SourceElementPositioningStrategies.OPERATOR_MODIFIER)
// Super
val SUPER_IS_NOT_AN_EXPRESSION by error0<PsiElement>(SourceElementPositioningStrategies.REFERENCED_NAME_BY_QUALIFIED)
@@ -19,7 +19,6 @@ import org.jetbrains.kotlin.fir.analysis.checkers.hasModifier
import org.jetbrains.kotlin.fir.analysis.checkers.isSupertypeOf
import org.jetbrains.kotlin.fir.analysis.checkers.overriddenFunctions
import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors
import org.jetbrains.kotlin.diagnostics.reportOn
import org.jetbrains.kotlin.fir.containingClassLookupTag
import org.jetbrains.kotlin.fir.declarations.FirSimpleFunction
import org.jetbrains.kotlin.fir.declarations.utils.isInline
@@ -32,6 +31,8 @@ import org.jetbrains.kotlin.fir.types.impl.ConeClassLikeTypeImpl
import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.name.StandardClassIds
import org.jetbrains.kotlin.name.isSubpackageOf
import org.jetbrains.kotlin.util.OperatorNameConventions
import org.jetbrains.kotlin.util.OperatorNameConventions.ASSIGNMENT_OPERATIONS
import org.jetbrains.kotlin.util.OperatorNameConventions.BINARY_OPERATION_NAMES
import org.jetbrains.kotlin.util.OperatorNameConventions.COMPARE_TO
@@ -76,8 +77,24 @@ object FirOperatorModifierChecker : FirSimpleFunctionChecker() {
return
}
}
checkReplaceableLegacyOperators(declaration, context, reporter)
}
private fun checkReplaceableLegacyOperators(declaration: FirSimpleFunction, context: CheckerContext, reporter: DiagnosticReporter) {
val replacement = OperatorNameConventions.MOD_OPERATORS_REPLACEMENT[declaration.name] ?: return
val diagnostic = if (
declaration.symbol.callableId.packageName.isSubpackageOf(StandardClassIds.BASE_KOTLIN_PACKAGE) ||
!context.languageVersionSettings.supportsFeature(LanguageFeature.ProhibitOperatorMod)
) {
FirErrors.DEPRECATED_BINARY_MOD
} else {
FirErrors.FORBIDDEN_BINARY_MOD
}
reporter.reportOn(declaration.source, diagnostic, declaration.symbol, replacement.asString(), context)
}
}
private interface Check : (CheckerContext, FirSimpleFunction) -> String? {
@@ -214,6 +214,8 @@ import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors.EXTENSION_PROPERT
import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors.FINAL_SUPERTYPE
import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors.FINAL_UPPER_BOUND
import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors.FLOAT_LITERAL_OUT_OF_RANGE
import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors.DEPRECATED_BINARY_MOD
import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors.FORBIDDEN_BINARY_MOD
import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors.FORBIDDEN_VARARG_PARAMETER_TYPE
import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors.FUNCTION_CALL_EXPECTED
import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors.FUNCTION_DECLARATION_WITH_NO_NAME
@@ -712,6 +714,8 @@ object FirErrorsDefaultMessages : BaseDiagnosticRendererFactory() {
"Multiple extensions tried to alter this assignement at the same time. Extensions: {0}",
COLLECTION(NULLABLE_STRING)
)
map.put(DEPRECATED_BINARY_MOD, "Convention for ''{0}'' is forbidden. Use ''{1}''", SYMBOL, STRING)
map.put(FORBIDDEN_BINARY_MOD, "Deprecated convention for ''{0}''. Use ''{1}''", SYMBOL, STRING)
map.put(ILLEGAL_SELECTOR, "The expression cannot be a selector (occur after a dot)")
map.put(NO_RECEIVER_ALLOWED, "No receiver can be passed to this function or property")
@@ -4,6 +4,7 @@ class A() {
var x = 5
}
@Suppress("DEPRECATED_BINARY_MOD")
operator fun A.modAssign(y: Int) { throw RuntimeException("mod has been called instead of rem") }
operator fun A.remAssign(y: Int) { x %= y + 1 }
@@ -3,6 +3,7 @@
class A() {
var x = 5
@Suppress("DEPRECATED_BINARY_MOD")
operator fun mod(y: Int) { throw RuntimeException("mod has been called instead of rem") }
operator fun rem(y: Int) { x -= y }
}
@@ -2,12 +2,12 @@
// !DIAGNOSTICS: -UNUSED_PARAMETER
class OldAndNew {
operator fun modAssign(x: Int) {}
<!DEPRECATED_BINARY_MOD!>operator<!> fun modAssign(x: Int) {}
operator fun remAssign(x: Int) {}
}
class OnlyOld {
operator fun modAssign(x: Int) {}
<!DEPRECATED_BINARY_MOD!>operator<!> fun modAssign(x: Int) {}
}
class OnlyNew {
@@ -16,11 +16,11 @@ class OnlyNew {
class Sample
operator fun Sample.modAssign(x: Int) {}
<!DEPRECATED_BINARY_MOD!>operator<!> fun Sample.modAssign(x: Int) {}
operator fun Sample.remAssign(x: Int) {}
class ModAndRemAssign {
operator fun mod(x: Int): ModAndRemAssign = ModAndRemAssign()
<!DEPRECATED_BINARY_MOD!>operator<!> fun mod(x: Int): ModAndRemAssign = ModAndRemAssign()
operator fun remAssign(x: Int) {}
}
@@ -2,12 +2,12 @@
// !DIAGNOSTICS: -UNUSED_PARAMETER
class OldAndNew {
operator fun mod(x: Int) {}
<!DEPRECATED_BINARY_MOD!>operator<!> fun mod(x: Int) {}
operator fun rem(x: Int) {}
}
class OnlyOld {
operator fun mod(x: Int) {}
<!DEPRECATED_BINARY_MOD!>operator<!> fun mod(x: Int) {}
}
class OnlyNew {
@@ -16,11 +16,11 @@ class OnlyNew {
class Sample
operator fun Sample.mod(x: Int) {}
<!DEPRECATED_BINARY_MOD!>operator<!> fun Sample.mod(x: Int) {}
operator fun Sample.rem(x: Int) {}
class IntAndUnit {
operator fun mod(x: Int) = 0
<!DEPRECATED_BINARY_MOD!>operator<!> fun mod(x: Int) = 0
operator fun rem(x: Int): Int = 0
}
@@ -2,23 +2,23 @@
// !DIAGNOSTICS: -UNUSED_PARAMETER
object ModAndRem {
operator fun mod(x: Int) {}
<!DEPRECATED_BINARY_MOD!>operator<!> fun mod(x: Int) {}
operator fun rem(x: Int) {}
}
object OldMod {
operator fun mod(x: Int) {}
<!DEPRECATED_BINARY_MOD!>operator<!> fun mod(x: Int) {}
}
object ModAndRemExtension
operator fun ModAndRemExtension.mod(x: Int) {}
<!DEPRECATED_BINARY_MOD!>operator<!> fun ModAndRemExtension.mod(x: Int) {}
operator fun ModAndRemExtension.rem(x: Int) {}
object ModExtension
operator fun ModExtension.mod(x: Int) {}
<!DEPRECATED_BINARY_MOD!>operator<!> fun ModExtension.mod(x: Int) {}
object ModMemberAndRemExtension {
operator fun mod(x: Int) {}
<!DEPRECATED_BINARY_MOD!>operator<!> fun mod(x: Int) {}
}
operator fun ModMemberAndRemExtension.rem(x: Int) {}
@@ -2,7 +2,7 @@
// !DIAGNOSTICS: -UNUSED_PARAMETER
object OldMod {
operator fun mod(x: Int) {}
<!DEPRECATED_BINARY_MOD!>operator<!> fun mod(x: Int) {}
}
object RemExtension
@@ -2,16 +2,16 @@
// !DIAGNOSTICS: -UNUSED_PARAMETER
object ModAndRem {
operator fun mod(x: Int) {}
<!FORBIDDEN_BINARY_MOD!>operator<!> fun mod(x: Int) {}
operator fun rem(x: Int) {}
operator fun modAssign(x: Int) {}
<!FORBIDDEN_BINARY_MOD!>operator<!> fun modAssign(x: Int) {}
operator fun remAssign(x: Int) {}
}
object JustMod {
operator fun mod(x: Int) {}
operator fun modAssign(x: Int) {}
<!FORBIDDEN_BINARY_MOD!>operator<!> fun mod(x: Int) {}
<!FORBIDDEN_BINARY_MOD!>operator<!> fun modAssign(x: Int) {}
}
fun foo() {
@@ -1,17 +0,0 @@
// !LANGUAGE: -ProhibitOperatorMod
// !DIAGNOSTICS: -UNUSED_PARAMETER, -EXTENSION_SHADOWED_BY_MEMBER
class ModAndRemAssign {
operator fun mod(x: Int) = ModAndRemAssign()
operator fun mod(x: String) = ModAndRemAssign()
operator fun modAssign(x: String) {}
operator fun remAssign(x: Int) {}
}
operator fun ModAndRemAssign.mod(x: String) = ModAndRemAssign()
operator fun ModAndRemAssign.modAssign(x: String) {}
fun test() {
val modAndRemAssign = ModAndRemAssign()
modAndRemAssign %= 1
}
@@ -1,3 +1,4 @@
// FIR_IDENTICAL
// !LANGUAGE: -ProhibitOperatorMod
// !DIAGNOSTICS: -UNUSED_PARAMETER, -EXTENSION_SHADOWED_BY_MEMBER
@@ -1,13 +0,0 @@
// !LANGUAGE: -ProhibitOperatorMod
// !DIAGNOSTICS: -UNUSED_PARAMETER
class Foo {
}
operator fun Foo.mod(x: Int): Foo = Foo()
operator fun Foo.rem(x: Int): Int = 0
fun foo() {
takeInt(Foo() % 1)
}
fun takeInt(x: Int) {}
@@ -1,3 +1,4 @@
// FIR_IDENTICAL
// !LANGUAGE: -ProhibitOperatorMod
// !DIAGNOSTICS: -UNUSED_PARAMETER
@@ -1,13 +0,0 @@
// !LANGUAGE: -ProhibitOperatorMod
// !DIAGNOSTICS: -UNUSED_PARAMETER
class Foo {
operator fun mod(x: Int): Foo = Foo()
operator fun rem(x: Int): Int = 0
}
fun foo() {
takeInt(Foo() % 1)
}
fun takeInt(x: Int) {}
@@ -1,3 +1,4 @@
// FIR_IDENTICAL
// !LANGUAGE: -ProhibitOperatorMod
// !DIAGNOSTICS: -UNUSED_PARAMETER
@@ -1,16 +0,0 @@
// !LANGUAGE: -ProhibitOperatorMod
// !DIAGNOSTICS: -UNUSED_PARAMETER
class A {
companion object {
operator fun A.rem(x: Int) = 0
}
fun test() {
operator fun A.mod(x: Int) = ""
takeInt(A() % 123)
}
}
fun takeInt(x: Int) {}
@@ -1,3 +1,4 @@
// FIR_IDENTICAL
// !LANGUAGE: -ProhibitOperatorMod
// !DIAGNOSTICS: -UNUSED_PARAMETER
@@ -1,30 +0,0 @@
// !LANGUAGE: -ProhibitOperatorMod
// !DIAGNOSTICS: -UNUSED_PARAMETER
object B
class A {
operator fun B.rem(x: Int) = 0
}
fun test1() {
operator fun B.mod(x: Int) = ""
with(A()) {
takeInt(B % 10)
}
}
class C {
operator fun B.mod(x: Int) = ""
}
fun test2() {
operator fun B.rem(x: Int) = 0
with(C()) {
takeInt(B % 10)
}
}
fun takeInt(x: Int) {}
@@ -1,3 +1,4 @@
// FIR_IDENTICAL
// !LANGUAGE: -ProhibitOperatorMod
// !DIAGNOSTICS: -UNUSED_PARAMETER
@@ -1,20 +0,0 @@
// !LANGUAGE: -ProhibitOperatorMod
// !DIAGNOSTICS: -UNUSED_PARAMETER
class A {
operator fun Int.mod(s: String) = 4
}
class B {
operator fun Int.rem(s: String) = ""
}
fun test() {
with(B()) {
with(A()) {
takeString(1 % "")
}
}
}
fun takeString(s: String) {}
@@ -1,3 +1,4 @@
// FIR_IDENTICAL
// !LANGUAGE: -ProhibitOperatorMod
// !DIAGNOSTICS: -UNUSED_PARAMETER
@@ -1,15 +0,0 @@
// !LANGUAGE: -ProhibitOperatorMod
// !DIAGNOSTICS: -UNUSED_PARAMETER
class Foo {
operator fun mod(x: Int): Foo = Foo()
}
operator fun Foo.rem(x: Int): Int = 0
fun foo() {
takeInt(Foo() % 1)
}
fun takeInt(x: Int) {}
@@ -1,3 +1,4 @@
// FIR_IDENTICAL
// !LANGUAGE: -ProhibitOperatorMod
// !DIAGNOSTICS: -UNUSED_PARAMETER
@@ -1,17 +0,0 @@
// !LANGUAGE: -ProhibitOperatorMod
// !DIAGNOSTICS: -UNUSED_PARAMETER, -EXTENSION_SHADOWED_BY_MEMBER
class ModAndRemAssign {
operator fun mod(x: Int) = ModAndRemAssign()
operator fun mod(x: String) = ModAndRemAssign()
operator fun modAssign(x: String) {}
operator fun rem(x: Int) = ModAndRemAssign()
}
operator fun ModAndRemAssign.mod(x: String) = ModAndRemAssign()
operator fun ModAndRemAssign.modAssign(x: String) {}
fun test() {
var modAndRemAssign = ModAndRemAssign()
modAndRemAssign %= 1
}
@@ -1,3 +1,4 @@
// FIR_IDENTICAL
// !LANGUAGE: -ProhibitOperatorMod
// !DIAGNOSTICS: -UNUSED_PARAMETER, -EXTENSION_SHADOWED_BY_MEMBER
@@ -1,12 +0,0 @@
// !LANGUAGE: -ProhibitOperatorMod
// !DIAGNOSTICS: -UNUSED_PARAMETER
object RemAndModAssign {
operator fun modAssign(x: String) {}
operator fun rem(x: Int) = RemAndModAssign
}
fun test2() {
var c = RemAndModAssign
c %= 123
}
@@ -1,3 +1,4 @@
// FIR_IDENTICAL
// !LANGUAGE: -ProhibitOperatorMod
// !DIAGNOSTICS: -UNUSED_PARAMETER
@@ -6,7 +6,7 @@ object A {
@Deprecated("Use mod instead", ReplaceWith("mod"), DeprecationLevel.HIDDEN)
operator fun rem(x: Int) = 0
operator fun mod(x: Int) = ""
<!DEPRECATED_BINARY_MOD!>operator<!> fun mod(x: Int) = ""
}
fun test() {
@@ -0,0 +1,6 @@
// FIR_IDENTICAL
object Rem {
<!FORBIDDEN_BINARY_MOD!>operator<!> fun mod(x: Int) {}
<!FORBIDDEN_BINARY_MOD!>operator<!> fun modAssign(x: Int) {}
}
@@ -0,0 +1,10 @@
package
public object Rem {
private constructor Rem()
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 final operator fun mod(/*0*/ x: kotlin.Int): kotlin.Unit
public final operator fun modAssign(/*0*/ x: kotlin.Int): kotlin.Unit
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
@@ -35499,6 +35499,12 @@ public class DiagnosticTestGenerated extends AbstractDiagnosticTest {
runTest("compiler/testData/diagnostics/testsWithStdLib/kt54410.kt");
}
@Test
@TestMetadata("kt55747.kt")
public void testKt55747() throws Exception {
runTest("compiler/testData/diagnostics/testsWithStdLib/kt55747.kt");
}
@Test
@TestMetadata("kt8050.kt")
public void testKt8050() throws Exception {
@@ -92,4 +92,7 @@ object OperatorNameConventions {
@JvmField
val DELEGATED_PROPERTY_OPERATORS = setOf(GET_VALUE, SET_VALUE, PROVIDE_DELEGATE)
@JvmField
val MOD_OPERATORS_REPLACEMENT = mapOf(MOD to REM, MOD_ASSIGN to REM_ASSIGN)
}