Skip to content

Commit 50f6ddb

Browse files
author
Nathan Hawes
committed
[sourcekitd][CursorInfo] Make sure we handle the implict VarDelcs being introduced in CastStmt bodies
When CursorInfo finds a reference to a VarDecl that's implicit but has a parent VarDecl (according to VarDecl::getParentVarDecl), act as if we found the parent instead.
1 parent a4340ff commit 50f6ddb

File tree

2 files changed

+69
-4
lines changed

2 files changed

+69
-4
lines changed

lib/IDE/SwiftSourceDocInfo.cpp

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,19 @@ bool CursorInfoResolver::tryResolve(ValueDecl *D, TypeDecl *CtorTyRef,
7979
if (!D->hasName())
8080
return false;
8181

82-
if (Loc == LocToResolve) {
83-
CursorInfo.setValueRef(D, CtorTyRef, ExtTyRef, IsRef, Ty, ContainerType);
84-
return true;
82+
if (Loc != LocToResolve)
83+
return false;
84+
85+
if (auto *VD = dyn_cast<VarDecl>(D)) {
86+
// Handle references to the implicitly generated vars in case statements
87+
// matching multiple patterns
88+
if (VD->isImplicit()) {
89+
if (auto * Parent = VD->getParentVarDecl())
90+
D = Parent;
91+
}
8592
}
86-
return false;
93+
CursorInfo.setValueRef(D, CtorTyRef, ExtTyRef, IsRef, Ty, ContainerType);
94+
return true;
8795
}
8896

8997
bool CursorInfoResolver::tryResolve(ModuleEntity Mod, SourceLoc Loc) {
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
2+
enum X {
3+
case first(Int, String)
4+
case second(Int, String)
5+
case third(Int, String)
6+
case fourth(Int, String)
7+
case fifth(Int, String)
8+
}
9+
10+
let p = X.first(3, "hello")
11+
12+
switch p {
13+
case .first(let x, let y)
14+
print("foo \(x) \(y)")
15+
fallthrough
16+
case .second(let x, let y), .third(let x, let y):
17+
print("bar \(x) \(y)")
18+
default:
19+
print("other")
20+
}
21+
22+
// RUN: %sourcekitd-test -req=cursor -pos=13:19 %s -- %mcp_opt %s | %FileCheck -check-prefixes=CHECKX,CHECK1DECL %s
23+
// RUN: %sourcekitd-test -req=cursor -pos=14:18 %s -- %mcp_opt %s | %FileCheck -check-prefixes=CHECKX,CHECK1REF %s
24+
25+
// CHECK1DECL: source.lang.swift.decl.var.local (13:19-13:20)
26+
// CHECK1REF: source.lang.swift.ref.var.local (13:19-13:20)
27+
28+
// RUN: %sourcekitd-test -req=cursor -pos=16:20 %s -- %mcp_opt %s | %FileCheck -check-prefixes=CHECKX,CHECK2DECL %s
29+
// RUN: %sourcekitd-test -req=cursor -pos=16:42 %s -- %mcp_opt %s | %FileCheck -check-prefixes=CHECKX,CHECK2DECL2 %s
30+
// RUN: %sourcekitd-test -req=cursor -pos=17:18 %s -- %mcp_opt %s | %FileCheck -check-prefixes=CHECKX,CHECK2REF %s
31+
32+
// CHECK2DECL: source.lang.swift.decl.var.local (16:20-16:21)
33+
// CHECK2DECL2: source.lang.swift.decl.var.local (16:42-16:43)
34+
// CHECK2REF: source.lang.swift.ref.var.local (16:20-16:21)
35+
36+
// CHECKX: x
37+
// CHECKX: s:33cursor_vardecl_across_fallthrough1xL_Sivp
38+
// CHECKX: Int
39+
40+
41+
// RUN: %sourcekitd-test -req=cursor -pos=13:26 %s -- %mcp_opt %s | %FileCheck -check-prefixes=CHECKY,CHECK3DECL %s
42+
// RUN: %sourcekitd-test -req=cursor -pos=14:23 %s -- %mcp_opt %s | %FileCheck -check-prefixes=CHECKY,CHECK3REF %s
43+
44+
// CHECK3DECL: source.lang.swift.decl.var.local (13:26-13:27)
45+
// CHECK3REF: source.lang.swift.ref.var.local (13:26-13:27)
46+
47+
// RUN: %sourcekitd-test -req=cursor -pos=16:27 %s -- %mcp_opt %s | %FileCheck -check-prefixes=CHECKY,CHECK4DECL %s
48+
// RUN: %sourcekitd-test -req=cursor -pos=16:49 %s -- %mcp_opt %s | %FileCheck -check-prefixes=CHECKY,CHECK4DECL2 %s
49+
// RUN: %sourcekitd-test -req=cursor -pos=17:23 %s -- %mcp_opt %s | %FileCheck -check-prefixes=CHECKY,CHECK4REF %s
50+
51+
// CHECK4DECL: source.lang.swift.decl.var.local (16:27-16:28)
52+
// CHECK4DECL2: source.lang.swift.decl.var.local (16:49-16:50)
53+
// CHECK4REF: source.lang.swift.ref.var.local (16:27-16:28)
54+
55+
// CHECKY: y
56+
// CHECKY: s:33cursor_vardecl_across_fallthrough1yL_SSvp
57+
// CHECKY: String

0 commit comments

Comments
 (0)