K2: handle type parameter vs nested class conflict in body resolve properly

This commit does two things:
- prioritize type parameter scopes against static scopes in body resolve
(effectively it's a revert of KT-58028 fix)
- consider type parameters as inapplicable callable, so during callable
resolve we can go up the tower and still resolve to static scope

This allows both KT-58028 and KT-63377 to work properly
#KT-63377 Fixed
This commit is contained in:
Mikhail Glukhikh
2023-11-22 18:19:35 +01:00
committed by Space Team
parent 4e938d852c
commit 83cfcc30c6
25 changed files with 86 additions and 73 deletions
@@ -53,8 +53,8 @@ FILE: multipleBounds.kt
}
public final fun test(t: R|T|): R|kotlin/Unit| {
R|?|.<Unresolved name: foo>#()
R|?|.<Unresolved name: bar>#()
R|?<Type parameter T in qualified access>#|.<Unresolved name: foo>#()
R|?<Type parameter T in qualified access>#|.<Unresolved name: bar>#()
R|<local>/t|.R|Jet87/A.foo|()
R|<local>/t|.R|Jet87/B.bar|()
}
@@ -96,8 +96,8 @@ FILE: multipleBounds.kt
}
public final fun <T : R|Jet87/A|, R|Jet87/B|> test2(t: R|T|): R|kotlin/Unit| {
R|?|.<Unresolved name: foo>#()
R|?|.<Unresolved name: bar>#()
R|?<Type parameter T in qualified access>#|.<Unresolved name: foo>#()
R|?<Type parameter T in qualified access>#|.<Unresolved name: bar>#()
R|<local>/t|.R|Jet87/A.foo|()
R|<local>/t|.R|Jet87/B.bar|()
}
@@ -362,6 +362,9 @@ private fun mapInapplicableCandidateError(
qualifiedAccessSource
)
// Reported later
is TypeParameterAsExpression -> null
else -> genericDiagnostic
}
}.distinct()
@@ -608,7 +608,13 @@ fun createConeDiagnosticForCandidateWithError(
}
CandidateApplicability.INAPPLICABLE_WRONG_RECEIVER -> ConeInapplicableWrongReceiver(listOf(candidate))
CandidateApplicability.K2_NO_COMPANION_OBJECT -> ConeNoCompanionObject(candidate)
else -> ConeInapplicableCandidateError(applicability, candidate)
else -> {
if (TypeParameterAsExpression in candidate.diagnostics) {
ConeTypeParameterInQualifiedAccess(candidate.symbol as FirTypeParameterSymbol)
} else {
ConeInapplicableCandidateError(applicability, candidate)
}
}
}
}
@@ -24,6 +24,7 @@ sealed class CallKind(vararg resolutionSequence: ResolutionStage) {
LowerPriorityIfDynamic,
ConstraintSystemForks,
CheckIncompatibleTypeVariableUpperBounds,
TypeParameterAsCallable,
)
object SyntheticSelect : CallKind(
@@ -59,6 +60,7 @@ sealed class CallKind(vararg resolutionSequence: ResolutionStage) {
LowerPriorityIfDynamic,
ConstraintSystemForks,
CheckIncompatibleTypeVariableUpperBounds,
TypeParameterAsCallable,
)
object DelegatingConstructorCall : CallKind(
@@ -780,3 +780,11 @@ internal object ConstraintSystemForks : ResolutionStage() {
}
}
}
internal object TypeParameterAsCallable : ResolutionStage() {
override suspend fun check(candidate: Candidate, callInfo: CallInfo, sink: CheckerSink, context: ResolutionContext) {
if (candidate.symbol is FirTypeParameterSymbol) {
sink.yieldDiagnostic(TypeParameterAsExpression)
}
}
}
@@ -471,10 +471,11 @@ class BodyResolveContext(
// Otherwise, reuse staticsAndCompanion.
val forConstructorHeader = if (typeParameterScope != null) {
towerDataContext
.addNonLocalScope(typeParameterScope)
.addNonLocalTowerDataElements(towerElementsForClass.superClassesStaticsAndCompanionReceivers)
.run { towerElementsForClass.companionReceiver?.let { addReceiver(null, it) } ?: this }
.addNonLocalScopesIfNotNull(towerElementsForClass.companionStaticScope, towerElementsForClass.staticScope)
// Note: scopes here are in reverse order, so type parameter scope is the most prioritized
.addNonLocalScope(typeParameterScope)
} else {
staticsAndCompanion
}
@@ -146,4 +146,6 @@ class AmbiguousValuesForContextReceiverParameter(
object ResolutionResultOverridesOtherToPreserveCompatibility : ResolutionDiagnostic(RESOLVED)
object AdaptedCallableReferenceIsUsedWithReflection : ResolutionDiagnostic(RESOLVED_WITH_ERROR)
object AdaptedCallableReferenceIsUsedWithReflection : ResolutionDiagnostic(RESOLVED_WITH_ERROR)
object TypeParameterAsExpression : ResolutionDiagnostic(INAPPLICABLE)