Skip to content

Commit a9dba6a

Browse files
authored
Merge pull request #20971 from rudkx/fix-sr9102-5.0
[5.0] Get the ModelAssistant source compatibility project building with the swift-5.0-branch
2 parents ed6eb11 + 9099ad9 commit a9dba6a

File tree

3 files changed

+42
-2
lines changed

3 files changed

+42
-2
lines changed

lib/Sema/CSBindings.cpp

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,7 @@ ConstraintSystem::getPotentialBindings(TypeVariableType *typeVar) {
365365
auto &tc = getTypeChecker();
366366
bool hasNonDependentMemberRelationalConstraints = false;
367367
bool hasDependentMemberRelationalConstraints = false;
368+
bool sawNilLiteral = false;
368369
for (auto constraint : constraints) {
369370
switch (constraint->getKind()) {
370371
case ConstraintKind::Bind:
@@ -481,8 +482,10 @@ ConstraintSystem::getPotentialBindings(TypeVariableType *typeVar) {
481482
// If there is a 'nil' literal constraint, we might need optional
482483
// supertype bindings.
483484
if (constraint->getProtocol()->isSpecificProtocol(
484-
KnownProtocolKind::ExpressibleByNilLiteral))
485+
KnownProtocolKind::ExpressibleByNilLiteral)) {
486+
sawNilLiteral = true;
485487
addOptionalSupertypeBindings = true;
488+
}
486489

487490
// If there is a default literal type for this protocol, it's a
488491
// potential binding.
@@ -721,6 +724,32 @@ ConstraintSystem::getPotentialBindings(TypeVariableType *typeVar) {
721724
result.Bindings.clear();
722725
}
723726

727+
// Revise any optional-of-function-types we may try to nil literals
728+
// to be non-throwing so they don't inadvertantly result in rethrows
729+
// diagnostics.
730+
if (sawNilLiteral) {
731+
for (auto &binding : result.Bindings) {
732+
auto nested = binding.BindingType->lookThroughAllOptionalTypes();
733+
if (!nested)
734+
continue;
735+
736+
if (!nested->is<FunctionType>())
737+
continue;
738+
739+
// Remove throws from the nested function type.
740+
binding.BindingType =
741+
binding.BindingType.transform([&](Type inner) -> Type {
742+
auto *fnTy = dyn_cast<FunctionType>(inner.getPointer());
743+
if (!fnTy)
744+
return inner;
745+
746+
auto extInfo = fnTy->getExtInfo().withThrows(false);
747+
return FunctionType::get(fnTy->getParams(), fnTy->getResult(),
748+
extInfo);
749+
});
750+
}
751+
}
752+
724753
return result;
725754
}
726755

lib/Sema/TypeCheckError.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,9 @@ class AbstractFunction {
146146
// Look through closure capture lists.
147147
} else if (auto captureList = dyn_cast<CaptureListExpr>(fn)) {
148148
fn = captureList->getClosureBody();
149+
// Look through optional evaluations.
150+
} else if (auto optionalEval = dyn_cast<OptionalEvaluationExpr>(fn)) {
151+
fn = optionalEval->getSubExpr()->getValueProvidingExpr();
149152
} else {
150153
break;
151154
}
@@ -710,7 +713,10 @@ class ApplyClassifier {
710713

711714
// If it doesn't have function type, we must have invalid code.
712715
Type argType = fn.getType();
713-
auto argFnType = (argType ? argType->getAs<AnyFunctionType>() : nullptr);
716+
if (!argType) return Classification::forInvalidCode();
717+
718+
auto argFnType =
719+
argType->lookThroughAllOptionalTypes()->getAs<AnyFunctionType>();
714720
if (!argFnType) return Classification::forInvalidCode();
715721

716722
// If it doesn't throw, this argument does not cause the call to throw.

test/Constraints/sr9102.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// RUN: %target-typecheck-verify-swift
2+
3+
func test(_ a: [Int], _ f: ((Int) -> Bool)?) {
4+
_ = a.filter(f!)
5+
}

0 commit comments

Comments
 (0)