Skip to content

Commit 6bb659c

Browse files
committed
[Diagnostics] Add a diagnostic for incorrect use of trailing closures
1 parent d87d22d commit 6bb659c

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

lib/Sema/CSDiagnostics.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5491,3 +5491,20 @@ bool ExtraneousCallFailure::diagnoseAsError() {
54915491
removeParensFixIt(diagnostic);
54925492
return true;
54935493
}
5494+
5495+
bool InvalidUseOfTrailingClosure::diagnoseAsError() {
5496+
auto *anchor = getAnchor();
5497+
auto &cs = getConstraintSystem();
5498+
5499+
emitDiagnostic(anchor->getLoc(), diag::trailing_closure_bad_param,
5500+
getToType())
5501+
.highlight(anchor->getSourceRange());
5502+
5503+
if (auto overload = getChoiceFor(cs.getCalleeLocator(getLocator()))) {
5504+
if (auto *decl = overload->choice.getDeclOrNull()) {
5505+
emitDiagnostic(decl, diag::decl_declared_here, decl->getFullName());
5506+
}
5507+
}
5508+
5509+
return true;
5510+
}

lib/Sema/CSDiagnostics.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1921,6 +1921,15 @@ class ExtraneousCallFailure final : public FailureDiagnostic {
19211921
bool diagnoseAsError() override;
19221922
};
19231923

1924+
class InvalidUseOfTrailingClosure final : public ArgumentMismatchFailure {
1925+
public:
1926+
InvalidUseOfTrailingClosure(Expr *root, ConstraintSystem &cs, Type argType,
1927+
Type paramType, ConstraintLocator *locator)
1928+
: ArgumentMismatchFailure(root, cs, argType, paramType, locator) {}
1929+
1930+
bool diagnoseAsError() override;
1931+
};
1932+
19241933
} // end namespace constraints
19251934
} // end namespace swift
19261935

lib/Sema/CSFix.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -996,7 +996,10 @@ RemoveInvalidCall *RemoveInvalidCall::create(ConstraintSystem &cs,
996996
}
997997

998998
bool AllowInvalidUseOfTrailingClosure::diagnose(Expr *expr, bool asNote) const {
999-
return false;
999+
auto &cs = getConstraintSystem();
1000+
InvalidUseOfTrailingClosure failure(expr, cs, getFromType(), getToType(),
1001+
getLocator());
1002+
return failure.diagnose(asNote);
10001003
}
10011004

10021005
AllowInvalidUseOfTrailingClosure *

0 commit comments

Comments
 (0)