Skip to content

Commit ab40bb1

Browse files
committed
[Diagnostics] Don't suggest argument destructuring fix-it if closure is malformed
If closure expression is malformed don't try to suggest a fix-it about destructuring, because there is no way to figure out where it would actually go in the source. Resolves: rdar://problem/51576862 (cherry picked from commit ebe0e42)
1 parent 9729868 commit ab40bb1

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

lib/Sema/CSDiagnostics.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2479,21 +2479,31 @@ bool ClosureParamDestructuringFailure::diagnoseAsError() {
24792479
auto bodyStmts = closureBody->getElements();
24802480

24812481
SourceLoc bodyLoc;
2482+
SourceLoc inLoc = closure->getInLoc();
2483+
// If location for `in` is unknown we can't proceed
2484+
// since we'll not be able to figure out source line
2485+
// to place the fix-it on.
2486+
if (inLoc.isInvalid())
2487+
return true;
2488+
24822489
// If the body is empty let's put the cursor
24832490
// right after "in", otherwise make it start
24842491
// location of the first statement in the body.
24852492
if (bodyStmts.empty())
2486-
bodyLoc = Lexer::getLocForEndOfToken(sourceMgr, closure->getInLoc());
2493+
bodyLoc = Lexer::getLocForEndOfToken(sourceMgr, inLoc);
24872494
else
24882495
bodyLoc = bodyStmts.front().getStartLoc();
24892496

2497+
if (bodyLoc.isInvalid())
2498+
return true;
2499+
24902500
SmallString<64> fixIt;
24912501
llvm::raw_svector_ostream OS(fixIt);
24922502

24932503
// If this is multi-line closure we'd have to insert new lines
24942504
// in the suggested 'let' to keep the structure of the code intact,
24952505
// otherwise just use ';' to keep everything on the same line.
2496-
auto inLine = sourceMgr.getLineNumber(closure->getInLoc());
2506+
auto inLine = sourceMgr.getLineNumber(inLoc);
24972507
auto bodyLine = sourceMgr.getLineNumber(bodyLoc);
24982508
auto isMultiLineClosure = bodyLine > inLine;
24992509
auto indent =
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// RUN: not %target-swift-frontend %s -typecheck
2+
3+
func foo() -> [String] {
4+
let dict: [String: String] = [:]
5+
return dict.filter({ (_

0 commit comments

Comments
 (0)