Skip to content

Commit ebe0e42

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
1 parent 379d88c commit ebe0e42

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
@@ -2660,21 +2660,31 @@ bool ClosureParamDestructuringFailure::diagnoseAsError() {
26602660
auto bodyStmts = closureBody->getElements();
26612661

26622662
SourceLoc bodyLoc;
2663+
SourceLoc inLoc = closure->getInLoc();
2664+
// If location for `in` is unknown we can't proceed
2665+
// since we'll not be able to figure out source line
2666+
// to place the fix-it on.
2667+
if (inLoc.isInvalid())
2668+
return true;
2669+
26632670
// If the body is empty let's put the cursor
26642671
// right after "in", otherwise make it start
26652672
// location of the first statement in the body.
26662673
if (bodyStmts.empty())
2667-
bodyLoc = Lexer::getLocForEndOfToken(sourceMgr, closure->getInLoc());
2674+
bodyLoc = Lexer::getLocForEndOfToken(sourceMgr, inLoc);
26682675
else
26692676
bodyLoc = bodyStmts.front().getStartLoc();
26702677

2678+
if (bodyLoc.isInvalid())
2679+
return true;
2680+
26712681
SmallString<64> fixIt;
26722682
llvm::raw_svector_ostream OS(fixIt);
26732683

26742684
// If this is multi-line closure we'd have to insert new lines
26752685
// in the suggested 'let' to keep the structure of the code intact,
26762686
// otherwise just use ';' to keep everything on the same line.
2677-
auto inLine = sourceMgr.getLineNumber(closure->getInLoc());
2687+
auto inLine = sourceMgr.getLineNumber(inLoc);
26782688
auto bodyLine = sourceMgr.getLineNumber(bodyLoc);
26792689
auto isMultiLineClosure = bodyLine > inLine;
26802690
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)