Prohibit renaming operator to another operator on import.

This commit is contained in:
Dmitry Petrov
2017-01-19 13:51:15 +03:00
parent 9625c32527
commit 03a697a707
6 changed files with 57 additions and 4 deletions
@@ -179,6 +179,8 @@ public interface Errors {
DiagnosticFactory1<KtImportDirective, String> CONFLICTING_IMPORT = DiagnosticFactory1.create(ERROR, PositioningStrategies.IMPORT_ALIAS);
DiagnosticFactory0<KtSimpleNameExpression> OPERATOR_RENAMED_ON_IMPORT = DiagnosticFactory0.create(ERROR);
// Modifiers
DiagnosticFactory2<PsiElement, KtModifierKeywordToken, KtModifierKeywordToken> INCOMPATIBLE_MODIFIERS = DiagnosticFactory2.create(ERROR);
@@ -190,6 +190,7 @@ public class DefaultErrorMessages {
MAP.put(CANNOT_BE_IMPORTED, "Cannot import ''{0}'', functions and properties can be imported only from packages or objects", TO_STRING);
MAP.put(PACKAGE_CANNOT_BE_IMPORTED, "Packages cannot be imported");
MAP.put(CONFLICTING_IMPORT, "Conflicting import, imported name ''{0}'' is ambiguous", STRING);
MAP.put(OPERATOR_RENAMED_ON_IMPORT, "Operator renamed to a different operator on import");
MAP.put(PLATFORM_CLASS_MAPPED_TO_KOTLIN, "This class shouldn''t be used in Kotlin. Use {0} instead.", CLASSES_OR_SEPARATED);
MAP.put(CANNOT_INFER_PARAMETER_TYPE, "Cannot infer a type for this parameter. Please specify it explicitly.");
@@ -29,13 +29,12 @@ import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.platform.PlatformToKotlinClassMap
import org.jetbrains.kotlin.psi.KtImportDirective
import org.jetbrains.kotlin.psi.KtPsiUtil
import org.jetbrains.kotlin.resolve.BindingTrace
import org.jetbrains.kotlin.resolve.PlatformClassesMappedToKotlinChecker
import org.jetbrains.kotlin.resolve.QualifiedExpressionResolver
import org.jetbrains.kotlin.resolve.isHiddenInResolution
import org.jetbrains.kotlin.psi.KtReferenceExpression
import org.jetbrains.kotlin.resolve.*
import org.jetbrains.kotlin.resolve.scopes.DescriptorKindFilter
import org.jetbrains.kotlin.resolve.scopes.ImportingScope
import org.jetbrains.kotlin.storage.StorageManager
import org.jetbrains.kotlin.types.expressions.OperatorConventions
import org.jetbrains.kotlin.util.collectionUtils.concat
import org.jetbrains.kotlin.utils.Printer
import java.util.*
@@ -110,6 +109,8 @@ class LazyImportResolver(
explicitClassImports.put(alias, importDirective)
}
}
checkResolvedImportDirective(importDirective)
}
for ((alias, import) in explicitClassImports.entries()) {
if (alias.all { it == '_' }) {
@@ -126,6 +127,18 @@ class LazyImportResolver(
}
}
private fun checkResolvedImportDirective(importDirective: KtImportDirective) {
val importedReference = KtPsiUtil.getLastReference(importDirective.importedReference ?: return) ?: return
val importedDescriptor = traceForImportResolve.bindingContext.get(BindingContext.REFERENCE_TARGET, importedReference) ?: return
val aliasName = importDirective.aliasName
if (importedDescriptor is FunctionDescriptor && importedDescriptor.isOperator &&
aliasName != null && OperatorConventions.isConventionName(Name.identifier(aliasName))) {
traceForImportResolve.report(Errors.OPERATOR_RENAMED_ON_IMPORT.on(importedReference))
}
}
override fun forceResolveImport(importDirective: KtImportDirective) {
getImportScope(importDirective)
}
@@ -0,0 +1,16 @@
// !DIAGNOSTICS: -UNUSED_PARAMETER
// FILE: a.kt
package a
interface A
operator fun A.plus(other: A): A = this
// FILE: b.kt
package b
import a.A
import a.<!OPERATOR_RENAMED_ON_IMPORT!>plus<!> as minus
fun test(a1: A, a2: A) =
a1 - a2
@@ -0,0 +1,15 @@
package
package a {
public operator fun a.A.plus(/*0*/ other: a.A): a.A
public interface A {
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
}
}
package b {
public fun test(/*0*/ a1: a.A, /*1*/ a2: a.A): a.A
}
@@ -9589,6 +9589,12 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTest {
doTest(fileName);
}
@TestMetadata("OperatorRenameOnImport.kt")
public void testOperatorRenameOnImport() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/imports/OperatorRenameOnImport.kt");
doTest(fileName);
}
@TestMetadata("PackageLocalClassNotImported.kt")
public void testPackageLocalClassNotImported() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/imports/PackageLocalClassNotImported.kt");