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)
}