FIR checker: carry original candidate in ConeDiagnosticWithCandidates
This is needed by IDE when resolving calls because candidates carry more information than just the targeting symbol.
This commit is contained in:
committed by
Ilya Kirillov
parent
b2e1dfa6db
commit
58067e0a1e
@@ -11,6 +11,7 @@ import org.jetbrains.kotlin.analysis.api.fir.annotations.mapAnnotationParameters
|
||||
import org.jetbrains.kotlin.analysis.api.fir.evaluate.FirAnnotationValueConverter
|
||||
import org.jetbrains.kotlin.fir.diagnostics.ConeDiagnostic
|
||||
import org.jetbrains.kotlin.fir.expressions.FirFunctionCall
|
||||
import org.jetbrains.kotlin.fir.expressions.FirImplicitInvokeCall
|
||||
import org.jetbrains.kotlin.fir.expressions.FirQualifiedAccessExpression
|
||||
import org.jetbrains.kotlin.fir.references.FirErrorNamedReference
|
||||
import org.jetbrains.kotlin.fir.references.FirNamedReference
|
||||
@@ -22,7 +23,6 @@ import org.jetbrains.kotlin.fir.symbols.impl.FirNamedFunctionSymbol
|
||||
import org.jetbrains.kotlin.analysis.api.symbols.KtSymbol
|
||||
import org.jetbrains.kotlin.fir.FirSession
|
||||
import org.jetbrains.kotlin.fir.expressions.FirAnnotation
|
||||
import org.jetbrains.kotlin.fir.expressions.FirImplicitInvokeCall
|
||||
import org.jetbrains.kotlin.fir.psi
|
||||
import org.jetbrains.kotlin.psi.KtAnnotationEntry
|
||||
import org.jetbrains.kotlin.psi.KtCallElement
|
||||
@@ -71,6 +71,7 @@ internal fun ConeDiagnostic.getCandidateSymbols(): Collection<FirBasedSymbol<*>>
|
||||
emptyList()
|
||||
}
|
||||
is ConeDiagnosticWithCandidates -> candidateSymbols
|
||||
is ConeDiagnosticWithSymbol<*> -> listOf(symbol)
|
||||
else -> emptyList()
|
||||
}
|
||||
|
||||
|
||||
+1
-1
@@ -357,7 +357,7 @@ private class ElementsToShortenCollector(
|
||||
return when (this) {
|
||||
is ConeClassErrorType -> when (val diagnostic = this.diagnostic) {
|
||||
// Tolerate code that misses type parameters while shortening it.
|
||||
is ConeUnmatchedTypeArgumentsError -> diagnostic.candidateSymbol.classId
|
||||
is ConeUnmatchedTypeArgumentsError -> diagnostic.symbol.classId
|
||||
else -> null
|
||||
}
|
||||
is ConeClassLikeType -> lookupTag.classId
|
||||
|
||||
+1
-1
@@ -56,7 +56,7 @@ internal object FirReferenceResolveHelper {
|
||||
|
||||
val symbol = resolvedSymbol ?: run {
|
||||
val diagnostic = (this as? FirErrorTypeRef)?.diagnostic
|
||||
(diagnostic as? ConeUnmatchedTypeArgumentsError)?.candidateSymbol
|
||||
(diagnostic as? ConeUnmatchedTypeArgumentsError)?.symbol
|
||||
}
|
||||
|
||||
return symbol?.fir?.buildSymbol(symbolBuilder)
|
||||
|
||||
+2
-2
@@ -5,8 +5,8 @@
|
||||
|
||||
package org.jetbrains.kotlin.fir.analysis.checkers.expression
|
||||
|
||||
import org.jetbrains.kotlin.fir.analysis.checkers.context.CheckerContext
|
||||
import org.jetbrains.kotlin.diagnostics.DiagnosticReporter
|
||||
import org.jetbrains.kotlin.fir.analysis.checkers.context.CheckerContext
|
||||
import org.jetbrains.kotlin.fir.expressions.FirResolvedQualifier
|
||||
import org.jetbrains.kotlin.fir.resolve.diagnostics.ConeDeprecated
|
||||
import org.jetbrains.kotlin.fir.symbols.impl.FirRegularClassSymbol
|
||||
@@ -14,7 +14,7 @@ import org.jetbrains.kotlin.fir.symbols.impl.FirRegularClassSymbol
|
||||
object FirDeprecatedQualifierChecker : FirResolvedQualifierChecker() {
|
||||
override fun check(expression: FirResolvedQualifier, context: CheckerContext, reporter: DiagnosticReporter) {
|
||||
expression.nonFatalDiagnostics.filterIsInstance<ConeDeprecated>().forEach { diagnostic ->
|
||||
FirDeprecationChecker.reportDeprecation(diagnostic.source, diagnostic.candidateSymbol, diagnostic.deprecationInfo, reporter, context)
|
||||
FirDeprecationChecker.reportDeprecation(diagnostic.source, diagnostic.symbol, diagnostic.deprecationInfo, reporter, context)
|
||||
}
|
||||
if (expression.resolvedToCompanionObject) {
|
||||
val companionSymbol = (expression.symbol as? FirRegularClassSymbol)?.companionObjectSymbol ?: return
|
||||
|
||||
+5
-4
@@ -25,6 +25,7 @@ import org.jetbrains.kotlin.fir.resolve.inference.model.ConeLambdaArgumentConstr
|
||||
import org.jetbrains.kotlin.fir.symbols.impl.FirBackingFieldSymbol
|
||||
import org.jetbrains.kotlin.fir.symbols.impl.FirCallableSymbol
|
||||
import org.jetbrains.kotlin.fir.symbols.impl.FirNamedFunctionSymbol
|
||||
import org.jetbrains.kotlin.fir.symbols.impl.FirRegularClassSymbol
|
||||
import org.jetbrains.kotlin.fir.types.*
|
||||
import org.jetbrains.kotlin.lexer.KtTokens
|
||||
import org.jetbrains.kotlin.name.SpecialNames
|
||||
@@ -58,9 +59,9 @@ private fun ConeDiagnostic.toKtDiagnostic(
|
||||
(this.candidateSymbol.safeAs<FirCallableSymbol<*>>()?.name ?: SpecialNames.NO_NAME_PROVIDED).asString()
|
||||
)
|
||||
}
|
||||
is ConeVisibilityError -> FirErrors.INVISIBLE_REFERENCE.createOn(source, this.candidateSymbol)
|
||||
is ConeVisibilityError -> FirErrors.INVISIBLE_REFERENCE.createOn(source, this.symbol)
|
||||
is ConeInapplicableWrongReceiver -> FirErrors.UNRESOLVED_REFERENCE_WRONG_RECEIVER.createOn(source, this.candidateSymbols)
|
||||
is ConeNoCompanionObject -> FirErrors.NO_COMPANION_OBJECT.createOn(source, this.candidateSymbol)
|
||||
is ConeNoCompanionObject -> FirErrors.NO_COMPANION_OBJECT.createOn(source, this.candidateSymbol as FirRegularClassSymbol)
|
||||
is ConeAmbiguityError -> when {
|
||||
applicability.isSuccess -> FirErrors.OVERLOAD_RESOLUTION_AMBIGUITY.createOn(source, this.candidates.map { it.symbol })
|
||||
applicability == CandidateApplicability.UNSAFE_CALL -> {
|
||||
@@ -90,11 +91,11 @@ private fun ConeDiagnostic.toKtDiagnostic(
|
||||
is ConeUnexpectedTypeArgumentsError -> FirErrors.TYPE_ARGUMENTS_NOT_ALLOWED.createOn(this.source ?: source)
|
||||
is ConeIllegalAnnotationError -> FirErrors.NOT_AN_ANNOTATION_CLASS.createOn(source, this.name.asString())
|
||||
is ConeWrongNumberOfTypeArgumentsError ->
|
||||
FirErrors.WRONG_NUMBER_OF_TYPE_ARGUMENTS.createOn(this.source, this.desiredCount, this.candidateSymbol)
|
||||
FirErrors.WRONG_NUMBER_OF_TYPE_ARGUMENTS.createOn(this.source, this.desiredCount, this.symbol)
|
||||
is ConeOuterClassArgumentsRequired ->
|
||||
FirErrors.OUTER_CLASS_ARGUMENTS_REQUIRED.createOn(qualifiedAccessSource ?: source, this.symbol)
|
||||
is ConeNoTypeArgumentsOnRhsError ->
|
||||
FirErrors.NO_TYPE_ARGUMENTS_ON_RHS.createOn(qualifiedAccessSource ?: source, this.desiredCount, this.candidateSymbol)
|
||||
FirErrors.NO_TYPE_ARGUMENTS_ON_RHS.createOn(qualifiedAccessSource ?: source, this.desiredCount, this.symbol)
|
||||
is ConeSimpleDiagnostic -> when {
|
||||
source.kind is KtFakeSourceElementKind && source.kind != KtFakeSourceElementKind.ReferenceInAtomicQualifiedAccess -> null
|
||||
else -> this.getFactory(source).createOn(qualifiedAccessSource ?: source)
|
||||
|
||||
@@ -644,7 +644,7 @@ class FirCallResolver(
|
||||
val candidate = candidates.singleOrNull()
|
||||
|
||||
val diagnostic = if (expectedCallKind == CallKind.Function) {
|
||||
ConeFunctionCallExpectedError(name, candidates.any { isValueParametersNotEmpty(it) }, candidates.map { it.symbol })
|
||||
ConeFunctionCallExpectedError(name, candidates.any { isValueParametersNotEmpty(it) }, candidates)
|
||||
} else {
|
||||
val singleExpectedCandidate = expectedCandidates?.singleOrNull()
|
||||
|
||||
@@ -654,7 +654,7 @@ class FirCallResolver(
|
||||
}
|
||||
|
||||
if (fir is FirRegularClass) {
|
||||
ConeResolutionToClassifierError(fir.symbol)
|
||||
ConeResolutionToClassifierError(singleExpectedCandidate!!, fir.symbol)
|
||||
} else {
|
||||
val coneType = explicitReceiver?.typeRef?.coneType
|
||||
when {
|
||||
@@ -764,12 +764,12 @@ class FirCallResolver(
|
||||
private fun createConeDiagnosticForCandidateWithError(
|
||||
applicability: CandidateApplicability,
|
||||
candidate: Candidate
|
||||
): ConeDiagnosticWithCandidates {
|
||||
): ConeDiagnostic {
|
||||
return when (applicability) {
|
||||
CandidateApplicability.HIDDEN -> ConeHiddenCandidateError(candidate.symbol)
|
||||
CandidateApplicability.HIDDEN -> ConeHiddenCandidateError(candidate)
|
||||
CandidateApplicability.VISIBILITY_ERROR -> ConeVisibilityError(candidate.symbol)
|
||||
CandidateApplicability.INAPPLICABLE_WRONG_RECEIVER -> ConeInapplicableWrongReceiver(listOf(candidate.symbol))
|
||||
CandidateApplicability.NO_COMPANION_OBJECT -> ConeNoCompanionObject(candidate.symbol as FirRegularClassSymbol)
|
||||
CandidateApplicability.INAPPLICABLE_WRONG_RECEIVER -> ConeInapplicableWrongReceiver(listOf(candidate))
|
||||
CandidateApplicability.NO_COMPANION_OBJECT -> ConeNoCompanionObject(candidate)
|
||||
else -> ConeInapplicableCandidateError(applicability, candidate)
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -138,7 +138,7 @@ private fun buildReflectionType(
|
||||
) to callableReferenceAdaptation
|
||||
}
|
||||
is FirVariable -> createKPropertyType(fir, receiverType, returnTypeRef, candidate) to null
|
||||
else -> ConeClassErrorType(ConeUnsupportedCallableReferenceTarget(fir)) to null
|
||||
else -> ConeClassErrorType(ConeUnsupportedCallableReferenceTarget(candidate)) to null
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+5
-5
@@ -542,15 +542,15 @@ open class FirExpressionsResolveTransformer(transformer: FirBodyResolveTransform
|
||||
}
|
||||
|
||||
fun reportAmbiguity(): FirStatement {
|
||||
val operatorCallSymbol = operatorCallReference?.candidateSymbol
|
||||
val assignmentCallSymbol = assignCallReference?.candidateSymbol
|
||||
val operatorCallCandidate = operatorCallReference?.candidate
|
||||
val assignmentCallCandidate = assignCallReference?.candidate
|
||||
|
||||
requireNotNull(operatorCallSymbol)
|
||||
requireNotNull(assignmentCallSymbol)
|
||||
requireNotNull(operatorCallCandidate)
|
||||
requireNotNull(assignmentCallCandidate)
|
||||
|
||||
return buildErrorExpression {
|
||||
source = assignmentOperatorStatement.source
|
||||
diagnostic = ConeOperatorAmbiguityError(listOf(operatorCallSymbol, assignmentCallSymbol))
|
||||
diagnostic = ConeOperatorAmbiguityError(listOf(operatorCallCandidate, assignmentCallCandidate))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+31
-25
@@ -7,7 +7,6 @@ package org.jetbrains.kotlin.fir.resolve.diagnostics
|
||||
|
||||
import kotlinx.collections.immutable.ImmutableList
|
||||
import org.jetbrains.kotlin.KtSourceElement
|
||||
import org.jetbrains.kotlin.fir.declarations.FirCallableDeclaration
|
||||
import org.jetbrains.kotlin.fir.declarations.FirVariable
|
||||
import org.jetbrains.kotlin.fir.diagnostics.ConeDiagnostic
|
||||
import org.jetbrains.kotlin.fir.diagnostics.ConeDiagnosticWithSource
|
||||
@@ -26,13 +25,20 @@ sealed interface ConeUnresolvedError : ConeDiagnostic {
|
||||
val qualifier: String?
|
||||
}
|
||||
|
||||
interface ConeDiagnosticWithSymbol<S : FirBasedSymbol<*>> : ConeDiagnostic {
|
||||
val symbol: S
|
||||
}
|
||||
|
||||
interface ConeDiagnosticWithCandidates : ConeDiagnostic {
|
||||
val candidateSymbols: Collection<FirBasedSymbol<*>>
|
||||
val candidates: Collection<AbstractCandidate>
|
||||
val candidateSymbols: Collection<FirBasedSymbol<*>> get() = candidates.map { it.symbol }
|
||||
}
|
||||
|
||||
interface ConeDiagnosticWithSingleCandidate : ConeDiagnosticWithCandidates {
|
||||
val candidate: AbstractCandidate
|
||||
val candidateSymbol: FirBasedSymbol<*> get() = candidate.symbol
|
||||
override val candidates: Collection<AbstractCandidate> get() = listOf(candidate)
|
||||
override val candidateSymbols: Collection<FirBasedSymbol<*>> get() = listOf(candidateSymbol)
|
||||
val candidateSymbol: FirBasedSymbol<*>
|
||||
}
|
||||
|
||||
class ConeUnresolvedReferenceError(val name: Name? = null) : ConeUnresolvedError {
|
||||
@@ -57,7 +63,7 @@ class ConeUnresolvedNameError(val name: Name) : ConeUnresolvedError {
|
||||
class ConeFunctionCallExpectedError(
|
||||
val name: Name,
|
||||
val hasValueParameters: Boolean,
|
||||
override val candidateSymbols: Collection<FirBasedSymbol<*>>
|
||||
override val candidates: Collection<AbstractCandidate>
|
||||
) : ConeDiagnosticWithCandidates {
|
||||
override val reason: String get() = "Function call expected: $name(${if (hasValueParameters) "..." else ""})"
|
||||
}
|
||||
@@ -66,23 +72,26 @@ class ConeFunctionExpectedError(val expression: String, val type: ConeKotlinType
|
||||
override val reason: String get() = "Expression '$expression' of type '$type' cannot be invoked as a function"
|
||||
}
|
||||
|
||||
class ConeResolutionToClassifierError(override val candidateSymbol: FirRegularClassSymbol) : ConeDiagnosticWithSingleCandidate {
|
||||
class ConeResolutionToClassifierError(
|
||||
override val candidate: AbstractCandidate,
|
||||
override val candidateSymbol: FirRegularClassSymbol
|
||||
) : ConeDiagnosticWithSingleCandidate {
|
||||
override val reason: String get() = "Resolution to classifier"
|
||||
}
|
||||
|
||||
class ConeHiddenCandidateError(
|
||||
override val candidateSymbol: FirBasedSymbol<*>
|
||||
override val candidate: AbstractCandidate
|
||||
) : ConeDiagnosticWithSingleCandidate {
|
||||
override val reason: String get() = "HIDDEN: ${describeSymbol(candidateSymbol)} is deprecated with DeprecationLevel.HIDDEN"
|
||||
}
|
||||
|
||||
class ConeVisibilityError(
|
||||
override val candidateSymbol: FirBasedSymbol<*>
|
||||
) : ConeDiagnosticWithSingleCandidate {
|
||||
override val reason: String get() = "HIDDEN: ${describeSymbol(candidateSymbol)} is invisible"
|
||||
override val symbol: FirBasedSymbol<*>
|
||||
) : ConeDiagnosticWithSymbol<FirBasedSymbol<*>> {
|
||||
override val reason: String get() = "HIDDEN: ${describeSymbol(symbol)} is invisible"
|
||||
}
|
||||
|
||||
class ConeInapplicableWrongReceiver(override val candidateSymbols: Collection<FirBasedSymbol<*>>) : ConeDiagnosticWithCandidates {
|
||||
class ConeInapplicableWrongReceiver(override val candidates: Collection<AbstractCandidate>) : ConeDiagnosticWithCandidates {
|
||||
override val reason: String
|
||||
get() = "None of the following candidates is applicable because of receiver type mismatch: ${
|
||||
candidateSymbols.map { describeSymbol(it) }
|
||||
@@ -91,21 +100,20 @@ class ConeInapplicableWrongReceiver(override val candidateSymbols: Collection<Fi
|
||||
|
||||
class ConeInapplicableCandidateError(
|
||||
val applicability: CandidateApplicability,
|
||||
val candidate: AbstractCandidate,
|
||||
override val candidate: AbstractCandidate,
|
||||
) : ConeDiagnosticWithSingleCandidate {
|
||||
override val reason: String get() = "Inapplicable($applicability): ${describeSymbol(candidateSymbol)}"
|
||||
override val candidateSymbol: FirBasedSymbol<*> get() = candidate.symbol
|
||||
}
|
||||
|
||||
class ConeNoCompanionObject(
|
||||
override val candidateSymbol: FirRegularClassSymbol
|
||||
override val candidate: AbstractCandidate
|
||||
) : ConeDiagnosticWithSingleCandidate {
|
||||
override val reason: String
|
||||
get() = "Classifier ''$candidateSymbol'' does not have a companion object, and thus must be initialized here"
|
||||
}
|
||||
|
||||
class ConeConstraintSystemHasContradiction(
|
||||
val candidate: AbstractCandidate,
|
||||
override val candidate: AbstractCandidate,
|
||||
) : ConeDiagnosticWithSingleCandidate {
|
||||
override val reason: String get() = "CS errors: ${describeSymbol(candidateSymbol)}"
|
||||
override val candidateSymbol: FirBasedSymbol<*> get() = candidate.symbol
|
||||
@@ -121,13 +129,13 @@ class ConeArgumentTypeMismatchCandidateError(
|
||||
class ConeAmbiguityError(
|
||||
val name: Name,
|
||||
val applicability: CandidateApplicability,
|
||||
val candidates: Collection<AbstractCandidate>
|
||||
override val candidates: Collection<AbstractCandidate>
|
||||
) : ConeDiagnosticWithCandidates {
|
||||
override val reason: String get() = "Ambiguity: $name, ${candidateSymbols.map { describeSymbol(it) }}"
|
||||
override val candidateSymbols: Collection<FirBasedSymbol<*>> get() = candidates.map { it.symbol }
|
||||
}
|
||||
|
||||
class ConeOperatorAmbiguityError(override val candidateSymbols: Collection<FirBasedSymbol<*>>) : ConeDiagnosticWithCandidates {
|
||||
class ConeOperatorAmbiguityError(override val candidates: Collection<AbstractCandidate>) : ConeDiagnosticWithCandidates {
|
||||
override val reason: String get() = "Operator overload ambiguity. Compatible candidates: ${candidateSymbols.map { describeSymbol(it) }}"
|
||||
}
|
||||
|
||||
@@ -145,14 +153,13 @@ class ConeIllegalAnnotationError(val name: Name) : ConeDiagnostic {
|
||||
override val reason: String get() = "Not a legal annotation: $name"
|
||||
}
|
||||
|
||||
interface ConeUnmatchedTypeArgumentsError : ConeDiagnosticWithSingleCandidate {
|
||||
interface ConeUnmatchedTypeArgumentsError : ConeDiagnosticWithSymbol<FirClassLikeSymbol<*>> {
|
||||
val desiredCount: Int
|
||||
override val candidateSymbol: FirClassLikeSymbol<*>
|
||||
}
|
||||
|
||||
class ConeWrongNumberOfTypeArgumentsError(
|
||||
override val desiredCount: Int,
|
||||
override val candidateSymbol: FirClassLikeSymbol<*>,
|
||||
override val symbol: FirClassLikeSymbol<*>,
|
||||
source: KtSourceElement
|
||||
) : ConeDiagnosticWithSource(source), ConeUnmatchedTypeArgumentsError {
|
||||
override val reason: String get() = "Wrong number of type arguments"
|
||||
@@ -160,7 +167,7 @@ class ConeWrongNumberOfTypeArgumentsError(
|
||||
|
||||
class ConeNoTypeArgumentsOnRhsError(
|
||||
override val desiredCount: Int,
|
||||
override val candidateSymbol: FirClassLikeSymbol<*>
|
||||
override val symbol: FirClassLikeSymbol<*>
|
||||
) : ConeUnmatchedTypeArgumentsError {
|
||||
override val reason: String get() = "No type arguments on RHS"
|
||||
}
|
||||
@@ -175,9 +182,8 @@ class ConeInstanceAccessBeforeSuperCall(val target: String) : ConeDiagnostic {
|
||||
override val reason: String get() = "Cannot access ''${target}'' before superclass constructor has been called"
|
||||
}
|
||||
|
||||
class ConeUnsupportedCallableReferenceTarget(val fir: FirCallableDeclaration) : ConeDiagnosticWithSingleCandidate {
|
||||
override val reason: String get() = "Unsupported declaration for callable reference: ${fir.render()}"
|
||||
override val candidateSymbol: FirBasedSymbol<*> get() = fir.symbol
|
||||
class ConeUnsupportedCallableReferenceTarget(override val candidate: AbstractCandidate) : ConeDiagnosticWithSingleCandidate {
|
||||
override val reason: String get() = "Unsupported declaration for callable reference: ${candidate.symbol.fir.render()}"
|
||||
}
|
||||
|
||||
class ConeTypeParameterSupertype(val symbol: FirTypeParameterSymbol) : ConeDiagnostic {
|
||||
@@ -207,9 +213,9 @@ class ConeUnsupportedDynamicType : ConeUnsupported("Dynamic types are not suppor
|
||||
|
||||
class ConeDeprecated(
|
||||
val source: KtSourceElement?,
|
||||
override val candidateSymbol: FirBasedSymbol<*>,
|
||||
override val symbol: FirBasedSymbol<*>,
|
||||
val deprecationInfo: DeprecationInfo
|
||||
) : ConeDiagnosticWithSingleCandidate {
|
||||
) : ConeDiagnosticWithSymbol<FirBasedSymbol<*>> {
|
||||
override val reason: String get() = "Deprecated: ${deprecationInfo.message}"
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user