Merge pull request #42559 from xedin/se-0352-as-any-coercion

[TypeChecker] SE-0352: Require coercion if result type contains existential(s) that would loose generic requirements
This commit is contained in:
Pavel Yaskevich
2022-05-27 09:44:31 -07:00
committed by GitHub
9 changed files with 500 additions and 3 deletions

View File

@@ -1479,13 +1479,14 @@ shouldOpenExistentialCallArgument(
return None;
// An argument expression that explicitly coerces to an existential
// disables the implicit opening of the existential.
// disables the implicit opening of the existential unless it's
// wrapped in parens.
if (argExpr) {
if (auto argCast = dyn_cast<ExplicitCastExpr>(
argExpr->getSemanticsProvidingExpr())) {
if (auto typeRepr = argCast->getCastTypeRepr()) {
if (auto toType = cs.getType(typeRepr)) {
if (toType->isAnyExistentialType())
if (!isa<ParenExpr>(argExpr) && toType->isAnyExistentialType())
return None;
}
}
@@ -11463,6 +11464,18 @@ ConstraintSystem::simplifyApplicableFnConstraint(
result2, opened.second->getExistentialType(), opened.first,
TypePosition::Covariant);
}
// If result type has any erased existential types it requires explicit
// `as` coercion.
if (AddExplicitExistentialCoercion::isRequired(
*this, func2->getResult(), openedExistentials, locator)) {
if (!shouldAttemptFixes())
return SolutionKind::Error;
if (recordFix(AddExplicitExistentialCoercion::create(
*this, result2, getConstraintLocator(locator))))
return SolutionKind::Error;
}
}
// The result types are equivalent.
@@ -13121,6 +13134,7 @@ ConstraintSystem::SolutionKind ConstraintSystem::simplifyFixConstraint(
case FixKind::DropAsyncAttribute:
case FixKind::AllowSwiftToCPointerConversion:
case FixKind::AllowTupleLabelMismatch:
case FixKind::AddExplicitExistentialCoercion:
llvm_unreachable("handled elsewhere");
}