Skip to content

[5.0] Get the ModelAssistant source compatibility project building with the swift-5.0-branch #20971

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion lib/Sema/CSBindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,7 @@ ConstraintSystem::getPotentialBindings(TypeVariableType *typeVar) {
auto &tc = getTypeChecker();
bool hasNonDependentMemberRelationalConstraints = false;
bool hasDependentMemberRelationalConstraints = false;
bool sawNilLiteral = false;
for (auto constraint : constraints) {
switch (constraint->getKind()) {
case ConstraintKind::Bind:
Expand Down Expand Up @@ -481,8 +482,10 @@ ConstraintSystem::getPotentialBindings(TypeVariableType *typeVar) {
// If there is a 'nil' literal constraint, we might need optional
// supertype bindings.
if (constraint->getProtocol()->isSpecificProtocol(
KnownProtocolKind::ExpressibleByNilLiteral))
KnownProtocolKind::ExpressibleByNilLiteral)) {
sawNilLiteral = true;
addOptionalSupertypeBindings = true;
}

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

// Revise any optional-of-function-types we may try to nil literals
// to be non-throwing so they don't inadvertantly result in rethrows
// diagnostics.
if (sawNilLiteral) {
for (auto &binding : result.Bindings) {
auto nested = binding.BindingType->lookThroughAllOptionalTypes();
if (!nested)
continue;

if (!nested->is<FunctionType>())
continue;

// Remove throws from the nested function type.
binding.BindingType =
binding.BindingType.transform([&](Type inner) -> Type {
auto *fnTy = dyn_cast<FunctionType>(inner.getPointer());
if (!fnTy)
return inner;

auto extInfo = fnTy->getExtInfo().withThrows(false);
return FunctionType::get(fnTy->getParams(), fnTy->getResult(),
extInfo);
});
}
}

return result;
}

Expand Down
8 changes: 7 additions & 1 deletion lib/Sema/TypeCheckError.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,9 @@ class AbstractFunction {
// Look through closure capture lists.
} else if (auto captureList = dyn_cast<CaptureListExpr>(fn)) {
fn = captureList->getClosureBody();
// Look through optional evaluations.
} else if (auto optionalEval = dyn_cast<OptionalEvaluationExpr>(fn)) {
fn = optionalEval->getSubExpr()->getValueProvidingExpr();
} else {
break;
}
Expand Down Expand Up @@ -710,7 +713,10 @@ class ApplyClassifier {

// If it doesn't have function type, we must have invalid code.
Type argType = fn.getType();
auto argFnType = (argType ? argType->getAs<AnyFunctionType>() : nullptr);
if (!argType) return Classification::forInvalidCode();

auto argFnType =
argType->lookThroughAllOptionalTypes()->getAs<AnyFunctionType>();
if (!argFnType) return Classification::forInvalidCode();

// If it doesn't throw, this argument does not cause the call to throw.
Expand Down
5 changes: 5 additions & 0 deletions test/Constraints/sr9102.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// RUN: %target-typecheck-verify-swift

func test(_ a: [Int], _ f: ((Int) -> Bool)?) {
_ = a.filter(f!)
}