Skip to content

Commit 0b47a45

Browse files
committed
Parse: The implicit defer body function should have a valid source location
Otherwise we can't produce good diagnostics pointing at the right 'defer' block.
1 parent 397b620 commit 0b47a45

File tree

2 files changed

+5
-28
lines changed

2 files changed

+5
-28
lines changed

Diff for: lib/Parse/ParseStmt.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -966,7 +966,7 @@ ParserResult<Stmt> Parser::parseStmtDefer() {
966966
StaticSpellingKind::None,
967967
/*FuncLoc=*/ SourceLoc(),
968968
name,
969-
/*NameLoc=*/ SourceLoc(),
969+
/*NameLoc=*/ PreviousLoc,
970970
/*Throws=*/ false, /*ThrowsLoc=*/ SourceLoc(),
971971
/*generic params*/ nullptr,
972972
params,

Diff for: lib/Sema/TypeCheckCaptures.cpp

+4-27
Original file line numberDiff line numberDiff line change
@@ -29,29 +29,6 @@
2929
#include "llvm/ADT/SmallPtrSet.h"
3030
using namespace swift;
3131

32-
static SourceLoc getCaptureLoc(AnyFunctionRef AFR) {
33-
if (auto AFD = AFR.getAbstractFunctionDecl()) {
34-
if (auto *FD = dyn_cast<FuncDecl>(AFD)) {
35-
if (FD->isDeferBody()) {
36-
// HACK: Defer statements generate implicit FuncDecls, and hence do
37-
// not have valid source locations. Instead, use the location of
38-
// the body.
39-
return FD->getBody()->getLBraceLoc();
40-
}
41-
}
42-
43-
return AFD->getLoc();
44-
} else {
45-
auto ACE = AFR.getAbstractClosureExpr();
46-
if (auto CE = dyn_cast<ClosureExpr>(ACE)) {
47-
if (!CE->getInLoc().isInvalid())
48-
return CE->getInLoc();
49-
}
50-
51-
return ACE->getLoc();
52-
}
53-
}
54-
5532
namespace {
5633

5734
class FindCapturedVars : public ASTWalker {
@@ -80,7 +57,7 @@ class FindCapturedVars : public ASTWalker {
8057
DynamicSelf(DynamicSelf),
8158
OpaqueValue(OpaqueValue),
8259
AFR(AFR) {
83-
CaptureLoc = getCaptureLoc(AFR);
60+
CaptureLoc = AFR.getLoc();
8461
}
8562

8663
/// Check if the type of an expression references any generic
@@ -355,11 +332,11 @@ class FindCapturedVars : public ASTWalker {
355332

356333
if (GenericParamCaptureLoc.isInvalid())
357334
if (captureInfo.hasGenericParamCaptures())
358-
GenericParamCaptureLoc = getCaptureLoc(innerClosure);
335+
GenericParamCaptureLoc = innerClosure.getLoc();
359336

360337
if (DynamicSelfCaptureLoc.isInvalid())
361338
if (captureInfo.hasDynamicSelfCapture()) {
362-
DynamicSelfCaptureLoc = getCaptureLoc(innerClosure);
339+
DynamicSelfCaptureLoc = innerClosure.getLoc();
363340
DynamicSelf = captureInfo.getDynamicSelfType();
364341
}
365342

@@ -643,7 +620,7 @@ void TypeChecker::computeCaptures(AnyFunctionRef AFR) {
643620
AFR.getBody()->walk(finder);
644621

645622
if (AFR.hasType() && !AFR.isObjC()) {
646-
finder.checkType(AFR.getType(), getCaptureLoc(AFR));
623+
finder.checkType(AFR.getType(), AFR.getLoc());
647624
}
648625

649626
// If this is an init(), explicitly walk the initializer values for members of

0 commit comments

Comments
 (0)