[FIR] Additional errors to distinguish resolution to classifier cases

When a call is resolved to a classifier, only a single error message was
being used for multiple cases. This lead to confusion as the default
message may not be applicable to a given error case. Added additional
errors and messages to distinguish between these error cases.

#KT-57251 Fixed
This commit is contained in:
Brian Norman
2023-06-02 09:34:49 -05:00
committed by Space Team
parent ac79633590
commit 0ff9982b31
44 changed files with 582 additions and 39 deletions
@@ -31,7 +31,7 @@ class Owner {
fun test() {
val o = Owner()
o.foo()
val err = Owner.<!RESOLUTION_TO_CLASSIFIER!>Inner<!>()
val err = Owner.<!INNER_CLASS_CONSTRUCTOR_NO_RECEIVER!>Inner<!>()
err.<!UNRESOLVED_REFERENCE!>baz<!>()
val i = o.Inner()
i.gau()
@@ -9,6 +9,6 @@ class Outer<T> {
}
fun test() {
foo.Outer<Int>.<!RESOLUTION_TO_CLASSIFIER!>Inner<!>(42)
foo.Outer<Int>.<!RESOLUTION_TO_CLASSIFIER!>Inner<!>(42)::<!UNRESOLVED_REFERENCE!>method<!>
foo.Outer<Int>.<!INNER_CLASS_CONSTRUCTOR_NO_RECEIVER!>Inner<!>(42)
foo.Outer<Int>.<!INNER_CLASS_CONSTRUCTOR_NO_RECEIVER!>Inner<!>(42)::<!UNRESOLVED_REFERENCE!>method<!>
}
@@ -159,6 +159,15 @@ object DIAGNOSTICS_LIST : DiagnosticList("FirErrors") {
parameter<String>("expression")
parameter<ConeKotlinType>("type")
}
val INTERFACE_AS_FUNCTION by error<PsiElement> {
parameter<FirRegularClassSymbol>("classSymbol")
}
val EXPECT_CLASS_AS_FUNCTION by error<PsiElement> {
parameter<FirRegularClassSymbol>("classSymbol")
}
val INNER_CLASS_CONSTRUCTOR_NO_RECEIVER by error<PsiElement> {
parameter<FirRegularClassSymbol>("classSymbol")
}
val RESOLUTION_TO_CLASSIFIER by error<PsiElement> {
parameter<FirRegularClassSymbol>("classSymbol")
}
@@ -168,6 +168,9 @@ object FirErrors {
val ILLEGAL_SELECTOR by error0<PsiElement>()
val NO_RECEIVER_ALLOWED by error0<PsiElement>()
val FUNCTION_EXPECTED by error2<PsiElement, String, ConeKotlinType>(SourceElementPositioningStrategies.REFERENCED_NAME_BY_QUALIFIED)
val INTERFACE_AS_FUNCTION by error1<PsiElement, FirRegularClassSymbol>()
val EXPECT_CLASS_AS_FUNCTION by error1<PsiElement, FirRegularClassSymbol>()
val INNER_CLASS_CONSTRUCTOR_NO_RECEIVER by error1<PsiElement, FirRegularClassSymbol>()
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)
@@ -228,6 +228,7 @@ import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors.EXPOSED_PROPERTY_
import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors.DEPRECATED_IDENTITY_EQUALS
import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors.EXPECTED_EXTERNAL_DECLARATION
import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors.EXPECTED_TAILREC_FUNCTION
import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors.EXPECT_CLASS_AS_FUNCTION
import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors.FORBIDDEN_BINARY_MOD
import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors.FORBIDDEN_IDENTITY_EQUALS
import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors.FORBIDDEN_IDENTITY_EQUALS_WARNING
@@ -295,9 +296,11 @@ import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors.INITIALIZER_TYPE_
import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors.INLINE_CLASS_CONSTRUCTOR_WRONG_PARAMETERS_SIZE
import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors.INLINE_PROPERTY_WITH_BACKING_FIELD
import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors.INLINE_SUSPEND_FUNCTION_TYPE_UNSUPPORTED
import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors.INNER_CLASS_CONSTRUCTOR_NO_RECEIVER
import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors.INNER_CLASS_INSIDE_VALUE_CLASS
import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors.INNER_CLASS_OF_GENERIC_THROWABLE_SUBCLASS
import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors.INSTANCE_ACCESS_BEFORE_SUPER_CALL
import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors.INTERFACE_AS_FUNCTION
import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors.INTERFACE_WITH_SUPERCLASS
import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors.INT_LITERAL_OUT_OF_RANGE
import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors.INVALID_CHARACTERS
@@ -737,11 +740,14 @@ object FirErrorsDefaultMessages : BaseDiagnosticRendererFactory() {
FUNCTION_EXPECTED,
"Expression ''{0}'' of type {1} cannot be invoked as a function. The function ''invoke()'' is not found", TO_STRING, RENDER_TYPE
)
map.put(INTERFACE_AS_FUNCTION, "Interface ''{0}'' does not have constructors.", SYMBOL)
map.put(EXPECT_CLASS_AS_FUNCTION, "Expected class ''{0}'' does not have default constructor.", SYMBOL)
map.put(
RESOLUTION_TO_CLASSIFIER,
"Constructor of the inner class {0} can only be called with a receiver of the containing class",
INNER_CLASS_CONSTRUCTOR_NO_RECEIVER,
"Constructor of the inner class ''{0}'' can only be called with a receiver of the containing class",
SYMBOL
)
map.put(RESOLUTION_TO_CLASSIFIER, "Resolution to the classifier ''{0}'' is not appropriate here.", SYMBOL)
map.put(
AMBIGUOUS_ALTERED_ASSIGN,
"Multiple extensions tried to alter this assignment at the same time. Extensions: {0}",
@@ -8,12 +8,15 @@ package org.jetbrains.kotlin.fir.analysis.diagnostics
import org.jetbrains.kotlin.KtFakeSourceElementKind
import org.jetbrains.kotlin.KtNodeTypes
import org.jetbrains.kotlin.KtSourceElement
import org.jetbrains.kotlin.descriptors.ClassKind
import org.jetbrains.kotlin.diagnostics.*
import org.jetbrains.kotlin.fir.FirSession
import org.jetbrains.kotlin.fir.analysis.checkers.declaration.isLocalMember
import org.jetbrains.kotlin.fir.analysis.getChild
import org.jetbrains.kotlin.fir.builder.FirSyntaxErrors
import org.jetbrains.kotlin.fir.declarations.utils.isExpect
import org.jetbrains.kotlin.fir.declarations.utils.isInfix
import org.jetbrains.kotlin.fir.declarations.utils.isInner
import org.jetbrains.kotlin.fir.declarations.utils.isOperator
import org.jetbrains.kotlin.fir.diagnostics.*
import org.jetbrains.kotlin.fir.resolve.calls.*
@@ -52,7 +55,15 @@ private fun ConeDiagnostic.toKtDiagnostic(
is ConeUnresolvedTypeQualifierError -> FirErrors.UNRESOLVED_REFERENCE.createOn(source, this.qualifier)
is ConeFunctionCallExpectedError -> FirErrors.FUNCTION_CALL_EXPECTED.createOn(source, this.name.asString(), this.hasValueParameters)
is ConeFunctionExpectedError -> FirErrors.FUNCTION_EXPECTED.createOn(source, this.expression, this.type)
is ConeResolutionToClassifierError -> FirErrors.RESOLUTION_TO_CLASSIFIER.createOn(source, this.candidateSymbol)
is ConeResolutionToClassifierError -> when (this.candidateSymbol.classKind) {
ClassKind.INTERFACE -> FirErrors.INTERFACE_AS_FUNCTION.createOn(source, this.candidateSymbol)
ClassKind.CLASS -> when {
this.candidateSymbol.isInner -> FirErrors.INNER_CLASS_CONSTRUCTOR_NO_RECEIVER.createOn(source, this.candidateSymbol)
this.candidateSymbol.isExpect -> FirErrors.EXPECT_CLASS_AS_FUNCTION.createOn(source, this.candidateSymbol)
else -> FirErrors.RESOLUTION_TO_CLASSIFIER.createOn(source, this.candidateSymbol)
}
else -> FirErrors.RESOLUTION_TO_CLASSIFIER.createOn(source, this.candidateSymbol)
}
is ConeHiddenCandidateError -> {
// Usages of callables with @Deprecated(DeprecationLevel.HIDDEN) should look like unresolved references.
// See: https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-deprecated/