Skip to content

Commit 00cf28a

Browse files
committed
Fix indexing constructors with generic parameters
Previously in the case of a constructor like `A<Int>(value: 1)` `Fn->getLoc()` returned the location of `>(value: 1)` while the actual location we're looking for is correctly the start of `A<Int>(value: 1)`. This adjusts the location we're looking up to use the start location of the constructor instead. Fixes: #54532
1 parent d445a18 commit 00cf28a

File tree

3 files changed

+49
-4
lines changed

3 files changed

+49
-4
lines changed

lib/IDE/SourceEntityWalker.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -815,9 +815,13 @@ passReference(ValueDecl *D, Type Ty, SourceLoc BaseNameLoc, SourceRange Range,
815815

816816
if (auto *TD = dyn_cast<TypeDecl>(D)) {
817817
if (!CtorRefs.empty() && BaseNameLoc.isValid()) {
818-
Expr *Fn = CtorRefs.back()->getFn();
819-
if (Fn->getLoc() == BaseNameLoc) {
820-
D = ide::getReferencedDecl(Fn).second.getDecl();
818+
ConstructorRefCallExpr *Ctor = CtorRefs.back();
819+
SourceLoc CtorLoc = Ctor->getFn()->getLoc();
820+
if (auto *TE = dyn_cast<TypeExpr>(Ctor->getBase()))
821+
CtorLoc = TE->getTypeRepr()->getLoc();
822+
823+
if (CtorLoc == BaseNameLoc) {
824+
D = ide::getReferencedDecl(Ctor->getFn()).second.getDecl();
821825
if (D == nullptr) {
822826
assert(false && "Unhandled constructor reference");
823827
return true;

test/IDE/annotation.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ class GenCls<T> {
129129
}
130130

131131
func test2() {
132-
// CHECK: <Class@[[@LINE-19]]:7>GenCls</Class><<iStruct@>Int</iStruct>>()
132+
// CHECK: <Ctor@[[@LINE-17]]:3-Class@[[@LINE-19]]:7>GenCls</Ctor><<iStruct@>Int</iStruct>>()
133133
GenCls<Int>()
134134
}
135135

test/Index/index_generic_params.swift

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,44 @@ extension Wrapper2.NonGenericWrapped where Wrapper2Param: P1 {
8686
extension MyUnknownType where Wrapper2Param: P1 {
8787
func foo(x: Wrapper2Param) {}
8888
}
89+
90+
// MARK: - Test indexing a generic initializer
91+
92+
struct A<T> { // CHECK: [[@LINE]]:8 | struct/Swift | A | [[A_USR:.*]] | Def | rel: 0
93+
init(value: T) {} // CHECK: [[@LINE]]:3 | constructor/Swift | init(value:) | [[A_init_USR:.*]] | Def,RelChild | rel: 1
94+
}
95+
96+
// CHECK: [[@LINE+2]]:5 | struct/Swift | A | [[A_USR]] | Ref | rel: 0
97+
// CHECK: [[@LINE+1]]:5 | constructor/Swift | init(value:) | [[A_init_USR]] | Ref,Call | rel: 0
98+
_ = A(value: 1)
99+
// CHECK: [[@LINE+3]]:5 | struct/Swift | A | [[A_USR]] | Ref | rel: 0
100+
// CHECK: [[@LINE+2]]:5 | constructor/Swift | init(value:) | [[A_init_USR]] | Ref,Call | rel: 0
101+
// CHECK: [[@LINE+1]]:7 | struct/Swift | Int | s:Si | Ref | rel: 0
102+
_ = A<Int>(value: 1)
103+
104+
enum B { // CHECK: [[@LINE]]:6 | enum/Swift | B | [[B_USR:.*]] | Def | rel: 0
105+
struct Nested<T> { // CHECK: [[@LINE]]:10 | struct/Swift | Nested | [[B_Nested_USR:.*]] | Def,RelChild | rel: 1
106+
init(value: T) {} // CHECK: [[@LINE]]:5 | constructor/Swift | init(value:) | [[B_Nested_init_USR:.*]] | Def,RelChild | rel: 1
107+
}
108+
}
109+
110+
// CHECK: [[@LINE+3]]:5 | enum/Swift | B | [[B_USR]] | Ref | rel: 0
111+
// CHECK: [[@LINE+2]]:7 | struct/Swift | Nested | [[B_Nested_USR]] | Ref | rel: 0
112+
// CHECK: [[@LINE+1]]:7 | constructor/Swift | init(value:) | [[B_Nested_init_USR]] | Ref,Call | rel: 0
113+
_ = B.Nested(value: 1)
114+
// CHECK: [[@LINE+4]]:5 | enum/Swift | B | [[B_USR]] | Ref | rel: 0
115+
// CHECK: [[@LINE+3]]:7 | struct/Swift | Nested | [[B_Nested_USR]] | Ref | rel: 0
116+
// CHECK: [[@LINE+2]]:7 | constructor/Swift | init(value:) | [[B_Nested_init_USR]] | Ref,Call | rel: 0
117+
// CHECK: [[@LINE+1]]:14 | struct/Swift | Int | s:Si | Ref | rel: 0
118+
_ = B.Nested<Int>(value: 1)
119+
120+
enum C { // CHECK: [[@LINE]]:6 | enum/Swift | C | [[C_USR:.*]] | Def | rel: 0
121+
struct Nested { // CHECK: [[@LINE]]:10 | struct/Swift | Nested | [[C_Nested_USR:.*]] | Def,RelChild | rel: 1
122+
init(value: Int) {} // CHECK: [[@LINE]]:5 | constructor/Swift | init(value:) | [[C_Nested_init_USR:.*]] | Def,RelChild | rel: 1
123+
}
124+
}
125+
126+
// CHECK: [[@LINE+3]]:5 | enum/Swift | C | [[C_USR]] | Ref | rel: 0
127+
// CHECK: [[@LINE+2]]:7 | struct/Swift | Nested | [[C_Nested_USR]] | Ref | rel: 0
128+
// CHECK: [[@LINE+1]]:7 | constructor/Swift | init(value:) | [[C_Nested_init_USR]] | Ref,Call | rel: 0
129+
_ = C.Nested(value: 1)

0 commit comments

Comments
 (0)