Skip to content

Commit 2979d4a

Browse files
committed
[Trailing closures] Remove dynamically-dead warning about behavior change.
With the constraint solver preferring backward scanning to forward scanning, there is no need to point out the ambiguity: we will always, consistently warn about backward scanning when it produced a result that was different from the forward scan.
1 parent 25d4012 commit 2979d4a

File tree

2 files changed

+2
-139
lines changed

2 files changed

+2
-139
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1186,15 +1186,6 @@ ERROR(extra_trailing_closure_in_call,none,
11861186
ERROR(trailing_closure_bad_param,none,
11871187
"trailing closure passed to parameter of type %0 that does not "
11881188
"accept a closure", (Type))
1189-
WARNING(unlabeled_trailing_closure_changed_behavior,none,
1190-
"since Swift 5.3, unlabeled trailing closure argument matches parameter %0 rather than parameter %1",
1191-
(Identifier, Identifier))
1192-
WARNING(unlabeled_trailing_closure_changed_behavior_same_param_name,none,
1193-
"since Swift 5.3, unlabeled trailing closure argument matches earlier parameter %0 rather than later parameter with the same name",
1194-
(Identifier))
1195-
NOTE(trailing_closure_select_parameter,none,
1196-
"label the argument with %0 to %select{retain the pre-Swift 5.3 behavior|silence this warning for Swift 5.3 and newer}1",
1197-
(Identifier, unsigned))
11981189
WARNING(unlabeled_trailing_closure_deprecated,none,
11991190
"backward matching of the unlabeled trailing closure is deprecated; label the argument with %0 to suppress this warning",
12001191
(Identifier))

lib/Sema/CSApply.cpp

Lines changed: 2 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -5455,22 +5455,6 @@ static bool hasCurriedSelf(ConstraintSystem &cs, ConcreteDeclRef callee,
54555455
return false;
54565456
}
54575457

5458-
/// Determine the arity of the given parameter of function type.
5459-
static unsigned functionParameterArity(AnyFunctionType::Param param) {
5460-
Type paramType = param.getPlainType();
5461-
if (param.isVariadic())
5462-
paramType = ParamDecl::getVarargBaseTy(paramType);
5463-
5464-
paramType = paramType->lookThroughAllOptionalTypes();
5465-
5466-
if (param.isAutoClosure()) {
5467-
paramType = paramType->castTo<AnyFunctionType>()->getResult();
5468-
paramType = paramType->lookThroughAllOptionalTypes();
5469-
}
5470-
5471-
return paramType->castTo<AnyFunctionType>()->getNumParams();
5472-
}
5473-
54745458
/// Attach a Fix-It to the given diagnostic to give the trailing closure
54755459
/// argument a label.
54765460
static void labelTrailingClosureArgument(
@@ -5561,113 +5545,6 @@ static unsigned findParamBindingArgument(
55615545
llvm_unreachable("No parameter binds the argument?");
55625546
}
55635547

5564-
/// SE-0286 changed the direction in which the unlabeled trailing closure
5565-
/// argument is matched to a parameter, from backward (the pre-Swift 5.3
5566-
/// semantics) to forward (after SE-0286). Identify cases where this may
5567-
/// have resulted in a silent change in behavior.
5568-
static void maybeWarnAboutTrailingClosureBindingChange(
5569-
ConcreteDeclRef callee,
5570-
Expr *fn,
5571-
Expr *arg,
5572-
ArrayRef<AnyFunctionType::Param> args,
5573-
ArrayRef<AnyFunctionType::Param> params,
5574-
const ParameterListInfo &paramInfo,
5575-
Optional<unsigned> unlabeledTrailingClosureIndex,
5576-
ArrayRef<ParamBinding> parameterBindings) {
5577-
5578-
if (!unlabeledTrailingClosureIndex)
5579-
return;
5580-
5581-
if (*unlabeledTrailingClosureIndex != args.size() - 1)
5582-
return;
5583-
5584-
// Find the parameter that bound the unlabeled trailing closure argument.
5585-
unsigned paramIdx = findParamBindingArgument(
5586-
parameterBindings, *unlabeledTrailingClosureIndex);
5587-
5588-
// If this parameter requires an argument, it would have been unfilled
5589-
// prior to SE-2086; there is nothing to diagnose.
5590-
if (parameterRequiresArgument(params, paramInfo, paramIdx))
5591-
return;
5592-
5593-
// Look for a later parameter that could match a trailing closure; the
5594-
// last one of these would have been picked prior to SE-0286.
5595-
Optional<unsigned> matchingBackwardParamIdx;
5596-
for (unsigned backwardParamIdx :
5597-
range(paramIdx + 1, parameterBindings.size())) {
5598-
if (!paramInfo.acceptsUnlabeledTrailingClosureArgument(backwardParamIdx))
5599-
continue;
5600-
5601-
matchingBackwardParamIdx = backwardParamIdx;
5602-
}
5603-
5604-
// If there is no other parameter that could match the unlabeled trailing
5605-
// closure, there is nothing to diagnose.
5606-
if (!matchingBackwardParamIdx)
5607-
return;
5608-
5609-
// Do a simple arity check; if the matched parameter and backward-matched
5610-
// parameter accept functions with different arity, this would not have
5611-
// type-checked with the backward scan, so there is nothing to report.
5612-
if (functionParameterArity(params[paramIdx]) !=
5613-
functionParameterArity(params[*matchingBackwardParamIdx]))
5614-
return;
5615-
5616-
// Dig out the trailing closure.
5617-
Expr *trailingClosure = findTrailingClosureArgument(arg);
5618-
5619-
// Determine the names of the parameters that would be matched by the
5620-
// forward and backward scans.
5621-
Identifier paramName = params[paramIdx].getLabel();
5622-
Identifier backwardParamName = params[*matchingBackwardParamIdx].getLabel();
5623-
5624-
// Produce a diagnostic referencing the callee.
5625-
ASTContext &ctx = params[paramIdx].getPlainType()->getASTContext();
5626-
auto noteCallee = [&] {
5627-
auto decl = callee.getDecl();
5628-
if (!decl)
5629-
return;
5630-
5631-
auto diag = ctx.Diags.diagnose(
5632-
decl, diag::decl_multiple_defaulted_closure_parameters,
5633-
decl->getName(), paramName, backwardParamName);
5634-
5635-
// Dig out the parameter declarations so we can highlight them.
5636-
if (const ParameterList *paramList = getParameterList(decl)) {
5637-
diag.highlight(paramList->get(paramIdx)->getLoc());
5638-
diag.highlight(paramList->get(*matchingBackwardParamIdx)->getLoc());
5639-
}
5640-
};
5641-
5642-
// If the parameters have the same name, provide a custom diagnostic and then
5643-
// bail out early; there are no useful notes we can provide here.
5644-
if (paramName == backwardParamName) {
5645-
ctx.Diags.diagnose(trailingClosure->getStartLoc(), diag::unlabeled_trailing_closure_changed_behavior_same_param_name,
5646-
paramName);
5647-
noteCallee();
5648-
return;
5649-
}
5650-
5651-
// Produce the diagnostic.
5652-
ctx.Diags.diagnose(trailingClosure->getStartLoc(), diag::unlabeled_trailing_closure_changed_behavior, paramName,
5653-
backwardParamName);
5654-
5655-
// Produce a note with a Fix-It describing how to resolve the ambiguity.
5656-
auto diagResolution = [&](Identifier paramName, unsigned which) {
5657-
// Emit the note.
5658-
auto diag = ctx.Diags.diagnose(
5659-
trailingClosure->getStartLoc(), diag::trailing_closure_select_parameter,
5660-
paramName, which);
5661-
labelTrailingClosureArgument(
5662-
ctx, fn, arg, paramName, trailingClosure, diag);
5663-
};
5664-
5665-
diagResolution(backwardParamName, 0);
5666-
diagResolution(paramName, 1);
5667-
5668-
noteCallee();
5669-
}
5670-
56715548
/// Warn about the use of the deprecated "backward" scan for matching the
56725549
/// unlabeled trailing closure. It was needed to properly type check, but
56735550
/// this code will break with a future version of Swift.
@@ -5787,13 +5664,8 @@ Expr *ExprRewriter::coerceCallArguments(
57875664
// FIXME: Eventually, we want to enforce that we have either argTuple or
57885665
// argParen here.
57895666

5790-
// Warn if there was a recent change in trailing closure binding semantics
5791-
// that might have lead to a silent change in behavior.
5792-
if (trailingClosureMatching == TrailingClosureMatching::Forward) {
5793-
maybeWarnAboutTrailingClosureBindingChange(
5794-
callee, apply ? apply->getFn() : nullptr, arg, args, params, paramInfo,
5795-
unlabeledTrailingClosureIndex, parameterBindings);
5796-
} else {
5667+
// Warn about the backward scan being deprecated.
5668+
if (trailingClosureMatching == TrailingClosureMatching::Backward) {
57975669
warnAboutTrailingClosureBackwardScan(
57985670
callee, apply ? apply->getFn() : nullptr, arg, params,
57995671
unlabeledTrailingClosureIndex, parameterBindings);

0 commit comments

Comments
 (0)