[FE] Convert specific diagnostic for actual function with default arguments into a common incompatibility
^KT-59665 Fixed Review: https://jetbrains.team/p/kt/reviews/11039/timeline It's better to have this logic in common place (AbstractExpectActualCompatibilityChecker) to avoid missing compilation errors in the future This commit fixes: 1. Missing compilation error for actual function with default arguments for 'actual typealias' KT-59665 2. Missing compilation error for actual function with default arguments for actual fake-override KT-59665 Alternative solution for KT-59665 is to create a special checker. "incompatibility" vs "special checker": Arguments for common incompatibility: - What if we had a rule that expect and actual default params must match? If so then it certainly would be an incompatibility. - Technically, we do the matching of expect and actual params (because we allow default params in common ancestors of expect and actual declarations). - It's hard to check that the actual definition doesn't use default params because `ExpectedActualResolver.findActualForExpected` filters out fake-overrides and doesn't return them. It's not clear logic for me, that I'm afraid to touch. implicitActualFakeOverride_AbstractMap.kt test breaks if you drop this weird logic - WEAK incompatibilities can be considered as "checkers". So it doesn't matter how it's implemented, as a "incompatibility" or a "checker" Arguments against common incompatibility: - Although we match expect and actual declarations to allow default params in common ancestors of expect and actual declarations, it's still can be considered that we check that the actual declaration doesn't have default params. And it doesn't feel right that we check correctness of the actual declaration in expect-actual matcher. - ~~It may change the rules of expect actual matching~~ (It's not true, because ActualFunctionWithDefaultParameters is declared as WEAK incompatibility)
This commit is contained in:
+16
@@ -376,6 +376,22 @@ object AbstractExpectActualCompatibilityChecker {
|
||||
}
|
||||
}
|
||||
|
||||
if (shouldCheckAbsenceOfDefaultParamsInActual) {
|
||||
// "Default parameters in actual" check is required only for functions, because only functions can have parameters
|
||||
if (actualDeclaration is FunctionSymbolMarker && expectDeclaration is FunctionSymbolMarker) {
|
||||
// Actual annotation constructors can have default argument values; their consistency with arguments in the expected annotation
|
||||
// is checked in ExpectedActualDeclarationChecker.checkAnnotationConstructors
|
||||
if (!actualDeclaration.isAnnotationConstructor() &&
|
||||
// If default params came from common supertypes of actual class and expect class then it's a valid code.
|
||||
// Here we filter out such default params.
|
||||
(actualDeclaration.allOverriddenDeclarationsRecursive() - expectDeclaration.allOverriddenDeclarationsRecursive().toSet())
|
||||
.flatMap { it.valueParameters }.any { it.hasDefaultValue }
|
||||
) {
|
||||
return Incompatible.ActualFunctionWithDefaultParameters
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!equalsBy(expectedValueParameters, actualValueParameters) { it.isVararg }) {
|
||||
return Incompatible.ValueParameterVararg
|
||||
}
|
||||
|
||||
+8
@@ -39,6 +39,9 @@ interface ExpectActualMatchingContext<T : DeclarationSymbolMarker> : TypeSystemC
|
||||
val enumConstructorsAreAlwaysCompatible: Boolean
|
||||
get() = false
|
||||
|
||||
// Try to drop it once KT-61105 is fixed
|
||||
val shouldCheckAbsenceOfDefaultParamsInActual: Boolean
|
||||
|
||||
/**
|
||||
* This flag determines, how visibilities for classes/typealiases will be matched
|
||||
* - `false` means that visibilities should be identical
|
||||
@@ -115,6 +118,11 @@ interface ExpectActualMatchingContext<T : DeclarationSymbolMarker> : TypeSystemC
|
||||
val CallableSymbolMarker.typeParameters: List<TypeParameterSymbolMarker>
|
||||
val FunctionSymbolMarker.valueParameters: List<ValueParameterSymbolMarker>
|
||||
|
||||
/**
|
||||
* Returns all symbols that are overridden by [this] symbol
|
||||
*/
|
||||
fun FunctionSymbolMarker.allOverriddenDeclarationsRecursive(): Sequence<CallableSymbolMarker>
|
||||
|
||||
val CallableSymbolMarker.valueParameters: List<ValueParameterSymbolMarker>
|
||||
get() = (this as? FunctionSymbolMarker)?.valueParameters ?: emptyList()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user