Skip to content

[CSSimplify] Avoid resolving extraneous (trailing) closures #78035

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 1 commit into from
Dec 9, 2024
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
21 changes: 21 additions & 0 deletions lib/Sema/CSSimplify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1620,6 +1620,21 @@ static ConstraintSystem::TypeMatchResult matchCallArguments(
cs, contextualType, extraArguments,
cs.getConstraintLocator(locator));

for (const auto &extraArg : extraArguments) {
auto argument = argList->get(extraArg.first);
auto argType = extraArg.second.getPlainType();

// Prevent closure resolution by binding it to a placeholder
// because the main issue here is invalid overload and
// errors produced from the closure body are going to be
// superfluous.
if (isExpr<ClosureExpr>(argument.getExpr())) {
cs.recordTypeVariablesAsHoles(argType);
} else {
cs.recordAnyTypeVarAsPotentialHole(argType);
}
}

if (cs.recordFix(fix, /*impact=*/extraArguments.size() * 5))
return cs.getTypeMatchFailure(locator);
}
Expand Down Expand Up @@ -11371,6 +11386,12 @@ ConstraintSystem::SolutionKind ConstraintSystem::simplifyFallbackTypeConstraint(
return SolutionKind::Unsolved;
}

// Propagate placeholders into an inferred closure type. Without this
// we'd produce superfluous diagnostics about parameter/result types.
if (defaultableType->isPlaceholder() && locator.directlyAt<ClosureExpr>()) {
recordTypeVariablesAsHoles(fallbackType);
}

// Otherwise, any type is fine.
return SolutionKind::Solved;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// RUN: %target-typecheck-verify-swift

func test(_ v: [Int]) {
let result = v.filter { }.flatMap(\.wrong) {
// expected-error@-1 {{type for closure argument list expects 1 argument, which cannot be implicitly ignored}}
// expected-error@-2 {{cannot convert value of type '()' to closure result type 'Bool'}}
// expected-error@-3 {{value of type 'Int' has no member 'wrong'}}
// expected-error@-4 {{extra trailing closure passed in call}}
print(result)
}

let otherResult = v.filter { _ in false }.flatMap(\.wrong, { $0 }, 42)
// expected-error@-1 {{value of type 'Int' has no member 'wrong'}}
// expected-error@-2 {{extra arguments at positions #2, #3 in call}}
}