Skip to content

Commit 881e2b5

Browse files
committed
[IDE] Run RecontextualizeClosures in TypeCheckASTNodeAtLocRequest
1 parent 2820645 commit 881e2b5

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

lib/Sema/TypeCheckStmt.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1869,10 +1869,17 @@ bool TypeCheckASTNodeAtLocRequest::evaluate(
18691869
auto i = patternInit->getBindingIndex();
18701870
PBD->getPattern(i)->forEachVariable(
18711871
[](VarDecl *VD) { (void)VD->getInterfaceType(); });
1872-
if (PBD->getInit(i)) {
1872+
if (auto Init = PBD->getInit(i)) {
18731873
if (!PBD->isInitializerChecked(i)) {
18741874
typeCheckPatternBinding(PBD, i,
18751875
/*LeaveClosureBodyUnchecked=*/true);
1876+
// Retrieve the accessor's body to trigger RecontextualizeClosures
1877+
// This is important to get the correct USR of variables defined
1878+
// in closures initializing lazy variables.
1879+
PBD->getPattern(i)->forEachVariable([](VarDecl *VD) {
1880+
VD->visitEmittedAccessors(
1881+
[&](AccessorDecl *accessor) { (void)accessor->getBody(); });
1882+
});
18761883
return false;
18771884
}
18781885
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// RUN: %sourcekitd-test -req=cursor -pos=5:13 %s -- %s | %FileCheck %s
2+
3+
class MyClass {
4+
lazy var calculatorContext: Int = {
5+
let context = 1
6+
return context
7+
}()
8+
}
9+
10+
// CHECK: s:34cursor_in_closure_initializing_var7MyClassC17calculatorContextSivgSiyXEfU_7contextL_Sivp

0 commit comments

Comments
 (0)