Skip to content

Commit d3ab488

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 543eb68 commit d3ab488

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
@@ -1174,15 +1174,6 @@ ERROR(extra_trailing_closure_in_call,none,
11741174
ERROR(trailing_closure_bad_param,none,
11751175
"trailing closure passed to parameter of type %0 that does not "
11761176
"accept a closure", (Type))
1177-
WARNING(unlabeled_trailing_closure_changed_behavior,none,
1178-
"since Swift 5.3, unlabeled trailing closure argument matches parameter %0 rather than parameter %1",
1179-
(Identifier, Identifier))
1180-
WARNING(unlabeled_trailing_closure_changed_behavior_same_param_name,none,
1181-
"since Swift 5.3, unlabeled trailing closure argument matches earlier parameter %0 rather than later parameter with the same name",
1182-
(Identifier))
1183-
NOTE(trailing_closure_select_parameter,none,
1184-
"label the argument with %0 to %select{retain the pre-Swift 5.3 behavior|silence this warning for Swift 5.3 and newer}1",
1185-
(Identifier, unsigned))
11861177
WARNING(unlabeled_trailing_closure_deprecated,none,
11871178
"backward matching of the unlabeled trailing closure is deprecated; label the argument with %0 to suppress this warning",
11881179
(Identifier))

lib/Sema/CSApply.cpp

Lines changed: 2 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -5521,22 +5521,6 @@ static bool hasCurriedSelf(ConstraintSystem &cs, ConcreteDeclRef callee,
55215521
return false;
55225522
}
55235523

5524-
/// Determine the arity of the given parameter of function type.
5525-
static unsigned functionParameterArity(AnyFunctionType::Param param) {
5526-
Type paramType = param.getPlainType();
5527-
if (param.isVariadic())
5528-
paramType = ParamDecl::getVarargBaseTy(paramType);
5529-
5530-
paramType = paramType->lookThroughAllOptionalTypes();
5531-
5532-
if (param.isAutoClosure()) {
5533-
paramType = paramType->castTo<AnyFunctionType>()->getResult();
5534-
paramType = paramType->lookThroughAllOptionalTypes();
5535-
}
5536-
5537-
return paramType->castTo<AnyFunctionType>()->getNumParams();
5538-
}
5539-
55405524
/// Attach a Fix-It to the given diagnostic to give the trailing closure
55415525
/// argument a label.
55425526
static void labelTrailingClosureArgument(
@@ -5627,113 +5611,6 @@ static unsigned findParamBindingArgument(
56275611
llvm_unreachable("No parameter binds the argument?");
56285612
}
56295613

5630-
/// SE-0286 changed the direction in which the unlabeled trailing closure
5631-
/// argument is matched to a parameter, from backward (the pre-Swift 5.3
5632-
/// semantics) to forward (after SE-0286). Identify cases where this may
5633-
/// have resulted in a silent change in behavior.
5634-
static void maybeWarnAboutTrailingClosureBindingChange(
5635-
ConcreteDeclRef callee,
5636-
Expr *fn,
5637-
Expr *arg,
5638-
ArrayRef<AnyFunctionType::Param> args,
5639-
ArrayRef<AnyFunctionType::Param> params,
5640-
const ParameterListInfo &paramInfo,
5641-
Optional<unsigned> unlabeledTrailingClosureIndex,
5642-
ArrayRef<ParamBinding> parameterBindings) {
5643-
5644-
if (!unlabeledTrailingClosureIndex)
5645-
return;
5646-
5647-
if (*unlabeledTrailingClosureIndex != args.size() - 1)
5648-
return;
5649-
5650-
// Find the parameter that bound the unlabeled trailing closure argument.
5651-
unsigned paramIdx = findParamBindingArgument(
5652-
parameterBindings, *unlabeledTrailingClosureIndex);
5653-
5654-
// If this parameter requires an argument, it would have been unfilled
5655-
// prior to SE-2086; there is nothing to diagnose.
5656-
if (parameterRequiresArgument(params, paramInfo, paramIdx))
5657-
return;
5658-
5659-
// Look for a later parameter that could match a trailing closure; the
5660-
// last one of these would have been picked prior to SE-0286.
5661-
Optional<unsigned> matchingBackwardParamIdx;
5662-
for (unsigned backwardParamIdx :
5663-
range(paramIdx + 1, parameterBindings.size())) {
5664-
if (!paramInfo.acceptsUnlabeledTrailingClosureArgument(backwardParamIdx))
5665-
continue;
5666-
5667-
matchingBackwardParamIdx = backwardParamIdx;
5668-
}
5669-
5670-
// If there is no other parameter that could match the unlabeled trailing
5671-
// closure, there is nothing to diagnose.
5672-
if (!matchingBackwardParamIdx)
5673-
return;
5674-
5675-
// Do a simple arity check; if the matched parameter and backward-matched
5676-
// parameter accept functions with different arity, this would not have
5677-
// type-checked with the backward scan, so there is nothing to report.
5678-
if (functionParameterArity(params[paramIdx]) !=
5679-
functionParameterArity(params[*matchingBackwardParamIdx]))
5680-
return;
5681-
5682-
// Dig out the trailing closure.
5683-
Expr *trailingClosure = findTrailingClosureArgument(arg);
5684-
5685-
// Determine the names of the parameters that would be matched by the
5686-
// forward and backward scans.
5687-
Identifier paramName = params[paramIdx].getLabel();
5688-
Identifier backwardParamName = params[*matchingBackwardParamIdx].getLabel();
5689-
5690-
// Produce a diagnostic referencing the callee.
5691-
ASTContext &ctx = params[paramIdx].getPlainType()->getASTContext();
5692-
auto noteCallee = [&] {
5693-
auto decl = callee.getDecl();
5694-
if (!decl)
5695-
return;
5696-
5697-
auto diag = ctx.Diags.diagnose(
5698-
decl, diag::decl_multiple_defaulted_closure_parameters,
5699-
decl->getName(), paramName, backwardParamName);
5700-
5701-
// Dig out the parameter declarations so we can highlight them.
5702-
if (const ParameterList *paramList = getParameterList(decl)) {
5703-
diag.highlight(paramList->get(paramIdx)->getLoc());
5704-
diag.highlight(paramList->get(*matchingBackwardParamIdx)->getLoc());
5705-
}
5706-
};
5707-
5708-
// If the parameters have the same name, provide a custom diagnostic and then
5709-
// bail out early; there are no useful notes we can provide here.
5710-
if (paramName == backwardParamName) {
5711-
ctx.Diags.diagnose(trailingClosure->getStartLoc(), diag::unlabeled_trailing_closure_changed_behavior_same_param_name,
5712-
paramName);
5713-
noteCallee();
5714-
return;
5715-
}
5716-
5717-
// Produce the diagnostic.
5718-
ctx.Diags.diagnose(trailingClosure->getStartLoc(), diag::unlabeled_trailing_closure_changed_behavior, paramName,
5719-
backwardParamName);
5720-
5721-
// Produce a note with a Fix-It describing how to resolve the ambiguity.
5722-
auto diagResolution = [&](Identifier paramName, unsigned which) {
5723-
// Emit the note.
5724-
auto diag = ctx.Diags.diagnose(
5725-
trailingClosure->getStartLoc(), diag::trailing_closure_select_parameter,
5726-
paramName, which);
5727-
labelTrailingClosureArgument(
5728-
ctx, fn, arg, paramName, trailingClosure, diag);
5729-
};
5730-
5731-
diagResolution(backwardParamName, 0);
5732-
diagResolution(paramName, 1);
5733-
5734-
noteCallee();
5735-
}
5736-
57375614
/// Warn about the use of the deprecated "backward" scan for matching the
57385615
/// unlabeled trailing closure. It was needed to properly type check, but
57395616
/// this code will break with a future version of Swift.
@@ -5850,13 +5727,8 @@ Expr *ExprRewriter::coerceCallArguments(
58505727
// FIXME: Eventually, we want to enforce that we have either argTuple or
58515728
// argParen here.
58525729

5853-
// Warn if there was a recent change in trailing closure binding semantics
5854-
// that might have lead to a silent change in behavior.
5855-
if (trailingClosureMatching == TrailingClosureMatching::Forward) {
5856-
maybeWarnAboutTrailingClosureBindingChange(
5857-
callee, apply ? apply->getFn() : nullptr, arg, args, params, paramInfo,
5858-
unlabeledTrailingClosureIndex, parameterBindings);
5859-
} else {
5730+
// Warn about the backward scan being deprecated.
5731+
if (trailingClosureMatching == TrailingClosureMatching::Backward) {
58605732
warnAboutTrailingClosureBackwardScan(
58615733
callee, apply ? apply->getFn() : nullptr, arg, params,
58625734
unlabeledTrailingClosureIndex, parameterBindings);

0 commit comments

Comments
 (0)